예제 #1
0
def extract_parser(modulepath, func_with_argparse):
    source = read_client_module(modulepath)

    nodes = ast.parse(''.join(source))
    funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
    assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)

    main_func = get_nodes_by_containing_attr(funcs, func_with_argparse)[0]
    parse_args_assignment = get_nodes_by_containing_attr(
        main_func.body, 'parse_args')[0]

    # ast reports the line no of a block structure as the start of the structure,
    # not the end, so we look for the line no of the next node after main()
    # and use that as the end of the main() function.
    try:
        restart_line = nodes.body[nodes.body.index(main_func) + 1].lineno - 1
    except IndexError:
        restart_line = len(source)

    module_source = format_source_to_return_parser(
        source,
        cutoff_line=parse_args_assignment.lineno,
        restart_line=restart_line,
        col_offset=parse_args_assignment.col_offset,
        parser_name=parse_args_assignment.value.func.value.id)
    client_module = modules.load(module_source)
    return getattr(client_module, func_with_argparse)()
예제 #2
0
def extract_parser(modulepath):
  source = read_client_module(modulepath)

  nodes = ast.parse(''.join(source))
  funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
  assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)

  main_func = get_nodes_by_containing_attr(funcs, 'main')[0]
  parse_args_assignment = get_nodes_by_containing_attr(main_func.body, 'parse_args')[0]

  # ast reports the line no of a block structure as the start of the structure,
  # not the end, so we look for the line no of the next node after main()
  # and use that as the end of the main() function.
  try:
    restart_line = nodes.body[nodes.body.index(main_func)+1].lineno - 1
  except IndexError:
    restart_line = len(source)

  module_source = format_source_to_return_parser(
    source,
    cutoff_line=parse_args_assignment.lineno,
    restart_line=restart_line,
    col_offset=parse_args_assignment.col_offset,
    parser_name=parse_args_assignment.value.func.value.id
  )
  client_module = modules.load(module_source)
  return client_module.main()
예제 #3
0
  def test_load_creates_and_imports_module_from_string_source(self):
    module_source = '''
some_var = 1234

def fooey():
  return 10

    '''
    module = modules.load(module_source)
    self.assertEqual(10, module.fooey())
예제 #4
0
def extract_parser(modulepath):
  source = read_client_module(modulepath)

  nodes = ast.parse(''.join(source))
  funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
  assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)

  main_func = get_nodes_by_containing_attr(funcs, 'main')[0]
  parse_args_assignment = get_nodes_by_containing_attr(main_func.body, 'parse_args')[0]

  module_source = format_source_to_return_parser(
    source,
    cutoff_line=parse_args_assignment.lineno,
    restart_line=main_func.body[-1].lineno,
    col_offset=parse_args_assignment.col_offset,
    parser_name=parse_args_assignment.value.func.value.id
  )
  client_module = modules.load(module_source)
  return client_module.main()
예제 #5
0
def extract_parser(modulepath):
    source = read_client_module(modulepath)

    nodes = ast.parse(''.join(source))
    funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
    assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)

    main_func = get_nodes_by_containing_attr(funcs, 'main')[0]
    parse_args_assignment = get_nodes_by_containing_attr(
        main_func.body, 'parse_args')[0]

    module_source = format_source_to_return_parser(
        source,
        cutoff_line=parse_args_assignment.lineno,
        restart_line=main_func.body[-1].lineno,
        col_offset=parse_args_assignment.col_offset,
        parser_name=parse_args_assignment.value.func.value.id)
    client_module = modules.load(module_source)
    return client_module.main()
예제 #6
0
def test_load_creates_and_imports_module_from_string_source():
    module = modules.load(module_source)
    assert 10 == module.fooey()
예제 #7
0
def test_load_creates_and_imports_module_from_string_source():
    module = modules.load(module_source)
    assert 10 == module.fooey()