Skip to content

Commit

Permalink
Add initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanoch committed Feb 20, 2017
0 parents commit 1293869
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions flake8_translation_activate.py
Original file line number Diff line number Diff line change
@@ -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')))]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flake8
51 changes: 51 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
],
)

0 comments on commit 1293869

Please sign in to comment.