2 from os import error, makedirs
3 from os.path import dirname, getmtime
5 default_src_path = '/etc/invirt/master.yaml'
6 default_cache_path = '/var/lib/invirt/invirt.json'
8 try: default_loader = yaml.CSafeLoader
9 except: default_loader = yaml.SafeLoader
12 "Utility to that emulates with Python 2.5's `with closing(rsrc)`."
13 try: return func(rsrc)
16 def load(src_path = default_src_path,
17 cache_path = default_cache_path,
18 force_refresh = False):
20 Try loading the configuration from the faster-to-load JSON cache at
21 cache_path. If it doesn't exist or is outdated, load the configuration
22 instead from the original YAML file at src_path and regenerate the cache.
23 I assume I have the permissions to write to the cache directory.
28 src_mtime = getmtime(src_path)
29 try: cache_mtime = getmtime(cache_path)
30 except OSError: do_refresh = True
31 else: do_refresh = src_mtime > cache_mtime
34 # reload the source and regenerate the cache
35 cfg = wrap(file(src_path), lambda f: yaml.load(f, default_loader))
36 wrap(file(cache_path, 'w'), lambda f: f.write(json.write(cfg)))
38 cfg = wrap(file(cache_path), lambda f: json.read(f.read()))