Esempio n. 1
0
 def invoke_macro(self, macro, args, original_parser):
     parsed_args = []
     for arg in args:
         p = Parser(arg)
         p.changequote('[',']')
         macro_handler.add_macros(MACROS, p)
         stream = StringIO()
         p.parse(stream=stream)
         parsed_args.append(stream.getvalue())
     return getattr(self, macro)(parsed_args)
Esempio n. 2
0
    def translate_toplevel(self, commands):
        main = filter(lambda x: isinstance(x, ast.FunctionDef) and x.name == 'main', self.template.body)[0]
        main.body.extend(flatten(self.translate_commands(commands)))
        substassign = filter(lambda x: isinstance(x, ast.Assign) and x.targets[0].id == 'SUBSTS', self.template.body)[0]
        substassign.value.args = [ast.List([ast.Str(s) for s in self.macro_handler.substs], ast.Load())]
        make_arg_parser = filter(lambda x: isinstance(x, ast.FunctionDef) and x.name == 'make_arg_parser', self.template.body)[0]
        make_arg_parser.body[-1:-1] = self.make_argparse_arguments()


p = Parser(sys.stdin.read())
p.changequote('[',']')
macro_handler = MacroHandler()
macro_handler.add_macros(MACROS, p)

stream = StringIO()
# Parse m4
p.parse(stream=stream)
shell = stream.getvalue()
if len(sys.argv) > 1:
    sys.stdout.write(shell)
    sys.exit(0)

# now translate shell to Python
template_file = os.path.join(os.path.dirname(__file__), 'template.py')
template = ast.parse(open(template_file, 'r').read())

translator = ShellTranslator(macro_handler, template)
translator.translate(shell, toplevel=True)

sys.stdout.write(meta.dump_python_source(template))