#StackBounty: #python #python-2.7 #abstract-syntax-tree can i handle imports in ast?

Bounty: 250

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)

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.