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
Example #2
0
 def test_split_line(self):
   line = "parser = ArgumentParser(description='Example Argparse Program')"
   self.assertEqual("parser", code_prep.split_line(line)[0])
   self.assertEqual("ArgumentParser(description='Example Argparse Program')", code_prep.split_line(line)[1])