METADATA 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Metadata-Version: 2.1
  2. Name: asttokens
  3. Version: 2.0.5
  4. Summary: Annotate AST trees with source code positions
  5. Home-page: https://github.com/gristlabs/asttokens
  6. Author: Dmitry Sagalovskiy, Grist Labs
  7. Author-email: dmitry@getgrist.com
  8. License: Apache 2.0
  9. Keywords: code,ast,parse,tokenize,refactor
  10. Platform: UNKNOWN
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  14. Classifier: Topic :: Software Development :: Code Generators
  15. Classifier: Topic :: Software Development :: Compilers
  16. Classifier: Topic :: Software Development :: Interpreters
  17. Classifier: Topic :: Software Development :: Pre-processors
  18. Classifier: Environment :: Console
  19. Classifier: Operating System :: OS Independent
  20. Classifier: Programming Language :: Python :: 2
  21. Classifier: Programming Language :: Python :: 2.7
  22. Classifier: Programming Language :: Python :: 3
  23. Classifier: Programming Language :: Python :: 3.5
  24. Classifier: Programming Language :: Python :: 3.6
  25. Classifier: Programming Language :: Python :: 3.7
  26. Classifier: Programming Language :: Python :: 3.8
  27. Classifier: Programming Language :: Python :: 3.9
  28. Classifier: Programming Language :: Python :: Implementation :: CPython
  29. Classifier: Programming Language :: Python :: Implementation :: PyPy
  30. Requires-Dist: six
  31. Provides-Extra: test
  32. Requires-Dist: astroid ; extra == 'test'
  33. Requires-Dist: pytest ; extra == 'test'
  34. ASTTokens
  35. =========
  36. .. image:: https://img.shields.io/pypi/v/asttokens.svg
  37. :target: https://pypi.python.org/pypi/asttokens/
  38. .. image:: https://img.shields.io/pypi/pyversions/asttokens.svg
  39. :target: https://pypi.python.org/pypi/asttokens/
  40. .. image:: https://travis-ci.org/gristlabs/asttokens.svg?branch=master
  41. :target: https://travis-ci.org/gristlabs/asttokens
  42. .. image:: https://readthedocs.org/projects/asttokens/badge/?version=latest
  43. :target: http://asttokens.readthedocs.io/en/latest/index.html
  44. .. image:: https://coveralls.io/repos/github/gristlabs/asttokens/badge.svg
  45. :target: https://coveralls.io/github/gristlabs/asttokens
  46. .. Start of user-guide
  47. The ``asttokens`` module annotates Python abstract syntax trees (ASTs) with the positions of tokens
  48. and text in the source code that generated them.
  49. It makes it possible for tools that work with logical AST nodes to find the particular text that
  50. resulted in those nodes, for example for automated refactoring or highlighting.
  51. Installation
  52. ------------
  53. asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/::
  54. pip install asttokens
  55. The code is on GitHub: https://github.com/gristlabs/asttokens.
  56. The API Reference is here: http://asttokens.readthedocs.io/en/latest/api-index.html.
  57. Usage
  58. -----
  59. ASTTokens works with both Python2 and Python3.
  60. ASTTokens can annotate both trees built by `ast <https://docs.python.org/2/library/ast.html>`_,
  61. AND those built by `astroid <https://github.com/PyCQA/astroid>`_.
  62. Here's an example:
  63. .. code-block:: python
  64. import asttokens, ast
  65. source = "Robot('blue').walk(steps=10*n)"
  66. atok = asttokens.ASTTokens(source, parse=True)
  67. Once the tree has been marked, nodes get ``.first_token``, ``.last_token`` attributes, and
  68. the ``ASTTokens`` object offers helpful methods:
  69. .. code-block:: python
  70. attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
  71. print(atok.get_text(attr_node))
  72. start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
  73. print(atok.text[:start] + 'RUN' + atok.text[end:])
  74. Which produces this output:
  75. .. code-block:: text
  76. Robot('blue').walk
  77. Robot('blue').RUN(steps=10*n)
  78. The ``ASTTokens`` object also offers methods to walk and search the list of tokens that make up
  79. the code (or a particular AST node), which is more useful and powerful than dealing with the text
  80. directly.
  81. Contribute
  82. ----------
  83. To contribute:
  84. 1. Fork this repository, and clone your fork.
  85. 2. Install the package with test dependencies (ideally in a virtualenv) with::
  86. pip install -e '.[test]'
  87. 3. Run tests in your current interpreter with the command ``pytest`` or ``python -m pytest``.
  88. 4. Run tests across all supported interpreters with the ``tox`` command. You will need to have the interpreters installed separately. We recommend ``pyenv`` for that. Use ``tox -p auto`` to run the tests in parallel.
  89. 5. By default certain tests which take a very long time to run are skipped, but they are run on travis CI. To run them locally, set the environment variable ``ASTTOKENS_SLOW_TESTS``. For example run ``ASTTOKENS_SLOW_TESTS=1 tox`` to run the full suite of tests.