Skip to main content

pylama -- Code audit tool for python

Project description

Code audit tool for Python and JavaScript. Pylama wraps these tools:

  • pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna;

  • pydocstyle (formerly pep257 by Vladimir Keleshev) © 2014, Amir Rachum;

  • PyFlakes © 2005-2013, Kevin Watters;

  • Mccabe © Ned Batchelder;

  • Pylint © 2013, Logilab (should be installed ‘pylama_pylint’ module);

  • Radon © Michele Lacchia

  • gjslint © The Closure Linter Authors (should be installed ‘pylama_gjslint’ module);

Build Status Coverals Version Donate

Docs are available at https://pylama.readthedocs.org/. Pull requests with documentation enhancements and/or fixes are awesome and most welcome.

Requirements:

  • Python (2.6, 2.7, 3.2, 3.3)

  • To use JavaScript checker (gjslint) you need to install python-gflags with pip install python-gflags.

  • If your tests are failing on Win platform you are missing: curses - http://www.lfd.uci.edu/~gohlke/pythonlibs/ (The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals)

Installation:

Pylama could be installed using pip: ::

$ pip install pylama

Quickstart

Pylama is easy to use and really fun for checking code quality. Just run pylama and get common output from all pylama plugins (pycodestyle, PyFlakes and etc)

Recursive check the current directory.

$ pylama

Recursive check a path.

$ pylama <path_to_directory_or_file>

Ignore errors

$ pylama -i W,E501

Choose code checkers

$ pylama -l "pycodestyle,mccabe"

Choose code checkers for JavaScript:

$ pylama --linters=gjslint --ignore=E:0010 <path_to_directory_or_file>

Set Pylama (checkers) options

Command line options

$ pylama --help

usage: pylama [-h] [--verbose] [--version] [--format {pycodestyle,pylint}]
              [--select SELECT] [--sort SORT] [--linters LINTERS]
              [--ignore IGNORE] [--skip SKIP] [--report REPORT] [--hook]
              [--async] [--options OPTIONS] [--force] [--abspath]
              [paths [paths ...]]

Code audit tool for python.

positional arguments:
  paths                 Paths to files or directories for code check.

optional arguments:
  -h, --help            show this help message and exit
  --verbose, -v         Verbose mode.
  --version             show program's version number and exit
  --format {pycodestyle,pylint}, -f {pycodestyle,pylint}
                        Choose errors format (pycodestyle, pylint).
  --select SELECT, -s SELECT
                        Select errors and warnings. (comma-separated list)
  --sort SORT           Sort result by error types. Ex. E,W,D
  --linters LINTERS, -l LINTERS
                        Select linters. (comma-separated). Choices are
                        mccabe,pycodestyle,pyflakes,pydocstyle.
  --ignore IGNORE, -i IGNORE
                        Ignore errors and warnings. (comma-separated)
  --skip SKIP           Skip files by masks (comma-separated, Ex.
                        */messages.py)
  --report REPORT, -r REPORT
                        Send report to file [REPORT]
  --hook                Install Git (Mercurial) hook.
  --async               Enable async mode. Usefull for checking a lot of
                        files. Dont supported with pylint.
  --options OPTIONS, -o OPTIONS
                        Select configuration file. By default is
                        '<CURDIR>/pylama.ini'
  --force, -F           Force code checking (if linter doesnt allow)
  --abspath, -a         Use absolute paths in output.

File modelines

You can set options for Pylama inside a source files. Use pylama modeline for this.

Format:

# pylama:{name1}={value1}:{name2}={value2}:...
.. Somethere in code
# pylama:ignore=W:select=W301

Disable code checking for current file:

.. Somethere in code
# pylama:skip=1

The options have a must higher priority.

Skip lines (noqa)

Just add # noqa in end of line for ignore.

def urgent_fuction():
    unused_var = 'No errors here' # noqa

Configuration files

