Skip to content

Commit f736e26

Browse files
Improve function utils_write_JSON_file()
1 parent 1b14e50 commit f736e26

3 files changed

Lines changed: 9 additions & 34 deletions

File tree

resources/db.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,8 @@ def db_get_asset_cache_row(cfg, cache_index_dic, catalog_name, category_name):
11741174
# -------------------------------------------------------------------------------------------------
11751175
# Load and save a bunch of JSON files
11761176
# -------------------------------------------------------------------------------------------------
1177-
#
11781177
# Accepts a list of JSON files to be loaded. Displays a progress dialog.
11791178
# Returns a dictionary with the context of the loaded files.
1180-
#
11811179
def db_load_files(db_files):
11821180
log_debug('db_load_files() Loading {} JSON database files...'.format(len(db_files)))
11831181
db_dic = {}
@@ -1192,15 +1190,15 @@ def db_load_files(db_files):
11921190

11931191
return db_dic
11941192

1195-
def db_save_files(db_files, json_write_func = utils_write_JSON_file):
1193+
def db_save_files(db_files):
11961194
log_debug('db_save_files() Saving {} JSON database files...'.format(len(db_files)))
11971195
d_text = 'Saving databases...'
11981196
pDialog = KodiProgressDialog()
11991197
pDialog.startProgress(d_text, len(db_files))
12001198
for f_item in db_files:
12011199
dict_data, db_name, db_path = f_item
12021200
pDialog.updateProgressInc('{}\nDatabase [COLOR orange]{}[/COLOR]'.format(d_text, db_name))
1203-
json_write_func(db_path, dict_data)
1201+
utils_write_JSON_file(db_path, dict_data)
12041202
pDialog.endProgress()
12051203

12061204
# -------------------------------------------------------------------------------------------------

resources/mame.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4955,12 +4955,6 @@ def mame_build_MAME_main_database(cfg, st_dic):
49554955

49564956
# --- Save databases ---
49574957
log_info('Saving database JSON files...')
4958-
if OPTION_LOWMEM_WRITE_JSON:
4959-
json_write_func = utils_write_JSON_file_lowmem
4960-
log_debug('Using utils_write_JSON_file_lowmem() JSON writer')
4961-
else:
4962-
json_write_func = utils_write_JSON_file
4963-
log_debug('Using utils_write_JSON_file() JSON writer')
49644958
db_files = [
49654959
[machines, 'MAME machines main', cfg.MAIN_DB_PATH.getPath()],
49664960
[renderdb_dic, 'MAME render DB', cfg.RENDER_DB_PATH.getPath()],
@@ -4981,7 +4975,7 @@ def mame_build_MAME_main_database(cfg, st_dic):
49814975
# --- Save control_dic after everything is saved ---
49824976
[control_dic, 'Control dictionary', cfg.MAIN_CONTROL_PATH.getPath()],
49834977
]
4984-
db_save_files(db_files, json_write_func)
4978+
db_save_files(db_files)
49854979

49864980
# Return a dictionary with references to the objects just in case they are needed after
49874981
# this function (in "Build everything", for example). This saves time, because databases do not
@@ -5545,30 +5539,21 @@ def mame_build_ROM_audit_databases(cfg, st_dic, db_dic_in):
55455539
db_safe_edit(control_dic, 't_MAME_Audit_DB_build', time.time())
55465540

55475541
# --- Save databases ---
5548-
if OPTION_LOWMEM_WRITE_JSON:
5549-
json_write_func = utils_write_JSON_file_lowmem
5550-
log_debug('Using utils_write_JSON_file_lowmem() JSON writer')
5551-
else:
5552-
json_write_func = utils_write_JSON_file
5553-
log_debug('Using utils_write_JSON_file() JSON writer')
55545542
db_files = [
55555543
[audit_roms_dic, 'MAME ROM Audit', cfg.ROM_AUDIT_DB_PATH.getPath()],
55565544
[machine_archives_dic, 'Machine file list', cfg.ROM_SET_MACHINE_FILES_DB_PATH.getPath()],
55575545
# --- Save control_dic after everything is saved ---
55585546
[control_dic, 'Control dictionary', cfg.MAIN_CONTROL_PATH.getPath()],
55595547
]
5560-
db_save_files(db_files, json_write_func)
5548+
db_save_files(db_files)
55615549
# Add data generated in this function to dictionary for caller code use.
55625550
db_dic_in['audit_roms'] = audit_roms_dic
55635551
db_dic_in['machine_archives'] = machine_archives_dic
55645552

