Ignore:
Timestamp:
Oct 24, 2008, 3:35:21 AM (16 years ago)
Author:
broder
Message:

Now that we're using Python 2.5, we can actually write with statements

File:
1 edited

Legend:

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

    r816 r1197  
     1from __future__ import with_statement
     2
    13import json
    24from invirt.common import *
    35from os import rename
    46from os.path import getmtime
     7from contextlib import closing
    58
    69default_src_path   = '/etc/invirt/master.yaml'
    710default_cache_path = '/var/lib/invirt/cache.json'
    8 lock_file          = '/var/lib/invirt/cache.lock'
     11lock_path          = '/var/lib/invirt/cache.lock'
    912
    1013def load(src_path = default_src_path,
     
    5457        # lock with other concurrent reads).  This isolation is accomplished
    5558        # using an atomic filesystem rename in the refreshing stage.
    56         try: ns.cfg = with_closing(file(cache_path)) (
    57                 lambda f: json.read(f.read()))
     59        try:
     60            with closing(file(cache_path)) as f:
     61                ns.cfg = json.read(f.read())
    5862        except: do_refresh = True
    5963
     
    6973        except: loader = yaml.SafeLoader
    7074        try:
    71             @with_lock_file(lock_file)
    72             def refresh_cache():
    73                 ns.cfg = with_closing(file(src_path)) (
    74                         lambda f: yaml.load(f, loader))
    75                 try: with_closing(file(cache_path + '.tmp', 'w')) (
    76                         lambda f: f.write(json.write(ns.cfg)))
     75            with lock_file(lock_path):
     76                with closing(file(src_path)) as f:
     77                    ns.cfg = yaml.load(f, loader)
     78                try:
     79                    with closing(file(cache_path + '.tmp', 'w')) as f:
     80                        f.write(json.write(ns.cfg))
    7781                except: pass # silent failure
    7882                else: rename(cache_path + '.tmp', cache_path)
    7983        except IOError:
    80             ns.cfg = with_closing(file(src_path)) (
    81                     lambda f: yaml.load(f, loader))
     84            with closing(file(src_path)) as f:
     85                ns.cfg = yaml.load(f, loader)
    8286    return ns.cfg
    8387
Note: See TracChangeset for help on using the changeset viewer.