Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump to black24 #86

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
54 changes: 29 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
60 changes: 33 additions & 27 deletions src/pypnusershub/__main__.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 3 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L3

Added line #L3 was not covered by tests

"""
Command line tools to initialize the database.
Expand All @@ -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

Check warning on line 14 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L14

Added line #L14 was not covered by tests


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)

Check warning on line 22 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L22

Added line #L22 was not covered by tests
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(

Check warning on line 25 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L25

Added line #L25 was not covered by tests
(
"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")

Check warning on line 35 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L35

Added line #L35 was not covered by tests


# 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")

Check warning on line 41 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L41

Added line #L41 was not covered by tests
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(

Check warning on line 46 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L46

Added line #L46 was not covered by tests
"This will delete all the content of "
" the 'utilisateurs' db. Are you sure ? [N/y] "
)
if confirm != "y":
print('Abort')
print("Abort")

Check warning on line 51 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L51

Added line #L51 was not covered by tests
sys.exit(0)

print('Deleting schema')
print("Deleting schema")

Check warning on line 54 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L54

Added line #L54 was not covered by tests
run_db_cmd(delete_schema, args.db_uri)


Expand All @@ -55,38 +61,38 @@


def call_load_fixtures(args):
print('Loading fixtures')
print("Loading fixtures")

Check warning on line 64 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L64

Added line #L64 was not covered by tests
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")

Check warning on line 70 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L70

Added line #L70 was not covered by tests

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)

Check warning on line 77 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L76-L77

Added lines #L76 - L77 were not covered by tests
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)

Check warning on line 81 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L80-L81

Added lines #L80 - L81 were not covered by tests
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)

Check warning on line 85 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L84-L85

Added lines #L84 - L85 were not covered by tests
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)

Check warning on line 89 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L88-L89

Added lines #L88 - L89 were not covered by tests
parser_load_fixture.set_defaults(func=call_load_fixtures)

return parser


if __name__ == '__main__':
if __name__ == "__main__":

Check warning on line 95 in src/pypnusershub/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/__main__.py#L95

Added line #L95 was not covered by tests
parser = make_cmd_parser()
args = parser.parse_args()
args.func(args)
8 changes: 4 additions & 4 deletions src/pypnusershub/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 8 in src/pypnusershub/env.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/env.py#L8

Added line #L8 was not covered by tests
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)

Check warning on line 16 in src/pypnusershub/env.py

View check run for this annotation

Codecov / codecov/patch

src/pypnusershub/env.py#L16

Added line #L16 was not covered by tests
ma_module = import_module(ma_module_name)
ma = getattr(ma_module, ma_object_name)
else:
Expand Down
6 changes: 2 additions & 4 deletions src/pypnusershub/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +33,8 @@ def upgrade():
);
END;
$BODY$ ;
""")
"""
)


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2022-04-28 10:35:41.272095

"""

from alembic import op
import sqlalchemy as sa

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -207,4 +211,5 @@ def downgrade():
d.id_application
FROM utilisateurs.v_roleslist_forall_applications d
WHERE d.groupe = false;
""")
"""
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2021-08-24 15:39:57.784074

"""

import importlib.resources

from alembic import op
Expand All @@ -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"
)
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading
Loading