How to use the astor.codegen.to_source function in astor

To help you get started, we’ve selected a few astor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github berkerpeksag / astor / astor / anti8.py View on Github external
oldpath = srcpath
            dstpath = srcpath.replace(srctree, dsttree, 1)
            if not dstpath.startswith(dsttree):
                raise ValueError("%s not a subdirectory of %s" %
                                                (dstpath, dsttree))
            os.makedirs(dstpath)

        srcfname = os.path.join(srcpath, fname)
        logging.info('Converting ' + srcfname)
        try:
            srcast = parsefile(srcfname)
        except handled_exceptions:
            badfiles.add(srcfname)
            continue

        dsttxt = to_source(srcast)

        if not readonly:
            dstfname = os.path.join(dstpath, fname)
            with open(dstfname, 'w') as f:
                f.write(dsttxt)

        # As a sanity check, make sure that ASTs themselves
        # round-trip OK
        dstast = ast.parse(dsttxt) if readonly else parsefile(dstfname)
        srcast = striplinecol(srcast)
        dstast = striplinecol(dstast)
        if srcast != dstast:
            broken.append((srcfname, srcast, dstast))

    if badfiles:
        logging.warning('')