def loadModule(cls, name): """Load a hardware module.""" # Module name sanity check try: if not name.strip(): raise ValueError for c in name: if not isalnum(c) and c != "_": raise ValueError except ValueError: raise AwlSimError("Hardware module name '%s' " "is invalid." % name) # Create import name (add prefix) importName = name prefix = "awlsimhw_" if importName.lower().startswith(prefix) and\ not importName.startswith(prefix): raise AwlSimError("Hardware module name: '%s' " "prefix of '%s' must be all-lower-case." %\ (prefix, name)) if not importName.startswith(prefix): importName = prefix + importName # Try to import the module try: mod = importModule(importName) except ImportError as e: raise AwlSimError("Failed to import hardware interface " "module '%s' (import name '%s'): %s" %\ (name, importName, str(e))) return cls(name, importName, mod)
def getInterface(self): """Get the HardwareInterface class.""" hwClassName = "HardwareInterface" hwClass = getattr(self.mod, hwClassName, None) if not hwClass: raise AwlSimError("Hardware module '%s' (import name '%s') " "does not have a '%s' class." %\ (self.name, self.importName, hwClassName)) return hwClass
def raiseException(self, errorText): """Throw an exception.""" raise AwlSimError("[%s hardware module] %s" %\ (self.name, errorText))