METADATA 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Metadata-Version: 2.1
  2. Name: asttokens
  3. Version: 2.4.1
  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. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  13. Classifier: Topic :: Software Development :: Code Generators
  14. Classifier: Topic :: Software Development :: Compilers
  15. Classifier: Topic :: Software Development :: Interpreters
  16. Classifier: Topic :: Software Development :: Pre-processors
  17. Classifier: Environment :: Console
  18. Classifier: Operating System :: OS Independent
  19. Classifier: Programming Language :: Python :: 2
  20. Classifier: Programming Language :: Python :: 2.7
  21. Classifier: Programming Language :: Python :: 3
  22. Classifier: Programming Language :: Python :: 3.5
  23. Classifier: Programming Language :: Python :: 3.6
  24. Classifier: Programming Language :: Python :: 3.7
  25. Classifier: Programming Language :: Python :: 3.8
  26. Classifier: Programming Language :: Python :: 3.9
  27. Classifier: Programming Language :: Python :: 3.10
  28. Classifier: Programming Language :: Python :: 3.11
  29. Classifier: Programming Language :: Python :: 3.12
  30. Classifier: Programming Language :: Python :: Implementation :: CPython
  31. Classifier: Programming Language :: Python :: Implementation :: PyPy
  32. License-File: LICENSE
  33. Requires-Dist: six >=1.12.0
  34. Requires-Dist: typing ; python_version < "3.5"
  35. Provides-Extra: astroid
  36. Requires-Dist: astroid <2,>=1 ; (python_version < "3") and extra == 'astroid'
  37. Requires-Dist: astroid <4,>=2 ; (python_version >= "3") and extra == 'astroid'
  38. Provides-Extra: test
  39. Requires-Dist: pytest ; extra == 'test'
  40. Requires-Dist: astroid <2,>=1 ; (python_version < "3") and extra == 'test'
  41. Requires-Dist: astroid <4,>=2 ; (python_version >= "3") and extra == 'test'
  42. ASTTokens
  43. =========
  44. .. image:: https://img.shields.io/pypi/v/asttokens.svg
  45. :target: https://pypi.python.org/pypi/asttokens/
  46. .. image:: https://img.shields.io/pypi/pyversions/asttokens.svg
  47. :target: https://pypi.python.org/pypi/asttokens/
  48. .. image:: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml/badge.svg
  49. :target: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml
  50. .. image:: https://readthedocs.org/projects/asttokens/badge/?version=latest
  51. :target: http://asttokens.readthedocs.io/en/latest/index.html
  52. .. image:: https://coveralls.io/repos/github/gristlabs/asttokens/badge.svg
  53. :target: https://coveralls.io/github/gristlabs/asttokens
  54. .. Start of user-guide
  55. The ``asttokens`` module annotates Python abstract syntax trees (ASTs) with the positions of tokens
  56. and text in the source code that generated them.
  57. It makes it possible for tools that work with logical AST nodes to find the particular text that
  58. resulted in those nodes, for example for automated refactoring or highlighting.
  59. Installation
  60. ------------
  61. asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/::
  62. pip install asttokens
  63. The code is on GitHub: https://github.com/gristlabs/asttokens.
  64. The API Reference is here: http://asttokens.readthedocs.io/en/latest/api-index.html.
  65. Usage
  66. -----
  67. ASTTokens works with both Python2 and Python3.
  68. ASTTokens can annotate both trees built by `ast <https://docs.python.org/2/library/ast.html>`_,
  69. AND those built by `astroid <https://github.com/PyCQA/astroid>`_.
  70. Here's an example:
  71. .. code-block:: python
  72. import asttokens, ast
  73. source = "Robot('blue').walk(steps=10*n)"
  74. atok = asttokens.ASTTokens(source, parse=True)
  75. Once the tree has been marked, nodes get ``.first_token``, ``.last_token`` attributes, and
  76. the ``ASTTokens`` object offers helpful methods:
  77. .. code-block:: python
  78. attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
  79. print(atok.get_text(attr_node))
  80. start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
  81. print(atok.text[:start] + 'RUN' + atok.text[end:])
  82. Which produces this output:
  83. .. code-block:: text
  84. Robot('blue').walk
  85. Robot('blue').RUN(steps=10*n)
  86. The ``ASTTokens`` object also offers methods to walk and search the list of tokens that make up
  87. the code (or a particular AST node), which is more useful and powerful than dealing with the text
  88. directly.
  89. Contribute
  90. ----------
  91. To contribute:
  92. 1. Fork this repository, and clone your fork.
  93. 2. Install the package with test dependencies (ideally in a virtualenv) with::
  94. pip install -e '.[test]'
  95. 3. Run tests in your current interpreter with the command ``pytest`` or ``python -m pytest``.
  96. 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.
  97. 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.