Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entrypoint for command line usage #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ If you are lazy, "str2json" will apply the "parse" method of ast on a string for

>>> str2json(open('some_python_source_file.py').read())

You can also run via the command line to parse Python source from stdin and print to stdout:

::

$ ast2json < some_python_source_file.py


Example
=======

Expand Down
9 changes: 7 additions & 2 deletions ast2json/ast2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
from _ast import AST
from ast import parse
from .types import BUILTIN_PURE, BUILTIN_STR, BUILTIN_BYTES
Expand Down Expand Up @@ -65,6 +66,10 @@ def get_value(attr_value):
raise Exception("unknown case for '%s' of type '%s'" % (attr_value, type(attr_value)))


def main(stream=sys.stdin):
print(json.dumps(ast2json(parse(stream.read())), indent=4))


if __name__ == '__main__':
import json
print(json.dumps(ast2json(parse(open(__file__, "r").read())), indent=4))
with open(__file__, "r") as f:
main(f)
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
keywords='ast',
packages=find_packages(),
install_requires=[],
entry_points={
'console_scripts': [
'ast2json = ast2json.ast2json:main',
]
},
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
Expand Down