-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumns.py
More file actions
207 lines (177 loc) · 6.67 KB
/
columns.py
File metadata and controls
207 lines (177 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from enum import Enum
from typing import List, Optional
from openpyxl.worksheet.datavalidation import DataValidation
import csv
# Database configuration
# Note - The contents of this has to be changed in prod!
# Database to download backups from
DATABASE = {
"database_name": "patients",
"host_name": "localhost",
"user_name": "jieqiboh",
"password": "postgres",
"port_number": "5432"
}
# The database on this machine
CURRENT_DATABASE = {
"database_name": "patients",
"host_name": "localhost",
"user_name": "jieqiboh",
"password": "postgres",
"port_number": "5432"
}
class DataType(Enum):
def __new__(cls, sql_type, validation: Optional[DataValidation] = None):
obj = object.__new__(cls)
obj._value_ = sql_type
obj.validation = validation
return obj
# DataType definitions with associated DataValidation rules
INTEGER = (
"INTEGER",
DataValidation(
type="whole",
operator="between",
formula1="-9999",
formula2="9999",
showErrorMessage=True,
error="Entry must be a whole number between -9999 and 9999",
errorTitle="Invalid Entry",
),
)
TEXT = ("TEXT")
VARCHAR_1 = (
"VARCHAR(1)",
DataValidation(
type="textLength", # Ensures the length of the text
operator="equal",
formula1="1", # Text must be exactly 1 character
showErrorMessage=True,
error="Entry must be a single character",
errorTitle="Invalid Entry",
),
)
BOOLEAN = (
"BOOLEAN",
DataValidation(
type="list",
formula1='"TRUE,FALSE"',
showErrorMessage=True,
error="Entry must be either 'TRUE' or 'FALSE'",
errorTitle="Invalid Entry",
),
)
DATE = (
"DATE",
DataValidation(
type="date",
showErrorMessage=True,
error="Entry must be a valid date",
errorTitle="Invalid Entry",
),
)
NUMERIC_5_1 = (
"NUMERIC(5, 1)",
DataValidation(
type="decimal",
operator="between",
formula1="-9999.9",
formula2="9999.9",
showErrorMessage=True,
error="Entry must be a number between -9999.9 and 9999.9",
errorTitle="Invalid Entry",
),
)
BYTEA = ("BYTEA")
class FieldDescriptor:
"""
Describes a field
"""
def __init__(self, sql_name: str, go_name: str, json_name: str, datasheet_name: str, csv_name: str,
datatype: DataType, colour: str, required: bool = False, datasheet_range: str = None):
self.sql_name: str = sql_name # SQL column name
self.go_name: str = go_name # Go struct field name
self.json_name: str = json_name # JSON field name
self.datasheet_name: str = datasheet_name # DataSheet column name
self.csv_name: str = csv_name # CSV column name in patientdata.csv
self.datatype: DataType = datatype # Data type of the field (Loosely follows SQL type)
self.colour: str = colour # Column colour
self.required: bool = required # Whether the field is required
self.datasheet_range: str = datasheet_range # Range of the column in the datasheet
def __repr__(self):
return self.sql_name
class CategoryDescriptor:
"""
Describes a Category
"""
def __init__(self, name: str, fields: list):
self.name: str = name
self.fields: List[FieldDescriptor] = fields # fields is expected to be a list of Field objects
def __repr__(self):
return self.name
class Types:
def __init__(self, csv_path: str):
self.categories: List[CategoryDescriptor] = [] # An array of CategoryDescriptor objects
self.csv_sql_map = {}
self.load_from_csv(csv_path) # Has to come first!
self.initMaps()
def load_from_csv(self, csv_path: str):
# Dictionary to store category-to-fields mapping
category_map = {}
with open(csv_path, mode="r") as file:
reader = csv.DictReader(file)
for row in reader:
# Extract data for the field
category = row["Category Dropdown"]
sql_name = row["SQL Name"]
go_name = row["Go Name"]
json_name = row["JSON Name"]
datasheet_name = row["Datasheet Name"]
csv_name = row["CSV Name"]
datatype = row["Data Type"]
required = row["Required"].strip().upper() == "TRUE"
datasheet_range = row["Datasheet Range"]
colour = row["Colour"]
# Create a FieldDescriptor
field = FieldDescriptor(
sql_name=sql_name,
go_name=go_name,
json_name=json_name,
datasheet_name=datasheet_name,
csv_name=csv_name,
datatype=DataType[datatype],
required=required,
datasheet_range=datasheet_range,
colour=colour
)
# Add the field to the appropriate category
# Create new category if it doesn't exist
if category not in category_map:
category_map[category] = []
category_map[category].append(field)
# Convert category_map to CategoryDescriptors
for category, fields in category_map.items():
self.categories.append(CategoryDescriptor(name=category, fields=fields))
def initMaps(self):
"""
Initialize some maps used for name conversion
"""
csv_sql_map = {}
for category in self.categories:
csv_sql_map[category.name] = {}
csv_sql_map[category.name]["id"] = "id" # Hardcoded id and vid
csv_sql_map[category.name]["vid"] = "vid" # Hardcoded id and vid
for field in category.fields:
# Get the sql name and csv name for each field to generate the map
csv_sql_map[category.name][field.csv_name] = field.sql_name
self.csv_sql_map = csv_sql_map
def __repr__(self):
return f"Types(categories={self.categories})"
if __name__ == "__main__":
types = Types("types.csv")
# Print header with increased padding
print(f"{'Category':<25} {'Field':<30} {'DataType':<25} {'Required':<10}")
print("=" * 90)
for category in types.categories:
for field in category.fields:
print(f"{category.name:<25} {field.sql_name:<30} {field.datatype:<25} {str(field.required):<10}")