Exemple #1
0
def migrateFile(
                filePath, compiledPatches, compiledInfos,
                hasPatchModule=False, options=None, encoding="UTF-8"):

    logging.info("  - File: %s" % filePath)

    # Read in original content
    fileContent = filetool.read(filePath, encoding)

    fileId = extractFileContentId(fileContent);

    # Apply patches
    patchedContent = fileContent

    if hasPatchModule and fileId is not None:

        import patch
        tree = treegenerator.createSyntaxTree(tokenizer.parseStream(fileContent))

        # If there were any changes, compile the result
        if patch.patch(fileId, tree):
            options.prettyPrint = True  # make sure it's set
            result = [u'']
            result = pretty.prettyNode(tree, options, result)
            patchedContent = u''.join(result)

    # apply RE patches
    patchedContent = regtool(patchedContent, compiledPatches, True, filePath)
    patchedContent = regtool(patchedContent, compiledInfos, False, filePath)

    # Write file
    if patchedContent != fileContent:
        logging.info("    - %s has been modified. Storing modifications ..." % filePath)
        filetool.save(filePath, patchedContent, encoding)
Exemple #2
0
 def toJavascript(self):
     from ecmascript.backend import pretty
     def options(): pass
     pretty.defaultOptions(options)
     result = [u'']
     result = pretty.prettyNode(self, options, result)
     return u''.join(result)
Exemple #3
0
def migrateFile(
                filePath, compiledPatches, compiledInfos,
                hasPatchModule=False, options=None, encoding="UTF-8"):

    logging.info("  - File: %s" % filePath)

    # Read in original content
    fileContent = filetool.read(filePath, encoding)

    fileId = extractFileContentId(fileContent);

    # Apply patches
    patchedContent = fileContent

    if hasPatchModule and fileId is not None:

        import patch
        tree = treegenerator.createFileTree(tokenizer.parseStream(fileContent))

        # If there were any changes, compile the result
        if patch.patch(fileId, tree):
            options.prettyPrint = True  # make sure it's set
            result = [u'']
            result = pretty.prettyNode(tree, options, result)
            patchedContent = u''.join(result)

    # apply RE patches
    patchedContent = regtool(patchedContent, compiledPatches, True, filePath)
    patchedContent = regtool(patchedContent, compiledInfos, False, filePath)

    # Write file
    if patchedContent != fileContent:
        logging.info("    - %s has been modified. Storing modifications ..." % filePath)
        filetool.save(filePath, patchedContent, encoding)
Exemple #4
0
def compile(node, opts, enableBreaks=False, enableVerbose=False):

    options = opts
    options.prettypIndentString          = eval("'" + options.prettypIndentString + "'")
    options.prettypCommentsInlinePadding = eval("'" + options.prettypCommentsInlinePadding + "'")
                                                              # allow for escapes like "\t"
    # split trailing comment cols into an array
    if (options.prettypCommentsTrailingCommentCols and
        isinstance(options.prettypCommentsTrailingCommentCols, basestring)):
        options.prettypCommentsTrailingCommentCols = [int(column.strip()) for column in options.prettypCommentsTrailingCommentCols.split(",")]
        options.prettypCommentsTrailingCommentCols.sort() # make sure they are ascending!
    # or make sure it's a list of int's
    elif (isinstance(options.prettypCommentsTrailingCommentCols, list) and
        reduce(lambda y,z: y and z,
               [isinstance(x,int) for x in options.prettypCommentsTrailingCommentCols],
               True)):
        options.prettypCommentsTrailingCommentCols.sort() # make sure they are ascending!
    # or pass
    else:
        #raise TypeError, "Unsuitable type for option --pretty-print-comments-trailing-commentCols"
        pass

    result       = [u""]

    if opts.prettyPrint:
        comment.fill(node)
        result = prettyM.prettyNode(node, opts, result)
    else:
        packer = Packer()
        result = packer.serializeNode(node, opts, result, enableBreaks)

    return u"".join(result)
Exemple #5
0
 def toJavascript(self):
     from ecmascript.backend import pretty
     def options(): pass
     pretty.defaultOptions(options)
     result = [u'']
     result = pretty.prettyNode(self, options, result)
     return u''.join(result)
Exemple #6
0
        def serializeFormatted(tree):
            # provide minimal pretty options
            def options(): pass
            pretty.defaultOptions(options)
            options.prettypCommentsBlockAdd = False  # turn off comment filling

            result = [u'']
            result = pretty.prettyNode(tree, options, result)

            return u''.join(result)
