예제 #1
0
파일: generate.py 프로젝트: ryfow/critic
def generateHighlight(repository_path, sha1, language, output_file=None):
    highlighter = createHighlighter(language)
    if not highlighter: return False

    source = gitutils.Repository.readObject(repository_path, "blob", sha1)
    source = textutils.decode(source)

    if output_file:
        highlighter(source, output_file, None)
    else:
        output_path = syntaxhighlight.generateHighlightPath(sha1, language)

        try: os.makedirs(os.path.dirname(output_path), 0750)
        except OSError, error:
            if error.errno == errno.EEXIST: pass
            else: raise

        output_file = open(output_path + ".tmp", "w")
        contexts_path = output_path + ".ctx"

        highlighter(source, output_file, contexts_path)

        output_file.close()

        os.chmod(output_path + ".tmp", 0660)
        os.rename(output_path + ".tmp", output_path)
예제 #2
0
    def fromGitObject(db, repository, gitobject, commit_id=None):
        assert gitobject.type == "commit"

        data = gitobject.data
        parents = []

        while True:
            line, data = data.split('\n', 1)

            if not line:
                break

            key, value = line.split(' ', 1)

            if key == 'tree': tree = value
            elif key == 'parent': parents.append(value)
            elif key == 'author': author = CommitUserTime.fromValue(value)
            elif key == 'committer': committer = CommitUserTime.fromValue(value)

        message = textutils.decode(data).encode("utf-8")

        commit = Commit(repository, commit_id, gitobject.sha1, parents, author,
                        committer, message, tree)
        commit.__cache(db)
        return commit
예제 #3
0
def generateHighlight(repository_path, sha1, language, mode, output_file=None):
    highlighter = createHighlighter(language)
    if not highlighter: return False

    source = gitutils.Repository.readObject(repository_path, "blob", sha1)
    source = textutils.decode(source)

    if output_file:
        highlighter(source, output_file, None)
    else:
        output_path = syntaxhighlight.generateHighlightPath(
            sha1, language, mode)

        try:
            os.makedirs(os.path.dirname(output_path), 0750)
        except OSError as error:
            if error.errno == errno.EEXIST: pass
            else: raise

        output_file = open(output_path + ".tmp", "w")
        contexts_path = output_path + ".ctx"

        if mode == "json":
            outputter = JSONOutputter(output_file)
        else:
            outputter = HTMLOutputter(output_file)

        highlighter(source, outputter, contexts_path)

        output_file.close()

        os.chmod(output_path + ".tmp", 0660)
        os.rename(output_path + ".tmp", output_path)

    return True
예제 #4
0
    def loadNewLines(self, highlighted=False, request_highlight=False):
        """Load the lines of the new version of the file, optionally highlighted."""

        from diff.parse import splitlines

        if self.new_sha1 is None or self.new_sha1 == '0' * 40:
            self.new_plain = []
            self.new_highlighted = []
            return
        elif self.new_mode and self.new_mode == "160000":
            self.new_plain = self.new_highlighted = ["Subproject commit %s" % self.new_sha1]
            return

        if highlighted:
            if self.new_highlighted and self.new_is_highlighted: return
            else:
                self.new_is_highlighted = True
                language = self.getLanguage(use_content="new")
                if language:
                    data = syntaxhighlight.readHighlight(self.repository, self.new_sha1, self.path, language, request=request_highlight)
                elif self.new_highlighted: return
                else:
                    data = htmlutils.htmlify(textutils.decode(self.repository.fetch(self.new_sha1).data))
                self.new_highlighted = splitlines(data)
                self.new_eof_eol = data and data[-1] in "\n\r"
        else:
            if self.new_plain: return
            else:
                data = self.repository.fetch(self.new_sha1).data
                self.new_plain = splitlines(data)
                self.new_eof_eol = data and data[-1] in "\n\r"
예제 #5
0
파일: html.py 프로젝트: Aessy/critic
    def process(token):
        if token[0] == '<':
            return Tag(token)
        else:
            def replace_decimal(match):
                return unichr(int(match.group(1)))

            token = textutils.decode(token)
            token = re_decimal_entity.sub(replace_decimal, token)
            token = token.encode("utf-8")

            return token.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&")
예제 #6
0
def jsify(what, as_json=False):
    if what is None: return "null"
    elif isinstance(what, bool): return "true" if what else "false"
    elif isinstance(what, int) or isinstance(what, long): return str(what)
    else:
        what = textutils.decode(what)
        result = json.dumps(what)
        if not as_json:
            quote = result[0]
            return result.replace("</", "<%s+%s/" % (quote, quote)).replace("<!", "<%s+%s!" % (quote, quote))
        else:
            return result
예제 #7
0
def jsify(what, as_json=False):
    if what is None: return "null"
    elif isinstance(what, bool): return "true" if what else "false"
    elif isinstance(what, int) or isinstance(what, long): return str(what)
    else:
        what = textutils.decode(what)
        result = json.dumps(what)
        if not as_json:
            quote = result[0]
            return result.replace("</", "<%s+%s/" % (quote, quote)).replace(
                "<!", "<%s+%s!" % (quote, quote))
        else:
            return result
