Ignore:
Timestamp:
Jul 29, 2008, 1:35:57 AM (16 years ago)
Author:
y_z
Message:
  • moved more generic code into common package
  • silently fail if cache fails
  • load the configuration on module load
  • produce a struct-based representation of the configuration
  • allowing full exception messages for OSErrors (default behavior)
  • added some dependencies specs
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py

    r771 r778  
    11import json, yaml
     2from invirt.common import *
    23from os import error, makedirs
    34from os.path import dirname, getmtime
     
    89try:    default_loader = yaml.CSafeLoader
    910except: default_loader = yaml.SafeLoader
    10 
    11 def wrap(rsrc, func):
    12     "Utility to that emulates with Python 2.5's `with closing(rsrc)`."
    13     try: return func(rsrc)
    14     finally: rsrc.close()
    1511
    1612def load(src_path = default_src_path,
     
    3127        else:           do_refresh  = src_mtime > cache_mtime
    3228
     29    if not do_refresh:
     30        # try reading from the cache first
     31        try: cfg = wrap(file(cache_path), lambda f: json.read(f.read()))
     32        except: do_refresh = True
     33
    3334    if do_refresh:
    3435        # reload the source and regenerate the cache
    3536        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)))
    37     else:
    38         cfg = wrap(file(cache_path), lambda f: json.read(f.read()))
     37        try: wrap(file(cache_path, 'w'), lambda f: f.write(json.write(cfg)))
     38        except: pass # silent failure
    3939    return cfg
    4040
     41dicts = load()
     42structs = dicts2struct(dicts)
     43
    4144# vim:et:sw=4:ts=4
Note: See TracChangeset for help on using the changeset viewer.