kwalitee package

Submodules

kwalitee.config module

kwalitee base configuration.

To change it, put a config.py into your instance.

kwalitee.config.CONTEXT

Context for Github.

kwalitee.config.COMPONENTS

List of supported components.

kwalitee.config.ACCESS_TOKEN

Github access token. Used to post statuses and comments. It MUST be set.

kwalitee.config.AUTO_CREATE

Allow anyone to add its repository by setting up the webhook on Github.

Default: False

kwalitee.config.CHECK_COMMIT_MESSAGES

Enable the commit message checks.

Default: True

kwalitee.config.CHECK_WIP

Enable the work-in-progress pull requests checks. Disabled by default.

Default: False

kwalitee.config.CHECK_LICENSE

Enable the license checks.

Default: True

kwalitee.config.CHECK_PEP8

Enable the PEP8 checks.

Default: True

kwalitee.config.CHECK_PEP257

Enable the PEP257 checks.

Default: True

kwalitee.config.CHECK_PYFLAKES

Enable the PyFlakes checks. PEP8 checks are required.

Default: True

kwalitee.config.IGNORE

Error codes to ignore.

Default: ['E123', 'E226', 'E24', 'E501', 'E265']

kwalitee.config.SELECT

Error codes to specially enable.

Default: []

kwalitee.config.WORKER_TIMEOUT

Background worker job time window.

Any job taking longer than that will be killed.

RQ default timeout is 180 seconds

kwalitee.config.MIN_REVIEWERS

Minimum number of reviewers for py:func:message check <.kwalitee.check_message>.

Default: 3

kwalitee.config.LABEL_WIP

Label to apply for a work-in-progress pull request.

Default: "in_work"

kwalitee.config.LABEL_REVIEW

Label to apply for a pull request that needs more reviewers.

Default: "in_review"

kwalitee.config.LABEL_READY

Label to apply for a pull request that passes all the checks.

Default: "in_integration"

kwalitee.config.EXCLUDES

List of regex of excluded files.

Default: []

kwalitee.config.ALT_SIGNATURES = ('Reported-by',)

Alternative signatures recognized but not counted as reviewers.

kwalitee.config.COMMIT_MSG_TEMPLATE = '{component}: description (max 50 chars, using nouns)\n\n* Detailed description formatted as a bullet list (using present tense).\n\nSigned-off-by: {author}\n{extra}'

Template used to generate the commit message from the git hook.

kwalitee.config.GITHUB = 'https://github.com/'

Github base URL.

kwalitee.config.GITHUB_REPO = 'https://github.com/{account}/{repository}/'

Github repository URL template.

kwalitee.config.HOOK_TEMPLATE = '#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport sys\nfrom kwalitee import create_app\nfrom kwalitee.hooks import {hook}\n\nif __name__ == "__main__":\n with create_app().app_context():\n sys.exit({hook}(sys.argv))\n'

Template used to generate the git hooks, customize at will.

kwalitee.config.PEP257_MATCH = '(?!test_).*\\.py'

Files checked for PEP257 conformance.

kwalitee.config.PEP257_MATCH_DIR = '[^\\.].*'

Directories checkes for PEP257 conformance.

kwalitee.config.SIGNATURES = ('Signed-off-by', 'Co-authored-by', 'Tested-by', 'Reviewed-by', 'Acked-by')

Authors and reviewers signatures.

kwalitee.config.TRUSTED_DEVELOPERS = []

Super developers who’s code never fail.

kwalitee.factory module

kwalitee factory for Flask application.

class kwalitee.factory.ShaConverter(map)[source]

Bases: werkzeug.routing.BaseConverter

Werkzeug routing converter for sha-1 (truncated or full).

regex = u'(?!/)(?:[a-fA-F0-9]{40}|[a-fA-F0-9]{7})'
weight = 150
kwalitee.factory.create_app(name=None, config=None)[source]

Create the Flask application.

kwalitee.hooks module

Git hooks.

kwalitee.hooks.commit_msg_hook(argv)[source]

Hook: for checking commit message (prevent commit).

kwalitee.hooks.post_commit_hook(argv=None)[source]

