From 12938698aa68f715383d053883473512ff509540 Mon Sep 17 00:00:00 2001 From: Niels Van Och Date: Mon, 20 Feb 2017 09:16:00 +0100 Subject: [PATCH] Add initial version --- LICENSE | 21 +++++++++++++ README.rst | 25 ++++++++++++++++ flake8_translation_activate.py | 54 ++++++++++++++++++++++++++++++++++ requirements.txt | 1 + setup.py | 51 ++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 LICENSE create mode 100644 README.rst create mode 100644 flake8_translation_activate.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a126e57 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 VikingCo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..1bb9240 --- /dev/null +++ b/README.rst @@ -0,0 +1,25 @@ +Flake8 translation.activate +=========================== + +Check for usages of translation.activate(). + +This module provides a plugin for ``flake8``, the Python code checker. + + +Installation +------------ + +You can install or upgrade ``flake8-translation-activate`` with these commands:: + + $ pip install flake8-translation-activate + $ pip install --upgrade flake8-translation-activate + + +Plugin for Flake8 +----------------- + +When both ``flake8`` and ``flake8-translation-activate`` are installed, the plugin is +available in ``flake8``:: + + $ flake8 --version + 2.0 (pep8: 1.4.5, flake8-translation-activate: 1.0, pyflakes: 0.6.1) diff --git a/flake8_translation_activate.py b/flake8_translation_activate.py new file mode 100644 index 0000000..a9dce5c --- /dev/null +++ b/flake8_translation_activate.py @@ -0,0 +1,54 @@ +import ast +import tokenize + +from sys import stdin + +__version__ = '1.0' + +TRANSLATION_ACTIVATE_ERROR_CODE = 'T006' +TRANSLATION_ACTIVATE_ERROR_MESSAGE = 'call to django.translation.activate() found' +ACTIVATE_IMPORT_ERROR_MESSAGE = 'import of django.translation.activate() found' + + +class TranslationActivateChecker(object): + name = 'flake8-translation-activate' + version = __version__ + ignores = () + + def __init__(self, tree, filename='(none)'): + self.tree = tree + self.filename = (filename == 'stdin' and stdin) or filename + + def run(self): + # Get lines to ignore + if self.filename == stdin: + noqa = _get_noqa_lines(self.filename) + else: + with open(self.filename, 'r') as file_to_check: + noqa = _get_noqa_lines(file_to_check.readlines()) + + # Run the actual check + errors = [] + for node in ast.walk(self.tree): + if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == 'activate' and node.func.value.id == 'translation' and node.lineno not in noqa: + errors.append({ + 'message': '{0} {1}'.format(TRANSLATION_ACTIVATE_ERROR_CODE, TRANSLATION_ACTIVATE_ERROR_MESSAGE), + 'line': node.lineno, + 'col': node.col_offset, + }) + if isinstance(node, ast.ImportFrom) and node.module == 'django.translation' and 'activate' in [alias.name for alias in node.names] and node.lineno not in noqa: + errors.append({ + 'message': '{0} {1}'.format(TRANSLATION_ACTIVATE_ERROR_CODE, ACTIVATE_IMPORT_ERROR_MESSAGE), + 'line': node.lineno, + 'col': node.col_offset, + }) + + # Yield the found errors + for error in errors: + yield (error.get("line"), error.get("col"), error.get("message"), type(self)) + + +def _get_noqa_lines(code): + tokens = tokenize.generate_tokens(lambda L=iter(code): next(L)) + return [token[2][0] for token in tokens if token[0] == tokenize.COMMENT and + (token[1].endswith('noqa') or (isinstance(token[0], str) and token[0].endswith('noqa')))] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3930480 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flake8 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8d8d27a --- /dev/null +++ b/setup.py @@ -0,0 +1,51 @@ +from __future__ import with_statement +from setuptools import setup + + +def get_version(fname='flake8_translation_activate.py'): + with open(fname) as f: + for line in f: + if line.startswith('__version__'): + return eval(line.split('=')[-1]) + + +def get_long_description(): + descr = [] + for fname in ('README.rst',): + with open(fname) as f: + descr.append(f.read()) + return '\n\n'.join(descr) + +install_requires = ['flake8'] + +setup( + name='flake8-translation-activate', + version=get_version(), + description="translation activate plugin for flake8", + long_description=get_long_description(), + keywords='flake8 translation activate', + author='Unleashed NV', + author_email='development@unleashed.be', + url='https://github.com/unleashed/flake8-translation-activate', + license='MIT', + py_modules=['flake8_translation_activate'], + zip_safe=False, + entry_points={ + 'flake8.extension': [ + 'flake8_translation_activate = flake8_translation_activate:TranslationActivateChecker', + ], + }, + install_requires=install_requires, + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Environment :: Console', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 3', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Software Development :: Quality Assurance', + ], +)