When starting Pylama try loading configuration file.

The programm searches for the first matching ini-style configuration file in the directories of command line argument. Pylama looks for the configuration in this order:

pylama.ini
setup.cfg
tox.ini
pytest.ini

You could set configuration file manually by “-o” option.

Pylama search sections with name starts pylama.

Section pylama contains a global options, like linters and skip.

[pylama]
format = pylint
skip = */.tox/*,*/.env/*
linters = pylint,mccabe
ignore = F0401,C0111,E731

Set Code-checkers’ options

You could set options for special code checker with pylama configurations.

[pylama:pyflakes]
builtins = _

[pylama:pycodestyle]
max_line_length = 100

[pylama:pylint]
max_line_length = 100
disable = R

See code checkers documentation for more info.

Set options for file (group of files)

You could set options for special file (group of files) with sections:

The options have a higher priority than in the pylama section.

[pylama:*/pylama/main.py]
ignore = C901,R0914,W0212
select = R

[pylama:*/tests.py]
ignore = C0110

[pylama:*/setup.py]
skip = 1

Pytest integration

Pylama have Pytest support. The package automatically register self as pytest plugin when during installation. Also pylama suports pytest_cache plugin.

Check files with pylama

pytest --pylama ...

Recomended way to settings pylama options when using pytest — configuration files (see below).

Writing a linter

You can write a custom extension for Pylama. Custom linter should be a python module. Name should be like ‘pylama_<name>’.

In ‘setup.py’ should be defined ‘pylama.linter’ entry point.

setup(
    # ...
    entry_points={
        'pylama.linter': ['lintername = pylama_lintername.main:Linter'],
    }
    # ...
)

‘Linter’ should be instance of ‘pylama.lint.Linter’ class. Must implemented two methods:

‘allow’ take a path and returned true if linter could check this file for errors. ‘run’ take a path and meta keywords params and return list of errors.

Example:

Just virtual ‘WOW’ checker.

setup.py:

setup(
    name='pylama_wow',
    install_requires=[ 'setuptools' ],
    entry_points={
        'pylama.linter': ['wow = pylama_wow.main:Linter'],
    }
    # ...
)

pylama_wow.py:

from pylama.lint import Linter as BaseLinter

class Linter(BaseLinter):

    def allow(self, path):
        return 'wow' in path

    def run(self, path, **meta):
        with open(path) as f:
            if 'wow' in f.read():
                return [{
                    lnum: 0,
                    col: 0,
                    text: 'Wow has been finded.',
                    type: 'WOW'
                }]

Run pylama from python code

from pylama.main import check_path, parse_options

my_redefined_options = {...}
my_path = '...'
options = parse_options([my_path], **my_redefined_options)
errors = check_path(options)

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/pylama/issues

Contributing

Development of adrest happens at GitHub: https://github.com/klen/pylama

Contributors

See AUTHORS.

License

Licensed under a BSD license.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylama-7.2.3.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

pylama-7.2.3-py2.py3-none-any.whl (29.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pylama-7.2.3.tar.gz.

File metadata

  • Download URL: pylama-7.2.3.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pylama-7.2.3.tar.gz
Algorithm Hash digest
SHA256 b3cfb3ef9424ca64e0c7177f737199d6e9d370968279396fbd66f99538655e0e
MD5 ce8ba1ed382778d540d576ed42fb53bf
BLAKE2b-256 998b6dfe845f14c7d7c5fb62345f023645d94d0f02363c4fc27f52ea1b41c314

See more details on using hashes here.

File details

Details for the file pylama-7.2.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pylama-7.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 087c5de1e2d727dc95f7f2bb46e6e9f96f124fc153e0f62e907144cdcda27f47
MD5 2a5a62f5a1a58b48d223f291a283bd41
BLAKE2b-256 4ffe6829766417ede942c9be57c67d715b683464ec6c3927d0611af0073acd7e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page