def _build_argparser_from_client_source(self, source_code):
    '''
    runs the client code by evaling each line.

    Example input Code:
      parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter)
      parser.add_argument("-r", "--recursive", dest="recurse", action="store_true", help="recurse into subfolders [default: %(default)s]")
      parser.add_argument("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %(default)s]")

    Method extracts the instance name (e.g. parser) from the first line,
    and instantiates it in a local variable by evaling the rest of the lines.
    Each subsequent line updates the local variable in turn.
    '''
    imports = filter(lambda x: 'gooey' not in x, code_prep.take_imports(source_code))
    arg_code = code_prep.drop_imports(source_code)
    updated_source_code = code_prep.update_parser_varname('clients_parser', arg_code)

    for _import in imports:
      exec(_import)

    first_line = updated_source_code.pop(0)
    clients_parser, assignment = code_prep.split_line(first_line)
    clients_parser = eval(assignment)

    for line in updated_source_code:
      eval(line)
    return clients_parser
예제 #2
0
  def test_update_parser_varname_assigns_new_name_to_parser_var__multiline(self):
    lines = '''
import argparse
from argparse import ArgumentParser
parser = ArgumentParser(description='Example Argparse Program')
parser.parse_args()
    '''.split('\n')

    self.assertEqual(
      "jarser = ArgumentParser(description='Example Argparse Program')",
      code_prep.update_parser_varname('jarser', lines)[2]
    )
예제 #3
0
 def test_update_parser_varname_assigns_new_name_to_parser_var(self):
   line = ["parser = ArgumentParser(description='Example Argparse Program')"]
   self.assertEqual(
     "jarser = ArgumentParser(description='Example Argparse Program')",
     code_prep.update_parser_varname('jarser', line)[0]
   )