diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-11 18:12:18 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-11 18:12:18 +0300 |
commit | 90ef70abfa57397ce34aab4e9b23a7623e48d66f (patch) | |
tree | f1614b9ea928b561d14ad725f6242fed40d79710 /astroid/builder.py | |
parent | bd25fc260e6f66086746eec57ccfe87d6fd56aaa (diff) | |
download | astroid-git-90ef70abfa57397ce34aab4e9b23a7623e48d66f.tar.gz |
Add a new convenience API, `astroid.parse`.
This API can be used to retrieve an astroid AST from a source code string,
similar to how ast.parse can be used to obtain a Python AST from a source string.
This is the test_utils.build_module promoted to a public API.
Diffstat (limited to 'astroid/builder.py')
-rw-r--r-- | astroid/builder.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/astroid/builder.py b/astroid/builder.py index 7f3a0661..8f0a1d07 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -25,6 +25,7 @@ from __future__ import with_statement import _ast import os import sys +import textwrap from astroid import bases from astroid import exceptions @@ -233,3 +234,15 @@ class AstroidBuilder(raw_building.InspectBuilder): values.append(node) except exceptions.InferenceError: pass + + +def parse(code, module_name='', path=None): + """Parses a source string in order to obtain an astroid AST from it + + :param str code: The code for the module. + :param str module_name: The name for the module, if any + :param str path: The path for the module + """ + code = textwrap.dedent(code) + return AstroidBuilder(MANAGER).string_build( + code, modname=module_name, path=path) |