Skip to content

Commit

Permalink
Merge pull request #1 from ryan-rs/feature/ASC-457/implement_flake8_p…
Browse files Browse the repository at this point in the history
…lugin

ASC-457 Enforce Python Filename Formatting
  • Loading branch information
zreichert authored May 30, 2018
2 parents 4c348c5 + 595f353 commit c64ef8d
Show file tree
Hide file tree
Showing 22 changed files with 712 additions and 7 deletions.
15 changes: 9 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

# PyCharm / JetBrains project stuff
.idea/
*.iml

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand All @@ -8,6 +13,7 @@ __pycache__/

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
Expand All @@ -23,7 +29,6 @@ wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -54,7 +59,6 @@ coverage.xml
# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
Expand All @@ -81,14 +85,13 @@ celerybeat-schedule
# SageMath parsed files
*.sage.py

# Environments
# dotenv
.env

# virtualenv
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
Expand Down
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Config file for automatic testing at travis-ci.org

language: python
python:
- 3.6
- 3.5
- 3.4
- 2.7

# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -U tox-travis

# Command to run tests, e.g. python setup.py test
script: tox
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
=======
History
=======
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2018 Rackspace US, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
8 changes: 8 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include CONTRIBUTING.rst
include HISTORY.rst
include LICENSE
include README.rst

recursive-include tests *
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
111 changes: 111 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
.PHONY: clean clean-test clean-pyc clean-build clean-venv check-venv help install-editable
.DEFAULT_GOAL := help

SHELL := /bin/bash
export VIRTUALENVWRAPPER_PYTHON := /usr/bin/python

define BROWSER_PYSCRIPT
import os, webbrowser, sys

try:
from urllib import pathname2url
except:
from urllib.request import pathname2url

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT

define PRINT_HELP_PYSCRIPT
import re, sys

for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT

BROWSER := python -c "$$BROWSER_PYSCRIPT"

help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

check-venv: ## verify that the user is running in a Python virtual environment
@if [ -z "$(VIRTUALENVWRAPPER_SCRIPT)" ]; then echo 'Python virtualenvwrapper not installed!' && exit 1; fi
@if [ -z "$(VIRTUAL_ENV)" ]; then echo 'Not running within a virtual environment!' && exit 1; fi

clean: clean-build clean-pyc clean-test ## remove all build, test, coverage, artifacts and wipe virtualenv

clean-build: ## remove build artifacts
rm -fr build/
rm -fr dist/
rm -fr .eggs/
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -f {} +

clean-pyc: ## remove Python file artifacts
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +

clean-test: ## remove test and coverage artifacts
rm -fr .tox/
rm -f .coverage
rm -fr htmlcov/
rm -fr .pytest_cache/

clean-venv: uninstall check-venv ## remove all packages from current virtual environment
@source virtualenvwrapper.sh && wipeenv || echo "Skipping wipe of environment"

lint: ## check style with flake8
flake8 flake8_filename setup.py tests --ignore N

test: ## run tests quickly with the default Python
py.test

test-all: ## run tests on every Python version with tox
tox

