METADATA 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Metadata-Version: 2.1
  2. Name: asttokens
  3. Version: 3.0.0
  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 :: 3.8
  20. Classifier: Programming Language :: Python :: 3.9
  21. Classifier: Programming Language :: Python :: 3.10
  22. Classifier: Programming Language :: Python :: 3.11
  23. Classifier: Programming Language :: Python :: 3.12
  24. Classifier: Programming Language :: Python :: 3.13
  25. Classifier: Programming Language :: Python :: Implementation :: CPython
  26. Classifier: Programming Language :: Python :: Implementation :: PyPy
  27. Requires-Python: >=3.8
  28. License-File: LICENSE
  29. Provides-Extra: astroid
  30. Requires-Dist: astroid<4,>=2; extra == "astroid"
  31. Provides-Extra: test
  32. Requires-Dist: astroid<4,>=2; extra == "test"
  33. Requires-Dist: pytest; extra == "test"
  34. Requires-Dist: pytest-cov; extra == "test"
  35. Requires-Dist: pytest-xdist; extra == "test"
  36. ASTTokens
  37. =========
  38. .. image:: https://img.shields.io/pypi/v/asttokens.svg
  39. :target: https://pypi.python.org/pypi/asttokens/
  40. .. image:: https://img.shields.io/pypi/pyversions/asttokens.svg
  41. :target: https://pypi.python.org/pypi/asttokens/
  42. .. image:: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml/badge.svg
  43. :target: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml
  44. .. image:: https://readthedocs.org/projects/asttokens/badge/?version=latest
  45. :target: http://asttokens.readthedocs.io/en/latest/index.html
  46. .. image:: https://coveralls.io/repos/github/gristlabs/asttokens/badge.svg
  47. :target: https://coveralls.io/github/gristlabs/asttokens
  48. .. Start of user-guide
  49. The ``asttokens`` module annotates Python abstract syntax trees (ASTs) with the positions of tokens
  50. and text in the source code that generated them.
  51. It makes it possible for tools that work with logical AST nodes to find the particular text that
  52. resulted in those nodes, for example for automated refactoring or highlighting.
  53. Installation
  54. ------------
  55. asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/::
  56. pip install asttokens
  57. The code is on GitHub: https://github.com/gristlabs/asttokens.
  58. The API Reference is here: http://asttokens.readthedocs.io/en/latest/api-index.html.
  59. Usage
  60. -----
  61. ASTTokens can annotate both trees built by `ast <https://docs.python.org/2/library/ast.html>`_,
  62. AND those built by `astroid <https://github.com/PyCQA/astroid>`_.
  63. Here's an example:
  64. .. code-block:: python
  65. import asttokens, ast
  66. source = "Robot('blue').walk(steps=10*n)"
  67. atok = asttokens.ASTTokens(source, parse=True)
  68. Once the tree has been marked, nodes get ``.first_token``, ``.last_token`` attributes, and
  69. the ``ASTTokens`` object offers helpful methods:
  70. .. code-block:: python
  71. attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
  72. print(atok.get_text(attr_node))
  73. start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
  74. print(atok.text[:start] + 'RUN' + atok.text[end:])
  75. Which produces this output:
  76. .. code-block:: text
  77. Robot('blue').walk
  78. Robot('blue').RUN(steps=10*n)
  79. The ``ASTTokens`` object also offers methods to walk and search the list of tokens that make up
  80. the code (or a particular AST node), which is more useful and powerful than dealing with the text
  81. directly.
  82. Contribute
  83. ----------
  84. To contribute:
  85. 1. Fork this repository, and clone your fork.
  86. 2. Install the package with test dependencies (ideally in a virtualenv) with::
  87. pip install -e '.[test]'
  88. 3. Run tests in your current interpreter with the command ``pytest`` or ``python -m pytest``.
  89. 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.
  90. 5. By default certain tests which take a very long time to run are skipped, but they are run in CI.
  91. These are marked using the ``pytest`` marker ``slow`` and can be run on their own with ``pytest -m slow`` or as part of the full suite with ``pytest -m ''``.