From 65ed0d678b8ad90988ad64287f69e44dbacc7eea Mon Sep 17 00:00:00 2001 From: Ben Steffen Date: Tue, 26 Nov 2019 16:50:58 -0500 Subject: [PATCH] Remove dicts and structs globals with config, which is a plain dict, and streamline json function usage --- python/invirt/config.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/python/invirt/config.py b/python/invirt/config.py index 3ed586c..b06e74b 100644 --- a/python/invirt/config.py +++ b/python/invirt/config.py @@ -59,11 +59,11 @@ def list_files(): yield from run_parts_list(SRC_DIRPATH) def load_master(): - config = dict() + cfg = dict() for filename in list_files(): with open(filename) as f: - augment(config, yaml.load(f, yaml.CSafeLoader)) - return config + augment(cfg, yaml.load(f, yaml.CSafeLoader)) + return cfg def get_src_mtime(): return max(max(os.path.getmtime(filename) for filename in list_files()), @@ -77,9 +77,7 @@ def load(force_refresh=False): I assume I have the permissions to write to the cache directory. """ - # Namespace container for state variables, so that they can be updated by - # closures. - ns = struct() + cfg = dict() if force_refresh: do_refresh = True @@ -118,7 +116,7 @@ def load(force_refresh=False): # using an atomic filesystem rename in the refreshing stage. try: with open(CACHE_PATH) as f: - ns.cfg = json.read(f.read()) + cfg = json.load(f) except: do_refresh = True @@ -131,17 +129,17 @@ def load(force_refresh=False): # acquire the lock, just try to load the master configuration. try: with invirt.common.open_locked(LOCK_PATH): - ns.cfg = load_master() + cfg = load_master() try: with open(CACHE_PATH + '.tmp', 'w') as f: - f.write(json.write(ns.cfg)) + json.dump(cfg, f) except: pass # silent failure else: os.rename(CACHE_PATH + '.tmp', CACHE_PATH) except IOError: - ns.cfg = load_master() - return ns.cfg + cfg = load_master() -dicts = load() -structs = dicts2struct(dicts, '') + return cfg + +config = load() -- 1.7.9.5