Exemple #7
0
    def serializeFormatted(self, tree):
        # provide minimal pretty options
        def options(): pass
        pretty.defaultOptions(options)
        options.prettypCommentsBlockAdd = False  # turn off comment filling

        result = [u'']
        result = pretty.prettyNode(tree, options, result)

        return u''.join(result)
Exemple #8
0
def _compileTree(tree, prettyFlag):
    result = [u'']

    if prettyFlag:
        # Set options
        def optns(): pass
        optns = pretty.defaultOptions(optns)
        #optns.prettypCommentsBlockAdd = False
        result = pretty.prettyNode(tree, optns, result)
    else:
        result =  Packer().serializeNode(tree, None, result, True)

    return u''.join(result)
Exemple #9
0
def _compileTree(tree, prettyFlag):
    result = [u'']

    if prettyFlag:
        # Set options
        def optns():
            pass

        optns = pretty.defaultOptions(optns)
        #optns.prettypCommentsBlockAdd = False
        result = pretty.prettyNode(tree, optns, result)
    else:
        result = Packer().serializeNode(tree, None, result, True)

    return u''.join(result)
    def runPrettyPrinting(self, classesObj):
        if not isinstance(self._job.get("pretty-print", False), types.DictType):
            return

        self._console.info("Pretty-printing code...")
        self._console.indent()
        ppsettings = ExtMap(self._job.get("pretty-print"))  # get the pretty-print config settings

        # init options
        def options(): pass
        pretty.defaultOptions(options)

        # modify according to config
        if 'general/indent-string' in ppsettings:
            options.prettypIndentString = ppsettings.get('general/indent-string')
        if 'comments/block/add' in ppsettings:
            options.prettypCommentsBlockAdd = ppsettings.get('comments/trailing/keep-column')
        if 'comments/trailing/keep-column' in ppsettings:
            options.prettypCommentsTrailingKeepColumn = ppsettings.get('comments/trailing/keep-column')
        if 'comments/trailing/comment-cols' in ppsettings:
            options.prettypCommentsTrailingCommentCols = ppsettings.get('comments/trailing/comment-cols')
        if 'comments/trailing/padding' in ppsettings:
            options.prettypCommentsInlinePadding = ppsettings.get('comments/trailing/padding')
        if 'code/align-with-curlies' in ppsettings:
            options.prettypAlignBlockWithCurlies = ppsettings.get('code/align-with-curlies')
        if 'code/open-curly/newline-before' in ppsettings:
            options.prettypOpenCurlyNewlineBefore = ppsettings.get('code/open-curly/newline-before')
        if 'code/open-curly/indent-before' in ppsettings:
            options.prettypOpenCurlyIndentBefore = ppsettings.get('code/open-curly/indent-before')

        self._console.info("Pretty-printing files: ", False)
        numClasses = len(classesObj)
        for pos, classId in enumerate(classesObj):
            self._console.progress(pos+1, numClasses)
            tree = classesObj[classId].tree()
            result = [u'']
            result = pretty.prettyNode(tree, options, result)
            compiled = u''.join(result)
            filetool.save(self._classes[classId].path, compiled)

        self._console.outdent()

        return
Exemple #11
0
def compile(node, opts, enableBreaks=False, enableVerbose=False):

    options = opts
    options.prettypIndentString = eval("'" + options.prettypIndentString + "'")
    options.prettypCommentsInlinePadding = eval(
        "'" + options.prettypCommentsInlinePadding + "'")
    # allow for escapes like "\t"
    # split trailing comment cols into an array
    if (options.prettypCommentsTrailingCommentCols and isinstance(
            options.prettypCommentsTrailingCommentCols, basestring)):
        options.prettypCommentsTrailingCommentCols = [
            int(column.strip())
            for column in options.prettypCommentsTrailingCommentCols.split(",")
        ]
        options.prettypCommentsTrailingCommentCols.sort(
        )  # make sure they are ascending!
    # or make sure it's a list of int's
    elif (isinstance(options.prettypCommentsTrailingCommentCols, list)
          and reduce(lambda y, z: y and z, [
              isinstance(x, int)
              for x in options.prettypCommentsTrailingCommentCols
          ], True)):
        options.prettypCommentsTrailingCommentCols.sort(
        )  # make sure they are ascending!
    # or pass
    else:
        #raise TypeError, "Unsuitable type for option --pretty-print-comments-trailing-commentCols"
        pass

    result = [u""]

    if opts.prettyPrint:
        comment.fill(node)
        result = prettyM.prettyNode(node, opts, result)
    else:
        packer = Packer()
        result = packer.serializeNode(node, opts, result, enableBreaks)

    return u"".join(result)