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 Option(ed_xml.EdXml): """General Option <option type="view" value="*.py"/> """ class meta: tagname = "option" type = ed_xml.String(required=True) value = ed_xml.String(required=True)
class IPCArg(ed_xml.EdXml): """Xml object for holding the list of args @verbatim <arg name="test" value="x"/> @endverbatim """ class meta: tagname = EDXML_ARG name = ed_xml.String(required=True) value = ed_xml.String(required=True)
class Result(ed_xml.EdXml): """Individual result <result line="0" errType="Warning" errMsg="Line too long"/> """ class meta: tagname = "result" line = ed_xml.String(required=True) errType = ed_xml.String(required=True) errMsg = ed_xml.String(required=True)
class File(ed_xml.EdXml): """Xml element to represent a file item (used by ProjectTemplate) <file name='__init__.py'> # File: __init__.py ''' docstring ''' </file> """ class meta: tagname = "file" name = ed_xml.String(required=True) data = ed_xml.String(tagname="data", required=False) # optional specify initial file contents
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 IPCFile(ed_xml.EdXml): """Xml object for holding the list of files @verbatim <file value="/path/to/file"/> @endverbatim """ class meta: tagname = EDXML_FILE value = ed_xml.String(required=True)
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 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 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 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
class CommandList(ed_xml.EdXml): class meta: tagname = "commandlist" default = ed_xml.String(required=True) commands = ed_xml.List(Command)
class Command(ed_xml.EdXml): class meta: tagname = "command" name = ed_xml.String(required=True) execute = ed_xml.String(required=True)
class HotspotPattern(ed_xml.EdXml): class meta: tagname = "hotspot" pattern = ed_xml.String(required=True)
class ErrorPattern(ed_xml.EdXml): class meta: tagname = "error" pattern = ed_xml.String(required=True)
class ProjectXml(Folder): """Main project structure""" class meta: tagname = "project" version = ed_xml.String(default=PYPROJ_XML_SCHEMA_VERSION)