Package: ast2src
DESCRIPTION: ast2src - reverse compile ast tree back to legal python source codeREQUIRES: LINUX OS AND PYTHON3.1QUICK TEST: $ python3.1 setup.py build dev --quicktestSUMMARY:ast2src is a pure python module.ast2src is a python ast tree hack.RECENT CHANGELOG:20100121 - added ast2src20091231 - added <<<< and >>>> sugar20091224 - added pseudomethod interactive console - revamped pseudomethod import hook20091224 - modularized package - fix install issues - added sdist check20091209 - improved documentation20091205 - moved source code to c++20091116 - package integratedDEMO 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, sysif 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: ###### INITimport ast2src as _MODULEif '_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 aadef _import(ss, globals = globals()):for aa in ss.split(' '): globals[aa] = __import__(aa)return identityclass 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 BUGclass PseudoSugar(ast.NodeVisitor, importlib.abc.Finder, importlib.abc.PyLoader):## convenience function@staticmethoddef 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, sysif 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: ###### INITimport ast2src as _MODULEif '_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 aadef _import(ss, globals = globals()):for aa in ss.split(' '): globals[aa] = __import__(aa)return identityclass 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 BUGclass PseudoSugar(ast.NodeVisitor, importlib.abc.Finder, importlib.abc.PyLoader):## convenience function@staticmethoddef 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. ...