root/gaphor/tags/gaphor-0.12.0/gaphor/services/properties.py

Revision 1229, 3.2 kB (checked in by arj..@yirdis.nl, 2 years ago)

implemented Properties service application wide

Line 
1 #!/usr/bin/env python
2 """
3 """
4
5 import sys
6 import types
7 import os
8 import pprint
9 from zope import interface
10 from gaphor.interfaces import IService
11
12 if os.name == 'nt':
13     home = 'USERPROFILE'
14 else:
15     home = 'HOME'
16
17 user_data_dir = os.path.join(os.getenv(home), '.gaphor')
18
19
20 _no_default = object()
21
22 class Properties(object):
23     """
24     The Properties class holds a collection of application wide properties.
25
26     Properties are persisted
27     """
28     interface.implements(IService)
29
30     def __init__(self, backend=None):
31         self._resources = {}
32         self._backend = backend or FileBackend()
33
34     def init(self, app):
35         self._backend.load(self)
36    
37     def shutdown(self):
38         self._backend.save(self)
39
40     def __call__(self, r, default=_no_default):
41         return self.get(r, default)
42
43     def save(self):
44         self._backend.save(self)
45
46     def _items(self):
47         return self._resources.iteritems()
48
49     def dump(self, stream=sys.stdout):
50         """
51         TODO: define resources that are persistent (have to be saved
52         and loaded.
53         """
54         import pprint
55         pprint.pprint(self._resources.items(), stream)
56
57     def get(self, r, default=_no_default):
58         """
59         Locate a property.
60
61         Resource should be the class of the resource to look for or a string. In
62         case of a string the resource will be looked up in the GConf configuration.
63         """
64         _resources = self._resources
65
66         # Return the existing resource
67         try:
68             return _resources[r]
69         except KeyError:
70             pass
71
72         if default is not _no_default:
73             self.set(r, default)
74             return default
75         raise KeyError, 'No resource with name "%s"' % r
76
77     def set(self, r, value):
78         """
79         Set a property to a specific value.
80
81         No smart things are done with classes and class names (like the
82         resource() method does).
83         """
84         self._resources[r] = value
85
86     def persist(self, r, value):
87         """
88         Save the property to a persistent storage.
89         """
90         self._backend.update(r, value)
91
92
93 class FileBackend(object):
94     """
95     Resource backend that stores data to a resource file
96     ($HOME/.gaphor/resource).
97     """
98     RESOURCE_FILE='resources'
99    
100     def __init__(self):
101         pass
102
103     def get_filename(self, datadir, create=False):
104         if create and not os.path.exists(datadir):
105             os.mkdir(datadir)
106         return os.path.join(datadir, self.RESOURCE_FILE)
107
108     def load(self, resource):
109         filename = self.get_filename(user_data_dir)
110         if os.path.exists(filename) and os.path.isfile(filename):
111             f = open(filename)
112             d = f.read()
113             f.close()
114             for k, v in eval(d).iteritems():
115                 resource.set(k, v)
116
117     def save(self, resource):
118         """
119         Save persist resources from the resources dictionary.
120         @resource is the Resource instance
121         @persistent is a list of persistent resource names.
122         """
123         filename = self.get_filename(user_data_dir, create=True)
124         f = open(filename, 'w')
125         pprint.pprint(resource._resources, f)
126
127     def update(self, r, value):
128         pass
129
130
131 # vim:sw=4:et
Note: See TracBrowser for help on using the browser.