📄 nescapp.py
字号:
raise Exception("Anonymous struct reference", child) else: #otherwise, raise an exception #(but first make sure the right kind of unknown type is displayed) if xmlDefinition.tagName == "type-tag": xmlDefinition = child raise Exception("Unknown type", xmlDefinition)class NescEnums( object ) : """A class that holds all enums defined in a specific nesc application. usage: myEnums = NescEnums('/path/to/nescDecls.xml') print myEnums var = myEnums.enumName """ def __init__( self, xmlFilename=None, applicationName="Unknown App" ) : self.applicationName = applicationName self._enums = [] if type(xmlFilename) == str: xmlFilename = findBuildFile(xmlFilename, "nescDecls.xml") xmlFilename = minidom.parse(xmlFilename) self.createEnumsFromXml(xmlFilename) def __getitem__(self, key) : if key in self._enums : return self.__dict__[key] else: raise AttributeError("No such enum defined") def createEnumsFromXml(self, dom) : #now define all the struct types enumDefs = [node for node in dom.getElementsByTagName("enum")] integer = re.compile('^I:(\d+)$') hexidecimal = re.compile('^(0x[\dabcdefABCDEF]+)$') for enumDef in enumDefs : name = enumDef.getAttribute("name") if name in self._enums : continue value = enumDef.getAttribute("value") match = integer.match(value) if match != None : self.__dict__[name] = int(match.groups()[0]) else : match = hexidecimal.match(value) if match != None : self.__dict__[name] = int(match.groups()[0], 16) else : self.__dict__[name] = value self._enums.append(name) namedEnums = [node for node in dom.getElementsByTagName("namedEnum")] for namedEnum in namedEnums : name = namedEnum.getAttribute("name") self.__dict__[name] = NescEnums(namedEnum,name) self._enums.append(name) def __repr__(self) : return "%s object at %s:\n\n\t%s" % (self.__class__, hex(id(self)), str(self)) def __str__(self) : """ Print all available enums.""" string = "\n" for key in self._enums : string += "\t%s = %s\n" % (key, str(self[key])) return string class NescMsgs( object ) : """A class that holds all msgs defined in a specific nesc application. It assumes a struct is a message if AM_STRUCTNAME is defined. usage: myMsgs = NescMsgs(myTypes, myEnums[, applicationName]) print myMsgs var = myMsgs.msgName """ def __init__( self, types, enums, applicationName="Unknown App" ) : self.applicationName = applicationName msgTypes = [enum for enum in enums._enums if enum.find("AM_") ==0] name = re.compile("^AM_(\w+)$") self._msgNames = [] self._msgs = {} for msgType in msgTypes : if type(enums[msgType]) == int: msgName = name.match(msgType) if msgName != None : msgName = msgName.groups()[0] for key in types._typeNames : if key.lower() == msgName.lower() : msg = TosMsg(enums[msgType], types[key]) self._msgs[key] = msg self._msgNames.append(key) break def __getattr__(self, name) : if name in self._msgNames : return deepcopy(self._msgs[name]) else: raise AttributeError("No such message defined") def __getitem__(self, key) : if key in self._msgNames : return deepcopy(self._msgs[key]) else: raise AttributeError("No such message defined") def __repr__(self) : return "%s object at %s:\n\n\t%s" % (self.__class__, hex(id(self)), str(self)) def __str__(self) : """ Print all available msgs.""" string = "\n" for key in self._msgNames : string += "\t%5d : %s\n" % (self._msgs[key].amType, key) return string class Shortcut (object) : """A class that provides all rpc functions and ram symbols for a module""" def __init__(self, moduleName, rpc, ram): self.moduleName = moduleName self.rpc = rpc self.ram = ram def __getattr__(self, name) : try : return self.ram.__getattr__(name) except : try: return self.rpc.__getattr__(name) except : raise Exception("Module %s does not have rpc function or ram symbol %s" % ( self.moduleName, name)) def __repr__(self): return "%s object at %s:\n\n%s" % (self.__class__, hex(id(self)), str(self)) def __str__(self): string = "Module %s\n\n" % self.moduleName if self.ram: string += "%s\n" % str(self.ram) if self.rpc: string += "%s\n" % str(self.rpc) return stringclass NescApp( object ) : """A class that holds all types, enums, msgs, rpc commands and ram symbol definitions as defined for a specific nesc application. usage: myApp = nescApp('/path/to/nescDecls.xml') print myApp var = myApp.enums.enumName var = myApp.types.typeName """ def __init__( self, buildDir=None, motecom=None, tosbase=True, localCommOnly=False, applicationName="Unknown App" ) : """This function creates the NescEnums, NescTypes, NescMsgs, Rpc, and RamSymbol objects for a particular application. It also connects to the network using the motecom value""" #first, import all enums, types, msgs, rpc functions, and ram symbols self.applicationName = applicationName self.buildDir = buildDir self.motecom=motecom self.tosbase=tosbase self.localCommOnly=localCommOnly # Check for the nescDecls.xml file if not os.path.isfile("%snescDecls.xml" % buildDir) : raise Exception("""\nERROR: cannot find file \"%snescDecls.xml\".Your nesC app cannot be imported. Be sure that you compiled with the \"nescDecls\" option.\n\n""" % buildDir) # Import enums, types, and msgs self.enums = NescEnums(buildDir, applicationName) self.types = NescTypes(buildDir, applicationName) self.msgs = NescMsgs(self.types, self.enums, applicationName) # Connect to the network self.connections = [] if self.motecom != None: comm = Comm.Comm() comm.connect(self.motecom) self.connections.append(comm) # Import the rpc commands and ram symbols try: self.rpc = Rpc.Rpc(self) except Exception, e: if len(e.args)>0 and re.search("WARNING: cannot find file", e.args[0]) > 0 : print e.args[0] return # (if there are no rpc commands, we have nothing left to do) else : raise try: self.ramSymbols = RamSymbols.RamSymbols(self) except Exception, e: if re.search("The RamSymbolsM module was not compiled in", e.args[0]) > 0 : print e.args[0] else : raise pass # Create shortcuts to all the application modules moduleNames = {} self._moduleNames = [] moduleName = re.compile('^(\w+).') names = self.rpc._messages.keys() if self.__dict__.has_key("ramSymbols"): names.extend(self.ramSymbols._messages.keys()) names.sort() for name in names : match = moduleName.match(name) if match != None : moduleNames[match.groups(0)[0]] = True for name in moduleNames.keys() : try : rpc = self.rpc.__getattr__(name) except: rpc = None try : ram = self.ramSymbols.__getattr__(name) except: ram = None self.__dict__[name] = Shortcut(name, rpc, ram) self._moduleNames.append(name) self._moduleNames.sort() def __repr__(self) : return "%s object at %s:\n\n%s" % (self.__class__, hex(id(self)), str(self)) def __str__(self) : """ Print all application declarations.""" string = "%20s : %d\n" % ("Enums", len(self.enums._enums)) string += "%20s : %d\n" % ("Types", len(self.types._types)) string += "%20s : %d\n" % ("Messages", len(self.msgs._msgNames)) if self.__dict__.has_key("rpc") : string += "%20s : %d\n" % ("Rpc functions", len(self.rpc._messages.keys())) string += "%20s : %d\n" % ("Ram symbols", len(self.ramSymbols._messages.keys())) string += "%20s : " % ("Modules") for name in self._moduleNames : string += "%s\n%23s" % (name,"") return string
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -