source: people/peter.buschman/backup_monitoring/builtins/_builtins.py@ 976

Last change on this file since 976 was 976, checked in by peter, on Dec 6, 2011 at 10:19:33 AM

Raw checkin of current NetBackup / TSM parsing code.

File size: 4.0 KB
RevLine 
[976]1
2import __builtin__
3import filelike
4
5from ..dependencies.odict import OrderedDict
6
7__all__ = [
8 'adict',
9 'fmt_pair',
10 'StringAsFile',
11 'ExtendedDict',
12]
13
14
15##
16## Dictionary with attribute access
17##
18class adict(dict):
19
20 def __init__(self, dict={}):
21 self.update(dict)
22
23 def __getattr__(self, name):
24 if self.has_key(name):
25 return self[name]
26 else:
27 raise AttributeError, 'Attribute \'%s\' does not exist in dictionary' % (name)
28
29 def __setattr__(self, name, value):
30 self[name] = value
31
32
33##
34## Extended Dictionary Class
35##
36#class ExtendedDict(dict):
37#
38# def __init__(self, dict={}):
39# self.update(dict)
40#
41# def __getattr__(self, name):
42# if self.has_key(name):
43# return self[name]
44# else:
45# raise AttributeError, 'Attribute \'%s\' does not exist in dictionary' % (name)
46#
47# def __setattr__(self, name, value):
48# self[name] = value
49#
50# def __iter__(self):
51# return iter(self.values())
52
53
54##
55## Extended Dictionary Class
56##
57#class ExtendedDict(object):
58#
59# def __init__(self):
60# self.odict = OrderedDict()
61#
62# def __getitem__(self, key):
63# return self.odict[key]
64#
65# def __setitem__(self, key, value):
66# self.odict[key] = value
67#
68# def __getattr__(self, name):
69# if name in self.odict:
70# return self.odict[name]
71# else:
72# raise AttributeError, 'Attribute \'%s\' does not exist in dictionary' % (name)
73#
74# def __setattr__(self, name, value):
75# if name in self.odict:
76# pass
77# else:
78# object.__setattr__(self, name, value)
79#
80# def __iter__(self):
81# return self.odict.__iter__()
82#
83# def keys(self):
84# return self.odict.keys()
85
86
87##
88## Extended Dictionary Class
89##
90## INSPIRATION: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916
91##
92class ExtendedDict(OrderedDict):
93
94 def __init__(self, attribute=None, *args, **kwargs):
95
96 OrderedDict.__init__(self, *args, **kwargs)
97
98 def __getattr__(self, name):
99
100 if self.__dict__.has_key(name):
101 return OrderedDict.__getattr__(self, name)
102 else:
103 try:
104 return self.__getitem__(name)
105 except KeyError:
106 raise AttributeError(name)
107
108 def __setattr__(self, name, value):
109
110 #if self.__dict__.has_key(name):
111 # return OrderedDict.__setattr__(self, name, value)
112 #else:
113 # return OrderedDict.__setitem__(self, name, value)
114
115 if self.has_key(name):
116 return OrderedDict.__setitem__(self, name, value)
117 else:
118 return OrderedDict.__setattr__(self, name, value)
119
120 def __delattr__(self, name):
121
122 if self.has_key(name):
123 return OrderedDict.__delitem__(self, name)
124 else:
125 return OrderedDict.__delattr__(self, name)
126
127 def __iter__(self):
128
129 return OrderedDict.itervalues(self)
130
131
132##
133## Pretty print name / value pairs as name...: value.
134##
135def fmt_pair(name, value, width=25):
136 ln = len(name)
137 dotcount = 0
138 if ln <= width:
139 dotcount = width - ln
140 return '%s%s: %s' % (name, '.' * dotcount, value)
141
142
143##
144## String filelike object
145##
146class StringAsFile(filelike.FileLikeBase):
147
148 def __init__(self,string):
149 filelike.FileLikeBase.__init__(self)
150 self._string = string
151 self._pos = 0
152
153 def _read(self,sizehint=-1):
154 if self._pos >= len(self._string):
155 return None
156 if sizehint < 0:
157 newPos = len(self._string)
158 else:
159 newPos = min(self._pos + sizehint,len(self._string))
160 data = self._string[self._pos:newPos]
161 self._pos = newPos
162 return data
163
164
165##
166## Add our builtins to the builtin namespace
167##
168for name in __all__:
169 exec 'setattr(__builtin__, name, %s)' % (name)
Note: See TracBrowser for help on using the repository browser.