Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

DESCRIPTION: ast2src - reverse compile ast tree back to legal python source code

Project description

DESCRIPTION: ast2src - reverse compile ast tree back to legal python source code

REQUIRES: LINUX OS AND PYTHON3.1

QUICK TEST: $ python3.1 setup.py build dev --quicktest

SUMMARY:
ast2src is a pure python module.
ast2src is a python ast tree hack.

RECENT CHANGELOG:
20100121 - added ast2src
20091231 - added <<<< and >>>> sugar
20091224 - added pseudomethod interactive console - revamped pseudomethod import hook
20091224 - modularized package - fix install issues - added sdist check
20091209 - improved documentation
20091205 - moved source code to c++
20091116 - package integrated

DEMO USAGE:

PseudoSugar - adding hook <ast2src.PseudoSugar object at 0xb7ba856c> to sys.meta_path
>>> from ast2src import *

>>> ## let's use ast2src's source code as example
>>> import ast2src, ast
>>> source_code1 = open(ast2src.__file__).read()
>>> print( '\n' + re.sub('\n+', '\n', source_code1[:1024]) + '...' )

## import ast2src; reload(ast2src); from ast2src import *
import os, sys
if os.name != 'posix': sys.stderr.write('\nast2src requires linux os\n\n'); exit()
if sys.version_info[:2] != (3, 1): sys.stderr.write('\nast2src requires python3.1\n\n'); exit()
if 1: ###### INIT
import ast2src as _MODULE
if '_SETUP' not in globals(): _SETUP = sys.modules.get('ast2src.setup', None)
def closure(*args, **kwds): return lambda fnc: fnc(*args, **kwds)
def identity(aa): return aa
def _import(ss, globals = globals()):
for aa in ss.split(' '): globals[aa] = __import__(aa)
return identity
class Namespace(object):
def __init__(self, **kwds): vars(self).update(kwds)
@_import('ast builtins collections re ' #### pseudosugar compiler
'imp importlib importlib.abc importlib.util ' #### import hook
'locale') ## PYTHON BUG
class PseudoSugar(ast.NodeVisitor, importlib.abc.Finder, importlib.abc.PyLoader):
## convenience function
@staticmethod
def exec(ss, globa...

>>> ## compile source to ast
>>> ast_tree1 = compile(source_code1, '', 'exec', ast.PyCF_ONLY_AST)
>>> print( Ast2Src.debugnode(ast_tree1)[:1024], '...' )
<class '_ast.Module'> body [<_ast.Import object at 0x9c6ea2c>, <_ast.If object at 0x9c6e7cc>, <_ast.If object at 0x9ab2b0c>, <_ast.If object at 0x9ab2fec>]
<class '_ast.Import'> col_offset 0 lineno 2 names [<_ast.alias object at 0x9c6e2ec>, <_ast.alias object at 0x9c6e9ac>]
<class '_ast.alias'> asname None name 'os'
<class '_ast.alias'> asname None name 'sys'
<class '_ast.If'> body [<_ast.Expr object at 0x9ccc92c>, <_ast.Expr object at 0x9ab2acc>] col_offset 0 lineno 3 orelse [] test <_ast.Compare object at 0x9c6eccc>
<class '_ast.Compare'> col_offset 3 comparators [<_ast.Str object at 0x9ccc9ec>] left <_ast.Attribute object at 0x9c948cc> lineno 3 ops [<_ast.NotEq object at 0xb7b3682c>]
<class '_ast.Attribute'> attr 'name' col_offset 3 ctx <_ast.Load object at 0xb7bb2ccc> lineno 3 value <_ast.Name object at 0x9ccc46c>
<class '_ast.Name'> col_offset 3 ctx <_ast.Load object at 0xb7bb2ccc> id 'os' lineno 3
<class '_ast.Load'>
<class '_ast.Load'>
<class '_ast.NotEq'>
<class '_ast. ...

>>> ## reverse compile ast back to source
>>> source_code2 = Ast2Src.unparse(ast_tree1)
>>> print( '\n' + re.sub('\n+', '\n', source_code1[:1024]) + '...' )

## import ast2src; reload(ast2src); from ast2src import *
import os, sys
if os.name != 'posix': sys.stderr.write('\nast2src requires linux os\n\n'); exit()
if sys.version_info[:2] != (3, 1): sys.stderr.write('\nast2src requires python3.1\n\n'); exit()
if 1: ###### INIT
import ast2src as _MODULE
if '_SETUP' not in globals(): _SETUP = sys.modules.get('ast2src.setup', None)
def closure(*args, **kwds): return lambda fnc: fnc(*args, **kwds)
def identity(aa): return aa
def _import(ss, globals = globals()):
for aa in ss.split(' '): globals[aa] = __import__(aa)
return identity
class Namespace(object):
def __init__(self, **kwds): vars(self).update(kwds)
@_import('ast builtins collections re ' #### pseudosugar compiler
'imp importlib importlib.abc importlib.util ' #### import hook
'locale') ## PYTHON BUG
class PseudoSugar(ast.NodeVisitor, importlib.abc.Finder, importlib.abc.PyLoader):
## convenience function
@staticmethod
def exec(ss, globa...

>>> ## recompile reverse-compiled source
>>> ast_tree2 = compile(source_code2, '', 'exec', ast.PyCF_ONLY_AST)
>>> print( Ast2Src.debugnode(ast_tree2)[:1024], '...' )
<class '_ast.Module'> body [<_ast.Import object at 0x9ad7ecc>, <_ast.If object at 0x9b3316c>, <_ast.If object at 0x9b3abcc>, <_ast.If object at 0x9b3ae6c>]
<class '_ast.Import'> col_offset 0 lineno 2 names [<_ast.alias object at 0x9c2966c>, <_ast.alias object at 0x9c022ec>]
<class '_ast.alias'> asname None name 'os'
<class '_ast.alias'> asname None name 'sys'
<class '_ast.If'> body [<_ast.Expr object at 0x9b3aaac>, <_ast.Expr object at 0x9b3ab6c>] col_offset 0 lineno 3 orelse [] test <_ast.Compare object at 0x9b332ac>
<class '_ast.Compare'> col_offset 4 comparators [<_ast.Str object at 0x9b3a80c>] left <_ast.Attribute object at 0x9b3490c> lineno 3 ops [<_ast.NotEq object at 0xb7b3682c>]
<class '_ast.Attribute'> attr 'name' col_offset 4 ctx <_ast.Load object at 0xb7bb2ccc> lineno 3 value <_ast.Name object at 0x9b3a10c>
<class '_ast.Name'> col_offset 4 ctx <_ast.Load object at 0xb7bb2ccc> id 'os' lineno 3
<class '_ast.Load'>
<class '_ast.Load'>
<class '_ast.NotEq'>
<class '_ast. ...

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
ast2src-2010.01.21.ast2src.tar.gz (93.5 kB) Copy SHA256 hash SHA256 Source None Jan 23, 2010

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page