Package pyraf :: Module graphcap
[hide private]
[frames] | no frames]

Source Code for Module pyraf.graphcap

  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 # confidence high 
  6   
  7  import string, filecache 
  8   
9 -def merge(inlines):
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 # continuation 17 outbuff.append(tline[:-1]) 18 else: 19 outbuff.append(tline) 20 out.append(''.join(outbuff)) 21 outbuff = [] 22 return out
23
24 -def getAliases(entry):
25 # return list of aliases (and dump the comment) 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
31 -def getAttributes(entry):
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 # implies false 57 value = None 58 else: 59 # ignore silently, at least as long as IRAF has a bad 60 # entry in its distribution (illegal colons) 61 # print "problem reading graphcap attributes: ", attrstr 62 # print entry 63 pass 64 attr[attrname] = value 65 return attr
66
67 -def getDevices(devlist):
68 devices = {} 69 for devdef in devlist: 70 aliases = getAliases(devdef) 71 attributes = getAttributes(devdef) 72 for alias in aliases: 73 devices[alias] = attributes 74 return devices
75
76 -class GraphCap(filecache.FileCache):
77 78 """Graphcap class that automatically updates if file changes""" 79
80 - def __init__(self, graphcapPath):
81 filecache.FileCache.__init__(self, graphcapPath)
82
83 - def updateValue(self):
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
89 - def getValue(self):
90 return self.dict
91
92 - def __getitem__(self, key):
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
100 - def has_key(self, key):
101 dict = self.get() 102 if dict.has_key(key): 103 return 1 104 else: 105 return 0
106
107 -class Device:
108
109 - def __init__(self, devices, devname):
110 self.dict = devices 111 self.devname = devname
112
113 - def getAttribute(self, attrName):
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
128 - def __cmp__(self, other):
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
135 - def __getitem__(self, key):
136 return self.getAttribute(key)
137