88import logging
99from urllib .parse import urlparse
1010
11- logging .basicConfig (filename = 'app.log' , level = logging .DEBUG )
11+ os .makedirs ('logs' ,exist_ok = True )
12+ logfile = 'logs/app.log'
13+
14+ if os .path .exists (logfile ): os .remove (logfile )
15+ logging .basicConfig (filename = logfile , level = logging .INFO )
1216logger = logging .getLogger (__name__ )
1317
1418
1519sparql_url = 'http://fuseki:3030/n4o'
16- def lido2rdf_url (): return f'http://converter:5000/convert'
17- def importer_url (coll ): return f'http://importer:5020/collection/{ coll } /load'
20+ def lido2rdf_url ():
21+ return f'http://converter:5000/convert'
22+ def importer_url (coll ):
23+ return f'http://importer:5020/collection/{ coll } '
1824
1925
2026app = Flask (__name__ , template_folder = 'templates' , static_folder = 'static' , static_url_path = '/assets' )
@@ -84,10 +90,12 @@ def home():
8490 username = request .cookies .get ('username' )
8591 collection = 'default'
8692 if user := find_user (username ):
87- collection = user ['collection' ] or 'default'
88- data_dir = './admin_data/'
89- with open (data_dir + user ['samplefile' ], 'r' ) as f :
90- user ['profile_data' ] = f .read ()
93+ collection = user .get ('collection' , 'default' )
94+ user ['profile_data' ] = '<empty/>'
95+ if s_file := user .get ('samplefile' ):
96+ data_dir = './admin_data/'
97+ with open (data_dir + s_file , 'r' ) as f :
98+ user ['profile_data' ] = f .read ()
9199 return render_template ('index.html' , collection = collection , user = jsonify (user ).json )
92100 else :
93101 return redirect (url_for ('login' ))
@@ -134,19 +142,22 @@ def convert_lido():
134142 ''''Convert LIDO XML to RDF using the external service'''
135143 # curl -X POST -H "Content-Type: application/json" -d '{"data":"<lido/>", "format":"nt"}' converter:5000/runMappings
136144 data = request .json ['data' ]
137- logger .info (f'Converting LIDO 2 data of length { len (data )} ' )
145+ logger .info (f'Converting Lido data ( { len (data )} bytes) ' )
138146 return requests .post (f'http://{ lhost ()} :5000/convert' , data = data ).text
139147
140148
141149@app .route ('/import_ttl' , methods = ['POST' ])
142150def import_ttl ():
143151 ''''Import TTL data into the RDF store'''
144- if coll := request .json .get ('coll_index' ):
152+ if collection_index := request .json .get ('coll_index' ):
145153 if data := request .json ['data' ]:
146- fn = f'import_{ coll } .ttl'
147- with open (f'./data/{ fn } ' , 'w' ) as f : f .write (data )
148- res = requests .post (f'http://importer:5020/collection/{ coll } /receive?from={ fn } ' )
149- return requests .post (f'http://importer:5020/collection/{ coll } /load' ).text , res .status_code
154+ # Copy data to file, then call importer services receive and load
155+ import_file = f'import_{ collection_index } .ttl'
156+ with open (f'./data/{ import_file } ' , 'w' ) as f : f .write (data )
157+ service = importer_url (collection_index )
158+ res = requests .post (f'{ service } /receive?from={ import_file } ' )
159+ logger .info (f'Importing data by { service } , response: { res .status_code } ' )
160+ return requests .post (f'{ service } /load' ).text , res .status_code
150161 return jsonify (message = 'No data or collection index provided' )
151162
152163
@@ -159,7 +170,7 @@ def read_yaml(fname):
159170
160171if __name__ == '__main__' :
161172 parser = AP .ArgumentParser ()
162- parser .add_argument ('-w' , '--wsgi' , action = AP . BooleanOptionalAction , help = "Use WSGI server" )
173+ parser .add_argument ('-w' , '--wsgi' , action = 'store_true' , help = "Use WSGI server" )
163174 parser .add_argument ('-p' , '--port' , type = int , default = 5010 , help = "Server port" )
164175 parser .add_argument ('-c' , '--config' , type = str , default = "config.yaml" , help = "Config file" )
165176 args = parser .parse_args ()
@@ -168,7 +179,11 @@ def read_yaml(fname):
168179 if config_data := read_yaml (args .config ):
169180 sparql_url = config_data ["fuseki-server" ]["uri" ]
170181
171- if user_data := read_yaml ('users.yaml' ):
182+ user_data = read_yaml ('users.yaml' )
183+ if not user_data :
184+ user_data = read_yaml ('admin.yaml' )
185+
186+ if user_data :
172187 users = user_data ['users' ]
173188 else :
174189 quit (stderr = 'No users found in users.yaml' )
0 commit comments