00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2004 00004 # by 00005 # The Board of Trustees of the 00006 # Leland Stanford Junior University. 00007 # All rights reserved. 00008 # 00009 00010 00011 __facility__ = "Online" 00012 __abstract__ = "GLAST functional block base class" 00013 __author__ = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST LAT I&T/Online" 00014 __date__ = ("$Date: 2006/07/17 18:25:20 $").split(' ')[1] 00015 __version__ = "$Revision: 2.23 $" 00016 __release__ = "$Name: R04-12-00 $" 00017 __credits__ = "SLAC" 00018 00019 import LATTE.copyright_SLAC 00020 00021 import gAttr 00022 from LATTE.client.gNode import Node 00023 00024 class Gdb(Node): 00025 """\brief Base class for all GLAST LAT functional blocks. 00026 00027 Uses __getattr__ and __setattr__ methods to feature an SCL like syntax 00028 for referencing attributes 00029 """ 00030 00031 def __init__(self, client, parent, id, valid_attrs=[], addr=None, flags=None): 00032 """gNode constructor. 00033 00034 \param valid_regs A list of valid attribute names. 00035 """ 00036 Node.__init__(self, client) 00037 self.__parent = parent 00038 self.__id = id 00039 if addr is None: 00040 self.__addr = id 00041 else: 00042 self.__addr = addr 00043 00044 attrDict = _regList() 00045 self.__dict__['regs'] = attrDict 00046 00047 #Instantiate all attributes 00048 for attr in valid_attrs: 00049 attrDict[attr.getName()] = attr.create(self) 00050 00051 self.__dict__['_Gdb__flags'] = flags 00052 00053 def up(self): 00054 """Return the parent of this component 00055 """ 00056 return self.__parent 00057 00058 def id(self): 00059 """Return the hardware id of this component 00060 """ 00061 return self.__id 00062 00063 def addr(self): 00064 """Return the hardware address of this component 00065 """ 00066 return self.__addr 00067 00068 def root(self): 00069 """Return the root node in this node's hierarchy 00070 00071 \return The root node 00072 """ 00073 root = self 00074 while root.up() is not None: 00075 root = root.up() 00076 return root 00077 00078 def getIdTuple(self): 00079 """Return the node id(s) in this node's hierarchy 00080 00081 \return The root node 00082 """ 00083 idtuple = [] 00084 root = self 00085 while root.up() is not None: 00086 idtuple.append(root.id()) 00087 root = root.up() 00088 idtuple.reverse() 00089 return tuple(idtuple) 00090 00091 def getName(self): 00092 return self.__class__.__name__ 00093 00094 def getOpaque(self, name=None): 00095 """\brief Retrieves the named opaque data included in the schema 00096 The opaque data needs to be associated with a node therefore this 00097 method only returns the opaque data that is associated with that 00098 node. 00099 00100 \param name Name of the opaque XML data (optional) 00101 00102 \return A list of XML DOM objects matching \a name 00103 or if \a name is None a dictionary of a list 00104 of XML DOM objects 00105 """ 00106 root = self.root() 00107 rname = root.__class__.__name__ 00108 rdict = root.__dict__ 00109 opaque = rdict["_" + rname + '__opaque'] 00110 if self in opaque: 00111 if name is None: 00112 return opaque[self] 00113 elif name in opaque[self]: 00114 return opaque[self][name] 00115 00116 def getSerialNos(self, component=None): 00117 """\brief Retrieves the serial numbers associated with the node 00118 00119 \param component Hardware component identifier (optional) 00120 00121 \return A dictionary matching \a component 00122 or if \a component is None a dictionary of all 00123 components and their serial numbers 00124 """ 00125 root = self.root() 00126 rname = root.__class__.__name__ 00127 rdict = root.__dict__ 00128 serialnos = rdict["_" + rname + '__serialnos'] 00129 if self in serialnos: 00130 if component is None: 00131 return serialnos[self] 00132 elif component in serialnos[self]: 00133 return {component: serialnos[self][component]} 00134 00135 def getExcludeFlags(self): 00136 """\brief Retrieves the exclude reasons associated with the node 00137 00138 \return A list of excludes that match the current node, if there 00139 are no excludes specified then an empty list is returned. 00140 """ 00141 root = self.root() 00142 rname = root.__class__.__name__ 00143 rdict = root.__dict__ 00144 excludes = rdict["_" + rname + '__excludes'] 00145 if self in excludes: 00146 return excludes[self] 00147 else: 00148 return [] 00149 00150 def setFlag(self, key, value): 00151 """\brief Set generic flags for the node. 00152 00153 Flags are passed by gSchemaConfig when the <flags> section 00154 in the schema is parsed. 00155 00156 \param key Entry key, must already exists. 00157 \param value New value to be set for key. 00158 """ 00159 if key in self.__flags: 00160 self.__flags[key] = value 00161 else: 00162 raise AttributeError("Attribute " + key + " not found") 00163 00164 00165 def getFlag(self, key, integer=False): 00166 """\brief Returns the value which corresponds to key. 00167 00168 Returns the value which corresponds to key. 00169 00170 \param key Entry key. 00171 \return Flag value corresponding to key. 00172 """ 00173 if key in self.__flags: 00174 flag = self.__flags[key] 00175 if integer and isinstance(flag, str): return int(flag, 0) 00176 return flag 00177 else: 00178 raise AttributeError("Attribute " + key + " not found") 00179 00180 def attrIter(self): 00181 """\brief Iterator for attributes 00182 00183 Iterates over attribute objects belonging to the current 00184 node returning each object in the order that is specified 00185 in the node's __attrs member. 00186 """ 00187 attrs = getattr(self, "_%s__attrs" % self.__class__.__name__) 00188 for attr in attrs: 00189 yield self.regs[attr.getName()] 00190 00191 ## private: 00192 # 00193 # The following was an attempt to optimize access for mixed case attribute 00194 # names. Unfortunately we run into trouble with the tem.regs['configuration'] 00195 # type notation, and with constructs like 'for reg in regs' which returns 00196 # the attribute in each casing. We punt on it for now (Thu Aug 19 2004) 00197 # 00198 # def __setattr__(self, attribute, value): 00199 # attrDict = self.__dict__ 00200 # if attribute in attrDict: 00201 # attrDict[attribute] = value 00202 # return 00203 # try: 00204 # regs = attrDict['regs'] 00205 # except KeyError: 00206 # attrDict[attribute] = value 00207 # return 00208 # try: 00209 # regs[attribute].set(value, 0, self) 00210 # except KeyError: 00211 # try: 00212 # attr = regs[attribute.lower()] 00213 # except KeyError: 00214 # raise AttributeError("Attribute " + attribute + " not found") 00215 # regs[attribute] = attr 00216 # attr.set(value, 0, self) 00217 00218 # def __getattr__(self, attribute): 00219 # attrDict = self.__dict__ 00220 # if attribute in attrDict: 00221 # return attrDict[attribute] 00222 # try: 00223 # regs = attrDict['regs'] 00224 # return regs[attribute].get(0, self) 00225 # except KeyError: 00226 # try: 00227 # attr = regs[attribute.lower()] 00228 # except KeyError: 00229 # raise AttributeError("Attribute " + attribute + " not found") 00230 # regs[attribute] = attr 00231 # return attr.get(0, self) 00232 00233 def __setattr__(self, attribute, value): 00234 """Gets called when a syntax like 'gtem.configuration = 5' is used""" 00235 attrDict = self.__dict__ 00236 try: 00237 regs = attrDict['regs'] 00238 if attribute in regs: 00239 regs[attribute].set(value, 0, self) 00240 elif attribute in attrDict: 00241 attrDict[attribute] = value 00242 else: 00243 raise AttributeError("Attribute " + attribute + " not found") 00244 except KeyError: 00245 if 'regs' not in attrDict: 00246 attrDict[attribute] = value 00247 else: 00248 raise AttributeError("Attribute " + attribute + " not found") 00249 00250 def __getattr__(self, attribute): 00251 """Gets called when a syntax like 'print gtem.configuration' is used""" 00252 try: 00253 return self.__dict__['regs'][attribute].get(0, self) 00254 except KeyError: 00255 raise AttributeError("Attribute " + attribute + " not found") 00256 00257 class _regList(dict): 00258 def __init__(self): 00259 dict.__init__(self) 00260 00261 def __getitem__(self, attribute): 00262 return dict.__getitem__(self, attribute.lower()) 00263 00264 def __setitem__ (self, attribute, value): 00265 dict.__setitem__(self, attribute.lower(), value) 00266 00267 def get (self, attribute, default=None): 00268 return dict.get(self, attribute.lower(), default) 00269 00270 def has_key(self, attribute): 00271 return dict.has_key(self, attribute.lower()) 00272 00273 def __contains__(self, attribute): 00274 return dict.__contains__(self, attribute.lower()) 00275 00276 class Children(dict): 00277 BCASTID = 0xff 00278 00279 def __init__(self, parent, type): 00280 dict.__init__(self) 00281 self.__parent = parent 00282 self.__type = type 00283 self.__bcast = None 00284 00285 def remove(self, id): 00286 if id in self: 00287 self.__delitem__(id) 00288 elif id == Children.BCASTID: 00289 self.__bcast = None 00290 else: 00291 print "*** gDb: id %d not found for %s %d" %(id, self.__parent.getName(), self.__parent.id()) 00292 00293 def add(self, id): 00294 if id == Children.BCASTID: 00295 if self.__bcast is not None: 00296 print "*** gDb: bcast %s already exists for %s %d" \ 00297 %(self.__bcast.getName(), 00298 self.__parent.getName(), self.__parent.id()) 00299 else: 00300 self.__bcast = self.__type(self.__parent.client(), self.__parent, id) 00301 return self.__bcast 00302 elif id in self: 00303 print "*** gDb: child %s %d already exists for %s %d" \ 00304 %(self[id].getName(), id, 00305 self.__parent.getName(), self.__parent.id()) 00306 else: 00307 self[id] = self.__type(self.__parent.client(), self.__parent, id) 00308 return self[id] 00309 00310 def down(self, id): 00311 if id in self: 00312 return self[id] 00313 elif id == Children.BCASTID: 00314 return self.__bcast 00315 else: 00316 return None 00317 00318 def exists(self, id): 00319 if id in self: 00320 return True 00321 elif id == Children.BCASTID and self.__bcast is not None: 00322 return True 00323 return False 00324 00325 def all(self): 00326 return self.__bcast 00327 00328 class Lem(Node): 00329 ADDR_TEM_BASE = 0x00 00330 ADDR_GEM = 0x10 00331 ADDR_AEM = 0x11 00332 ADDR_EBM = 0x12 00333 ADDR_PDU_BASE = 0x13 00334 ADDR_CRU = 0x1E 00335 ADDR_SLV_BCAST = 0x1F 00336 ADDR_MASTER = 0x20 00337 00338 NPDUS = 2 00339 NTEMS = 16 00340 NCCCS = 4 00341 NCRCS = 4 00342 NCFES = 12 00343 NTCCS = 8 00344 NTRCS = 9 00345 NTFES = 24 00346 NARCS = 12 00347 NAFES = 18 00348 00349 CCCBCAST = 0xFF 00350 CRCBCAST = 0xFF 00351 CFEBCAST = 0xFF 00352 TCCBCAST = 0xFF 00353 TRCBCAST = 0xFF 00354 TFEBCAST = 0xFF 00355 ARCBCAST = 0x1F 00356 AFEBCAST = 0x1F 00357 00358 GEM_CC = 0 00359 GEM_TAM = 1 00360 GEM_STAT = 2 00361 GEM_SCHD = 3 00362 GEM_ROI = 4 00363 GEM_IE = 5 00364 GEM_WIN = 6 00365 00366 # Parity select 00367 PARITY_ODD = 0 00368 PARITY_EVEN = 1 00369 00370 # Parity type 00371 PARITY_CMD_STRING = 0 00372 PARITY_CMD_PAYLOAD = 1 00373 PARITY_ACCESS_DESC = 2 00374 00375 def __init__(self, client): 00376 Node.__init__(self, client) 00377 00378 def setParity(self, parity_type, parity_select): 00379 return self.cmdrsp('LEM_set_parity', parity_type, parity_select) 00380 00381 def getParity(self, parity_type): 00382 rsp = self.cmdrsp('LEM_get_parity', parity_type) 00383 return rsp.status()