From 007721868528d61cad165753a409f0996ebae831 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 7 May 2019 14:07:59 +0200 Subject: [PATCH] improve code style (#32866) - remove obviously dead code (reported by flake8) - fix PEP8 violations - rename variable using stdlib builtin names - use get_version() from combo's setup.py --- setup.py | 103 +++++++++++++++++-------------- src/authentic2_auth_fc/models.py | 8 +-- tests/settings.py | 16 +++++ tests/test_api.py | 16 +++++ tests/test_auth_fc.py | 16 +++++ 5 files changed, 108 insertions(+), 51 deletions(-) diff --git a/setup.py b/setup.py index 9ff7f3b..8641625 100755 --- a/setup.py +++ b/setup.py @@ -76,72 +76,81 @@ class eo_sdist(sdist): if os.path.exists('VERSION'): os.remove('VERSION') + class install_lib(_install_lib): def run(self): self.run_command('compile_translations') _install_lib.run(self) + def get_version(): '''Use the VERSION, if absent generates a version with git describe, if not - tag exists, take 0.0.0- and add the length of the commit log. + tag exists, take 0.0- and add the length of the commit log. ''' if os.path.exists('VERSION'): with open('VERSION', 'r') as v: return v.read() if os.path.exists('.git'): - p = subprocess.Popen(['git','describe','--dirty','--match=v*'], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen(['git', 'describe', '--dirty=.dirty', '--match=v*'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = p.communicate()[0] if p.returncode == 0: - return result.split()[0][1:].replace('-', '.') + result = result.decode('ascii').strip()[1:] # strip spaces/newlines and initial v + if '-' in result: # not a tagged version + real_number, commit_count, commit_hash = result.split('-', 2) + version = '%s.post%s+%s' % (real_number, commit_count, commit_hash) + else: + version = result + return version else: - return '0.0.0-%s' % len( - subprocess.check_output( - ['git', 'rev-list', 'HEAD']).splitlines()) - return '0.0.0' + return '0.0.post%s' % len( + subprocess.check_output( + ['git', 'rev-list', 'HEAD']).splitlines()) + return '0.0' README = file(os.path.join( os.path.dirname(__file__), 'README')).read() -setup(name='authentic2-auth-fc', - version=get_version(), - license='AGPLv3', - description='Authentic2 FranceConnect plugin', - long_description=README, - author="Entr'ouvert", - url='https://repos.entrouvert.org/authentic2-auth-fc.git', - author_email="info@entrouvert.com", - packages=find_packages('src'), - package_dir={ - '': 'src', - }, - package_data={ - 'authentic2_auth_fc': [ - 'templates/authentic2_auth_fc/*.html', - 'static/authentic2_auth_fc/css/*.css', - 'static/authentic2_auth_fc/js/*.js', - 'static/authentic2_auth_fc/img/*.png', - 'static/authentic2_auth_fc/img/*.svg', - 'locale/fr/LC_MESSAGES/django.po', - 'locale/fr/LC_MESSAGES/django.mo', - '*.json', - ], - }, - install_requires=[ - 'authentic2', - 'requests>2.11', - 'requests-oauthlib', +setup( + name='authentic2-auth-fc', + version=get_version(), + license='AGPLv3', + description='Authentic2 FranceConnect plugin', + long_description=README, + author="Entr'ouvert", + url='https://repos.entrouvert.org/authentic2-auth-fc.git', + author_email="info@entrouvert.com", + packages=find_packages('src'), + package_dir={ + '': 'src', + }, + package_data={ + 'authentic2_auth_fc': [ + 'templates/authentic2_auth_fc/*.html', + 'static/authentic2_auth_fc/css/*.css', + 'static/authentic2_auth_fc/js/*.js', + 'static/authentic2_auth_fc/img/*.png', + 'static/authentic2_auth_fc/img/*.svg', + 'locale/fr/LC_MESSAGES/django.po', + 'locale/fr/LC_MESSAGES/django.mo', + '*.json', ], - entry_points={ - 'authentic2.plugin': [ - 'authentic2-auth-fc = authentic2_auth_fc:Plugin', - ], - }, - cmdclass={ - 'build': build, - 'install_lib': install_lib, - 'compile_translations': compile_translations, - 'sdist': eo_sdist}, - zip_safe=False, + }, + install_requires=[ + 'authentic2', + 'requests>2.11', + 'requests-oauthlib', + ], + entry_points={ + 'authentic2.plugin': [ + 'authentic2-auth-fc = authentic2_auth_fc:Plugin', + ], + }, + cmdclass={ + 'build': build, + 'install_lib': install_lib, + 'compile_translations': compile_translations, + 'sdist': eo_sdist}, + zip_safe=False, ) diff --git a/src/authentic2_auth_fc/models.py b/src/authentic2_auth_fc/models.py index 533e9f7..698e7e0 100644 --- a/src/authentic2_auth_fc/models.py +++ b/src/authentic2_auth_fc/models.py @@ -30,11 +30,11 @@ from authentic2_auth_oidc.utils import parse_timestamp from . import app_settings -def base64url_decode(input): - rem = len(input) % 4 +def base64url_decode(encoded): + rem = len(encoded) % 4 if rem > 0: - input += b'=' * (4 - rem) - return base64.urlsafe_b64decode(input) + encoded += b'=' * (4 - rem) + return base64.urlsafe_b64decode(encoded) def parse_id_token(id_token, client_id=None, client_secret=None): diff --git a/tests/settings.py b/tests/settings.py index f08a2dd..556c81f 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,3 +1,19 @@ +# authentic2-auth-fc - authentic2 authentication for FranceConnect +# Copyright (C) 2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + import os LANGUAGE_CODE = 'en' diff --git a/tests/test_api.py b/tests/test_api.py index 53d0671..2016004 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,4 +1,20 @@ # -*- coding: utf-8 -*- +# authentic2-auth-fc - authentic2 authentication for FranceConnect +# Copyright (C) 2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + from authentic2_auth_fc.models import FcAccount diff --git a/tests/test_auth_fc.py b/tests/test_auth_fc.py index 7f0520b..8965d05 100644 --- a/tests/test_auth_fc.py +++ b/tests/test_auth_fc.py @@ -1,4 +1,20 @@ # -*- coding: utf-8 -*- +# authentic2-auth-fc - authentic2 authentication for FranceConnect +# Copyright (C) 2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + import pytest import re import urlparse