Skip to content

Commit

Permalink
lint to black 24
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Jan 26, 2024
1 parent 4b05b88 commit b3964e3
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 91 deletions.
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

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


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)


Expand All @@ -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)
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)
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:
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

0 comments on commit b3964e3

Please sign in to comment.