From 10cf51ef5e0c9cfd0921c0a2b1a3c9523ac49a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Heger?= Date: Sat, 21 Nov 2020 11:04:06 +0100 Subject: [PATCH] Better file handling and added comments for future self (refs #50 #53) --- loreroll/npc.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/loreroll/npc.py b/loreroll/npc.py index 9c2a491..49febd5 100644 --- a/loreroll/npc.py +++ b/loreroll/npc.py @@ -40,11 +40,22 @@ def _read_data(): to read the pre-parsed JSON version. If that fails or if it has been generated for a different YAML data version, read the source YAML and try and store the data in JSON format for future runs. - """ + Note that this approach could theoretically cause trouble if the user + didn't have write permission to the JSON file but if the appropriate JSON + data is shipped along with YAML source data, this shouldn't be an issue. + JSON will only be regenerated if YAML changes and if user can modify YAML, + write permission to JSON should be available as well since these two are in + the same directory. And even if that somehow isn't true, the consequence is + "only" a performance drop. + + Still there probably is a better solution - see github issue #53: + https://github.com/geckon/rollthelore/issues/53 + """ def _handle_caching_error(operation, error): - # Catch and ignore (just print warning) any error. This is just - # "caching" and we want to continue with the rest in any case. + """Print just a warning. + + This is "caching" and we want to continue in any case.""" print( f'WARNING: Could not {operation} the cache JSON file ' f'("{NPC_JSON_FILENAME}"): {error}' @@ -69,7 +80,8 @@ def _handle_caching_error(operation, error): npc_data = load(yaml_data, NPC_SCHEMA).data try: npc_data['yaml_datafile_checksum'] = yaml_data_checksum - json.dump(npc_data, open(NPC_JSON_FILENAME, 'w')) + with open(NPC_JSON_FILENAME, 'w') as json_datafile: + json.dump(npc_data, json_datafile) except Exception as error: _handle_caching_error('store', error) return npc_data