5565-
#
55665553
# Checks for errors before scanning for SL ROMs.
55675554
# Display a Kodi dialog if an error is found.
55685555
# Returns a dictionary of settings:
55695556
# options_dic['abort'] is always present.
5570-
#
5571-
#
55725557
def mame_check_before_build_MAME_catalogs(cfg, st_dic, control_dic):
55735558
kodi_reset_status(st_dic)
55745559

@@ -7232,12 +7217,6 @@ def mame_build_SoftwareLists_databases(cfg, st_dic, db_dic_in):
72327217
db_safe_edit(control_dic, 't_SL_DB_build', time.time())
72337218

72347219
# --- Save modified/created stuff in this function ---
7235-
if OPTION_LOWMEM_WRITE_JSON:
7236-
json_write_func = utils_write_JSON_file_lowmem
7237-
log_debug('Using utils_write_JSON_file_lowmem() JSON writer')
7238-
else:
7239-
json_write_func = utils_write_JSON_file
7240-
log_debug('Using utils_write_JSON_file() JSON writer')
72417220
db_files = [
72427221
# Fix this list of files!!!
72437222
[SL_catalog_dic, 'Software Lists index', cfg.SL_INDEX_PATH.getPath()],
@@ -7246,18 +7225,16 @@ def mame_build_SoftwareLists_databases(cfg, st_dic, db_dic_in):
72467225
# Save control_dic after everything is saved.
72477226
[control_dic, 'Control dictionary', cfg.MAIN_CONTROL_PATH.getPath()],
72487227
]
7249-
db_save_files(db_files, json_write_func)
7228+
db_save_files(db_files)
72507229
db_dic_in['SL_index'] = SL_catalog_dic
72517230
db_dic_in['SL_machines'] = SL_machines_dic
72527231
db_dic_in['SL_PClone_dic'] = SL_PClone_dic
72537232

72547233
# -------------------------------------------------------------------------------------------------
72557234
# ROM/CHD and asset scanner
72567235
# -------------------------------------------------------------------------------------------------
7257-
#
72587236
# Checks for errors before scanning for SL ROMs.
72597237
# Display a Kodi dialog if an error is found.
7260-
#
72617238
def mame_check_before_scan_MAME_ROMs(cfg, st_dic, options_dic, control_dic):
72627239
log_info('mame_check_before_scan_MAME_ROMs() Starting...')
72637240
kodi_reset_status(st_dic)

resources/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ def utils_load_JSON_file(json_filename, default_obj = {}, verbose = True):
365365
# Note that there is a bug in the json module where the ensure_ascii=False flag can produce
366366
# a mix of unicode and str objects.
367367
# See http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
368-
def utils_write_JSON_file(json_filename, json_data, verbose = True, pprint = False, lowmem = False):
368+
def utils_write_JSON_file(json_filename, json_data, verbose = True, pprint = False):
369369
l_start = time.time()
370370
if verbose: log_debug('utils_write_JSON_file() "{}"'.format(json_filename))
371371

372372
# Choose JSON iterative encoder or normal encoder.
373-
if lowmem:
374-
if verbose: log_debug('utils_write_JSON_file() Using lowmem option')
373+
if OPTION_LOWMEM_WRITE_JSON:
374+
if verbose: log_debug('utils_write_JSON_file() Using OPTION_LOWMEM_WRITE_JSON option')
375375
if pprint:
376376
jobj = json.JSONEncoder(ensure_ascii = False, sort_keys = True,
377377
indent = JSON_INDENT, separators = JSON_SEP)
@@ -396,7 +396,7 @@ def utils_write_JSON_file(json_filename, json_data, verbose = True, pprint = Fal
396396
# Write JSON to disk
397397
try:
398398
with io.open(json_filename, 'wt', encoding = 'utf-8') as file:
399-
if lowmem:
399+
if OPTION_LOWMEM_WRITE_JSON:
400400
# Chunk by chunk JSON writer, uses less memory but takes longer.
401401
for chunk in jobj.iterencode(json_data):
402402
file.write(text_type(chunk))

0 commit comments

Comments
 (0)