From cd9ce5933841550253705c68cebf0147f2f2b9ff Mon Sep 17 00:00:00 2001 From: Lingchaox Xin Date: Tue, 18 Jun 2013 17:39:33 +0800 Subject: [PATCH] Pylint for common/mapping.py Change-Id: Ibbac7014c7c02daeb4f034a504e1b96587128b1c --- common/mapping.py | 106 +++++++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/common/mapping.py b/common/mapping.py index b32554f..96defc0 100644 --- a/common/mapping.py +++ b/common/mapping.py @@ -1,3 +1,5 @@ +"""Handle XML data with Python objects""" + import re import xml.sax.handler import os @@ -9,30 +11,37 @@ def xml2obj(src): non_id_char = re.compile('[^_0-9a-zA-Z]') def _name_mangle(name): + """Split name though regular expressions""" return non_id_char.sub('_', name) class DataNode(object): + """A custom class used to wrap XML attributes and child elements""" def __init__(self): self._attrs = {} # XML attributes and child elements self.data = None # child text data def __len__(self): # treat single element as a list of 1 return 1 + def __setitem__(self, key, value): + self._attrs[key] = value def __getitem__(self, key): if isinstance(key, basestring): - return self._attrs.get(key,None) + return self._attrs.get(key, None) else: return [self][key] + def __delitem__(self, key): + self._attrs.pop(key) def __contains__(self, name): - return self._attrs.has_key(name) + return name in self._attrs def __nonzero__(self): return bool(self._attrs or self.data) def __getattr__(self, name): if name.startswith('__'): # need to do this for Python special methods??? raise AttributeError(name) - return self._attrs.get(name,None) + return self._attrs.get(name, None) def _add_xml_attr(self, name, value): + """Add XML attribute""" if name in self._attrs: # multiple attribute of the same name are represented by a list children = self._attrs[name] @@ -48,14 +57,17 @@ def xml2obj(src): items = sorted(self._attrs.items()) if self.data: items.append(('data', self.data)) - return u'{%s}' % ', '.join([u'%s:%s' % (k,repr(v)) for k,v in items]) + return u'{%s}' % ', '.join([u'%s:%s' % (k, repr(v)) for k, v in items]) class TreeBuilder(xml.sax.handler.ContentHandler): + """A wrapper class of ContentHandler""" def __init__(self): + xml.sax.handler.ContentHandler.__init__(self) self.stack = [] self.root = DataNode() self.current = self.root self.text_parts = [] + # pylint: disable-msg=C0103 def startElement(self, name, attrs): self.stack.append((self.current, self.text_parts)) self.current = DataNode() @@ -74,12 +86,13 @@ def xml2obj(src): obj = text or '' self.current, self.text_parts = self.stack.pop() self.current._add_xml_attr(_name_mangle(name), obj) + # pylint: enable-msg=C0103 def characters(self, content): self.text_parts.append(content) builder = TreeBuilder() - if isinstance(src,basestring): + if isinstance(src, basestring): xml.sax.parseString(src, builder) else: xml.sax.parse(src, builder) @@ -87,19 +100,20 @@ def xml2obj(src): class Mapping(): - """ class to handle mapping xml file """ + """A class to handle mapping xml file """ def __init__(self, mapping_file): """ convert xml to git obj """ if not os.path.isfile(mapping_file): - raise Exception, "Fatal: %s is not a regular file" %(mapping_file) + raise Exception, "Fatal: %s is not a regular file" % mapping_file - with open(mapping_file) as fh: - self.mapping_obj = xml2obj(''.join(fh.readlines())) + with open(mapping_file) as filei: + self.mapping_obj = xml2obj(''.join(filei.readlines())) if not self.mapping_obj: raise Exception, "Fatal: parser xml file %s failed" - def __encode_to_ascii(self, source): + @staticmethod + def __encode_to_ascii(source): """ encode unicode list element to ascii """ dest = [] for i in source: @@ -118,44 +132,34 @@ class Mapping(): # return None elif branch: - # - # - # or - # for brch in obj.branch: # Search the match branch if brch.name == branch: if brch.submission == 'N': return None else: - mapping.append(self.__encode_to_ascii([brch.OBS_project, - brch.OBS_staging_project, - brch.OBS_package])) + mapping.append(self.__encode_to_ascii(\ + [brch.OBS_project, + brch.OBS_staging_project, + brch.OBS_package])) else: for brch in obj.branch: if brch.submission == 'N': continue else: - mapping.append(self.__encode_to_ascii([brch.OBS_project, - brch.OBS_staging_project, - brch.OBS_package])) + mapping.append(self.__encode_to_ascii(\ + [brch.OBS_project, + brch.OBS_staging_project, + brch.OBS_package])) return mapping def get_submit_mapping(self, project, branch = None): - # Search in project list first + """Get submit mapping though project or branch""" for prj in self.mapping_obj.project: if prj.name == project or prj.name == '/' + project: - # - # - # or - # - mapping = self.__get_mapping(prj, branch) - if mapping == None: - # submission is blocked, return empty list - return [] - elif mapping: - # retrun if already found matched OBS target project - return mapping + # submission is blocked, return empty list, otherwise + # return matched OBS target project + return self.__get_mapping(prj, branch) or [] # Search in path list prj_path = project @@ -163,21 +167,10 @@ class Mapping(): # parent directory of project/directory prj_path = os.path.dirname(os.path.join('/', prj_path)) - # Match with default '/', of no branch match, return NULL - # - # - # - # if prj_path == '/': for path in self.mapping_obj.default.path: if path.name == '/': - mapping = self.__get_mapping(path, branch) - if not mapping: - # does not map to any obs project, return empty list - return [] - else: - # retrun if already found matched OBS target project - return mapping + return self.__get_mapping(path, branch) or [] # No branch match, return NULL return [] @@ -190,33 +183,31 @@ class Mapping(): # # # - mapping = self.__get_mapping(path, branch) - if mapping == None: - # submission is blocked, return empty list - return [] - elif mapping: - # retrun if already found matched OBS target project - return mapping + return self.__get_mapping(path, branch) or [] def get_integration_mapping(self, project, branch): + """Get integration mapping of project and branch""" # Search in project list for prj in self.mapping_obj.integration_testing.project: if prj.name == project: if prj.branch: for brch in prj.branch: if brch.name == branch: - # temporary project default is ${brch.OBS_project}:review + # temporary project default is + # ${brch.OBS_project}:review if brch.Temp_project: temp_project = brch.Temp_project else: - temp_project = "%s:review" %(brch.OBS_project) + temp_project = "%s:review" % brch.OBS_project if brch.OBS_package and brch.OBS_project: return [ brch.OBS_project, - "%s:%s" %(temp_project, brch.OBS_package)] + "%s:%s" % (temp_project, \ + brch.OBS_package)] elif brch.OBS_project: return [ brch.OBS_project, - "%s:%s" %(temp_project, os.path.basename(project))] + "%s:%s" % (temp_project, \ + os.path.basename(project))] # No branch match in project define, search default prj_path = project while True: @@ -230,10 +221,11 @@ class Mapping(): if brch.Temp_project: temp_project = brch.Temp_project else: - temp_project = "%s:review" %(brch.OBS_project) + temp_project = "%s:review" % brch.OBS_project return [brch.OBS_project, - "%s:%s" %(temp_project, os.path.basename(project))] + "%s:%s" % (temp_project, \ + os.path.basename(project))] return [] return [] return [] -- 2.7.4