class Handler(ed_xml.EdXml): class meta: tagname = "handler" name = ed_xml.String(required=True) id = ed_xml.String(required=True) # Sub elements commandlist = ed_xml.Model(CommandList, required=False) error = ed_xml.Model(ErrorPattern, required=False) hotspot = ed_xml.Model(HotspotPattern, required=False) def GetCommands(self): clist = dict() if self.commandlist: for cmd in self.commandlist.commands: clist[cmd.name] = cmd.execute return clist def GetErrorPattern(self): if self.error and self.error.pattern: return re.compile(self.error.pattern) return None def GetHotspotPattern(self): if self.hotspot and self.hotspot.pattern: return re.compile(self.hotspot.pattern) return None
class IPCCommand(ed_xml.EdXml): """IPC XML Command""" class meta: tagname = EDXML_IPC filelist = ed_xml.List(ed_xml.Model(IPCFile)) arglist = ed_xml.List(ed_xml.Model(IPCArg))
class ProjectAnalysis(ed_xml.EdXml): """Collection of L{AnalysisResults} for an entire project""" class meta: tagname = "projectanalysis" name = ed_xml.String(require=True) # Project Name resultsets = ed_xml.List(ed_xml.Model(type=AnalysisResults))
class TemplateCollection(ed_xml.EdXml): """List of ProjectTemplates <projectTemplates> <template projName='Foo'> ... <template projName='Bar'> ... </projectTemplates> """ class meta: tagname = "projectTemplates" templates = ed_xml.List(ed_xml.Model(type=ProjectTemplate)) def FindTemplate(self, name): """Find a template based on the given display name @param name: string @return: ProjectTemplateXml or None """ for t in self.templates: if t.GetDisplayName() == name: return t return None def GetTemplateNames(self): """Get a list of template display names""" return [t.GetDisplayName() for t in self.templates]
class Handler(ed_xml.EdXml): class meta: tagname = "handler" name = ed_xml.String(required=True) id = ed_xml.String(required=True) # Sub elements commandlist = ed_xml.Model(CommandList, required=False) error = ed_xml.Model(ErrorPattern, required=False) hotspot = ed_xml.Model(HotspotPattern, required=False) def GetDefaultCommand(self): """Get the default command""" default = u"" if self.commandlist: default = self.commandlist.default return default def GetCommands(self): """Get the list of commands""" clist = dict() if self.commandlist: for cmd in self.commandlist.commands: clist[cmd.name] = cmd.execute return clist def GetErrorPattern(self): """Get the handlers error pattern""" if self.error and self.error.pattern: return re.compile(self.error.pattern) return None def GetHotspotPattern(self): """Get the handlers hotspot pattern""" if self.hotspot and self.hotspot.pattern: return re.compile(self.hotspot.pattern) return None
class Folder(ed_xml.EdXml): """General folder container <folder path="/foo/test></folder>" """ class meta: tagname = "folder" name = ed_xml.String(required=True) optionsets = ed_xml.List(ed_xml.Model(type=OptionSet), required=False) packages = ed_xml.List(ed_xml.Model("package"), required=False) folders = ed_xml.List(ed_xml.Model("folder"), required=False) files = ed_xml.List(ed_xml.Model(type=File), required=False) def CreateOptionSet(self, setname): """Create a new option set. If an existing set of the same name already exists it will be returned instead @param setname: name to associate with the set @return: the option set """ optset = self.GetOptionSet(setname) if optset is None: optset = OptionSet(name=setname) self.optionsets.append(optset) return optset def GetOptionSet(self, setname): """Get the set of options associated with a given name @param setname: name of set @return: L{OptionSet} or None """ for optset in self.optionsets: if optset.name == setname: return optset return None
class OptionSet(ed_xml.EdXml): """Collection of Options used for grouping option categories together. <optionset name="analysis"> <option name='' value=''/> </optionset> """ class meta: tagname = "optionset" name = ed_xml.String(required=True) options = ed_xml.List(ed_xml.Model(type=Option)) def __GetOption(self, optname): for option in self.options: if option.type == optname: return option return None def DeleteOption(self, optname): """Remove an option from the set""" opt = self.__GetOption(optname) if opt: self.options.remove(opt) def GetOption(self, optname): """Get an option from the set @param optname: option name @return: L{Option} """ opt = self.__GetOption(optname) return opt def SetOption(self, optname, value): """Set an options value""" opt = self.__GetOption(optname) if opt: # Update Existing value opt.value = value else: # Add new option to set self.options.append(Option(type=optname, value=value))
class AnalysisResults(ed_xml.EdXml): """Top level XML object <pylint path="/path/to/file"></pylint> """ class meta: tagname = "pylint" path = ed_xml.String(required=True) results = ed_xml.List(ed_xml.Model(Result)) def AddResult(self, line, errType, errMsg): """Add a result to the result list @param line: line number @param errType: error type identifier @param errMsg: error message text """ result = Result() result.line = line result.errType = errType result.errMsg = errMsg self.results.append(result)
class ProjectTemplate(ed_xml.EdXml): """Project template XML The template id is used as the template name for user defined templates and as an internal identifier for the default provided ones. <template id='TemplateID'> </template> """ class meta: tagname = "template" id = ed_xml.String(required=True) # Optional default files and directories files = ed_xml.List(ed_xml.Model(type=ProjectXml.File), required=False) folders = ed_xml.List(ed_xml.Model(type=ProjectXml.Folder), required=False) packages = ed_xml.List(ed_xml.Model(type=ProjectXml.PyPackage), required=False) def GetDisplayName(self): """Get this templates display name @return: unicode """ name = u"" if self.id.startswith(u'__') and self.id.endswith(u'__'): name = MapDisplayName(self.id) if not name: name = self.id else: name = self.id return name def Create(self, basepath, projName): """Create the project on disk based on the template information. @param basepath: path to write project to @param projName: Project Name """ # TODO: deal with already existing folder of same name # TODO: create parent directories as necessary? assert os.path.exists(basepath) def CreateItems(obj, cpath): """Recursively execute the template""" # Create all files for fobj in obj.files: handle = open(os.path.join(cpath, fobj.name), 'wb') if fobj.data: handle.write(fobj.data.encode('utf-8')) # Recurse into each directory dirs = list() dirs.extend(obj.folders) dirs.extend(obj.packages) for dobj in dirs: dobj.name = dobj.name % dict(projName=projName) npath = os.path.join(cpath, dobj.name) os.mkdir(npath) CreateItems(dobj, npath) try: # Make toplevel project directory ppath = os.path.join(basepath, projName) if not os.path.exists(ppath): # Create the project from scratch os.mkdir(ppath) # Recursively execute template creation CreateItems(self, ppath) # else assume importing existing except OSError, msg: util.Log("[PyProject][err] Failed to create new project (%s)" % projName) util.Log("[PyProject][err] %s" % msg) return False # TODO: error reporting to user return True