expand with_* docstrings in invirt.common
[invirt/packages/invirt-base.git] / files / usr / share / python-support / sipb-xen-base / invirt / common.py
1 import unittest
2 from fcntl import flock, LOCK_EX, LOCK_UN
3 from os import remove
4
5 class struct(object):
6     'A simple namespace object.'
7     def __init__(self, d = {}, **kwargs):
8         'd is the dictionary to update my __dict__ with.'
9         self.__dict__.update(d)
10         self.__dict__.update(kwargs)
11
12 def dicts2struct(x):
13     """
14     Given a tree of lists/dicts, perform a deep traversal to transform all the
15     dicts to structs.
16     """
17     if type(x) == dict:
18         return struct((k, dicts2struct(v)) for k,v in x.iteritems())
19     elif type(x) == list:
20         return [dicts2struct(v) for v in x]
21     else:
22         return x
23
24 #
25 # Hacks to work around lack of Python 2.5's `with` statement.
26 #
27
28 def with_closing(rsrc):
29     """
30     Utility to emulate Python 2.5's `with closing(rsrc)` context manager.
31
32     E.g.,
33     @with_closing(file('/tmp/foo'))
34     def contents(f):
35         return f.read()
36     # now 'contents' is the contents of /tmp/foo
37     """
38     def wrapper(func):
39         try: return func(rsrc)
40         finally: rsrc.close()
41     return wrapper
42
43 def with_lock_file(path):
44     """
45     Context manager for lock files.  Example:
46
47     @with_lock_file('/tmp/mylock')
48     def input():
49         print 'locked'
50         return raw_input()
51     # prints 'locked'
52     print input # prints what raw_input() returned
53     """
54     def wrapper(func):
55         @with_closing(file(path, 'w'))
56         def g(f):
57             flock(f, LOCK_EX)
58             try: return func()
59             finally: flock(f, LOCK_UN)
60         remove(path)
61         return g
62     return wrapper
63
64 #
65 # Tests.
66 #
67
68 class common_tests(unittest.TestCase):
69     def test_dicts2structs(self):
70         dicts = {
71                 'atom': 0,
72                 'dict': { 'atom': 'atom', 'list': [1,2,3] },
73                 'list': [ 'atom', {'key': 'value'} ]
74                 }
75         structs = dicts2struct(dicts)
76         self.assertEqual(structs.atom,        dicts['atom'])
77         self.assertEqual(structs.dict.atom,   dicts['dict']['atom'])
78         self.assertEqual(structs.dict.list,   dicts['dict']['list'])
79         self.assertEqual(structs.list[0],     dicts['list'][0])
80         self.assertEqual(structs.list[1].key, dicts['list'][1]['key'])
81
82 if __name__ == '__main__':
83     unittest.main()