install: clean build uninstall ## install the package to the active Python's site-packages
pip install dist/*.whl

install-editable: ## install the package in editable mode
if pip list -e | grep 'flake8-filename'; then echo 'Editable package already installed'; else pip install -e .; fi

install-dev-requirements: ## install the requirements for development
pip install -r requirements.txt

develop: clean install-dev-requirements install-editable ## install necessary packages to setup a dev environment

build: ## build a wheel
python setup.py bdist_wheel

publish: ## publish package to PyPI
twine upload dist/*.whl

bump-major: ## bumps the version of by major
bumpversion major

bump-minor: ## bumps the version of by minor
bumpversion minor

bump-patch: ## bumps the version of by patch
bumpversion patch

uninstall: ## remove this package
pip uninstall flake8-filename -y || echo 'flake8-filename not installed'

release-major: install-dev-requirements bump-major lint install test publish ## package and upload a major release
echo 'Successfully released!'
echo 'Please push the newly created tag and commit to GitHub.'

release-minor: install-dev-requirements bump-minor lint install test publish ## package and upload a minor release
echo 'Successfully released!'
echo 'Please push the newly created tag and commit to GitHub.'

release-patch: install-dev-requirements bump-patch lint install test publish ## package and upload a patch release
echo 'Successfully released!'
echo 'Please push the newly created tag and commit to GitHub.'
58 changes: 58 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
===============
flake8-filename
===============

.. image:: https://img.shields.io/travis/rcbops/flake8-filename.svg
:target: https://travis-ci.org/rcbops/flake8-filename

A flake8 linter plug-in for validating that certain Python files comply with a user defined pattern.

Quick Start Guide
-----------------

1. Install ``flake8-filename`` from PyPI with pip::

$ pip install flake8-filename

2. Configure a mark that you would like to validate::

$ cd project_root/
$ vi .flake8

.. code-block:: ini
[flake8]
filename_check1 = filter_regex=test_.+
filename_regex=test_[\w-]+$
3. Run flake8::

$ flake8 tests/

Gotchas
-------

1. It is highly recommended to use this plugin inside of a virtualenv
2. A configuration is required by this plugin, if none is found the plugin will throw a N401 validation error for every file

Violation Codes
---------------

All possible violation codes are documented in violation_codes_


Example Configurations
----------------------

More example configurations can be found in configuration_

Credits
-------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _CONTRIBUTING.rst: CONTRIBUTING.rst
.. _configuration: docs/configuration.rst
.. _violation_codes: docs/violation_codes.rst
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage
2 changes: 2 additions & 0 deletions constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flake8>=3.5.0
pip<10.0.0
49 changes: 49 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=============
Configuration
=============

Location
========
The flake8-filename plug-in loads its configuration options from the same source as standard flake8 configuration.
Flake8 supports storing its configuration in the following places:

Your top-level user directory In your project in one of ``setup.cfg``, ``tox.ini``, or ``.flake8``.For more information
on configuration locations see:

Flake8_configuration_

Configuration
=============
You may configure up to 50 filename validators.

+---------------------+----------------------------------------------+-------------------------------------------------+
| Param Name + Valid Argument + Explanation +
+=====================+==============================================+=================================================+
| filter_regex + any valid regex that does not contain spaces + A regex to filter on certain Python files |
+---------------------+----------------------------------------------+-------------------------------------------------+
| filename_regex + any valid regex that does not contain spaces | A regex to validate filtered Python filenames |
+---------------------+----------------------------------------------+-------------------------------------------------+

**This plug-in will automatically strip the leading path and extension for the Python files under evaluation.**

Examples:
=========
All examples assume running against the following test file.


**test_example.py** : An example pytest::

def test_thing():
pass

**.flake8** : A simple configuration to validate that Python files that begin with "test\_" have "fun" in the name::

[flake8]
filename_check1 = filter_regex=test_.+
filename_regex=test_fun.*

**Shell Output** : evaluate if the filename contains "fun"::

./test_example.py:1:1: N501 filename failed regex validation 'test_fun.*'

.. _Flake8_configuration: http://flake8.pycqa.org/en/latest/user/configuration.html
3 changes: 3 additions & 0 deletions docs/example_config.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
filename_check1 = filter_regex=test_.+
filename_regex=test_[\w-]+$
31 changes: 31 additions & 0 deletions docs/release_process.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
===============
Release Process
===============

The easiest way to release a new version of flake8-filename is to use make.

1. First you will need to know the username and password for the account you want to use to release to PyPI shared_accounts_

2. You will need to make sure that you are on the master branch, your working directory is clean and up to date.

3. Decide if you are going to increment the major, minor, or patch version. You can refer to semver_ to help you make that decision.

4. Use the `release-major`, `release-minor`, or `release-patch`.

**make release** ::

make release-minor

5. The task will stop and prompt you for your PyPI username and password if you don't have these set in your `.pypirc` file.

6. Once the task has successfully completed you need to push the tag and commit.

**push tag** ::

git push origin && git push origin refs/tags/<tagname>

7. Create a release on GitHub. GitHub_release_

.. _semver: https://semver.org
.. _shared_accounts: https://rpc-openstack.atlassian.net/wiki/spaces/ASC/pages/143949893/Useful+Links#UsefulLinks-SharedAccounts
.. _GitHub_release: https://help.github.com/articles/creating-releases/
15 changes: 15 additions & 0 deletions docs/violation_codes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Error / Violation Codes
=======================

flake8-filename is a flake8 plugin that validates that certain Python files comply with a user defined pattern.
All error codes generated by this plugin begin with N. Here are all possible codes:

+---------------------------------------------------------------------------------------------------------+
| Code | Example Message |
+======+==================================================================================================+
| N401 + no configuration found ... please provide filename configuration in a flake8 config |
+------+--------------------------------------------------------------------------------------------------+
| N5XX | filename fails user defined regex pattern |
+------+--------------------------------------------------------------------------------------------------+

The codes referenced in the table above that end in XX are configurable. Up to 50 instances may be created.
Loading

0 comments on commit c64ef8d

Please sign in to comment.