From 2a1f35f86481949a3d91149cefac441ced881a16 Mon Sep 17 00:00:00 2001 From: Jacobe2169 Date: Fri, 26 Jan 2024 13:33:49 +0100 Subject: [PATCH 1/2] lint to black 24 --- setup.py | 54 +++++++++-------- src/pypnusershub/__main__.py | 60 ++++++++++--------- src/pypnusershub/env.py | 8 +-- src/pypnusershub/migrations/env.py | 6 +- .../10e87bc144cd_get_id_role_by_name.py | 11 ++-- ...e_add_unique_constraint_on_t_roles_uuid.py | 1 + ...5f5_fix_v_roleslist_forall_applications.py | 17 ++++-- .../72f227e37bdf_utilisateurs_sample_data.py | 5 +- ...f_add_additional_data_field_to_organism.py | 17 +++--- ...add_unique_constraint_on_uuid_organisme.py | 11 ++-- ...4bf21ac6238_fix_temp_user_organism_size.py | 5 +- ...f9d3b95946cd_set_code_profil_in_integer.py | 1 + ...fa35dfe5ff27_create_utilisateurs_schema.py | 11 +++- src/pypnusershub/tests/fixtures.py | 8 ++- src/pypnusershub/tests/test_utils.py | 4 +- 15 files changed, 128 insertions(+), 91 deletions(-) diff --git a/setup.py b/setup.py index 376fd76..3cda3d8 100755 --- a/setup.py +++ b/setup.py @@ -3,46 +3,50 @@ root_dir = Path(__file__).absolute().parent -with (root_dir / 'VERSION').open() as f: +with (root_dir / "VERSION").open() as f: version = f.read() -with (root_dir / 'README.md').open() as f: +with (root_dir / "README.md").open() as f: long_description = f.read() - setuptools.setup( - name='pypnusershub', + name="pypnusershub", version=version, description="Python lib to authenticate using PN's UsersHub", long_description=long_description, - long_description_content_type='text/markdown', - maintainer='Parcs nationaux des Écrins et des Cévennes', - maintainer_email='geonature@ecrins-parcnational.fr', - url='https://github.com/PnX-SI/UsersHub-authentification-module', - packages=setuptools.find_packages('src'), - package_dir={'': 'src'}, - package_data={'pypnusershub.migrations': ['data/*.sql']}, + long_description_content_type="text/markdown", + maintainer="Parcs nationaux des Écrins et des Cévennes", + maintainer_email="geonature@ecrins-parcnational.fr", + url="https://github.com/PnX-SI/UsersHub-authentification-module", + packages=setuptools.find_packages("src"), + package_dir={"": "src"}, + package_data={"pypnusershub.migrations": ["data/*.sql"]}, install_requires=( list(open("requirements-common.in", "r")) + list(open("requirements-dependencies.in", "r")) ), extras_require={ - 'tests': [ 'pytest', 'pytest-flask', ], + "tests": [ + "pytest", + "pytest-flask", + ], }, entry_points={ - 'alembic': [ - 'migrations = pypnusershub.migrations:versions', + "alembic": [ + "migrations = pypnusershub.migrations:versions", ], }, - classifiers=['Development Status :: 1 - Planning', - 'Intended Audience :: Developers', - 'Natural Language :: English', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'License :: OSI Approved :: GNU Affero General Public License v3', - 'Operating System :: OS Independent'], + classifiers=[ + "Development Status :: 1 - Planning", + "Intended Audience :: Developers", + "Natural Language :: English", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Operating System :: OS Independent", + ], ) diff --git a/src/pypnusershub/__main__.py b/src/pypnusershub/__main__.py index 7180fe1..0244fb0 100644 --- a/src/pypnusershub/__main__.py +++ b/src/pypnusershub/__main__.py @@ -1,7 +1,6 @@ # coding: utf8 -from __future__ import (unicode_literals, print_function, - absolute_import, division) +from __future__ import unicode_literals, print_function, absolute_import, division """ Command line tools to initialize the database. @@ -12,40 +11,47 @@ import sqlalchemy -from pypnusershub.db.tools import (init_schema, delete_schema, load_fixtures) +from pypnusershub.db.tools import init_schema, delete_schema, load_fixtures def run_db_cmd(func, db_uri, *args, **kwargs): - """ Run a function from pypnuserhub.db.tools with proper warnings """ + """Run a function from pypnuserhub.db.tools with proper warnings""" try: func(db_uri, *args, **kwargs) except sqlalchemy.exc.ArgumentError as e: - sys.exit('Unable to use the passed URI strings: %s' % e) + sys.exit("Unable to use the passed URI strings: %s" % e) except sqlalchemy.exc.OperationalError as e: if "authentication failed" in str(e): - sys.exit(("Unable to authenticate to '%s'. " - "Make sure to either provide a proper user/password " - "or execute this command as PostGreSQL " - "admin user (E.G: 'postgres') ") % db_uri) + sys.exit( + ( + "Unable to authenticate to '%s'. " + "Make sure to either provide a proper user/password " + "or execute this command as PostGreSQL " + "admin user (E.G: 'postgres') " + ) + % db_uri + ) raise - print('Done') + print("Done") # Wrap all calls to pypnuserhub.db.tools's function in run_db_cmd # to have good warning messages. def call_init_schema(args): - print('Initializing schema') + print("Initializing schema") run_db_cmd(init_schema, args.db_uri) def call_delete_schema(args): - confirm = input("This will delete all the content of " - " the 'utilisateurs' db. Are you sure ? [N/y] ") + confirm = input( + "This will delete all the content of " + " the 'utilisateurs' db. Are you sure ? [N/y] " + ) if confirm != "y": - print('Abort') + print("Abort") sys.exit(0) - print('Deleting schema') + print("Deleting schema") run_db_cmd(delete_schema, args.db_uri) @@ -55,38 +61,38 @@ def call_reset_schema(args): def call_load_fixtures(args): - print('Loading fixtures') + print("Loading fixtures") run_db_cmd(load_fixtures, args.db_uri) def make_cmd_parser(): - """ Create a CMD parser with subcommands for pypnuserhub.db.tools funcs""" - parser = argparse.ArgumentParser('python -m pypnuserhub') + """Create a CMD parser with subcommands for pypnuserhub.db.tools funcs""" + parser = argparse.ArgumentParser("python -m pypnuserhub") parser.set_defaults(func=lambda x: parser.print_usage(sys.stderr)) subparsers = parser.add_subparsers() - parser_init_schema = subparsers.add_parser('init_schema') - parser_init_schema.add_argument('db_uri', type=str) + parser_init_schema = subparsers.add_parser("init_schema") + parser_init_schema.add_argument("db_uri", type=str) parser_init_schema.set_defaults(func=call_init_schema) - parser_delete_schema = subparsers.add_parser('delete_schema') - parser_delete_schema.add_argument('db_uri', type=str) + parser_delete_schema = subparsers.add_parser("delete_schema") + parser_delete_schema.add_argument("db_uri", type=str) parser_delete_schema.set_defaults(func=call_delete_schema) - parser_reset_schema = subparsers.add_parser('reset_schema') - parser_reset_schema.add_argument('db_uri', type=str) + parser_reset_schema = subparsers.add_parser("reset_schema") + parser_reset_schema.add_argument("db_uri", type=str) parser_reset_schema.set_defaults(func=call_reset_schema) - parser_load_fixture = subparsers.add_parser('load_fixtures') - parser_load_fixture.add_argument('db_uri', type=str) + parser_load_fixture = subparsers.add_parser("load_fixtures") + parser_load_fixture.add_argument("db_uri", type=str) parser_load_fixture.set_defaults(func=call_load_fixtures) return parser -if __name__ == '__main__': +if __name__ == "__main__": parser = make_cmd_parser() args = parser.parse_args() args.func(args) diff --git a/src/pypnusershub/env.py b/src/pypnusershub/env.py index 37ea2d5..d55c67d 100644 --- a/src/pypnusershub/env.py +++ b/src/pypnusershub/env.py @@ -3,17 +3,17 @@ from flask_marshmallow import Marshmallow from flask_sqlalchemy import SQLAlchemy -db_path = environ.get('FLASK_SQLALCHEMY_DB') +db_path = environ.get("FLASK_SQLALCHEMY_DB") if db_path: - db_module_name, db_object_name = db_path.rsplit('.', 1) + db_module_name, db_object_name = db_path.rsplit(".", 1) db_module = import_module(db_module_name) db = getattr(db_module, db_object_name) else: db = SQLAlchemy() -marsmallow_path = environ.get('FLASK_MARSHMALLOW') +marsmallow_path = environ.get("FLASK_MARSHMALLOW") if marsmallow_path: - ma_module_name, ma_object_name = marsmallow_path.rsplit('.', 1) + ma_module_name, ma_object_name = marsmallow_path.rsplit(".", 1) ma_module = import_module(ma_module_name) ma = getattr(ma_module, ma_object_name) else: diff --git a/src/pypnusershub/migrations/env.py b/src/pypnusershub/migrations/env.py index f18633d..1cb5c61 100644 --- a/src/pypnusershub/migrations/env.py +++ b/src/pypnusershub/migrations/env.py @@ -10,7 +10,7 @@ # access to the values within the .ini file in use. config = context.config -sqlalchemy_uri = os.environ.get('SQLALCHEMY_URI') +sqlalchemy_uri = os.environ.get("SQLALCHEMY_URI") if sqlalchemy_uri: config.set_main_option("sqlalchemy.url", sqlalchemy_uri) @@ -68,9 +68,7 @@ def run_migrations_online(): ) with connectable.connect() as connection: - context.configure( - connection=connection, target_metadata=target_metadata - ) + context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() diff --git a/src/pypnusershub/migrations/versions/10e87bc144cd_get_id_role_by_name.py b/src/pypnusershub/migrations/versions/10e87bc144cd_get_id_role_by_name.py index 60311e3..015e046 100644 --- a/src/pypnusershub/migrations/versions/10e87bc144cd_get_id_role_by_name.py +++ b/src/pypnusershub/migrations/versions/10e87bc144cd_get_id_role_by_name.py @@ -5,19 +5,21 @@ Create Date: 2021-10-07 17:20:59.521063 """ + from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '10e87bc144cd' -down_revision = '951b8270a1cf' +revision = "10e87bc144cd" +down_revision = "951b8270a1cf" branch_labels = None depends_on = None def upgrade(): - op.execute(""" + op.execute( + """ CREATE FUNCTION utilisateurs.get_id_role_by_name(roleName character varying) RETURNS integer LANGUAGE plpgsql @@ -31,7 +33,8 @@ def upgrade(): ); END; $BODY$ ; - """) + """ + ) def downgrade(): diff --git a/src/pypnusershub/migrations/versions/112ccf1024ce_add_unique_constraint_on_t_roles_uuid.py b/src/pypnusershub/migrations/versions/112ccf1024ce_add_unique_constraint_on_t_roles_uuid.py index b0c2174..297badd 100644 --- a/src/pypnusershub/migrations/versions/112ccf1024ce_add_unique_constraint_on_t_roles_uuid.py +++ b/src/pypnusershub/migrations/versions/112ccf1024ce_add_unique_constraint_on_t_roles_uuid.py @@ -5,6 +5,7 @@ Create Date: 2022-04-28 10:35:41.272095 """ + from alembic import op import sqlalchemy as sa diff --git a/src/pypnusershub/migrations/versions/5b334b77f5f5_fix_v_roleslist_forall_applications.py b/src/pypnusershub/migrations/versions/5b334b77f5f5_fix_v_roleslist_forall_applications.py index 41e0d51..5eb5d54 100644 --- a/src/pypnusershub/migrations/versions/5b334b77f5f5_fix_v_roleslist_forall_applications.py +++ b/src/pypnusershub/migrations/versions/5b334b77f5f5_fix_v_roleslist_forall_applications.py @@ -5,19 +5,21 @@ Create Date: 2021-09-21 13:22:45.003976 """ + from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '5b334b77f5f5' -down_revision = '830cc8f4daef' +revision = "5b334b77f5f5" +down_revision = "830cc8f4daef" branch_labels = None depends_on = None def upgrade(): - op.execute(""" + op.execute( + """ DROP VIEW utilisateurs.v_userslist_forall_applications; DROP VIEW utilisateurs.v_roleslist_forall_applications; @@ -110,11 +112,13 @@ def upgrade(): d.id_application FROM utilisateurs.v_roleslist_forall_applications d WHERE d.groupe = false; - """) + """ + ) def downgrade(): - op.execute(""" + op.execute( + """ DROP VIEW utilisateurs.v_userslist_forall_applications; DROP VIEW utilisateurs.v_roleslist_forall_applications; @@ -207,4 +211,5 @@ def downgrade(): d.id_application FROM utilisateurs.v_roleslist_forall_applications d WHERE d.groupe = false; - """) + """ + ) diff --git a/src/pypnusershub/migrations/versions/72f227e37bdf_utilisateurs_sample_data.py b/src/pypnusershub/migrations/versions/72f227e37bdf_utilisateurs_sample_data.py index 0280740..091e595 100644 --- a/src/pypnusershub/migrations/versions/72f227e37bdf_utilisateurs_sample_data.py +++ b/src/pypnusershub/migrations/versions/72f227e37bdf_utilisateurs_sample_data.py @@ -5,6 +5,7 @@ Create Date: 2021-08-24 15:39:57.784074 """ + import importlib.resources from alembic import op @@ -21,7 +22,9 @@ def upgrade(): op.execute( - importlib.resources.read_text("pypnusershub.migrations.data", "utilisateurs-samples.sql") + importlib.resources.read_text( + "pypnusershub.migrations.data", "utilisateurs-samples.sql" + ) ) diff --git a/src/pypnusershub/migrations/versions/830cc8f4daef_add_additional_data_field_to_organism.py b/src/pypnusershub/migrations/versions/830cc8f4daef_add_additional_data_field_to_organism.py index d9547b0..7091344 100644 --- a/src/pypnusershub/migrations/versions/830cc8f4daef_add_additional_data_field_to_organism.py +++ b/src/pypnusershub/migrations/versions/830cc8f4daef_add_additional_data_field_to_organism.py @@ -5,29 +5,26 @@ Create Date: 2021-09-06 13:18:28.276081 """ + from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import JSONB # revision identifiers, used by Alembic. -revision = '830cc8f4daef' -down_revision = 'fa35dfe5ff27' +revision = "830cc8f4daef" +down_revision = "fa35dfe5ff27" branch_labels = None depends_on = None def upgrade(): op.add_column( - 'bib_organismes', - sa.Column('additional_data', JSONB, server_default='{}'), - schema='utilisateurs' + "bib_organismes", + sa.Column("additional_data", JSONB, server_default="{}"), + schema="utilisateurs", ) def downgrade(): - op.drop_column( - 'bib_organismes', - 'additional_data', - schema='utilisateurs' - ) + op.drop_column("bib_organismes", "additional_data", schema="utilisateurs") diff --git a/src/pypnusershub/migrations/versions/951b8270a1cf_add_unique_constraint_on_uuid_organisme.py b/src/pypnusershub/migrations/versions/951b8270a1cf_add_unique_constraint_on_uuid_organisme.py index df2c304..3fa0bf6 100644 --- a/src/pypnusershub/migrations/versions/951b8270a1cf_add_unique_constraint_on_uuid_organisme.py +++ b/src/pypnusershub/migrations/versions/951b8270a1cf_add_unique_constraint_on_uuid_organisme.py @@ -5,20 +5,23 @@ Create Date: 2021-09-23 09:44:23.613575 """ + from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '951b8270a1cf' -down_revision = '5b334b77f5f5' +revision = "951b8270a1cf" +down_revision = "5b334b77f5f5" branch_labels = None depends_on = None def upgrade(): - op.create_unique_constraint('bib_organismes_un', 'bib_organismes', ['uuid_organisme'], schema='utilisateurs') + op.create_unique_constraint( + "bib_organismes_un", "bib_organismes", ["uuid_organisme"], schema="utilisateurs" + ) def downgrade(): - op.drop_constraint('bib_organismes_un', 'bib_organismes', schema='utilisateurs') + op.drop_constraint("bib_organismes_un", "bib_organismes", schema="utilisateurs") diff --git a/src/pypnusershub/migrations/versions/f4bf21ac6238_fix_temp_user_organism_size.py b/src/pypnusershub/migrations/versions/f4bf21ac6238_fix_temp_user_organism_size.py index 59d5f5e..3b0b811 100644 --- a/src/pypnusershub/migrations/versions/f4bf21ac6238_fix_temp_user_organism_size.py +++ b/src/pypnusershub/migrations/versions/f4bf21ac6238_fix_temp_user_organism_size.py @@ -5,6 +5,7 @@ Create Date: 2023-06-30 15:02:59.198157 """ + from alembic import op import sqlalchemy as sa @@ -17,7 +18,9 @@ def upgrade(): - op.execute("ALTER TABLE utilisateurs.temp_users ALTER COLUMN organisme TYPE VARCHAR (250);") + op.execute( + "ALTER TABLE utilisateurs.temp_users ALTER COLUMN organisme TYPE VARCHAR (250);" + ) pass diff --git a/src/pypnusershub/migrations/versions/f9d3b95946cd_set_code_profil_in_integer.py b/src/pypnusershub/migrations/versions/f9d3b95946cd_set_code_profil_in_integer.py index bb3e842..5c7923f 100644 --- a/src/pypnusershub/migrations/versions/f9d3b95946cd_set_code_profil_in_integer.py +++ b/src/pypnusershub/migrations/versions/f9d3b95946cd_set_code_profil_in_integer.py @@ -5,6 +5,7 @@ Create Date: 2023-09-13 10:00:16.065637 """ + from alembic import op import sqlalchemy as sa diff --git a/src/pypnusershub/migrations/versions/fa35dfe5ff27_create_utilisateurs_schema.py b/src/pypnusershub/migrations/versions/fa35dfe5ff27_create_utilisateurs_schema.py index b8064c0..a4aaa54 100644 --- a/src/pypnusershub/migrations/versions/fa35dfe5ff27_create_utilisateurs_schema.py +++ b/src/pypnusershub/migrations/versions/fa35dfe5ff27_create_utilisateurs_schema.py @@ -5,6 +5,7 @@ Create Date: 2021-08-24 15:39:57.784074 """ + import importlib.resources from alembic import op @@ -12,14 +13,18 @@ # revision identifiers, used by Alembic. -revision = 'fa35dfe5ff27' +revision = "fa35dfe5ff27" down_revision = None -branch_labels = ('utilisateurs',) +branch_labels = ("utilisateurs",) depends_on = None def upgrade(): - op.execute(importlib.resources.read_text('pypnusershub.migrations.data', 'utilisateurs.sql')) + op.execute( + importlib.resources.read_text( + "pypnusershub.migrations.data", "utilisateurs.sql" + ) + ) def downgrade(): diff --git a/src/pypnusershub/tests/fixtures.py b/src/pypnusershub/tests/fixtures.py index 6a7d576..32298ff 100644 --- a/src/pypnusershub/tests/fixtures.py +++ b/src/pypnusershub/tests/fixtures.py @@ -2,7 +2,13 @@ from flask_login import logout_user from pypnusershub.env import db -from pypnusershub.db.models import Organisme, Application, User, Profils, UserApplicationRight +from pypnusershub.db.models import ( + Organisme, + Application, + User, + Profils, + UserApplicationRight, +) organism = { "nom_organisme": "test", diff --git a/src/pypnusershub/tests/test_utils.py b/src/pypnusershub/tests/test_utils.py index 3f1ec96..f92c5db 100644 --- a/src/pypnusershub/tests/test_utils.py +++ b/src/pypnusershub/tests/test_utils.py @@ -40,7 +40,9 @@ def test_set_cookie(self): assert cookie_attrs["Path"] == "/geonature" assert cookie_attrs["Expires"] != "" - logout_response = delete_cookie(response, key=key, application_url=application_url) + logout_response = delete_cookie( + response, key=key, application_url=application_url + ) cookie = logout_response.headers.getlist("Set-Cookie")[1] cookie_attrs = parse_cookie(cookie) From 6c862079edfb3a09a8936dd6f63278646de2951c Mon Sep 17 00:00:00 2001 From: Jacobe2169 Date: Fri, 26 Jan 2024 13:36:25 +0100 Subject: [PATCH 2/2] add lint ci --- .github/workflows/lint.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..20c6ab4 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,13 @@ +name: Lint + +on: [push, pull_request] + +jobs: + backend: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Backend code formatting check (Black) + uses: psf/black@stable