Пример #1
0
    def component_headers(self, element, args):
        """Format all relevant commit metadata in an email-style header box"""

        message   = args.message
        commit    = XML.dig(message.xml, "message", "body", "commit")
        source    = XML.dig(message.xml, "message", "source")
        author    = XML.dig(commit, "author")
        version   = XML.dig(commit, "version")
        revision  = XML.dig(commit, "revision")
        diffLines = XML.dig(commit, "diffLines")
        url       = XML.dig(commit, "url")
        log       = XML.dig(commit, "log")
        project   = XML.dig(source, "project")
        module    = XML.dig(source, "module")
        branch    = XML.dig(source, "branch")
        headers   = OrderedDict()

        if author:
            headers['Author'] = XML.shallowText(author)
        if project:
            headers['Project'] = XML.shallowText(project)
        if module:
            headers['Module'] = XML.shallowText(module)
        if branch:
            headers['Branch'] = XML.shallowText(branch)
        if version:
            headers['Version'] = XML.shallowText(version)
        if revision:
            headers['Revision'] = XML.shallowText(revision)
        if diffLines:
            headers['Changed Lines'] = XML.shallowText(diffLines)
        if url:
            headers['URL'] = tag('a', href=XML.shallowText(url))[ Util.extractSummary(url) ]

        return [Template.MessageHeaders(headers)]
Пример #2
0
    def component_files(self, element, args):
        """Format the contents of our <files> tag as a tree with nested lists"""
        from cia.LibCIA.Web import Template

        files = XML.dig(args.message.xml, "message", "body", "commit", "files")
        if not (files and XML.hasChildElements(files)):
            return []

        # First we organize the files into a tree of nested dictionaries.
        # The dictionary we ultimately have FileTree render maps each node
        # (file or directory) to a dictionary of its contents. The keys
        # in these dictionaries can be any Nouvelle-renderable object
        # produced by format_file.
        #
        # As a first step, we build a dictionary mapping path segment to
        # [fileTag, children] lists. We then create a visual representation
        # of each fileTag and generate the final dictionary.
        fileTree = {}
        for fileTag in XML.getChildElements(files):
            if fileTag.nodeName == 'file':
                # Separate the file into path segments and walk into our tree
                node = [None, fileTree]
                for segment in XML.shallowText(fileTag).split('/'):
                    if segment:
                        node = node[1].setdefault(segment, [None, {}])
                # The leaf node owns this fileTag
                node[0] = fileTag

        return [Template.FileTree(self.format_file_tree(fileTree))]
Пример #3
0
    def consolidateFiles(self, xmlFiles):
        """Given a <files> element, find the directory common to all files
           and return a 2-tuple with that directory followed by
           a list of files within that directory.
           """
        files = []
        if xmlFiles:
            for fileTag in XML.getChildElements(xmlFiles):
                if fileTag.nodeName == 'file':
                    files.append(XML.shallowText(fileTag))

        # If we only have one file, return it as the prefix.
        # This prevents the below regex from deleting the filename
        # itself, assuming it was a partial filename.
        if len(files) == 1:
            return files[0], []

        # Start with the prefix found by commonprefix,
        # then actually make it end with a directory rather than
        # possibly ending with part of a filename.
        prefix = re.sub("[^/]*$", "", posixpath.commonprefix(files))

        endings = []
        for file in files:
            ending = file[len(prefix):].strip()
            if ending == '':
                    ending = '.'
            endings.append(ending)
        return prefix, endings
Пример #4
0
 def element_like(self, element):
     """Compare a given variable to the element's content using SQL's 'LIKE' operator,
        not including leading and trailing whitespace. This is case-insensitive, and includes
        the '%' wildcard which may be placed at the beginning or end of the string.
        """
     return "(%s LIKE %s)" % (self.varLookup(element.getAttributeNS(None, 'var')),
                              quote(XML.shallowText(element).strip(), 'varchar'))
Пример #5
0
 def matches(self, msg):
     match = self.projectPath.queryObject(msg)
     if match:
         for node in match:
             project = XML.shallowText(node)
             if (project.strip().lower() in self.projects):
                 return True
     return False
Пример #6
0
 def textComponent(self, element, args, *path):
     """A convenience function for defining components that just look for a node
        in the message and return its shallowText.
        """
     element = XML.dig(args.message.xml, *path)
     if element:
         return [XML.shallowText(element)]
     else:
         return [MarkAsHidden()]
Пример #7
0
 def format(self, args):
     if not args.input:
         return
     project = XML.dig(args.message.xml, "message", "source", "project")
     if project:
         from cia.LibCIA.IRC.Formatting import format
         prefix = format("%s:" % XML.shallowText(project), 'bold') + " "
         return "\n".join([prefix + line for line in args.input.split("\n")])
     else:
         return args.input
Пример #8
0
    def component_text(self, element, args):
        """This is a generic version of textComponent, in which 'path' can
           be specified by users. Any textComponent can be rewritten as a
           <text> component.
           """
        path = element.getAttributeNS(None, 'path')
        if not path:
            raise XML.XMLValidityError("The 'path' attribute on <text> is required.")
        xp = XML.XPath(XML.pathShortcuts.get(path, path))

        nodes = xp.queryObject(args.message)
        if nodes:
            return [XML.shallowText(nodes[0])]
        else:
            return [MarkAsHidden()]
