• Stars
    star
    172
  • Rank 221,201 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Annotate Python AST trees with source text and token information

ASTTokens

https://readthedocs.org/projects/asttokens/badge/?version=latest

The asttokens module annotates Python abstract syntax trees (ASTs) with the positions of tokens and text in the source code that generated them.

It makes it possible for tools that work with logical AST nodes to find the particular text that resulted in those nodes, for example for automated refactoring or highlighting.

Installation

asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/:

pip install asttokens

The code is on GitHub: https://github.com/gristlabs/asttokens.

The API Reference is here: http://asttokens.readthedocs.io/en/latest/api-index.html.

Usage

ASTTokens works with both Python2 and Python3.

ASTTokens can annotate both trees built by ast, AND those built by astroid.

Here's an example:

import asttokens, ast
source = "Robot('blue').walk(steps=10*n)"
atok = asttokens.ASTTokens(source, parse=True)

Once the tree has been marked, nodes get .first_token, .last_token attributes, and the ASTTokens object offers helpful methods:

attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
print(atok.get_text(attr_node))
start, end = attr_node.last_token.startpos, attr_node.last_token.endpos
print(atok.text[:start] + 'RUN' + atok.text[end:])

Which produces this output:

Robot('blue').walk
Robot('blue').RUN(steps=10*n)

The ASTTokens object also offers methods to walk and search the list of tokens that make up the code (or a particular AST node), which is more useful and powerful than dealing with the text directly.

Contribute

To contribute:

  1. Fork this repository, and clone your fork.

  2. Install the package with test dependencies (ideally in a virtualenv) with:

    pip install -e '.[test]'
    
  3. Run tests in your current interpreter with the command pytest or python -m pytest.

  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.

  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.

More Repositories

1

grist-core

Grist is the evolution of spreadsheets.
TypeScript
6,985
star
2

ts-interface-checker

Runtime library to validate data against TypeScript interfaces.
TypeScript
323
star
3

grist-desktop

Desktop Grist, packaged with Electron
TypeScript
166
star
4

ts-interface-builder

Compile TypeScript interfaces into a description that allows runtime validation
TypeScript
132
star
5

mkdocs-windmill

Outstanding mkdocs theme with a focus on navigation and usability
CSS
105
star
6

grist-static

Showing Grist spreadsheets on a static website, without a special backend.
TypeScript
88
star
7

grist-widget

A repository of custom widgets to embed in Grist documents
JavaScript
56
star
8

grist-omnibus

an opinionated Grist+Dex+Traefik package for first-time self-hosters
JavaScript
52
star
9

yaml-cfn

Parser and schema for CloudFormation YAML templates
JavaScript
31
star
10

py_grist_api

Python client for interacting with Grist
Python
19
star
11

grist-help

Grist documentation and help center articles
HTML
13
star
12

grist-api

NodeJS client for interacting with Grist
TypeScript
13
star
13

grainjs

Javascript library from Grist Labs
TypeScript
13
star
14

grist-ee

The source code for self-managed Grist Enterprise.
TypeScript
11
star
15

aws-lambda-upload

Package and upload an AWS lambda with its minimal dependencies
JavaScript
7
star
16

secrets.js

Tiny node tool to share secrets using public key crypto
JavaScript
6
star
17

jupyterlite-widget

Python
3
star
18

grain-rpc

Typed RPC interface on top of an arbitrary communication channel
TypeScript
3
star
19

grist-pug-py-widget

Grist widget to directly develop custom widgets within Gristโ€™s UI.
JavaScript
3
star
20

grist-form-submit

Turn form submissions on webpages into new records in Grist documents
TypeScript
3
star
21

mocha-webdriver

Write Mocha style tests using selenium-webdriver, with many conveniences.
TypeScript
3
star
22

npm-check-shrinkwrap

Quickly check if contents of node_modules correspond to npm-shrinkwrap.json
JavaScript
2
star
23

grist-plugin-examples

2
star
24

collect-js-deps

Collect the minimal list of dependencies required by a JS file.
JavaScript
1
star
25

grist-zapier

Code behind the initial Grist/Zapier integration, exported as a reference.
JavaScript
1
star