예제 #8
0
    def __shush_to_hush__(self, type_id, payload):
        if type_id == "h":
            fields = [textutils.decode(f).strip() for f in ltd.split(payload)]

            if len(fields) >= 2 and fields[0] == "shush":
                payload = bytearray()

                payload.extend("hush\1".encode())

                for field in fields[1:]:
                    payload.extend(field.encode())

                self.log.debug("Message transformed: type='h', command='hush'")

        return type_id, payload
예제 #9
0
파일: analyze.py 프로젝트: yanlimin9/critic
def analyzeWhiteSpaceLine(deletedLine, insertedLine):
    deletedLine = textutils.decode(deletedLine)
    insertedLine = textutils.decode(insertedLine)

    deletedWords = filter(None, re_ws_words.findall(deletedLine))
    insertedWords = filter(None, re_ws_words.findall(insertedLine))

    sm = difflib.SequenceMatcher(None, deletedWords, insertedWords)
    lineDiff = []

    for tag, i1, i2, j1, j2 in sm.get_opcodes():
        if tag == 'replace':
            lineDiff.append("r%d-%d=%d-%d" % (offsetInLine(deletedWords, i1),
                                              offsetInLine(deletedWords, i2),
                                              offsetInLine(insertedWords, j1),
                                              offsetInLine(insertedWords, j2)))
        elif tag == 'delete':
            lineDiff.append("d%d-%d" % (offsetInLine(deletedWords, i1),
                                        offsetInLine(deletedWords, i2)))
        elif tag == 'insert':
            lineDiff.append("i%d-%d" % (offsetInLine(insertedWords, j1),
                                        offsetInLine(insertedWords, j2)))

    return ",".join(lineDiff)
예제 #10
0
    def process(token):
        if token[0] == '<':
            return Tag(token)
        else:

            def replace_decimal(match):
                return unichr(int(match.group(1)))

            token = textutils.decode(token)
            token = re_decimal_entity.sub(replace_decimal, token)
            token = token.encode("utf-8")

            return token.replace("&lt;",
                                 "<").replace("&gt;",
                                              ">").replace("&amp;", "&")
예제 #11
0
    def loadNewLines(self, highlighted=False, request_highlight=False):
        """Load the lines of the new version of the file, optionally highlighted."""

        from diff.parse import splitlines

        if self.new_sha1 is None or self.new_sha1 == '0' * 40:
            self.new_plain = []
            self.new_highlighted = []
            return
        elif self.new_mode and self.new_mode == "160000":
            self.new_plain = self.new_highlighted = [
                "Subproject commit %s" % self.new_sha1
            ]
            return

        if highlighted:
            if self.new_highlighted and self.new_is_highlighted: return
            else:
                self.new_is_highlighted = True
                language = self.getLanguage(use_content="new")
                if language:
                    data = syntaxhighlight.readHighlight(
                        self.repository,
                        self.new_sha1,
                        self.path,
                        language,
                        request=request_highlight)
                elif self.new_highlighted:
                    return
                else:
                    data = htmlutils.htmlify(
                        textutils.decode(
                            self.repository.fetch(self.new_sha1).data))
                self.new_highlighted = splitlines(data)
                self.new_eof_eol = data and data[-1] in "\n\r"
        else:
            if self.new_plain: return
            else:
                data = self.repository.fetch(self.new_sha1).data
                self.new_plain = splitlines(data)
                self.new_eof_eol = data and data[-1] in "\n\r"
예제 #12
0
    def __private_message_to_command__(self, type_id, payload):
        if type_id == "h":
            fields = [textutils.decode(f).strip() for f in ltd.split(payload)]

            if len(fields) >= 2 and fields[0] == "m":
                args = [arg.rstrip(" \0") for arg in fields[1].split(" ", 2)]

                if len(args) >= 2 and args[0] == core.NICKSERV:
                    type_id = "h"

                    payload = bytearray()

                    payload.extend(args[1].encode())
                    payload.append(1)

                    if len(args) == 3:
                        payload.extend(args[2].encode())

                    payload.append(0)

                    self.log.debug("Message transformed: type='%s', command='%s'", type_id, args[1])

        return type_id, payload
예제 #13
0
    async = mode == "json"
    source = None

    if language:
        path = generateHighlightPath(sha1, language, mode)

        if os.path.isfile(path):
            os.utime(path, None)
            source = open(path).read()
        elif os.path.isfile(path + ".bz2"):
            os.utime(path + ".bz2", None)
            source = bz2.BZ2File(path + ".bz2", "r").read()
        elif request:
            requestHighlights(repository, {sha1: (path, language)},
                              mode,
                              async=async)
            if mode == "json":
                raise HighlightRequested()
            return readHighlight(repository, sha1, path, language, False, mode)

    if not source:
        source = wrap(textutils.decode(repository.fetch(sha1).data), mode)

    return source


# Import for side-effects: these modules add strings to the LANGUAGES set to
# indicate which languages they support highlighting.
import cpp
import generic
예제 #14
0
 def fromValue(value):
     match = re_author_committer.match(value)
     return CommitUserTime(textutils.decode(match.group(1)).encode("utf-8"),
                           textutils.decode(match.group(2)).encode("utf-8"),
                           time.gmtime(int(match.group(3).split(" ")[0])))
예제 #15
0
 def wrapper(session_id, fields):
     fn(session_id, [textutils.decode(b).strip(" \0") for b in fields])