-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_model_xmls.py
174 lines (142 loc) · 5.48 KB
/
fetch_model_xmls.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
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
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "mysql-connector-python",
# "python-dotenv",
# ]
# ///
import sys
import os
###############################################################################
# Imports
###############################################################################
import mysql.connector
###############################################################################
# Constants
###############################################################################
from dotenv import load_dotenv
load_dotenv()
DB_HOST = os.getenv('DB_HOST')
DB_NAME = os.getenv('DB_NAME')
DB_USR = os.getenv('DB_USER')
DB_PWD = os.getenv('DB_PASSWORD')
###############################################################################
# Classes
###############################################################################
class NotFoundException(Exception):
'''class to signal empty responses from the database or google sheets
'''
def __init__(self, reason=None):
self.reason = reason
class DBHandler:
"""class to handle database related tasks
Methods
-------
create_connection
create and return a connector to the database
execute_sql
execute an arbitrary SQL statement
"""
def __init__(self):
self.db_host = DB_HOST
self.db_name = DB_NAME
self.db_usr = DB_USR
self.db_pwd = DB_PWD
def create_connection(self):
"""
create and return a connector to the database
Returns
-------
the connector
"""
# Use detect_types to receive parsed datetime objects
conn = mysql.connector.connect(host=self.db_host, database=self.db_name, user=self.db_usr, password=self.db_pwd)
return conn
def call_proc(self,query,values,conn=None,close=True):
res = None
try:
if not conn:
conn = self.create_connection()
c = conn.cursor()
c.callproc(query,values)
res = [r.fetchall() for r in c.stored_results()]
if len(res) < 2:
c.callproc(query,values)
res = [r.fetchall() for r in c.stored_results()]
if close:
conn.close()
return res
except Exception as e:
print(e)
###############################################################################
# Provide ready instances for importing
###############################################################################
handler = DBHandler()
def save_xml(model,p_search):
with open(f'{p_search}/'+"#".join(model[1].split('.')[:-1])+'.xml', 'w',encoding='utf8') as f:
f.write(model[2])
f.close()
def fetch_model_ids(p_GUID,p_passwort,p_ID,p_search):
# fetch all model IDs
try:
models = handler.call_proc("get_model_desc_by_runID",(p_GUID,p_passwort,p_ID,p_search),close=True)[1:]
except NotFoundException:
print(f'No models for the search input {p_search} found.')
return []
except Exception as e:
print(e)
exit(1)
ids = [m[0] for m in models[0]]
return ids
def fetch_model_info(p_GUID,p_passwort,p_search,model_id):
try:
model_info = handler.call_proc("get_model_info", (p_GUID,p_passwort,model_id),
close=True)[1:]
except NotFoundException:
print(f'No Model for model ID {model_id} found')
exit(1)
except Exception as e:
print(e)
exit(1)
for mi in model_info[0]:
save_xml(mi,p_search)
print("#".join(sys.argv))
if len(sys.argv) != 5:
if not os.getenv('USER_GUID'):
p_GUID = input('Enter GUID:')
else:
p_GUID = os.getenv('USER_GUID')
if not os.getenv('USER_PASSWORD'):
p_passwort = input('Enter passwort:')
else:
p_passwort = os.getenv('USER_PASSWORD')
if not os.getenv('USER_ID'):
p_ID = input('Enter ID:')
else:
p_ID = os.getenv('USER_ID')
print('Example for a search string to fetch all data from the Run 2667 in Cell 3 Channel A at the wavelength 380 '
'and only 2DSA-Monte Carlo Models:\n"2667.3A380%2DSA-MC"')
p_search = input('Now you can enter your search input. It should contain at least the runID you set while '
'importing the data. If you want specify the Cell, Channel, Wavelength you can do this '
'too. Make sure you seperate the RunId from the other stuff with a dot and enter than Cell '
'Channel or wavelength information, percentage sign should seperate the model information '
'from the other stuff. If you want all Cells or all channels or all wavelengths replace the '
'regarding information with a percentage sign\n')
else:
p_GUID,p_passwort,p_ID,p_search = sys.argv[1:]
model_ids = fetch_model_ids(p_GUID,p_passwort,p_ID,p_search)
if not model_ids:
print('No models found.')
exit(1)
try:
dir = os.path.dirname(__file__)
path_y = p_search.replace("/","").replace("\\","").replace(".","")
filename = os.path.join(dir, f'{path_y}')
os.mkdir(filename)
except Exception as e:
print(e)
pass
print(f'Starting exporting {len(model_ids)} xml files')
for mid in model_ids:
fetch_model_info(p_GUID,p_passwort,filename,mid)
print('Finished')