This repository was archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.py
More file actions
294 lines (226 loc) · 8.96 KB
/
uploader.py
File metadata and controls
294 lines (226 loc) · 8.96 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from configparser import ConfigParser
import click
from pathlib import Path
import psycopg2
from typing import List
import yaml
from utils.dataclasses import Module, Resource
'''Set program's verbosity. True to display actions on execution'''
isVerbose = False
def extract_config(filename='database.ini', section='postgresql') -> dict:
'''Extract all configuration information
:param filename: Path to the .ini configuration file
:param section: Section from which fetching all postgresql connection data
:return: A dict with all connection parameters
'''
if isVerbose:
print_info('Exctracting posgtres connection data')
# From: https://www.postgresqltutorial.com/postgresql-python/connect/
# Create a parser
parser = ConfigParser()
# Read config file
parser.read(filename)
# Get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception(
'Section {0} not found in the {1} file'.format(section, filename))
return db
def get_resources(path: Path) -> List[Resource]:
'''Retrieve all html resources inside the module folder
:param path: Path of the folder in which look for all resources
:return: A list of all found resources as objects
'''
if isVerbose:
print_info('Retrieve existing resource files')
resources: List[Path] = path.glob('**/*.html')
return [
Resource(
content=resource.read_text(encoding='utf8'),
name=resource.name)
for resource in resources]
def get_sorted_resources(resources: List[Resource]) -> List[Resource]:
'''Link all provided resource by alphabetical order
:param resources: The list of all resources to sort
:return: A list of the same resources sorted by alphabetical order
'''
if isVerbose:
print_info('Sort resources')
# Make a copy of all resource
sorted_resources = resources[:]
# Sort the copy and return it
sorted_resources.sort(key=lambda resource: resource.name)
return sorted_resources
def get_module(module: Path) -> Module:
'''Get module meta data
:param module: Path of the folder in which look for the .yaml module file
:return: The module's data as a Module object
'''
if isVerbose:
print_info('Retrieve module configuration file')
# Get all YAML files
module_conf_files = [
file
for file in module.glob('**/*')
if file.is_file() and file.suffix in ['.yml', '.yaml']]
# Assert there is only a unique YAML file for the current module
if len(module_conf_files) < 1:
raise FileNotFoundError('No module yaml configuration file found')
if len(module_conf_files) > 1:
raise FileNotFoundError('Multiple yaml configuration files found for this module')
return get_module_data(module_conf_files[0])
def get_module_data(module_file: Path) -> Module:
'''Extract module data from its configuration file'''
if isVerbose:
print_info('Extract module\'s data')
# Extract file data
data = yaml.safe_load(module_file.read_text(encoding='utf8'))
# Assert that each property is present
if 'module' not in data.keys():
raise KeyError('Field "module" missing in the module configuration file')
if not all(key in ('name', 'description') for key in data['module']):
raise KeyError(
'Unable to read "name" and "description" properties of the module'
'(are the keys missing ?)')
return Module(**data['module'])
def print_info(message: str) -> None:
'''Print a debug message
:param message: Message to display
'''
print(f'[INFO] {message}')
def record_module(
module: Module, subscription_plan_id: int,
config: dict = extract_config()) -> int:
'''Record the provided module in database
:param module: Module to record in database
:param subscription_plan_id: Id of its associated subscription plan
:param config: Postgresql connection parameters
:return: The newly inserted module id
'''
if isVerbose:
print_info(f'Record the module "{module.name}"')
sql = '''
INSERT INTO "module" ("ModuleDescription", "SubscriptionPlanId", "ModuleName")
VALUES(%s, %s, %s) RETURNING "Id";
'''
conn = None
module_id = 0
try:
# Connect to the PostgreSQL database
conn = psycopg2.connect(**config)
# Create a new cursor
cur = conn.cursor()
# Execute the INSERT statement
cur.execute(sql, (module.description, subscription_plan_id, module.name))
# get the generated id back
module_id = int(cur.fetchone()[0])
# Commit the changes to the database
conn.commit()
# Close communication with the database
cur.close()
finally:
if conn is not None:
conn.close()
return module_id
def record_resource(
resource: Resource, module_id: int, next_resource_id: int,
config: dict = extract_config()) -> int:
'''Record the provided resource in database
:param resource: Resource to record in database
:param module_id: Id of the module this resource is related to
:param next_resource_id: Id of the resource following this one, None if
this resource is the last one
:param config: Postgresql connection parameters
:return: The newly inserted resource id
'''
if isVerbose:
print_info(f'Record the resource from {resource.name}')
sql = '''
INSERT INTO "resource" ("ModuleId", "Content", "NextResourceId")
VALUES(%s, %s, %s) RETURNING "Id";
'''
conn = None
resource_id = 0
try:
# Connect to the PostgreSQL database
conn = psycopg2.connect(**config)
# Create a new cursor
cur = conn.cursor()
# Execute the INSERT statement
cur.execute(sql, (module_id, resource.content, next_resource_id))
# get the generated id back
resource_id = int(cur.fetchone()[0])
# Commit the changes to the database
conn.commit()
# Close communication with the database
cur.close()
finally:
if conn is not None:
conn.close()
return resource_id
def record_resources(
resources: List[Resource], module_id: int,
config: dict = extract_config()) -> None:
'''Record the provided resources in database
:param resources: The list of all Resource objects to insert
:param module_id: Id of the module this resource is related to
:param config: Postgresql connection parameters
'''
if isVerbose:
print_info('Start recording resources')
# Since all resources are linked to each other, we need the index of the
# second resource to track the first
# To achieve that, we need to invert the order and start tracking the last
# which is not linked to any other resource
resources = resources[::-1]
next_resource_id = None
for resource in resources:
next_resource_id = record_resource(resource, module_id, next_resource_id, config)
if isVerbose:
print_info('End resources recording')
@click.command()
@click.argument('path', type=click.Path(exists=True))
@click.option(
'--module',
'-m',
'module',
help='Path to the module description file',
type=click.Path(exists=True))
@click.option(
'--subscriptionplan',
'-s',
'subscription_plan_id',
default=1,
help='Id of the subscription plan to which this module is intended',
type=int)
@click.option(
'--verbose',
is_flag=True,
help='Set verbosity to True to display actions in the console on execution')
def upload(path, module, subscription_plan_id, verbose):
'''Upload a module and its resources in the correct order'''
# Set verbosity level
global isVerbose
isVerbose = verbose
if isVerbose:
print_info('Start uploading')
root_path = Path(path)
# Retrieve all resources
resources: List[Path] = get_resources(root_path)
# Sort and link them
resources = get_sorted_resources(resources)
# Retrieve module meta data
module_path = Path(module if module else path)
module = get_module(module_path)
# Add a new record for the module
module_id = record_module(module, subscription_plan_id)
# Add a new track for all its associated resources
record_resources(resources, module_id)
if isVerbose:
print_info('Done !')
if __name__ == "__main__":
upload()