Esempio n. 1
0
def is_valid_function(header_str, function_body, parent_folder):
    file_name = helper.generate_py_temp_name()
    temp_file = os.path.join(parent_folder, "%s.py" % file_name)
    content = "%s\n\n%s" % (header_str, function_body)
    cache.write_file(temp_file, content)
    validity = helper.is_valid_file(temp_file)
    cache.delete_file(temp_file)
    return validity
Esempio n. 2
0
def generate_for_file(dataset, file_name):
    sys.path.append(properties.PYTHON_PROJECTS_HOME)
    parent_folder = cache.get_parent_folder(file_name)
    store = get_store(dataset)
    visitor = statement_parser.StatementVisitor(file_name, dataset)
    visitor.parse()
    variable_map = create_variable_map(visitor)
    generated_functions = {}
    python_file = file_name.split(
        properties.PYTHON_PROJECTS_HOME)[-1][1:].split(".")[0]
    headers = [
        "import sys",
        "sys.path.append('%s')" % properties.PYTHON_PROJECTS_HOME,
        "from %s import *" % python_file.replace(os.path.sep, ".")
    ]
    header_str = "\n".join(headers)
    body = [header_str]
    for method in visitor.methods:
        if DEBUG:
            print("Method Name: %s. Statement Blocks: %d" %
                  (method.name, len(method.statement_blocks)))
        if method.name == a_consts.ROOT_SCOPE: continue
        for statement_group in method.get_statement_groups():
            if DEBUG:
                print("## SG")
                print_statements(statement_group)
                print("\n### Generated")
            # Save type for each function node
            function_meta = get_meta_for_statement_group(
                statement_group, method, variable_map,
                visitor.scope_variable_map[method.get_scope()].values())
            if not function_meta["is_valid"]:
                if DEBUG:
                    print("Not valid")
                continue
            function_nodes = create_function_nodes(statement_group,
                                                   function_meta)
            arg_types = {}
            for arg in function_meta['args']:
                arg_types[arg['name']] = arg['type'].to_bson()
            for function_name, function_node in function_nodes.items():
                function_body = astor.to_source(function_node)
                if is_valid_function(header_str, function_body, parent_folder):
                    body.append(function_body)
                    store.update_function_arg_type(function_name, arg_types)
            if DEBUG:
                print(len(function_nodes))
    content = "\n\n".join(body)
    write_file = os.path.join(parent_folder,
                              "%s.py" % helper.generate_py_file_name())
    cache.write_file(write_file, content)
    assert helper.is_valid_file(write_file)
    sys.path.append(properties.PYTHON_PROJECTS_HOME)
    return generated_functions
Esempio n. 3
0
def create_temp_function_file(variable, body):
    headers = "\n".join(
        ["import numpy", "import pandas", "np, pd = numpy, pandas"])
    file_path = os.path.join(
        props.MISCONCEPTIONS_HOME,
        "temp_func_%s.py" % helper.generate_random_string()[:6])
    func_name = helper.generate_function_name()
    args = ast.arguments(args=[ast.Name(id=str(variable))],
                         vararg=None,
                         kwarg=None,
                         defaults=[])
    func_node = ast.FunctionDef(name=func_name,
                                args=args,
                                body=as_nodes(body),
                                decorator_list=[])
    if func_node is None:
        return None
    func_str = astor.to_source(func_node)
    content = "\n\n".join([headers, func_str])
    cache.write_file(file_path, content)
    if helper.is_valid_file(file_path, props.PYTHON_SRC):
        return func_name, file_path
    cache.delete_file(file_path)
    return None, file_path
Esempio n. 4
0
def _test_import():
    print(helper.is_valid_file(TEMP_FUNC_PATH, props.PYTHON_SRC))