Monday 28 January 2019

can i handle imports in ast?

I want to parse and check config.py for admissible nodes. config.py can import other config files(which also must be checked).

Is there possibility in ast lib to parse ast.Import and ast.ImportFrom objects to ast.Module object?

Here code example: I am checking cfg file(path_to_config): But i want to check also the files that it imports

with open(path_to_config) as config_file:
    ast_tree = ast.parse(config_file.read())
    for script_object in ast_tree.body:
        if isinstance(script_object, ast.Import):
            # Imported file must be checked too
        elif isinstance(script_object, ast.ImportFrom):
            # Imported file must be checked too
        elif not _is_admissible_node(script_object):
            raise Exception("Config file '%s' contains unacceptable statements" % path_to_config)



from can i handle imports in ast?

No comments:

Post a Comment