1 """Finds device attributes from the graphcap
2
3 $Id: graphcap.py 1276 2010-10-04 14:44:39Z sontag $
4 """
5 from __future__ import division
6
7 import string, filecache
8
10 out = []
11 outbuff = []
12 for inline in inlines:
13 tline = inline.strip()
14 if len(tline) > 0 and tline[0] != '#':
15 if tline[-1] == '\\':
16
17 outbuff.append(tline[:-1])
18 else:
19 outbuff.append(tline)
20 out.append(''.join(outbuff))
21 outbuff = []
22 return out
23
25
26 aend = entry.find(':')
27 if aend<0:
28 raise ValueError("Graphcap entry does not have any colons\n%s" % entry)
29 return entry[:aend].split("|")[:-1]
30
32 abeg = entry.find(':')
33 if abeg<0:
34 raise ValueError("Graphcap entry does not have any colons\n%s" % entry)
35 astring = entry[abeg+1:]
36 attr = {}
37 attrlist = astring.split(':')
38 for attrstr in attrlist:
39 if attrstr.strip():
40 attrname = attrstr[:2]
41 attrval = attrstr[2:]
42 if len(attrstr) <= 2:
43 value = -1
44 elif attrval[0] == '=':
45 value = attrval[1:]
46 elif attrval[0] == '#':
47 try:
48 value = int(attrval[1:])
49 except ValueError:
50 try:
51 value = float(attrval[1:])
52 except ValueError:
53 print "problem reading graphcap"
54 raise
55 elif attrval[0] == '@':
56
57 value = None
58 else:
59
60
61
62
63 pass
64 attr[attrname] = value
65 return attr
66
75
77
78 """Graphcap class that automatically updates if file changes"""
79
82
84 """Called on init and if file changes"""
85 lines = open(self.filename,'r').readlines()
86 mergedlines = merge(lines)
87 self.dict = getDevices(mergedlines)
88
91
93 """Get up-to-date version of dictionary"""
94 dict = self.get()
95 if not dict.has_key(key):
96 print "Error: device not found in graphcap"
97 raise KeyError
98 return Device(dict, key)
99
101 dict = self.get()
102 if dict.has_key(key):
103 return 1
104 else:
105 return 0
106
108
110 self.dict = devices
111 self.devname = devname
112
114 dict = self.dict[self.devname]
115 value = None
116 while 1:
117 if dict.has_key(attrName):
118 value = dict[attrName]
119 break
120 else:
121 if dict.has_key('tc'):
122 nextdev = dict['tc']
123 dict = self.dict[nextdev]
124 else:
125 break
126 return value
127
129 if isinstance(other, Device):
130 return cmp(id(self.dict[self.devname]),
131 id(other.dict[other.devname]))
132 else:
133 return cmp(id(self), id(other))
134
137