README.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ASTTokens
  2. =========
  3. .. image:: https://img.shields.io/pypi/v/asttokens.svg
  4. :target: https://pypi.python.org/pypi/asttokens/
  5. .. image:: https://img.shields.io/pypi/pyversions/asttokens.svg
  6. :target: https://pypi.python.org/pypi/asttokens/
  7. .. image:: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml/badge.svg
  8. :target: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml
  9. .. image:: https://readthedocs.org/projects/asttokens/badge/?version=latest
  10. :target: http://asttokens.readthedocs.io/en/latest/index.html
  11. .. image:: https://coveralls.io/repos/github/gristlabs/asttokens/badge.svg
  12. :target: https://coveralls.io/github/gristlabs/asttokens
  13. .. Start of user-guide
  14. The ``asttokens`` module annotates Python abstract syntax trees (ASTs) with the positions of tokens
  15. and text in the source code that generated them.
  16. It makes it possible for tools that work with logical AST nodes to find the particular text that
  17. resulted in those nodes, for example for automated refactoring or highlighting.
  18. Installation
  19. ------------
  20. asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/::
  21. pip install asttokens
  22. The code is on GitHub: https://github.com/gristlabs/asttokens.
  23. The API Reference is here: http://asttokens.readthedocs.io/en/latest/api-index.html.
  24. Usage
  25. -----
  26. ASTTokens can annotate both trees built by `ast <https://docs.python.org/2/library/ast.html>`_,
  27. AND those built by `astroid <https://github.com/PyCQA/astroid>`_.
  28. Here's an example:
  29. .. code-block:: python
  30. import asttokens, ast
  31. source = "Robot('blue').walk(steps=10*n)"
  32. atok = asttokens.ASTTokens(source, parse=True)
  33. Once the tree has been marked, nodes get ``.first_token``, ``.last_token`` attributes, and
  34. the ``ASTTokens`` object offers helpful methods:
  35. .. code-block:: python
  36. attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
  37. print(atok.get_text(attr_node))
  38. start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
  39. print(atok.text[:start] + 'RUN' + atok.text[end:])
  40. Which produces this output:
  41. .. code-block:: text
  42. Robot('blue').walk
  43. Robot('blue').RUN(steps=10*n)
  44. The ``ASTTokens`` object also offers methods to walk and search the list of tokens that make up
  45. the code (or a particular AST node), which is more useful and powerful than dealing with the text
  46. directly.
  47. Contribute
  48. ----------
  49. To contribute:
  50. 1. Fork this repository, and clone your fork.
  51. 2. Install the package with test dependencies (ideally in a virtualenv) with::
  52. pip install -e '.[test]'
  53. 3. Run tests in your current interpreter with the command ``pytest`` or ``python -m pytest``.
  54. 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.
  55. 5. By default certain tests which take a very long time to run are skipped, but they are run in CI.
  56. 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 ''``.