Ejemplo n.º 1
0
def migrateFile(filePath, compiledPatches, compiledInfos, patchFile, 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 patchFile and fileId is not None:

        # import patch
        patch = {}
        execfile(patchFile, patch)
        tree = treegenerator.createFileTree(tokenizer.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)
            result = formatter_.formatNode(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)
Ejemplo n.º 2
0
def _compileTree(tree, prettyFlag):
    result = [u'']

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

    return u''.join(result)
Ejemplo n.º 3
0
def run_pretty(fileName, fileContent, options, args):
    #elif options.pretty:  # for testing formatter_2
    #    options = formatter.FormatterOptions()
    #    options = formatter.defaultOptions(options)
    #    print formatter.formatStream(tokens, options)
    tokens = tokenizer.Tokenizer().parseStream(fileContent, fileName)
    tree = treegenerator_3.createFileTree(tokens)  # use special tree
    optns = formatter.defaultOptions()
    optns.prettypCommentsBlockAdd = False
    result = [u'']
    result = formatter.formatNode(tree, optns, result)
    result = u''.join(result)
    print(result)
    return
Ejemplo n.º 4
0
def run_pretty(fileName, fileContent, options, args):
    #elif options.pretty:  # for testing formatter_2
    #    options = formatter.FormatterOptions()
    #    options = formatter.defaultOptions(options)
    #    print formatter.formatStream(tokens, options)
    tokens = tokenizer.Tokenizer().parseStream(fileContent, fileName)
    tree = treegenerator_3.createFileTree(tokens) # use special tree
    optns = formatter.defaultOptions()
    optns.prettypCommentsBlockAdd = False
    result = [u'']
    result = formatter.formatNode(tree, optns, result)
    result = u''.join(result)
    print(result)
    return
Ejemplo n.º 5
0
def _compileTree(tree, prettyFlag):
    result = [u'']

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

        optns = formatter.defaultOptions(optns)
        optns.prettypCommentsBlockAdd = False
        result = formatter.formatNode(tree, optns, result)
    else:
        result = Packer().serializeNode(tree, None, result, True)

    return u''.join(result)
Ejemplo n.º 6
0
def migrateFile(filePath,
                compiledPatches,
                compiledInfos,
                patchFile,
                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 patchFile and fileId is not None:

        #import patch
        patch = {}
        execfile(patchFile, patch)
        tree = treegenerator.createFileTree(
            tokenizer.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)
            result = formatter_.formatNode(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)