README.rst 3.2 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 works with both Python2 and Python3.
  27. ASTTokens can annotate both trees built by `ast <https://docs.python.org/2/library/ast.html>`_,
  28. AND those built by `astroid <https://github.com/PyCQA/astroid>`_.
  29. Here's an example:
  30. .. code-block:: python
  31. import asttokens, ast
  32. source = "Robot('blue').walk(steps=10*n)"
  33. atok = asttokens.ASTTokens(source, parse=True)
  34. Once the tree has been marked, nodes get ``.first_token``, ``.last_token`` attributes, and
  35. the ``ASTTokens`` object offers helpful methods:
  36. .. code-block:: python
  37. attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
  38. print(atok.get_text(attr_node))
  39. start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
  40. print(atok.text[:start] + 'RUN' + atok.text[end:])
  41. Which produces this output:
  42. .. code-block:: text
  43. Robot('blue').walk
  44. Robot('blue').RUN(steps=10*n)
  45. The ``ASTTokens`` object also offers methods to walk and search the list of tokens that make up
  46. the code (or a particular AST node), which is more useful and powerful than dealing with the text
  47. directly.
  48. Contribute
  49. ----------
  50. To contribute:
  51. 1. Fork this repository, and clone your fork.
  52. 2. Install the package with test dependencies (ideally in a virtualenv) with::
  53. pip install -e '.[test]'
  54. 3. Run tests in your current interpreter with the command ``pytest`` or ``python -m pytest``.
  55. 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.
  56. 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.