Пример #9
0
    def pathMatchTag(self, element, function, textExtractor=XML.shallowText):
        """Implements the logic common to all tags that test the text matched by
           an XPath against the text inside our element. The given function is used
           to determine if the text matches. This implements the properties common to
           several elements:

             - The caseSensitive attribute defaults to 1, but can be set to zero
               to force both strings to lowercase.

             - Each XPath match is tested separately, with 'or' semantics:
               if any of the XPath matches cause the provided function to match,
               this returns True

             - If there are no XPath matches, returns False
           """
        path = element.getAttributeNS(None, 'path')
        xp = XML.XPath(XML.pathShortcuts.get(path, path))

        # Are we doing a case sensitive match? Default is no.
        caseSensitive = element.getAttributeNS(None, 'caseSensitive')
        if caseSensitive:
            caseSensitive = int(caseSensitive)
        else:
            caseSensitive = 0

        text = XML.shallowText(element).strip()
        if not caseSensitive:
            text = text.lower()

        def filterMatch(msg):
            # Use queryobject then str() so that matched
            # nodes without any text still give us at least
            # the empty string. This is important so that <find>
            # with an empty search string can be used to test
            # for the existence of an XPath match.
            nodes = xp.queryObject(msg)
            if nodes:
                matchStrings = map(textExtractor, nodes)

                # Any of the XPath matches can make our match true
                for matchString in matchStrings:
                    matchString = matchString.strip()
                    if not caseSensitive:
                        matchString = matchString.lower()
                    if function(matchString, text):
                        return True
            return False
        return filterMatch
Пример #10
0
def getNormalizedLog(xml, tabWidth=8):
    """Given the DOM node for a <log> tag, return a list of
       text lines with whitespace normalized appropriately.
       This strips all whitespace from the right side, and homogeneously
       strips whitespace from the left side as much as possible.
       Leading and trailing blank lines are removed, but internal
       blank lines are not.
       """
    if not xml:
        return []

    lines = []
    maxLeftStrip = None

    for line in XML.shallowText(xml).split("\n"):
        # Expand tabs and strip righthand whitespace
        line = line.replace("\t", " "*tabWidth).rstrip()
        strippedLine = line.lstrip()

        # Blank lines don't count in determining the left strip amount
        if strippedLine:
            # Determine how much we can strip from the left side
            leftStrip = len(line) - len(strippedLine)

            # Determine the maximum amount of space we can strip
            # from the left side homogeneously across the whole text
            if maxLeftStrip is None or leftStrip < maxLeftStrip:
                maxLeftStrip = leftStrip

        # Skip leading blank lines
        if lines or strippedLine:
            lines.append(line)

    # Remove trailing blank lines
    while lines and not lines[-1].strip():
        del lines[-1]

    # Homogeneous left strip
    if maxLeftStrip is None:
        return lines
    else:
        return [line[maxLeftStrip:] for line in lines]
Пример #11
0
def getCrunchedLog(xml):
    """Given the DOM node for a <log> tag, return the log as
       a string with all groups of one or more whitespace
       characters replaced with a single space.
       """
    return re.sub("\s+", " ", XML.shallowText(xml)).strip()
Пример #12
0
 def param_widthLimit(self, tag):
     self.widthLimit = int(XML.shallowText(tag))
     if self.wrapWidth > self.widthLimit:
         self.wrapWidth = self.widthLimit
Пример #13
0
 def loadParametersFrom(self, xml):
     self.formattingCode = XML.shallowText(xml).strip()
Пример #14
0
 def rulesetReturn(msg):
     self.result = XML.shallowText(element)
     if not self.result:
         self.result = None
     raise RulesetReturnException()
Пример #15
0
 def component_url(self, element, args):
     element = XML.dig(args.message.xml, "message", "body", "commit", "url")
     if element:
         return [tag('a', href=XML.shallowText(element))[ 'link' ]]
     else:
         return [Message.MarkAsHidden()]
Пример #16
0
 def param_lineLimit(self, tag):
     self.lineLimit = int(XML.shallowText(tag))
Пример #17
0
 def element_match(self, element):
     """Compare a given variable exactly to the element's content, not including
        leading and trailing whitespace.
        """
     return "(%s = %s)" % (self.varLookup(element.getAttributeNS(None, 'var')),
                           quote(XML.shallowText(element).strip(), 'varchar'))
Пример #18
0
 def param_filesWidthLimit(self, tag):
     self.filesWidthLimit = int(XML.shallowText(tag))
Пример #19
0
 def getValue(self, message):
     project = XML.dig(message.xml, "message", "source", "project")
     if project:
         return XML.shallowText(project)
Пример #20
0
 def param_wrapWidth(self, tag):
     self.wrapWidth = int(XML.shallowText(tag))
Пример #21
0
 def format_timestamp(self, stamp):
     return ["Received ", Template.value[TimeUtil.formatRelativeDate(int(XML.shallowText(stamp)))]]