Hook: for checking commit message.

kwalitee.hooks.pre_commit_hook(argv=None)[source]

Hook: checking the staged files.

kwalitee.hooks.prepare_commit_msg_hook(argv)[source]

Hook: prepare a commit message.

kwalitee.hooks.run(command, raw_output=False)[source]

Run a command using subprocess.

Parameters:
  • command (str) – command line to be run
  • raw_output (bool) – does not attempt to convert the output as unicode
Returns:

error code, output (stdout) and error (stderr)

Return type:

tuple

kwalitee.kwalitee module

Kwalitee checks for PEP8, PEP257, PyFlakes and License.

kwalitee.kwalitee.SUPPORTED_FILES = (u'.py', u'.html', u'.tpl', u'.js', u'.jsx', u'.css', u'.less')

Supported file types.

kwalitee.kwalitee.check_file(filename, **kwargs)[source]

Perform static analysis on the given file.

Parameters:filename (str) – path of file to check.
Returns:errors sorted by line number or None if file is excluded
Return type:list
kwalitee.kwalitee.check_license(filename, **kwargs)[source]

Perform a license check on the given file.

The license format should be commented using # and live at the top of the file. Also, the year should be the current one.

Parameters:
  • filename (str) – path of file to check.
  • year (int) – default current year
  • ignore (list) – codes to ignore, e.g. ('L100', 'L101')
  • python_style (bool) – False for JavaScript or CSS files
Returns:

errors

Return type:

list

kwalitee.kwalitee.check_message(message, **kwargs)[source]

Check the message format.

Rules:

  • the first line must start by a component name
  • and a short description (52 chars),
  • then bullet points are expected
  • and finally signatures.
Parameters:
  • components (list) – compontents, e.g. ('auth', 'utils', 'misc')
  • signatures (list) – signatures, e.g. ('Signed-off-by', 'Reviewed-by')
  • alt_signatures (list) – alternative signatures, e.g. ('Tested-by',)
  • trusted (list) – optional list of reviewers, e.g. ('john.doe@foo.org',)
  • max_length (int) – optional maximum line length (by default: 72)
  • max_first_line (int) – optional maximum first line length (by default: 50)
  • allow_empty (bool) – optional way to allow empty message (by default: False)
Returns:

errors sorted by line number

Return type:

list

kwalitee.kwalitee.check_pep257(filename, **kwargs)[source]

Perform static analysis on the given file docstrings.

Parameters:
  • filename (str) – path of file to check.
  • ignore (list) – codes to ignore, e.g. (‘D400’,)
  • match (str) – regex the filename has to match to be checked
  • match_dir (str) – regex everydir in path should match to be checked
Returns:

errors

Return type:

list

kwalitee.kwalitee.check_pep8(filename, **kwargs)[source]

Perform static analysis on the given file.

Parameters:
  • filename (str) – path of file to check.
  • ignore (list) – codes to ignore, e.g. ('E111', 'E123')
  • select (list) – codes to explicitly select.
  • pyflakes (bool) – run the pyflakes checks too (default True)
Returns:

errors

Return type:

list

See also

pep8.Checker

kwalitee.kwalitee.get_options(config)[source]

Build the options from the Flask config.

kwalitee.kwalitee.is_file_excluded(filename, excludes)[source]

Check if the file should be excluded.

Parameters:
  • filename – file name
  • excludes – list of regex to match
Returns:

True if the file should be excluded

kwalitee.models module

Database models to persist the data over time.

class kwalitee.models.Account(name, email=None, token=None)[source]

Bases: flask_sqlalchemy.Model

Github account.

classmethod create(name, email=None, token=None)[source]

Create and commit and new account.

created_at

Date of creation.

email

Email.

classmethod find_or_create(name, email=None, token=None)[source]

Find or create an account.

id

Identifier

name

Username.

token

API Token.

classmethod update_or_create(name, email=None, token=None)[source]

Modify or create an account.

updated_at

Date of last modification.

class kwalitee.models.BranchStatus(commit, name, url, content=None)[source]

Bases: flask_sqlalchemy.Model

Status of a pull request.

