@@ -37,16 +37,19 @@ def _write_rows(writer, entity, name):
3737class EntityCsvExporter (object ):
3838 """Read a Discogs dump XML file and exports SQL table records as CSV.
3939 """
40- def __init__ (self , entity , in_dir , out_dir ,
40+ def __init__ (self , entity , in_file_or_dir , out_dir ,
4141 limit = None , bz2 = True ,
4242 dry_run = False , debug = False , max_hint = None , verbose = False ):
4343 self .entity = entity
4444 self .parser = _parsers [entity ]()
4545 self .max_hint = max_hint
4646 self .verbose = verbose
4747
48- lookup = 'discogs_[0-9]*_{}s.xml*' .format (entity )
49- self .pattern = os .path .join (in_dir , lookup )
48+ if os .path .isfile (in_file_or_dir ):
49+ self .pattern = in_file_or_dir
50+ else :
51+ lookup = 'discogs_[0-9]*_{}s.xml*' .format (entity )
52+ self .pattern = os .path .join (in_file_or_dir , lookup )
5053
5154 # where and how the exporter will write to
5255 self .out_dir = out_dir
@@ -287,8 +290,7 @@ def write_track_artists(self, writer, release):
287290
288291
289292def main (arguments ):
290- in_base = arguments ['INPUT' ]
291- out_base = arguments ['OUTPUT' ] or '.'
293+ out_base = arguments ['--output' ] or '.'
292294 limit = int (arguments ['--limit' ]) if arguments ['--limit' ] else None
293295 bz2_on = arguments ['--bz2' ]
294296 debug = arguments ['--debug' ]
@@ -312,14 +314,38 @@ def main(arguments):
312314 except Exception :
313315 pass
314316
315- for entity in arguments ['--export' ]:
316- expected_count = rough_counts ['{}s' .format (entity )]
317- exporter = _exporters [entity ](
318- in_base ,
319- out_base ,
320- limit = limit ,
321- bz2 = bz2_on ,
322- debug = debug ,
323- max_hint = min (expected_count , limit or expected_count ),
324- dry_run = dry_run )
325- exporter .export ()
317+ if arguments ["INPUT_DIR" ] and os .path .isdir (arguments ["INPUT_DIR" ]):
318+ # use --export to select the entities
319+ in_base = arguments ['INPUT_DIR' ]
320+ for entity in arguments ['--export' ]:
321+ expected_count = rough_counts ['{}s' .format (entity )]
322+ exporter = _exporters [entity ](
323+ in_base ,
324+ out_base ,
325+ limit = limit ,
326+ bz2 = bz2_on ,
327+ debug = debug ,
328+ max_hint = min (expected_count , limit or expected_count ),
329+ dry_run = dry_run )
330+ exporter .export ()
331+ elif arguments ["<INPUT_FILE>" ] or os .path .isfile (arguments ["INPUT_DIR" ]):
332+ files = []
333+ if arguments ["<INPUT_FILE>" ]:
334+ files = arguments ["<INPUT_FILE>" ]
335+ else :
336+ files = [arguments ["INPUT_DIR" ]]
337+ for in_file in files :
338+ for entity in _exporters :
339+ # discogs files are named discogs_{date}_{entity}s.xml
340+ if f"_{ entity } " in in_file :
341+ expected_count = rough_counts ['{}s' .format (entity )]
342+ exporter = _exporters [entity ](
343+ in_file ,
344+ out_base ,
345+ limit = limit ,
346+ bz2 = bz2_on ,
347+ debug = debug ,
348+ max_hint = min (expected_count , limit or expected_count ),
349+ dry_run = dry_run )
350+ exporter .export ()
351+ break
0 commit comments