-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (109 loc) · 4.25 KB
/
main.py
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
from utils import ConfigManager
from tasks import CSVTask, DXFTask, SVGTask, LookupTableTask
from persistence import MongoDBPersistenceManager
from utils.logger import Logger
from model.odm import ODMModel
import time
class Main():
"""
Main class, is the entry point of the application.
"""
def __init__(self):
self._config = ConfigManager("config/general.json")
def run_command(self, command, files):
"""
Call a command and pass the files we want to process.
Arguments:
- command: name of the command we want to run;
- files: files we want to process.
Returns: None
The command name will be used to call the respective routine in the Main
class. Using the time library to calculate the total time an the mean of
execution.
"""
start_time = time.time()
getattr(self, "run_"+command)(files)
end_time = time.time() - start_time
Logger.success("Total time :", end_time , "seconds")
if len(files):
Logger.success("Arithmetic mean:", end_time / len(files), "seconds")
def run_dxf(self, files):
"""
Process dxf files.
Arguments:
- files: files we want process.
Returns: None
Instantiates a MongoDBPersistenceManager and a DXFTask to process the dxf
files whit the apposite procedure.
"""
persistence = MongoDBPersistenceManager(self._config)
ODMModel.set_pm( persistence )
task = DXFTask(self._config)
task.perform_updates_on_files(files)
self.run_lookup()
def run_csv(self, files):
"""
Process csv files.
Arguments:
- files: files we want process.
Returns: None
Instantiates a MongoDBPersistenceManager and a CSVTask to process the csv
files whit the apposite procedure.
"""
persistence = MongoDBPersistenceManager(self._config)
ODMModel.set_pm( persistence )
task = CSVTask(self._config)
task.perform_updates_on_files(files)
self.run_lookup()
def run_svg(self, b_ids):
"""
Create svg files representing maps of buildings.
Arguments:
- b_ids: a list of string representing the building ids of the buildings
we want create/update svg files.
Returns: None
Instantiates a MongoDBPersistenceManager and a SVGTask to create the svg
files whit the apposite procedure.
If b_ids is None the CSVTask create svg files of every building in the DB.
"""
persistence = MongoDBPersistenceManager(self._config)
ODMModel.set_pm( persistence )
task = SVGTask(self._config)
task.perform_svg_update(b_ids)
def run_lookup(self, files=None):
"""
Create a new sqlite db filled with a lookup table, useful for client applications
to not overload the API server and to provide a better UX.
"""
task = LookupTableTask()
task.perform_update()
if __name__ == '__main__':
"""
Main procedure of the application, parse the arguments and call the
run_command function passing the command we want execute and the files we
want process.
"""
import argparse
parser = argparse.ArgumentParser(description = "Programma per l'aggiornamento dati del server Spazi Unimi.")
parser.add_argument('command', metavar='op', type=str, choices=["csv", "dxf", "svg", "lookup", "cow"],
help="Il commando da eseguire: dxf, csv, svg, lookup")
parser.add_argument('files', metavar='file', type=str, nargs='*',
help='I file su cui lavorare, a seconda del comando scelto.')
args = parser.parse_args()
# Development mode
from tasks.mergers import DataMerger
DataMerger.skip_geocoding = True
if(args.command in ["csv", "dxf"] and len(args.files) == 0):
print("Errore: Il comando "+args.command+" richiede almeno un file ."+args.command+" su cui lavorare.\n")
parser.print_help()
exit(1)
if(args.command == 'cow'):
try:
with open("assets/cow.txt") as fp:
for line in fp:
print(line, end="")
except:
print("There is no cow level.")
exit()
program = Main()
program.run_command(args.command, args.files)