commit
commit_id
content

Get the content of the status.

created_at
errors

Get the number of errors found.

classmethod find_or_create(commit, name, url, content=None)[source]

Find or create a commit status.

get_content()[source]

Get the content of the status.

id
is_pending()[source]

Return True is the commit status hasn’t been checked yet.

name
set_content(value)[source]

Set the content of the status.

state

Get the state.

updated_at
url
class kwalitee.models.CommitStatus(repository, sha, url, content=None)[source]

Bases: flask_sqlalchemy.Model

Status of a push.

content

Get the content of the status.

created_at
errors

Get the number of errors found.

classmethod find_or_create(repository, sha, url)[source]

Find or create a commit status.

get_content()[source]

Get the content of the status.

id
is_pending()[source]

Return True is the commit status hasn’t been checked yet.

repository
repository_id
set_content(value)[source]

Set the content of the status.

sha
state

Get the state.

updated_at
url
class kwalitee.models.Repository(owner, name)[source]

Bases: flask_sqlalchemy.Model

Github repository.

created_at
classmethod find_or_create(owner, name)[source]

Find or create a repository.

fullname

Get the fullname of the repository.

id
name
owner
owner_id
updated_at
kwalitee.models.init_app(app)[source]

Initialize the Flask app with db.

kwalitee.tasks module

Tasks to run on the worker.

kwalitee.tasks.get_headers(repository, config)[source]

Get the HTTP headers for the GitHub api.

This is required to post comments on GitHub on your behalf. Please update your configuration accordingly.

ACCESS_TOKEN = "deadbeef..."

It can also be overwritten per user.

$ kwalitee account add username --token=deadbeef...
Returns:HTTP headers
Return type:dict
kwalitee.tasks.pull_request(branch_status_id, pull_request_url, status_url, config)[source]

Performing all the tests on the pull request.

Then pings back the given status_url and update the issue labels.

Parameters:
  • branch_status_id (int) – identifier of the branch status.
  • pull_request_url (str) – github api pull request
  • status_url (str) – github api status url
  • config (dict) – configuration dictionary
Returns:

status body and applied labels

Return type:

dict

kwalitee.tasks.push(commit_status_id, commit_url, status_url, config)[source]

Performing all the tests on the commit.

Parameters:
  • commit_status_id – identifier of the branch status
  • commit_url – github api commit url
  • status_url – github api status url
  • config – configuration dictionary

kwalitee.views module

Views like in MTV.

kwalitee.views.account(account)[source]

Display the repositories linked with one account.

Parameters:account – name of the account
kwalitee.views.branch(account, repository, branch)[source]

Display the statuses of a branch.

Parameters:
  • account – name of the owner
  • repository – name of the repository
  • branch – name of the branch
kwalitee.views.branch_status(account, repository, branch, sha)[source]

Display the status of a pull request.

Parameters:
  • account – name of the owner
  • repository – name of the repository
  • branch – name of the branch
  • sha – commit identifier of the commit related with the branch
kwalitee.views.commit(account, repository, sha)[source]

Display the status of a commit.

Parameters:
  • account – name of the owner
  • repository – name of the repository
  • sha – identifier of the commit
kwalitee.views.index()[source]

Homepage that lists the accounts.

kwalitee.views.payload()[source]

Handle the GitHub events.

See also

Event Types <https://developer.github.com/v3/activity/events/types/>

kwalitee.views.repository(account, repository, limit=50)[source]

Display the recents commits and branches of a repository.

Parameters:
  • account – name of the owner
  • repository – name of the repository
  • limit – size of the commit window
kwalitee.views.status(sha)[source]

Show the status of a commit.

deprecated static files aren’t used anymore. To be removed at some point.

Parameters:sha – identifier of a commit.

kwalitee.worker module

Initialize Redis and setups the RQ worker.

kwalitee.worker.init_app(app)[source]

Initialize the RQ queue.

kwalitee.wsgi module

WSGI application with debug middleware if in debug mode.

Module contents

kwalitee Flask application and git hooks.

kwalitee.create_app(name=None, config=None)[source]

Create the Flask application.