A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree https://libcst.readthedocs.io/
Find a file
2019-08-09 17:24:32 -07:00
.circleci use cache to speedup pyre CI job 2019-07-24 19:25:31 -07:00
.github add initial PULL_REQUEST_TEMPLATE.md 2019-07-22 19:59:57 -07:00
docs/source Add docs for MaybeSentinel and RemovalSentinel 2019-08-08 11:04:02 -07:00
libcst Fix incorrect description of on_leave for CSTVisitor/CSTTransform. 2019-08-09 17:24:32 -07:00
stubs Add runtime type validation support 2019-07-22 19:53:49 -07:00
.editorconfig Add config files to make tools easier to use 2019-07-22 19:53:49 -07:00
.flake8 Remove Instagram-specific bits from flake8 config 2019-07-22 20:05:07 -07:00
.gitattributes Add the logo to the docs/source/_static/ directory 2019-08-05 10:44:17 -07:00
.gitignore Ignore build/ directory. 2019-08-07 14:14:35 -07:00
.pyre_configuration add pyre CI job 2019-07-22 20:04:29 -07:00
.readthedocs.yml [readthedoc] install libcst 2019-08-07 15:58:02 -07:00
CHANGELOG.md Add a changelog file. 2019-07-24 12:11:50 -07:00
CODE_OF_CONDUCT.md add Code Of Conduct file 2019-07-22 19:59:50 -07:00
CONTRIBUTING.md add test command and rephrase coding style. 2019-07-22 19:59:57 -07:00
LICENSE first commit 2019-05-29 11:32:49 -07:00
MANIFEST.in improve setup.py to be ready for publishing package to pypi 2019-08-06 16:09:47 -07:00
pyproject.toml Add config files to make tools easier to use 2019-07-22 19:53:49 -07:00
README.rst Add tox enviroment to run isort and black and update readme 2019-08-09 10:06:29 -07:00
requirements-dev.txt [document] add jupyter based tutorial page 2019-08-06 16:34:03 -07:00
requirements.txt [document] add jupyter based tutorial page 2019-08-06 16:34:03 -07:00
setup.py [document] add jupyter based tutorial page 2019-08-06 16:34:03 -07:00
tox.ini Add tox enviroment to run isort and black and update readme 2019-08-09 10:06:29 -07:00

.. image:: docs/source/_static/logo/horizontal.svg
   :width: 600 px
   :alt: LibCST

A Concrete Syntax Tree (CST) parser and serializer library for Python

|readthedocs-badge| |circleci-badge|

.. |readthedocs-badge| image:: https://readthedocs.org/projects/pip/badge/?version=latest&style=flat
   :target: https://libcst.readthedocs.io/en/latest/
   :alt: Documentation

.. |circleci-badge| image:: https://circleci.com/gh/Instagram/LibCST/tree/master.svg?style=shield&circle-token=f89ff46c689cf53116308db295a492d687bf5732
   :target: https://circleci.com/gh/Instagram/LibCST/tree/master
   :alt: CircleCI

.. intro-start

LibCST parses Python 3.7 source code as a CST tree that keeps all formatting
details (comments, whitespaces, parentheses, etc). It's useful for building 
automated refactoring (codemod) applications and linters.

.. intro-end

.. why-libcst-intro-start

LibCST creates a compromise between an Abstract Syntax Tree (AST) and a traditional
Concrete Syntax Tree (CST). By carefully reorganizing and naming node types and
fields, we've created a lossless CST that looks and feels like an AST.

.. why-libcst-intro-end

You can learn more about `the value that LibCST provides 
<https://libcst.readthedocs.io/en/latest/why_libcst.html>`__ and `our 
motivations for the project 
<https://libcst.readthedocs.io/en/latest/motivation.html>`__
in `our documentation <https://libcst.readthedocs.io/en/latest/index.html>`__.

::

    1 + 2

::

    BinaryOperation(
        left=Integer(
            value='1',
            lpar=[],
            rpar=[],
        ),
        operator=Add(
            whitespace_before=SimpleWhitespace(
                value=' ',
            ),
            whitespace_after=SimpleWhitespace(
                value=' ',
            ),
        ),
        right=Integer(
            value='2',
            lpar=[],
            rpar=[],
        ),
        lpar=[],
        rpar=[],
    )

Getting Started
===============

Examining a sample tree
-----------------------

To examine the tree that is parsed from a particular file, do the following:

::

    python -m libcst.tool print <some_py_file.py>

Alternatively you can import LibCST into a Python REPL and use the included parser
and pretty printing functions:

>>> import libcst as cst
>>> from libcst.tool import dump
>>> print(dump(cst.parse_expression("(1 + 2)")))
BinaryOperation(
  left=Integer(
    value='1',
  ),
  operator=Add(),
  right=Integer(
    value='2',
  ),
  lpar=[
    LeftParen(),
  ],
  rpar=[
    RightParen(),
  ],
)

For a more detailed usage example, `see our documentation 
<https://libcst.readthedocs.io/en/latest/usage.html>`__.

Installation
------------

LibCST requires Python 3.6+ and can be easily installed using most common Python
packaging tools. We recommend installing the latest stable release from 
`PyPI <https://pypi.org/project/libcst/>`_ with pip:

.. code-block:: shell

    pip install libcst

Development
-----------

Start by setting up and activating a virtualenv:

.. code-block:: shell

    git clone git@github.com:Instagram/LibCST.git libcst
    cd libcst
    python3 -m venv ../libcst-env/  # just an example, put this wherever you want
    source ../libcst-env/bin/activate
    pip install --upgrade pip  # optional, if you have an old system version of pip
    pip install -r requirements.txt -r requirements-dev.txt
    # If you're done with the virtualenv, you can leave it by running:
    deactivate

We use `isort <https://isort.readthedocs.io/en/stable/>`_ and `black <https://black.readthedocs.io/en/stable/>`_
to format code. To format changes to be conformant, run the following in the root:

.. code-block:: shell

    tox -e autofix

To run all tests, you'll need to install `tox <https://tox.readthedocs.io/en/latest/>`_
and do the following in the root:

.. code-block:: shell

    tox -e py37

You can also run individual tests by using unittest and specifying a module like
this:

.. code-block:: shell

    python -m unitttest libcst.tests.test_batched_visitor

See the `unittest documentation <https://docs.python.org/3/library/unittest.html>`_
for more examples of how to run tests.

We use `Pyre <https://github.com/facebook/pyre-check>`_ for type-checking. To
verify types for the library, do the following in the root:

.. code-block:: shell

    pyre check

To generate documents, do the following in the root:

.. code-block:: shell

    tox -e docs

License
=======

LibCST is MIT licensed, as found in the LICENSE file.