@@ -34,7 +34,7 @@ def do_outputs(output_data, out_mode, out_name, file_ext):
34
34
else :
35
35
try :
36
36
# output path is given in <out_mode>
37
- output_filename = os .path .join (out_mode , '_ ' .join ((out_name , file_ext )))
37
+ output_filename = os .path .join (out_mode , '' .join ((out_name , file_ext )))
38
38
if not os .path .exists (out_mode ):
39
39
os .makedirs (out_mode )
40
40
with open (output_filename , 'w' , encoding = 'utf-8' ) as outfile :
@@ -191,87 +191,69 @@ def map_xml(element, value, map_data, xml_root):
191
191
192
192
193
193
def status_note (msg ):
194
- print ('' .join (('[metabroker ] ' , msg )))
194
+ print ('' .join (('[o2rmeta][broker ] ' , msg )))
195
195
196
196
197
197
# Main
198
- if __name__ == "__main__" :
199
- if sys .version_info [0 ] < 3 :
200
- # py2
201
- status_note ('requires py3k or later' )
202
- sys .exit ()
198
+ def start (** kwargs ):
199
+ input_dir = kwargs .get ('i' , None )
200
+ output_dir = kwargs .get ('o' , None )
201
+ output_to_console = kwargs .get ('s' , None )
202
+ seperator = '#' #<-- make this generic
203
+ my_map = kwargs .get ('m' , None )
204
+ # output mode
205
+ if output_to_console :
206
+ output_mode = '@s'
207
+ elif output_dir :
208
+ output_mode = output_dir
209
+ if not os .path .isdir (output_dir ):
210
+ status_note ('' .join (('directory at <' , output_dir , '> will be created during extraction...' )))
203
211
else :
204
- my_version = 1
205
- my_mod = ''
206
- try :
207
- my_mod = datetime .datetime .fromtimestamp (os .stat (__file__ ).st_mtime )
208
- except OSError :
209
- pass
210
- status_note ('' .join (('v' , str (my_version ), ' - ' , str (my_mod ))))
211
- parser = argparse .ArgumentParser (description = 'description' )
212
- parser .add_argument ('-m' , '--map' , help = 'name of the mapping file' , required = True )
213
- parser .add_argument ('-i' , '--inputdir' , help = 'input directory' , required = True )
214
- group = parser .add_mutually_exclusive_group (required = True )
215
- group .add_argument ('-o' , '--outputdir' , help = 'output directory for extraction docs' )
216
- group .add_argument ('-s' , '--outputtostdout' , help = 'output the result of the extraction to stdout' , action = 'store_true' , default = False )
217
- args = parser .parse_args ()
218
- args_dict = vars (args )
219
- input_dir = args_dict ['inputdir' ]
220
- output_dir = args_dict ['outputdir' ]
221
- output_to_console = args_dict ['outputtostdout' ]
222
- seperator = '#' #<-- make this generic
223
- my_map = args_dict ['map' ]
224
- # output mode
225
- if output_to_console :
226
- output_mode = '@s'
227
- elif output_dir :
228
- output_mode = output_dir
229
- if not os .path .isdir (output_dir ):
230
- status_note ('' .join (('directory <' , output_dir , '> will be created during extraction...' )))
231
- else :
232
- # not possible currently because output arg group is on mutual exclusive
233
- output_mode = '@none'
212
+ # not possible currently because output arg group is on mutual exclusive
213
+ output_mode = '@none'
234
214
235
- # open map file and find out mode
236
- try :
237
- with open (os .path .join ('mappings' , my_map ), encoding = 'utf-8' ) as data_file :
238
- map_file = json .load (data_file )
239
- settings_data = map_file ['Settings' ]
240
- map_data = map_file ['Map' ]
241
- my_mode = settings_data ['mode' ]
242
- except :
243
- raise
244
- # distinguish format for output
245
- if my_mode == 'json' :
246
- for file in os .listdir (input_dir ):
247
- if os .path .basename (file ).startswith ('meta_' ):
248
- json_output = {}
249
- with open (os .path .join (input_dir , file ), encoding = 'utf-8' ) as data_file :
250
- test_data = json .load (data_file )
251
- for element in test_data :
252
- try :
253
- map_json (element , test_data [element ], map_data , json_output )
254
- except :
255
- raise
256
- do_outputs (json_output , output_mode , 'o2r_' + os .path .splitext (file )[0 ], '.json' )
257
- elif my_mode == 'txt' :
258
- # to do: handle txt based maps like bagit
259
- txt_output = ''
260
- do_outputs (txt_output , output_mode , '.txt' )
261
- elif my_mode == 'xml' :
262
- root = ET .Element (settings_data ['root' ])
263
- # to do: generify for complex xml maps
264
- root .set ('xmlns' , settings_data ['root@xmlns' ])
265
- root .set ('xmlns:xsi' , settings_data ['root@xmlns:xsi' ])
266
- root .set ('xsi:schemaLocation' , settings_data ['root@xsi:schemaLocation' ])
267
- with open (os .path .join ('tests' , 'meta_test1.json' ), encoding = 'utf-8' ) as data_file :
268
- test_data = json .load (data_file )
269
- for element in test_data :
270
- try :
271
- map_xml (element , test_data [element ], map_data , root )
272
- except :
273
- raise
274
- output = ET .tostring (root , encoding = 'utf8' , method = 'xml' )
275
- do_outputs (minidom .parseString (output ).toprettyxml (indent = '\t ' ), output_mode , '.xml' )
276
- else :
277
- print ('[metabroker] ! error: cannot process map mode of <' + my_map + '>' )
215
+ # open map file and find out mode
216
+ try :
217
+ with open (my_map , encoding = 'utf-8' ) as data_file :
218
+ map_file = json .load (data_file )
219
+ settings_data = map_file ['Settings' ]
220
+ map_data = map_file ['Map' ]
221
+ my_mode = settings_data ['mode' ]
222
+ except :
223
+ raise
224
+ # distinguish format for output
225
+ if my_mode == 'json' :
226
+ # try parse all possible metadata files:
227
+ for file in os .listdir (input_dir ):
228
+ if os .path .basename (file ).startswith ('metadata_' ):
229
+ json_output = {}
230
+ with open (os .path .join (input_dir , file ), encoding = 'utf-8' ) as data_file :
231
+ test_data = json .load (data_file )
232
+ for element in test_data :
233
+ try :
234
+ map_json (element , test_data [element ], map_data , json_output )
235
+ except :
236
+ raise
237
+ ##do_outputs(json_output, output_mode, 'o2r_'+os.path.splitext(file)[0], '.json')
238
+ do_outputs (json_output , output_mode , settings_data ['outputfile' ], '.json' )
239
+ elif my_mode == 'txt' :
240
+ # to do: handle txt based maps like bagit
241
+ txt_output = ''
242
+ do_outputs (txt_output , output_mode , '.txt' )
243
+ elif my_mode == 'xml' :
244
+ root = ET .Element (settings_data ['root' ])
245
+ # to do: generify for complex xml maps
246
+ root .set ('xmlns' , settings_data ['root@xmlns' ])
247
+ root .set ('xmlns:xsi' , settings_data ['root@xmlns:xsi' ])
248
+ root .set ('xsi:schemaLocation' , settings_data ['root@xsi:schemaLocation' ])
249
+ with open (os .path .join ('tests' , 'meta_test1.json' ), encoding = 'utf-8' ) as data_file :
250
+ test_data = json .load (data_file )
251
+ for element in test_data :
252
+ try :
253
+ map_xml (element , test_data [element ], map_data , root )
254
+ except :
255
+ raise
256
+ output = ET .tostring (root , encoding = 'utf8' , method = 'xml' )
257
+ do_outputs (minidom .parseString (output ).toprettyxml (indent = '\t ' ), output_mode , '.xml' )
258
+ else :
259
+ status_note ('! error: cannot process map mode of <' + my_map + '>' )
0 commit comments