示例#1
0
  def test_take_imports_drops_all_non_imports_statements(self):
    lines = '''
import argparse
from argparse import ArgumentParser
parser = ArgumentParser(description='Example Argparse Program')
parser.parse_args()
    '''.split('\n')[1:]

    self.assertEqual(2, len(list(code_prep.take_imports(lines))))
    self.assertEqual('import argparse', list(code_prep.take_imports(lines))[0])
  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