import os import sys import re args = sys.argv if len(args) < 2: print('need to pass path!') exit() if len(args) < 3: print('need to pass dbname!') path = args[1] dbname = args[2] extfilter = [] if len(args) >= 4: extfilters = args[3].split(',') print('Arg Path: {}'.format(path)) print('DB Name: {}'.format(dbname)) print('Ext Filters: {}'.format(extfilters)) output = '''{ "version": "1.2", "default_core_path": "", "default_core_name": "", "label_display_mode": 0, "right_thumbnail_mode": 0, "left_thumbnail_mode": 0, "items": [\n%ITEMS%] } ''' itemstr = '' for root, dirs, files in os.walk(path): for filename in sorted(files): filepath = '{}\\{}'.format(root, filename) filepath = filepath.replace('/', '\\') filepath = filepath.replace('\\', '\\\\') if len(extfilters) != 0 and not os.path.splitext(os.path.basename(filepath))[1] in extfilters: continue label = os.path.splitext(os.path.basename(filepath))[0] label = label.replace(' [!]', '') label = re.sub(r'^\d{4} - ', '', label) core_path = 'DETECT' core_name = 'DETECT' crc32 = '00000000|crc' itemstr += ''' { "path": "%PATH%", "label": "%LABEL%", "core_path": "%COREPATH%", "core_name": "%CORENAME%", "crc32": "%CRC%", "db_name": "%DBNAME%" },\n''' itemstr = itemstr.replace('%PATH%', filepath) itemstr = itemstr.replace('%LABEL%', label) itemstr = itemstr.replace('%COREPATH%', core_path) itemstr = itemstr.replace('%CORENAME%', core_name) itemstr = itemstr.replace('%CRC%', crc32) itemstr = itemstr.replace('%DBNAME%', dbname) output = output.replace('%ITEMS%', itemstr) with open('{}'.format(dbname), 'wt') as out_file: out_file.write(output)