-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
247 lines (198 loc) · 8.03 KB
/
Copy pathutils.py
File metadata and controls
247 lines (198 loc) · 8.03 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import datetime
import re
import json
def validate_json_file(file_path):
try:
with open(file_path, 'r') as file:
json.load(file)
print("The JSON file is valid.")
except (ValueError, FileNotFoundError) as e:
print("The JSON file is not valid:", str(e))
def validDate(date):
date_splitted = date.split('-')
try:
year = int(date_splitted[0])
month = int(date_splitted[1])
day = int(date_splitted[2])
except ValueError:
return False # could not convert parts to integers, invalid format
# check if year, month, and day are within valid ranges
if year < 1 or month < 1 or month > 12 or day < 1 or day > 31:
return False # invalid values for year, month, or day
# check for valid number of days for the given month and year
if month in [4, 6, 9, 11] and day > 30:
return False # April, June, September, and November have 30 days
if month == 2:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
if day > 29:
return False # leap year has 29 days in February
else:
if day > 28:
return False # non-leap year has 28 days in February
return True # valid date
def validDateTime(datetime_str):
try:
# split datetime string into its components
date_str, time_str = datetime_str.split('T')
year_str, month_str, day_str = date_str.split('-')
offset = re.split(r'\+|-|z|Z', time_str)
if len(offset) == 3: # checks whether or not the given datetime has both (+|-)HH::MM and z|Z offsets
return False
hour_str, minute_str, second_str = offset[0].split(':')
second_parts = second_str.split('.')
second_int = int(second_parts[0])
microsecond_int = int(second_parts[1]) if len(second_parts) > 1 else 0
if len(offset) == 2 and offset[1] != '':
offset_hour, offset_min = offset[1].split(':')
offset_hour = int(offset_hour)
offset_min = int(offset_min)
if offset_hour < 0 or offset_hour > 23 or offset_min < 0 or offset_min > 59:
return False
# parse date and time components into datetime object
dt = datetime.datetime(
year=int(year_str), month=int(month_str), day=int(day_str),
hour=int(hour_str), minute=int(minute_str), second=second_int,
microsecond=microsecond_int
)
return True # valid datetime
except (ValueError, IndexError):
return False # invalid datetime format
def validTime(time):
time_splitted = re.split(r'\+|-|z|Z', time)
if len(time_splitted) == 3:
return False
try:
if len(time_splitted) == 2 and time_splitted[1] != '':
offset_hour, offset_min = time_splitted[1].split(':')
offset_hour = int(offset_hour)
offset_min = int(offset_min)
if offset_hour < 0 or offset_hour > 23 or offset_min < 0 or offset_min > 59:
return False
time_splitted = time_splitted[0].split('.')
datetime.datetime.strptime(time_splitted[0], "%H:%M:%S")
except (ValueError, IndexError):
return False
return True
def validKey(str):
str_splitted = str.split('.')
for split in str_splitted:
if split[:2] == '\\\"':
return False
return True
def set_up_multiline_string(str):
lines = str.split('\n')
if len(lines) == 1:
return str
else:
backslash = False
result = ""
for line in lines:
if backslash:
line = line.lstrip()
if line != '' and line[-1] == '\\':
line = line[:-1]
backslash = True
else:
if line != '':
line = line + '\n'
backslash = False
result = result + line
if result[-1] == '\n':
result = result[:-1]
return result
def merge_dicts(dict1, dict2):
for key, value in dict2.items():
if key in dict1:
if isinstance(value, dict) and isinstance(dict1[key], dict):
dict1[key] = merge_dicts(dict1[key], value)
elif isinstance(value, set) and isinstance(dict1[key], set):
dict1[key] |= value
else:
raise SyntaxError(f"KEY {key} has been reinitialized with the VALUE {value}")
else:
dict1[key] = value
return dict1
def is_array_table(value):
for elem in value:
if isinstance(elem, dict):
return True
return False
def merge_elems(dict1, dict2):
for key, value in dict2.items():
if key in dict1:
#check both types
if isinstance(value, list) and is_array_table(value) and isinstance(dict1[key], list) and is_array_table(dict1[key]):
dict1 = join_toml_array_tables(dict1, {key : value})
elif isinstance(value, dict) and isinstance(dict1[key], list) and is_array_table(dict1[key]):
dict1[key][-1] = join_toml_array_tables(dict1[key][-1], value)
elif isinstance(value, dict) and isinstance(dict1[key], dict):
dict1[key] = merge_dicts(dict1[key], value)
elif isinstance(value, set) and isinstance(dict1[key], set):
dict1[key] |= value
else:
dict1[key] = value
return dict1
def build_dict(keys, value):
temp = keys.split('.')
result = dict()
for elem in temp:
if elem != temp[-1]:
add_to_dict_chain(result, elem, {})
else:
add_to_dict_chain(result, elem, value)
return result
def add_to_dict_chain(d, key, value):
while list(d.values()) != [] and isinstance(list(d.values())[-1], dict):
d = list(d.values())[-1]
d[key] = value
def add_to_array_chain(d, key, value):
while list(d.values()) != [] and isinstance(list(d.values())[-1][-1], dict):
d = list(d.values())[-1][-1]
d[key] = value
def get_key_index(key, d):
index = 0;
for elem in d:
if key in elem.keys():
return index
index += 1
return -1
def join_array_tables(dict1, dict2):
for key in dict2.keys():
if isinstance(dict1, dict) and key in dict1.keys():
if len(dict2[key]) == 1 and dict2[key][0] != {} and isinstance(list(dict2[key][0].values())[-1], list):
dict1[key] = join_array_tables(dict1[key], dict2[key][0])
else:
dict1[key] = dict1[key] + dict2[key] # TODO: Rever esta parte e olhar para o último exemplo do parser file (ver documentação do toml agane)
elif isinstance(dict1, dict):
dict1[key] = dict2[key]
else:
index = get_key_index(key, dict1)
if index < 0:
if isinstance(dict2[key], list):
dict1[-1][key] = dict2[key]
else:
dict1.append(dict2)
else:
if isinstance(dict2[key], list):
dict1[index][key] = join_array_tables(dict1[index][key], dict2[key][0])
else:
dict1.append(dict2)
return dict1
def join_toml_array_tables(dict1, dict2):
for key in dict2.keys():
if key in dict1.keys():
if len(dict2[key]) == 1 and isinstance(dict2[key], list) and dict2[key][0] != {} and isinstance(list(dict2[key][0].values())[-1], list):
if isinstance(dict1[key], list):
dict1[key][-1] = join_toml_array_tables(dict1[key][-1], dict2[key][0])
else:
dict1[key] = join_toml_array_tables(dict1[key], dict2[key][0])
else:
if isinstance(dict2[key], list):
dict1[key].append(dict2[key][0])
elif isinstance(dict1[key], list):
dict1[key][-1].update(dict2[key])
else:
dict1[key].update(dict2[key])
else:
dict1[key] = dict2[key]
return dict1