From dd72fd36ab5df4c10b7c2b52bf7ef6c8a9985040 Mon Sep 17 00:00:00 2001 From: Martin Babka Date: Sat, 23 Jan 2021 13:33:40 +0100 Subject: [PATCH 1/2] Upgrade injector --- .vscode/launch.json | 14 +++ requirements.txt | 3 +- setup.py | 3 +- src/zsl/__init__.py | 5 -- .../application/containers/web_container.py | 2 - src/zsl/application/error_handler.py | 8 +- src/zsl/application/initialization_context.py | 7 +- .../initializers/library_initializer.py | 9 +- .../initializers/service_initializer.py | 19 ++-- .../initializers/unittest_initializer.py | 14 ++- src/zsl/application/modules/alchemy_module.py | 86 ++++++++----------- src/zsl/application/modules/cache_module.py | 4 +- src/zsl/application/modules/celery_module.py | 8 +- src/zsl/application/modules/cli_module.py | 21 ++--- .../modules/error_handler_module.py | 22 ++--- src/zsl/application/modules/gearman_module.py | 15 ++-- src/zsl/application/modules/logger_module.py | 16 ++-- src/zsl/application/modules/task_router.py | 15 ++-- .../modules/web/web_context_module.py | 43 ++++------ src/zsl/application/service_application.py | 3 +- src/zsl/cache/redis_cache_module.py | 9 +- src/zsl/cache/redis_id_helper.py | 10 +-- src/zsl/contrib/alembic/alembic_module.py | 38 ++++---- src/zsl/contrib/sentry/sentry_module.py | 25 +++--- src/zsl/interface/celery/worker.py | 15 ++-- src/zsl/interface/gearman/task_filler.py | 6 +- src/zsl/interface/task.py | 4 +- src/zsl/interface/task_queue.py | 27 +++--- src/zsl/interface/web/performers/default.py | 4 +- src/zsl/interface/web/performers/resource.py | 4 +- src/zsl/interface/web/performers/task.py | 11 +-- src/zsl/interface/web/utils/execution.py | 4 +- .../interface/web/utils/response_headers.py | 15 ++-- src/zsl/resource/guard.py | 4 +- src/zsl/resource/model_resource.py | 15 ++-- src/zsl/router/method.py | 14 ++- src/zsl/router/task.py | 14 ++- src/zsl/service/service.py | 12 +-- src/zsl/task/task_data.py | 12 +-- src/zsl/task/task_decorator.py | 22 +++-- src/zsl/tasks/zsl/cors_test_task.py | 4 +- src/zsl/tasks/zsl/db_test_task.py | 8 +- src/zsl/tasks/zsl/kill_worker_task.py | 4 +- src/zsl/tasks/zsl/sum_task.py | 4 +- src/zsl/tasks/zsl/version_task.py | 4 +- src/zsl/tasks/zsl/with_request_task.py | 4 +- src/zsl/testing/db.py | 45 +++++----- src/zsl/testing/http.py | 9 +- src/zsl/utils/background_task.py | 5 +- src/zsl/utils/cache_helper.py | 5 +- src/zsl/utils/deploy/apiari_doc_generator.py | 6 +- src/zsl/utils/email_helper.py | 6 +- src/zsl/utils/gearman_helper.py | 7 +- src/zsl/utils/injection_helper.py | 71 +++------------ src/zsl/utils/redis_helper.py | 11 ++- src/zsl/utils/resource_helper.py | 22 ++--- src/zsl/utils/security_helper.py | 4 +- src/zsl/utils/testing.py | 6 +- tests/application/error_handling_test.py | 4 +- tests/application/version_test.py | 4 +- tests/db/helpers/pagination_parsing_test.py | 4 +- .../interface/cli/exec_task_from_cli_test.py | 9 +- tests/interface/web/cors_test.py | 8 +- tests/interface/web/task_router_test.py | 12 +-- .../run_dummy_worker_with_params_test.py | 4 +- .../run_dummy_worker_without_params_test.py | 4 +- tests/mocks.py | 16 ++-- tests/resource/read_only_resource.py | 15 ++-- tests/resource/resource_test_helper.py | 4 +- tests/resource/transactional_guard_test.py | 9 +- .../task_decorators/forbid_web_access_test.py | 4 +- tests/task_decorators/secured_task_test.py | 3 - tests/testing/db_test.py | 33 ++++--- tests/utils/redis_helper_test.py | 5 +- 74 files changed, 402 insertions(+), 543 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index baff97f8..3a08fbf6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -57,6 +57,20 @@ "tests/application/version_test.py" ] }, + { + "name": "Test: tests/resource/transactional_guard_test.py", + "type": "python", + "request": "launch", + "module": "pytest", + "env": { + "PYTHONPATH": "${workspaceFolder}/src:${workspaceFolder}/tests" + }, + "args": [ + "--capture=sys", + "--log-cli-level=DEBUG", + "tests/resource/transactional_guard_test.py" + ] + }, { "name": "Test: all", "type": "python", diff --git a/requirements.txt b/requirements.txt index c865af74..21c99e66 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,12 +5,11 @@ click>=7.0 enum34 future Flask>=1.1 -injector==0.12.1 +injector==0.18.4 redis>=3.2 requests>=2.22 SQLAlchemy>=1.3,<1.4 tox -typing>=3.7 Werkzeug>=0.16,<0.17 zsl_client pytest diff --git a/setup.py b/setup.py index 829e84bc..f6f8e884 100644 --- a/setup.py +++ b/setup.py @@ -7,10 +7,9 @@ requirements = [ 'flask>=1.1,<1.2', 'future', - 'injector==0.12.1', + 'injector==0.18.4', 'requests>=2.22', 'SQLAlchemy>=1.3,<1.4', - 'typing>=3.7', 'Werkzeug>=0.15,<0.17', ] diff --git a/src/zsl/__init__.py b/src/zsl/__init__.py index f0e9d50d..b31af47b 100644 --- a/src/zsl/__init__.py +++ b/src/zsl/__init__.py @@ -10,8 +10,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - from flask import Config from injector import Module @@ -24,9 +22,6 @@ Zsl = ServiceApplication -# placeholder for default value used in function declaration for arguments which will be injected -Injected = None - # just for autocomplete in various IDEs inject = inject Config = Config diff --git a/src/zsl/application/containers/web_container.py b/src/zsl/application/containers/web_container.py index cf8fbf9d..e5b8222b 100644 --- a/src/zsl/application/containers/web_container.py +++ b/src/zsl/application/containers/web_container.py @@ -2,8 +2,6 @@ :mod:`zsl.application.containers.web_container` ----------------------------------------------- """ -from __future__ import unicode_literals - from zsl.application.containers.core_container import CoreContainer from zsl.application.modules.web.web_context_module import WebContextModule diff --git a/src/zsl/application/error_handler.py b/src/zsl/application/error_handler.py index ad791183..fe2859f7 100644 --- a/src/zsl/application/error_handler.py +++ b/src/zsl/application/error_handler.py @@ -6,9 +6,6 @@ an error handler for a given exception type. It also provides default error handlers. """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from functools import wraps import http.client import logging @@ -122,9 +119,8 @@ def error_handler(f): @wraps(f) def error_handling_function(*args, **kwargs): - @inject(error_config=ErrorConfiguration) - def get_error_configuration(error_config): - # type:(ErrorConfiguration)->ErrorConfiguration + @inject + def get_error_configuration(error_config: ErrorConfiguration) -> ErrorConfiguration: return error_config def should_skip_handling(): diff --git a/src/zsl/application/initialization_context.py b/src/zsl/application/initialization_context.py index 7fa02860..9eda111f 100644 --- a/src/zsl/application/initialization_context.py +++ b/src/zsl/application/initialization_context.py @@ -7,11 +7,12 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals +from typing import Any, List class InitializationContext(object): - def __init__(self, unit_test=False, initializers=None): + + def __init__(self, unit_test: bool = False, initializers: List[Any] = None): if not initializers: initializers = [] @@ -19,7 +20,7 @@ def __init__(self, unit_test=False, initializers=None): self._initializers = initializers self._is_initialized = False - def get_unit_testing(self): + def get_unit_testing(self) -> bool: return self._unit_test unit_testing = property(get_unit_testing) diff --git a/src/zsl/application/initializers/library_initializer.py b/src/zsl/application/initializers/library_initializer.py index 868e2765..2a001090 100644 --- a/src/zsl/application/initializers/library_initializer.py +++ b/src/zsl/application/initializers/library_initializer.py @@ -2,9 +2,6 @@ :mod:`zsl.application.initializers.library_initializer` ------------------------------------------------------- """ -from __future__ import unicode_literals - -from builtins import object import logging import os import sys @@ -20,10 +17,12 @@ def append_paths(path, vendor_modules): class LibraryInitializer(object): + # TODO: Change to iface + """Add vendor modules to current path.""" @staticmethod - @inject(config=Config) - def initialize(config): + @inject + def initialize(config: Config) -> None: logging.debug("Initializing project external libraries.") external_libraries = config.get('EXTERNAL_LIBRARIES', None) diff --git a/src/zsl/application/initializers/service_initializer.py b/src/zsl/application/initializers/service_initializer.py index aa4edfe0..718d558c 100644 --- a/src/zsl/application/initializers/service_initializer.py +++ b/src/zsl/application/initializers/service_initializer.py @@ -2,30 +2,29 @@ :mod:`zsl.application.initializers.service_initializer` ------------------------------------------------------- """ -from __future__ import unicode_literals - -from builtins import object import importlib import logging from injector import Binder, singleton -from zsl import Config, Injected, inject +from zsl import Config, inject from zsl.utils.string_helper import camelcase_to_underscore class ServiceInitializer(object): + # TODO: change to iface + """Add outside services to application injector.""" @staticmethod - @inject(binder=Binder) - def _bind_service(package_name, cls_name, binder=Injected): + @inject + def _bind_service(package_name: str, cls_name: str, binder: Binder) -> None: """Bind service to application injector. :param package_name: service package :type package_name: str :param cls_name: service class :type cls_name: str - :param binder: current application binder, injected + :param binder: current application binder :type binder: Binder """ module = importlib.import_module(package_name) @@ -39,11 +38,11 @@ def _bind_service(package_name, cls_name, binder=Injected): logging.debug("Created {0} binding.".format(cls)) @staticmethod - @inject(config=Config) - def initialize(config): + @inject + def initialize(config: Config) -> None: """Initialize method. - :param config: current application config, injected + :param config: current application config :type config: Config """ service_injection_config = config.get('SERVICE_INJECTION', ()) diff --git a/src/zsl/application/initializers/unittest_initializer.py b/src/zsl/application/initializers/unittest_initializer.py index bd92a93e..1ac7308b 100644 --- a/src/zsl/application/initializers/unittest_initializer.py +++ b/src/zsl/application/initializers/unittest_initializer.py @@ -2,18 +2,16 @@ :mod:`zsl.application.initializers.unittest_initializer` -------------------------------------------------------- """ -from __future__ import unicode_literals - -from builtins import object - from zsl import ApplicationContext, Config, inject class UnitTestInitializer(object): + # TODO: Change to iface + """Initializer handling the unit test settings.""" @staticmethod - @inject(config=Config) - def initialize(config): + @inject + def initialize(config: Config) -> None: if not UnitTestInitializer.is_unit_testing(): return @@ -26,6 +24,6 @@ def initialize(config): 'TEST_DATABASE_ENGINE_PROPS'] @staticmethod - @inject(ctx=ApplicationContext) - def is_unit_testing(ctx): + @inject + def is_unit_testing(ctx: ApplicationContext) -> bool: return ctx.unit_testing diff --git a/src/zsl/application/modules/alchemy_module.py b/src/zsl/application/modules/alchemy_module.py index e2fc2517..e6430692 100644 --- a/src/zsl/application/modules/alchemy_module.py +++ b/src/zsl/application/modules/alchemy_module.py @@ -2,88 +2,80 @@ :mod:`zsl.application.modules.alchemy_module` --------------------------------------------- """ -from __future__ import unicode_literals - -from abc import ABCMeta, abstractmethod -from builtins import object +from abc import ABC, abstractmethod import logging +from typing import Callable, Type -from injector import Module, provides, singleton +from injector import Module, inject, provider, singleton from sqlalchemy import create_engine from sqlalchemy.engine.base import Engine from sqlalchemy.orm.session import Session, sessionmaker -from zsl import Config, inject +from zsl import Config class SessionHolder(object): - def __init__(self, sess_cls): + def __init__(self, sess_cls: Type[Session]) -> None: self._sess_cls = sess_cls - def __call__(self): - # type: () -> Session + def __call__(self) -> Session: return self._sess_cls() class SessionFactory(object): """Creates a db session with an open transaction.""" - @inject(session_holder=SessionHolder) - def __init__(self, session_holder): - # type: (SessionHolder)->None + @inject + def __init__(self, session_holder: SessionHolder) -> None: self._session_holder = session_holder - def create_session(self): - # type: ()->Session + def create_session(self) -> Session: return self._session_holder() class TransactionHolder(object): - def __init__(self): + def __init__(self) -> None: self._orm = None self._transaction_callback = [] self._in_transaction = False - def has_session(self): - # type: () -> bool + def has_session(self) -> bool: return self._orm is not None @property - def session(self): - # type: () -> Session + def session(self) -> Session: return self._orm @property - def in_transaction(self): + def in_transaction(self) -> bool: return self._in_transaction - def inject_session(self, session): - # type: (Session) -> None + def inject_session(self, session: Session) -> None: """Used to set the session in the @transactional decorator.""" self._orm = session self._in_transaction = True - def begin(self): + def begin(self) -> None: pass - def commit(self): + def commit(self) -> None: logging.getLogger(__name__).debug("Commit.") self._orm.commit() - def rollback(self): + def rollback(self) -> None: logging.getLogger(__name__).debug("Rollback.") self._orm.rollback() - def close(self): + def close(self) -> None: logging.getLogger(__name__).debug("Close.") self._orm.close() self._orm = None self._in_transaction = False - def append_transaction_callback(self, callback): + def append_transaction_callback(self, callback: Callable[[], None]) -> None: self._transaction_callback.append(callback) - def run_transaction_callbacks(self): + def run_transaction_callbacks(self) -> None: callbacks = self._transaction_callback self._transaction_callback = [] for c in callbacks: @@ -91,37 +83,33 @@ def run_transaction_callbacks(self): class EmptyTransactionalHolder(object): - def __init__(self): + def __init__(self) -> None: self._session = None @property - def session(self): + def session(self) -> Session: return self._session -class TransactionHolderFactory(object): - __metaclass__ = ABCMeta +class TransactionHolderFactory(ABC): @abstractmethod - def create_transaction_holder(self): - # type: ()->TransactionHolder - pass + def create_transaction_holder(self) -> TransactionHolder: + raise NotImplementedError() class DefaultTransactionHolderFactory(TransactionHolderFactory): - def create_transaction_holder(self): - # type: ()->TransactionHolder + def create_transaction_holder(self) -> TransactionHolder: return TransactionHolder() class AlchemyModule(Module): """Adds SQLAlchemy to current configuration.""" - @provides(Engine, scope=singleton) - @inject(config=Config) - def provide_engine(self, config): - # type: (Config) -> Engine - + @singleton + @provider + @inject + def provide_engine(self, config: Config) -> Engine: engine = create_engine(config['DATABASE_URI'], **config['DATABASE_ENGINE_PROPS']) logging.debug("Created DB configuration to {0}.". @@ -129,17 +117,17 @@ def provide_engine(self, config): return engine - @provides(SessionHolder, scope=singleton) - @inject(engine=Engine) - def provide_session_holder(self, engine): - # type: (Engine) -> SessionHolder - + @singleton + @provider + @inject + def provide_session_holder(self, engine: Engine) -> SessionHolder: session = SessionHolder(sessionmaker(engine, autocommit=False, expire_on_commit=False)) logging.debug("Created ORM session") return session - @provides(TransactionHolderFactory, scope=singleton) - def provide_transaction_holder_factory(self): + @singleton + @provider + def provide_transaction_holder_factory(self) -> TransactionHolderFactory: return DefaultTransactionHolderFactory() diff --git a/src/zsl/application/modules/cache_module.py b/src/zsl/application/modules/cache_module.py index 37f83094..c6ddcb65 100644 --- a/src/zsl/application/modules/cache_module.py +++ b/src/zsl/application/modules/cache_module.py @@ -29,7 +29,7 @@ def configure(self, binder): :param Binder binder: The binder object holding the binding context, we\ add cache to the binder. """ - redis_cache_module = RedisCacheModule() + redis_cache_module = binder.injector.get(RedisCacheModule) binder.bind( RedisCacheModule, to=redis_cache_module, @@ -41,7 +41,7 @@ def configure(self, binder): scope=singleton ) - redis_id_helper = RedisIdHelper() + redis_id_helper = binder.injector.get(RedisIdHelper) binder.bind( RedisIdHelper, to=redis_id_helper, diff --git a/src/zsl/application/modules/celery_module.py b/src/zsl/application/modules/celery_module.py index 9185351e..2dbb6634 100644 --- a/src/zsl/application/modules/celery_module.py +++ b/src/zsl/application/modules/celery_module.py @@ -7,7 +7,7 @@ import click from injector import Module, singleton -from zsl import Injected, Zsl +from zsl import Zsl from zsl.application.modules.cli_module import ZslCli from zsl.interface.celery.worker import (CeleryTaskQueueMainWorker, CeleryTaskQueueOutsideWorker, CeleryTaskQueueWorkerBase) @@ -37,10 +37,8 @@ def configure(self, binder): class CeleryCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> CeleryCli - + @inject + def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help='Celery related tasks.') def celery(): pass diff --git a/src/zsl/application/modules/cli_module.py b/src/zsl/application/modules/cli_module.py index d6b10050..410dddd4 100644 --- a/src/zsl/application/modules/cli_module.py +++ b/src/zsl/application/modules/cli_module.py @@ -5,9 +5,8 @@ import traceback import click -from injector import Binder, Module, singleton +from injector import Binder, Module, inject, singleton -from zsl import inject from zsl.interface.cli import cli from zsl.task.job_context import Job, JobContext, create_job from zsl.utils.injection_helper import simple_bind @@ -31,9 +30,8 @@ def __call__(self, *args, **kwargs): class ZslTaskCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> ZslTaskCli + @inject + def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.command(help="Execute a single task.") @click.argument('task_path', metavar='task') @click.argument('data', default=None, required=False) @@ -52,9 +50,8 @@ def task(task_path, data=None): class ZslTestCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> ZslTestCli + @inject + def __init__(self, zsl_cli: ZslCli) -> ZslTaskCli: @zsl_cli.cli.group(help="Perform unit tests.") def test(): pass @@ -65,9 +62,8 @@ def run(): class ZslGenerateCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> ZslGenerateCli + @inject + def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help="Perform unit tests.") def generate(): pass @@ -106,8 +102,7 @@ def apiary_doc(): class CliModule(Module): """Adds Alembic support for migrations.""" - def configure(self, binder): - # type: (Binder) -> None + def configure(self, binder: Binder) -> None: bindings = [ZslCli, ZslTaskCli, ZslTestCli, ZslGenerateCli] for binding in bindings: simple_bind(binder, binding, singleton) diff --git a/src/zsl/application/modules/error_handler_module.py b/src/zsl/application/modules/error_handler_module.py index bf08030b..78da51e3 100644 --- a/src/zsl/application/modules/error_handler_module.py +++ b/src/zsl/application/modules/error_handler_module.py @@ -1,24 +1,20 @@ -from injector import Binder, Module, provides, singleton +from injector import Binder, Module, inject, provider -from zsl import Config, inject +from zsl import Config from zsl.application.error_handler import register from zsl.errors import ERROR_CONFIG_NAME, ErrorConfiguration class ErrorHandlerModule(Module): - @provides(interface=ErrorConfiguration, scope=singleton) - @inject(config=Config) - def provide_error_config(self, config): + + @provider + @inject + def provide_error_config(self, config: Config) -> ErrorConfiguration: # type: (Config)->ErrorConfiguration return config.get(ERROR_CONFIG_NAME, ErrorConfiguration()) - def configure(self, binder): - # type: (Binder)->None - @inject(error_config=ErrorConfiguration) - def get_error_config(error_config): - # type: (ErrorConfiguration)->ErrorConfiguration - return error_config - + def configure(self, binder: Binder) -> None: + error_config = binder.injector.get(ErrorConfiguration) super(ErrorHandlerModule, self).configure(binder) - for handler in get_error_config().handlers: + for handler in error_config.handlers: register(handler) diff --git a/src/zsl/application/modules/gearman_module.py b/src/zsl/application/modules/gearman_module.py index 78ef0e9b..0af18721 100644 --- a/src/zsl/application/modules/gearman_module.py +++ b/src/zsl/application/modules/gearman_module.py @@ -2,8 +2,6 @@ :mod:`zsl.application.modules.gearman_module` --------------------------------------------- """ -from __future__ import unicode_literals - import click from injector import ClassProvider, Module, singleton @@ -16,23 +14,20 @@ class GearmanCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli)->GearmanCli - + @inject + def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help="Gearman related tasks.") - def gearman(): + def gearman() -> None: pass @gearman.command(help="run worker") - def worker(): - # type: () -> None + def worker() -> None: run_worker() @gearman.command(help="send task to queue") @click.argument('task_path', metavar='task') @click.argument('data', default=None, required=False) - def task_filler(task_path, data): + def task_filler(task_path, data) -> None: exec_task_filler(task_path, data) self._gearman = gearman diff --git a/src/zsl/application/modules/logger_module.py b/src/zsl/application/modules/logger_module.py index 825a0f9d..4e0455a2 100644 --- a/src/zsl/application/modules/logger_module.py +++ b/src/zsl/application/modules/logger_module.py @@ -2,13 +2,11 @@ :mod:`zsl.application.modules.logger_module` -------------------------------------------- """ -from __future__ import unicode_literals - import logging.config -from injector import Binder, Module +from injector import Binder, Module, inject -from zsl import Config, Zsl, inject +from zsl import Config, Zsl class LoggerModule(Module): @@ -16,14 +14,12 @@ class LoggerModule(Module): LOGGING_CONFIG_NAME = 'LOGGING' - def configure(self, binder): - # type: (Binder) -> None + def configure(self, binder: Binder) -> None: super(LoggerModule, self).configure(binder) - self.configure_logging() + binder.injector.call_with_injection(self.configure_logging) - @inject(config=Config, app=Zsl) - def configure_logging(self, config, app): - # type: (Config) -> None + @inject + def configure_logging(self, config: Config, app: Zsl) -> None: default_config = dict( version=1, root=dict( diff --git a/src/zsl/application/modules/task_router.py b/src/zsl/application/modules/task_router.py index 33ff02fc..69910ae7 100644 --- a/src/zsl/application/modules/task_router.py +++ b/src/zsl/application/modules/task_router.py @@ -2,13 +2,9 @@ :mod:`zsl.application.modules.task_router_module` ------------------------------------------------- """ -from __future__ import absolute_import, division, print_function, unicode_literals +from injector import ClassProvider, Module, inject, provider, singleton -from builtins import * - -from injector import ClassProvider, Module, provides, singleton - -from zsl import Config, inject +from zsl import Config from zsl.router.task import TASK_CONFIGURATION_NAME, TaskConfiguration, TaskRouter @@ -16,9 +12,10 @@ class TaskRouterModule(Module): """Adds task router to current configuration.""" task_provider = ClassProvider(TaskRouter) - @provides(interface=TaskConfiguration, scope=singleton) - @inject(config=Config) - def provide_task_configuration(self, config): + @singleton + @provider + @inject + def provide_task_configuration(self, config: Config) -> TaskConfiguration: default_config = self._create_default_configuration() return config.get(TASK_CONFIGURATION_NAME, default_config) diff --git a/src/zsl/application/modules/web/web_context_module.py b/src/zsl/application/modules/web/web_context_module.py index 88edd213..c028050f 100644 --- a/src/zsl/application/modules/web/web_context_module.py +++ b/src/zsl/application/modules/web/web_context_module.py @@ -1,13 +1,10 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * import logging from typing import Any import click -from injector import Binder, provides, singleton +from injector import Binder, inject, provider, singleton -from zsl import Config, Zsl, inject +from zsl import Config, Zsl from zsl.application.initialization_context import InitializationContext from zsl.application.modules.cli_module import ZslCli from zsl.application.modules.context_module import DefaultContextModule, default_initializers @@ -38,10 +35,8 @@ def initialize(): class WebCli(object): - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> None - + @inject + def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help='Web related tasks.') def web(): pass @@ -49,9 +44,8 @@ def web(): @web.command(help="Run web server and serve the application") @click.option('--host', '-h', help="host to bind to", default='127.0.0.1') @click.option('--port', '-p', help="port to bind to", default=5000) - @inject(web_handler=WebHandler) - def run(web_handler, host, port): - # type: (WebHandler, str, int)->None + @inject + def run(web_handler: WebHandler, host: str, port: int) -> None: web_handler.run_web(host=host, port=port) self._web = web @@ -62,8 +56,8 @@ def web(self): class WebHandler(object): - @inject(flask=Zsl) - def run_web(self, flask, host='127.0.0.1', port=5000, **options): + @inject + def run_web(self, flask: Zsl, host: str = '127.0.0.1', port: int = 5000, **options: Any): # type: (Zsl, str, int, **Any)->None """Alias for Flask.run""" return flask.run( @@ -81,22 +75,23 @@ def _create_context(self): logging.getLogger(__name__).debug("Creating web context.") return InitializationContext(initializers=web_initializers) - @provides(interface=WebCli, scope=singleton) - def provide_web_cli(self): + @singleton + @provider + def provide_web_cli(self) -> WebCli: return WebCli() - @provides(interface=WebHandler, scope=singleton) - def provide_web_handler(self): + @singleton + @provider + def provide_web_handler(self) -> WebHandler: return WebHandler() - @provides(interface=CORSConfiguration, scope=singleton) - @inject(config=Config) - def provide_cors_configuration(self, config): - # type: (Config)->CORSConfiguration + @singleton + @provider + @inject + def provide_cors_configuration(self, config: Config) -> CORSConfiguration: return config.get(CORS_CONFIGURATION_NAME, CORSConfiguration()) - def configure(self, binder): - # type: (Binder) -> None + def configure(self, binder: Binder) -> None: super(WebContextModule, self).configure(binder) simple_bind(binder, WebCli, singleton) create_task_mapping() diff --git a/src/zsl/application/service_application.py b/src/zsl/application/service_application.py index ad41a705..9e2270a8 100644 --- a/src/zsl/application/service_application.py +++ b/src/zsl/application/service_application.py @@ -187,8 +187,7 @@ def is_initialized(self): return self._dependencies_initialized @property - def injector(self): - # type: () -> Injector + def injector(self) -> Injector: return self._injector @injector.setter diff --git a/src/zsl/cache/redis_cache_module.py b/src/zsl/cache/redis_cache_module.py index 096de1b3..b569fd7b 100644 --- a/src/zsl/cache/redis_cache_module.py +++ b/src/zsl/cache/redis_cache_module.py @@ -4,19 +4,18 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - +from injector import inject import redis -from zsl import Config, Zsl, inject +from zsl import Config, Zsl from zsl.cache.cache_module import CacheModule class RedisCacheModule(CacheModule): """Abstraction layer for caching.""" - @inject(app=Zsl, config=Config) - def __init__(self, app, config): + @inject + def __init__(self, app: Zsl, config: Config) -> None: """Abstraction layer for caching.""" self._app = app self._config = config diff --git a/src/zsl/cache/redis_id_helper.py b/src/zsl/cache/redis_id_helper.py index 41da9db2..4e8bf628 100644 --- a/src/zsl/cache/redis_id_helper.py +++ b/src/zsl/cache/redis_id_helper.py @@ -4,11 +4,11 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - import logging -from zsl import Config, inject +from injector import inject + +from zsl import Config from zsl.cache.id_helper import IdHelper, decoder_identity, encoder_identity, model_key_generator from zsl.cache.redis_cache_module import RedisCacheModule @@ -16,8 +16,8 @@ class RedisIdHelper(IdHelper): CACHE_DEFAULT_TIMEOUT = 300 - @inject(config=Config, redis_cache_module=RedisCacheModule) - def __init__(self, config, redis_cache_module): + @inject + def __init__(self, config: Config, redis_cache_module: RedisCacheModule) -> None: """ Creates the id helper for caching support of AppModels. """ diff --git a/src/zsl/contrib/alembic/alembic_module.py b/src/zsl/contrib/alembic/alembic_module.py index 60b5a8e9..4c357c8f 100644 --- a/src/zsl/contrib/alembic/alembic_module.py +++ b/src/zsl/contrib/alembic/alembic_module.py @@ -19,7 +19,7 @@ class MyApplicationContainer(WebContainer): alembic = AlembicModule - @inject(zsl_cli=ZslCli) + @inject def main(zsl_cli: ZslCli) -> None: zsl_cli() @@ -65,8 +65,8 @@ def main(zsl_cli: ZslCli) -> None: .. code-block:: python - @inject(zsl_config=Config) - def run_migrations_offline(zsl_config): + @inject + def run_migrations_offline(zsl_config: Config): url = zsl_config['DATABASE_URI'] context.configure(url=url, target_metadata=target_metadata, literal_binds=True) @@ -74,8 +74,8 @@ def run_migrations_offline(zsl_config): context.run_migrations() - @inject(engine=Engine) - def run_migrations_online(engine): + @inject + def run_migrations_online(engine: Engine): with engine.connect() as connection: context.configure( connection=connection, @@ -87,18 +87,15 @@ def run_migrations_online(engine): """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * # NOQA import logging import os from typing import List import click from click.core import Context -from injector import Binder, Module, provides, singleton +from injector import Binder, Module, inject, noninjectable, provider, singleton -from zsl import Config, inject +from zsl import Config from zsl.application.modules.cli_module import ZslCli from zsl.contrib.alembic.alembic_config import AlembicConfiguration from zsl.utils.injection_helper import simple_bind @@ -116,9 +113,8 @@ def run_migrations_online(engine): class AlembicCli(object): """Alembic Cli interface support.""" - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> AlembicCli + @inject + def __init__(self, zsl_cli: ZslCli) -> None: logging.getLogger(__name__).debug("Creating Alembic CLI.") @zsl_cli.cli.command(help='Run alembic maintenance tasks.', @@ -137,9 +133,9 @@ def alembic(ctx): def alembic(self): return self._alembic - @inject(alembic_cfg=AlembicConfiguration) - def call_alembic(self, args, alembic_cfg): - # type: (List[str], AlembicConfiguration)->None + @inject + @noninjectable('args') + def call_alembic(self, args: List[str], alembic_cfg: AlembicConfiguration) -> None: is_initializing = len(args) and args[0] == 'init' alembic_directory = alembic_cfg.alembic_directory if is_initializing: @@ -165,12 +161,10 @@ class AlembicModule(Module): ALEMBIC_CONFIG_NAME = 'ALEMBIC' - @provides(AlembicConfiguration) - @inject(config=Config) - def provide_alembic_configuration(self, config): - # type: (Config) -> AlembicConfiguration + @singleton + @provider + def provide_alembic_configuration(self, config: Config) -> AlembicConfiguration: return config.get(AlembicModule.ALEMBIC_CONFIG_NAME) - def configure(self, binder): - # type: (Binder) -> None + def configure(self, binder: Binder) -> None: simple_bind(binder, AlembicCli, singleton) diff --git a/src/zsl/contrib/sentry/sentry_module.py b/src/zsl/contrib/sentry/sentry_module.py index 8eb585af..e43aeb06 100644 --- a/src/zsl/contrib/sentry/sentry_module.py +++ b/src/zsl/contrib/sentry/sentry_module.py @@ -42,12 +42,12 @@ class MyApplicationContainer(WebContainer): from builtins import * import logging -from injector import Binder, provides, singleton +from injector import Binder, inject, provider, singleton from raven import Client, setup_logging from raven.handlers.logging import SentryHandler from raven.transport import ThreadedRequestsHTTPTransport -from zsl import Config, Module, Zsl, inject +from zsl import Config, Module, Zsl from zsl.application.error_handler import register from zsl.application.modules.cli_module import ZslCli from zsl.contrib.sentry.sentry_config import SentryConfiguration @@ -69,9 +69,8 @@ class SentryCli(object): """Sentry CLI interface support.""" - @inject(zsl_cli=ZslCli) - def __init__(self, zsl_cli): - # type: (ZslCli) -> None + @inject + def __init__(self, zsl_cli: ZslCli) -> None: logging.getLogger(__name__).debug("Creating Sentry CLI.") @zsl_cli.cli.group() @@ -92,9 +91,8 @@ def sentry(self): class SentryErrorProcessor(ErrorProcessor): - @inject(config=SentryConfiguration, zsl=Zsl) - def __init__(self, config, zsl): - # type: (SentryConfiguration, Zsl)->None + @inject + def __init__(self, config: SentryConfiguration, zsl: Zsl) -> None: self._client = self._create_client(config, zsl) if config.register_logging_handler: @@ -128,13 +126,12 @@ class SentryModule(Module): SENTRY_CONFIG_NAME = 'SENTRY' - @provides(SentryConfiguration) - @inject(config=Config) - def provide_sentry_configuration(self, config): - # type: (Config) -> SentryConfiguration + @singleton + @provider + @inject + def provide_sentry_configuration(self, config: Config) -> SentryConfiguration: return config.get(SentryModule.SENTRY_CONFIG_NAME) - def configure(self, binder): - # type: (Binder) -> None + def configure(self, binder: Binder) -> None: simple_bind(binder, SentryCli, singleton) register(SentryErrorProcessor()) diff --git a/src/zsl/interface/celery/worker.py b/src/zsl/interface/celery/worker.py index 3085d956..0f6f3f44 100644 --- a/src/zsl/interface/celery/worker.py +++ b/src/zsl/interface/celery/worker.py @@ -6,12 +6,11 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals - import sys +from typing import Dict from celery import Celery, shared_task -from injector import Module, provides, singleton +from injector import Module, provider, singleton from zsl import Config, inject from zsl.interface.task_queue import TaskQueueWorker @@ -50,9 +49,8 @@ def run(self): self._app.logger.error("Running from celery worker, start from shell!") -@inject(config=Config) -def create_celery_app(config): - # type:(Config)->Celery +@inject +def create_celery_app(config: Config) -> Celery: celery_app = Celery() celery_app.config_from_object(config['CELERY']) return celery_app @@ -76,9 +74,8 @@ def run(self, argv): @shared_task -@inject(worker=CeleryTaskQueueWorkerBase) -def zsl_task(job_data, worker): - # type: (dict, CeleryTaskQueueWorkerBase) -> dict +@inject +def zsl_task(job_data: Dict, worker: CeleryTaskQueueWorkerBase) -> Dict: """This function will be registered as a celery task. :param job_data: task data diff --git a/src/zsl/interface/gearman/task_filler.py b/src/zsl/interface/gearman/task_filler.py index 2c4af2af..3ef6edd5 100644 --- a/src/zsl/interface/gearman/task_filler.py +++ b/src/zsl/interface/gearman/task_filler.py @@ -8,12 +8,12 @@ import json -from zsl import Config, Injected, inject +from zsl import Config, inject from zsl.gearman import gearman -@inject(config=Config) -def exec_task_filler(task_path, json_data, config=Injected): +@inject +def exec_task_filler(task_path: str, json_data: str, config: Config) -> None: print("Initializing client.") gm_client = gearman.GearmanClient(["{0}:{1}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) print("Client initialized.") diff --git a/src/zsl/interface/task.py b/src/zsl/interface/task.py index f76e6540..3073b525 100644 --- a/src/zsl/interface/task.py +++ b/src/zsl/interface/task.py @@ -137,8 +137,8 @@ def exec_task(task_path, data): return jc.task_callable(jc.task_data) -@inject(task_router=TaskRouter) -def create_task(task_path, task_router): +@inject +def create_task(task_path: str, task_router: TaskRouter): if isinstance(task_path, str): (task, task_callable) = task_router.route(task_path) elif task_path is Callable or isinstance(task_path, type): diff --git a/src/zsl/interface/task_queue.py b/src/zsl/interface/task_queue.py index 698355d5..9fc2c8d2 100644 --- a/src/zsl/interface/task_queue.py +++ b/src/zsl/interface/task_queue.py @@ -10,11 +10,9 @@ import abc import socket import traceback -from typing import Any +from typing import Any, Dict -from future.utils import with_metaclass - -from zsl import Config, Injected, Zsl, inject +from zsl import Config, Zsl, inject from zsl.router.task import TaskRouter from zsl.task.job_context import Job, JobContext @@ -24,16 +22,15 @@ class KillWorkerException(Exception): pass -@inject(app=Zsl, task_router=TaskRouter) -def execute_job(job, app=Injected, task_router=Injected): - # type: (Job, Zsl, TaskRouter) -> dict +@inject +def execute_job(job: Job, app: Zsl, task_router: TaskRouter) -> Dict: """Execute a job. :param job: job to execute :type job: Job - :param app: service application instance, injected + :param app: service application instance :type app: ServiceApplication - :param task_router: task router instance, injected + :param task_router: task router instance :type task_router: TaskRouter :return: task result :rtype: dict @@ -52,7 +49,7 @@ def execute_job(job, app=Injected, task_router=Injected): return {'task_name': job.path, 'data': result} -class TaskQueueWorker(with_metaclass(abc.ABCMeta, object)): +class TaskQueueWorker(abc.ABC): """Task queue worker abstraction. A task queue worker is responsible for communicating with a task queue and @@ -60,9 +57,8 @@ class TaskQueueWorker(with_metaclass(abc.ABCMeta, object)): It should be able to run as a stand alone application. """ - @inject(app=Zsl, config=Config) - def __init__(self, app, config): - # type: (Zsl, Config, TaskRouter) -> None + @inject + def __init__(self, app: Zsl, config: Config) -> None: self._app = app self._config = config self._should_stop = False @@ -122,9 +118,8 @@ def stop_worker(self): pass -@inject(worker=TaskQueueWorker) -def _get_worker(worker): - # type: (TaskQueueWorker) -> TaskQueueWorker +@inject +def _get_worker(worker: TaskQueueWorker) -> TaskQueueWorker: return worker diff --git a/src/zsl/interface/web/performers/default.py b/src/zsl/interface/web/performers/default.py index d22e19d1..5db0501f 100644 --- a/src/zsl/interface/web/performers/default.py +++ b/src/zsl/interface/web/performers/default.py @@ -16,8 +16,8 @@ from zsl.task.job_context import WebJobContext -@inject(app=Zsl) -def create_not_found_mapping(app): +@inject +def create_not_found_mapping(app: Zsl) -> None: @app.route("/", defaults={'path': ''}) @app.route("/") @append_headers diff --git a/src/zsl/interface/web/performers/resource.py b/src/zsl/interface/web/performers/resource.py index b20ddf5a..4f8f0b54 100644 --- a/src/zsl/interface/web/performers/resource.py +++ b/src/zsl/interface/web/performers/resource.py @@ -23,8 +23,8 @@ from zsl.utils.resource_helper import get_resource_task, parse_resource_path -@inject(app=Zsl) -def create_resource_mapping(app): +@inject +def create_resource_mapping(app: Zsl) -> None: @app.route("/resource/", methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) @append_headers diff --git a/src/zsl/interface/web/performers/task.py b/src/zsl/interface/web/performers/task.py index cb702db4..17fc2feb 100644 --- a/src/zsl/interface/web/performers/task.py +++ b/src/zsl/interface/web/performers/task.py @@ -9,8 +9,9 @@ import logging from flask import request +from flask.wrappers import Response -from zsl import Injected, Zsl, inject +from zsl import Zsl, inject from zsl.application.error_handler import error_handler from zsl.interface.web.utils.execution import convert_to_web_response, execute_web_task, notify_responders from zsl.interface.web.utils.request_data import extract_data @@ -19,20 +20,20 @@ from zsl.task.job_context import JobContext, WebJobContext -@inject(task_router=TaskRouter) +@inject @append_headers @notify_responders @convert_to_web_response @error_handler -def perform_web_task(namespace, path, task_router=Injected): +def perform_web_task(namespace: str, path: str, task_router: TaskRouter) -> Response: logging.getLogger(__name__).debug("Performing task %s.", path) jc = WebJobContext(path, extract_data(request), None, None, request) (task, task_callable) = task_router.route(namespace + '/' + path) return execute_web_task(jc, lambda: task_callable(jc.task_data)) -@inject(app=Zsl, task_configuration=TaskConfiguration) -def create_task_mapping(app, task_configuration): +@inject +def create_task_mapping(app: Zsl, task_configuration: TaskConfiguration) -> None: # type: (Zsl, TaskConfiguration)->None for namespace_configuration in task_configuration.namespaces: namespace = namespace_configuration.namespace.rstrip('/') diff --git a/src/zsl/interface/web/utils/execution.py b/src/zsl/interface/web/utils/execution.py index 2dfc8c62..4bf2d5ed 100644 --- a/src/zsl/interface/web/utils/execution.py +++ b/src/zsl/interface/web/utils/execution.py @@ -37,8 +37,8 @@ def _create_web_response(result): return make_response(result) -@inject(app=Zsl) -def execute_web_task(job_context, callable, app): +@inject +def execute_web_task(job_context: WebJobContext, callable: Callable, app: Zsl) -> Any: # type:(WebJobContext, Callable, Zsl)->Response app.logger.debug("Data found '%s'.", str(job_context.task_data.payload)) if request.method == 'OPTIONS': diff --git a/src/zsl/interface/web/utils/response_headers.py b/src/zsl/interface/web/utils/response_headers.py index 304fcd9c..a22ee0f4 100644 --- a/src/zsl/interface/web/utils/response_headers.py +++ b/src/zsl/interface/web/utils/response_headers.py @@ -4,8 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - from functools import wraps from flask import Response @@ -15,9 +13,8 @@ from zsl.task.task_decorator import CrossdomainWebTaskResponder -@inject(config=Config) -def append_crossdomain(response, config): - # type: (Response, Config)->None +@inject +def append_crossdomain(response: Response, config: Config) -> None: """ Adds the default crossdomain headers. Uses the :class:`zsl.task.task_decorator.CrossdomainWebTaskResponder` @@ -32,16 +29,16 @@ def append_crossdomain(response, config): CrossdomainWebTaskResponder().respond(response) -@inject(app=Zsl) -def append_asl(response, app): +@inject +def append_asl(response: Response, app: Zsl) -> None: response.headers['ZSL'] = app.get_version() -def append_cache(response): +def append_cache(response: Response): response.headers['Cache-Control'] = 'no-cache' -def append_all(response): +def append_all(response: Response): append_asl(response) append_cache(response) append_crossdomain(response) diff --git a/src/zsl/resource/guard.py b/src/zsl/resource/guard.py index 0a5a495c..a53c3c9c 100644 --- a/src/zsl/resource/guard.py +++ b/src/zsl/resource/guard.py @@ -63,8 +63,8 @@ class SimplePolicy(ResourcePolicy): class AdminPolicy(ResourcePolicy): '''Only admin has access''' - @inject(user_service=UserService) - def __init__(self, user_service): + @inject + def __init__(self, user_service: UserService): self._user_service = user_service @property diff --git a/src/zsl/resource/model_resource.py b/src/zsl/resource/model_resource.py index d6d8ea10..43f2ade3 100644 --- a/src/zsl/resource/model_resource.py +++ b/src/zsl/resource/model_resource.py @@ -13,15 +13,13 @@ .. moduleauthor:: Peter Morihladko , Martin Babka """ -from __future__ import unicode_literals - -from builtins import int, object from hashlib import sha256 import json import logging -from typing import Any, List, Union +from typing import Any, List, Type, Union from future.utils import viewitems +from injector import noninjectable from sqlalchemy.orm import class_mapper from sqlalchemy.orm.attributes import InstrumentedAttribute @@ -461,11 +459,14 @@ def _delete_collection(self, ctx): class CachedModelResource(ModelResource): """ - The cached resource - uses redis to cache the resource for the given amount of seconds. + The cached resource - uses redis to cache the resource for the given amount + of seconds. """ - @inject(cache_module=CacheModule, id_helper=IdHelper, logger=logging.Logger) - def __init__(self, model_cls, cache_module, id_helper, logger, timeout='short'): + @inject + def __init__(self, model_cls: Type, cache_module: CacheModule, + id_helper: IdHelper, logger: logging.Logger, + timeout: str = 'short'): super(CachedModelResource, self).__init__(model_cls) self._cache_module = cache_module self._id_helper = id_helper diff --git a/src/zsl/router/method.py b/src/zsl/router/method.py index ffd478c8..2b480eb0 100644 --- a/src/zsl/router/method.py +++ b/src/zsl/router/method.py @@ -4,16 +4,14 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - -from builtins import * import json import logging +from typing import Any, Callable from flask import request from flask.wrappers import Response -from zsl import Config, Injected, Zsl, inject +from zsl import Config, Zsl, inject from zsl.application.error_handler import error_handler from zsl.application.modules.web.configuration import MethodConfiguration from zsl.constants import MimeType @@ -79,8 +77,8 @@ def _get_method_configuration(config): return config.get(METHOD_CONFIG_NAME, MethodConfiguration()) -@inject(app=Zsl, config=Config) -def route(path, app=Injected, config=Injected, **options): +@inject +def route(path: str, app: Zsl, config: Config, **options: Any) -> Callable: def _decorator(f): method_config = _get_method_configuration(config) url = "/{0}{1}".format(method_config.url_prefix, path) @@ -92,6 +90,6 @@ def _decorator(f): return _decorator -@inject(config=Config) -def get_method_packages(config): +@inject +def get_method_packages(config: Config): return _get_method_configuration(config).packages diff --git a/src/zsl/router/task.py b/src/zsl/router/task.py index 78d639d1..fd76026f 100644 --- a/src/zsl/router/task.py +++ b/src/zsl/router/task.py @@ -19,15 +19,13 @@ class TaskNamespace(object): - def __init__(self, namespace, task_configuration): - # type: (str, TaskConfiguration)->None + def __init__(self, namespace: str, task_configuration: 'TaskConfiguration') -> None: self._task_packages = [] self._routes = {} self._task_configuration = task_configuration self._namespace = namespace - def add_packages(self, packages): - # type: (List[str])->TaskNamespace + def add_packages(self, packages: List[str]) -> 'TaskNamespace': """ Adds an automatic resolution of urls into tasks. :param packages: The url will determine package/module and the class. @@ -37,8 +35,7 @@ def add_packages(self, packages): self._task_packages += packages return self - def get_packages(self): - # type:()->List[str] + def get_packages(self) -> List[str]: return list(self._task_packages) def add_routes(self, routes): @@ -207,9 +204,8 @@ def _find_task_in_namespace(self, task_packages, package_path, class_name): class TaskRouter(object): - @inject(config=Config, task_configuration=TaskConfiguration) - def __init__(self, config, task_configuration): - # type: (Config, TaskConfiguration) -> None + @inject + def __init__(self, config: Config, task_configuration: TaskConfiguration) -> None: self._config = config self._task_configuration = task_configuration # type: TaskConfiguration self._strategies = [ diff --git a/src/zsl/service/service.py b/src/zsl/service/service.py index 5eaa3535..2e9ae7b8 100644 --- a/src/zsl/service/service.py +++ b/src/zsl/service/service.py @@ -34,8 +34,8 @@ class Service(TransactionalSupportMixin): Main service class. """ - @inject(app=Zsl, engine=Engine) - def __init__(self, app, engine): + @inject + def __init__(self, app: Zsl, engine: Engine) -> None: """Constructor - initializes and injects the needed libraries.""" super(Service, self).__init__() self._app = app @@ -46,13 +46,13 @@ def __init__(self, app, engine): def transactional(f): - @inject(session_factory=SessionFactory) - def _get_session_factory(session_factory): + @inject + def _get_session_factory(session_factory: SessionFactory) -> SessionFactory: # type: (SessionFactory) -> SessionFactory return session_factory - @inject(tx_holder_factory=TransactionHolderFactory) - def _get_tx_holder_factory(tx_holder_factory): + @inject + def _get_tx_holder_factory(tx_holder_factory: TransactionHolderFactory) -> TransactionHolderFactory: # type: (TransactionHolderFactory) -> TransactionHolderFactory return tx_holder_factory diff --git a/src/zsl/task/task_data.py b/src/zsl/task/task_data.py index e23ec738..8d68958d 100644 --- a/src/zsl/task/task_data.py +++ b/src/zsl/task/task_data.py @@ -4,17 +4,19 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals +from typing import Type -from builtins import * +from injector import noninjectable -from zsl import Injected, Zsl, inject +from zsl import Zsl, inject from zsl.utils.warnings import deprecated class TaskData(object): - @inject(app=Zsl) - def __init__(self, payload, app=Injected, payload_type=str): + + @inject + @noninjectable('payload_type') + def __init__(self, payload: str, app: Zsl, payload_type: Type = str) -> None: self._app = app self._payload = payload self._payload_type = payload_type diff --git a/src/zsl/task/task_decorator.py b/src/zsl/task/task_decorator.py index e8070105..7f0f552d 100644 --- a/src/zsl/task/task_decorator.py +++ b/src/zsl/task/task_decorator.py @@ -7,20 +7,18 @@ Jan Janco , Lubos Pis """ -from __future__ import unicode_literals - -from builtins import * from datetime import timedelta from functools import wraps import json import logging from os.path import os import traceback -from typing import Callable, List, Union +from typing import Callable, List, Optional, Union from flask import request +from injector import noninjectable -from zsl import Config, Injected, Zsl, inject +from zsl import Config, Zsl, inject from zsl.application.modules.web.cors import CORSConfiguration from zsl.constants import MimeType from zsl.db.model import AppModelJSONEncoder @@ -108,8 +106,8 @@ def json_output(f): @wraps(f) def json_output_decorator(*args, **kwargs): - @inject(config=Config) - def get_config(config): + @inject + def get_config(config: Config) -> Config: return config config = get_config() @@ -422,11 +420,11 @@ class CrossdomainWebTaskResponder(Responder): source: https://github.com/fengsp/flask-snippets/blob/master/decorators/http_access_control.py """ - @inject(app=Zsl, cors_config=CORSConfiguration) - def __init__(self, origin=None, methods=None, allow_headers=None, - expose_headers=None, max_age=None, app=Injected, - cors_config=Injected): - # type: (str, List[str], str, str, Union[int, timedelta], Zsl, CORSConfiguration)->None + @inject + @noninjectable('origin', 'methods', 'allow_headers', 'expose_headers', 'max_age') + def __init__(self, origin: Optional[str] = None, methods: Optional[List[str]] = None, + allow_headers: Optional[str] = None, expose_headers: Optional[str] = None, + max_age: Union[int, timedelta] = None, app: Zsl = None, cors_config: CORSConfiguration = None): self._app = app methods = join_list(methods, transform=lambda x: x.upper()) diff --git a/src/zsl/tasks/zsl/cors_test_task.py b/src/zsl/tasks/zsl/cors_test_task.py index 1dbf8d48..b17fcfbb 100644 --- a/src/zsl/tasks/zsl/cors_test_task.py +++ b/src/zsl/tasks/zsl/cors_test_task.py @@ -18,8 +18,8 @@ class CorsTestTask(object): - @inject(app=Zsl) - def __init__(self, app): + @inject + def __init__(self, app: Zsl) -> None: self._app = app @json_input diff --git a/src/zsl/tasks/zsl/db_test_task.py b/src/zsl/tasks/zsl/db_test_task.py index 8edc7e3d..c6902a25 100644 --- a/src/zsl/tasks/zsl/db_test_task.py +++ b/src/zsl/tasks/zsl/db_test_task.py @@ -6,10 +6,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - -from builtins import object - from flask import Response from injector import inject import sqlalchemy.engine @@ -30,8 +26,8 @@ class DbTestTask(object): @author: Martin Babka """ - @inject(db=sqlalchemy.engine.Engine, app=Zsl) - def __init__(self, db, app): + @inject + def __init__(self, db: sqlalchemy.engine.Engine, app: Zsl) -> None: self._db = db self._app = app self._app.logger.debug("Call from DbTestTesk.__init__, db {0}".format(db)) diff --git a/src/zsl/tasks/zsl/kill_worker_task.py b/src/zsl/tasks/zsl/kill_worker_task.py index 64f0f7db..0f77270d 100644 --- a/src/zsl/tasks/zsl/kill_worker_task.py +++ b/src/zsl/tasks/zsl/kill_worker_task.py @@ -18,8 +18,8 @@ class KillWorkerTask(object): - @inject(app=Zsl) - def __init__(self, app): + @inject + def __init__(self, app: Zsl) -> None: self._app = app @staticmethod diff --git a/src/zsl/tasks/zsl/sum_task.py b/src/zsl/tasks/zsl/sum_task.py index 33168398..7f53e551 100644 --- a/src/zsl/tasks/zsl/sum_task.py +++ b/src/zsl/tasks/zsl/sum_task.py @@ -18,8 +18,8 @@ class SumTask(object): - @inject(app=Zsl) - def __init__(self, app): + @inject + def __init__(self, app: Zsl) -> None: self._app = app @json_input diff --git a/src/zsl/tasks/zsl/version_task.py b/src/zsl/tasks/zsl/version_task.py index 8d6efcb0..f898960e 100644 --- a/src/zsl/tasks/zsl/version_task.py +++ b/src/zsl/tasks/zsl/version_task.py @@ -21,8 +21,8 @@ class VersionTask(object): Shows the versions of ASL and the various used libraries. """ - @inject(app=Zsl) - def __init__(self, app): + @inject + def __init__(self, app: Zsl) -> None: self._app = app @json_output diff --git a/src/zsl/tasks/zsl/with_request_task.py b/src/zsl/tasks/zsl/with_request_task.py index 0d112fc9..4e188b64 100644 --- a/src/zsl/tasks/zsl/with_request_task.py +++ b/src/zsl/tasks/zsl/with_request_task.py @@ -25,8 +25,8 @@ def __init__(self): Returns received data if any or 'empty' """ - @inject(app=Zsl) - def __init__(self, app): + @inject + def __init__(self, app: Zsl) -> None: self._app = app @json_input diff --git a/src/zsl/testing/db.py b/src/zsl/testing/db.py index 8244c9a2..83fbf84d 100644 --- a/src/zsl/testing/db.py +++ b/src/zsl/testing/db.py @@ -21,16 +21,14 @@ the same session. Also one should add :class:`.DbTestModule` to the test container when creating Zsl instance, see :ref:`unit-testing-zsl-instance`. """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * import logging -from injector import Module, provides, singleton +from injector import Module, inject, provider, singleton from sqlalchemy.engine import Engine from sqlalchemy.orm.session import Session -from zsl import inject +from zsl import Zsl +from zsl import inject as zsl_inject from zsl.application.modules.alchemy_module import TransactionHolder, TransactionHolderFactory from zsl.db.model.sql_alchemy import metadata from zsl.service.service import SessionFactory @@ -40,9 +38,8 @@ class TestSessionFactory(SessionFactory): """Factory always returning the single test transaction.""" _test_session = None - @inject(engine=Engine) - def create_test_session(self, engine): - # type: (Engine) -> Session + @zsl_inject + def create_test_session(self, engine: Engine) -> Session: assert TestSessionFactory._test_session is None metadata.bind = engine metadata.create_all(engine) @@ -84,19 +81,21 @@ class DbTestModule(Module): """Module fixing the :class:`zsl.service.service.SessionFactory` to our :class:`.TestSessionFactory`.""" - @provides(SessionFactory, scope=singleton) - def get_session_factory(self): - # type: ()->SessionFactory - return TestSessionFactory() + @singleton + @provider + @inject + def get_session_factory(self, app: Zsl) -> SessionFactory: + return app.injector.create_object(TestSessionFactory) - @provides(TestSessionFactory, scope=singleton) - @inject(session_factory=SessionFactory) - def get_test_session_factory(self, session_factory): - # type: (SessionFactory)->SessionFactory + @singleton + @provider + @inject + def get_test_session_factory(self, session_factory: SessionFactory) -> TestSessionFactory: return session_factory - @provides(TransactionHolderFactory, scope=singleton) - def provide_transaction_holder_factory(self): + @singleton + @provider + def provide_transaction_holder_factory(self) -> TransactionHolderFactory: return TestTransactionHolderFactory() @@ -106,16 +105,14 @@ class DbTestCase(object): _session = None - @inject(session_factory=TestSessionFactory) - def setUp(self, session_factory): - # type: (TestSessionFactory)->None + @zsl_inject + def setUp(self, session_factory: TestSessionFactory) -> None: super(DbTestCase, self).setUp() logging.getLogger(__name__).debug("DbTestCase.setUp") session_factory.create_test_session() - @inject(session_factory=TestSessionFactory) - def tearDown(self, session_factory): - # type: (TestSessionFactory)->None + @zsl_inject + def tearDown(self, session_factory: TestSessionFactory) -> None: # This will return the same transaction/session # as the one used in setUp. logging.getLogger(__name__).debug("DbTestCase.tearDown") diff --git a/src/zsl/testing/http.py b/src/zsl/testing/http.py index 1eb67968..d2b3f214 100644 --- a/src/zsl/testing/http.py +++ b/src/zsl/testing/http.py @@ -73,11 +73,10 @@ def extractResponseJSON(self, rv): # type: (Response) -> Dict return json.loads(rv.data.decode()) - @inject(app=Zsl) - def getHTTPClient(self, app): - # type: (Zsl) -> FlaskClient + @inject + def getHTTPClient(self, app: Zsl) -> FlaskClient: return app.test_client() - @inject(app=Zsl) - def getRequestContext(self, app): + @inject + def getRequestContext(self, app: Zsl): return app.test_request_context() diff --git a/src/zsl/utils/background_task.py b/src/zsl/utils/background_task.py index 266bbacc..3ab83ff0 100644 --- a/src/zsl/utils/background_task.py +++ b/src/zsl/utils/background_task.py @@ -34,9 +34,8 @@ def decorator_fn(fn): gearman = None - @inject(config=Config) - def gearman_connect(config): - # type: (Config) -> GearmanService + @inject + def gearman_connect(config: Config) -> GearmanService: if 'GEARMAN' not in config or 'host' not in config['GEARMAN'] or 'GEARMAN_TASK_NAME' not in config: raise Exception("Missing gearman settings (trying to use backgorund task)") diff --git a/src/zsl/utils/cache_helper.py b/src/zsl/utils/cache_helper.py index 23c13fd6..a0bdb0c3 100644 --- a/src/zsl/utils/cache_helper.py +++ b/src/zsl/utils/cache_helper.py @@ -20,8 +20,9 @@ class CacheDecorator(object): - @inject(id_helper=IdHelper) - def __init__(self, id_helper): + @inject + # TODO: Redo as module + def __init__(self, id_helper: IdHelper) -> None: self._id_helper = id_helper logging.debug("Initializing CacheDecorator.") diff --git a/src/zsl/utils/deploy/apiari_doc_generator.py b/src/zsl/utils/deploy/apiari_doc_generator.py index 6764e9d0..5445d59e 100644 --- a/src/zsl/utils/deploy/apiari_doc_generator.py +++ b/src/zsl/utils/deploy/apiari_doc_generator.py @@ -116,13 +116,13 @@ def get_doc(self): return "FORMAT: 1A\n\n" + "\n\n".join(self._docs) -@inject(task_router=TaskRouter) -def generate_apiary_doc(task_router): +@inject +def generate_apiary_doc(task_router: TaskRouter) -> None: """Generate apiary documentation. Create a Apiary generator and add application packages to it. - :param task_router: task router, injected + :param task_router: task router :type task_router: TaskRouter :return: apiary generator :rtype: ApiaryDoc diff --git a/src/zsl/utils/email_helper.py b/src/zsl/utils/email_helper.py index 087e74b1..83e38b7a 100644 --- a/src/zsl/utils/email_helper.py +++ b/src/zsl/utils/email_helper.py @@ -9,11 +9,11 @@ from email.mime.text import MIMEText import smtplib -from zsl import Config, Injected, inject +from zsl import Config, inject -@inject(config=Config) -def send_email(sender, receivers, subject, text=None, html=None, charset='utf-8', config=Injected): +@inject +def send_email(sender, receivers, subject, text=None, html=None, charset='utf-8', config: Config = None) -> None: """Sends an email. :param sender: Sender as string or None for default got from config. diff --git a/src/zsl/utils/gearman_helper.py b/src/zsl/utils/gearman_helper.py index de346505..2c20acd2 100644 --- a/src/zsl/utils/gearman_helper.py +++ b/src/zsl/utils/gearman_helper.py @@ -4,16 +4,15 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals - import json +from typing import Any from zsl import Config, inject from zsl.gearman import gearman -@inject(config=Config) -def schedule_gearman_task(path, data, config): +@inject +def schedule_gearman_task(path: str, data: Any, config: Config) -> None: gm_client = gearman.GearmanClient(["{0}:{1}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) gm_client.submit_job(config['GEARMAN_TASK_NAME'], json.dumps( {'path': path, 'data': json.dumps(data)}), wait_until_complete=False, background=True) diff --git a/src/zsl/utils/injection_helper.py b/src/zsl/utils/injection_helper.py index cfbe2df5..e599cd87 100644 --- a/src/zsl/utils/injection_helper.py +++ b/src/zsl/utils/injection_helper.py @@ -4,17 +4,14 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * # NOQA import functools import inspect import logging from typing import Type -from future.utils import viewitems -import injector -from injector import Binder, BindingKey, CallError, ClassProvider, Scope, reraise +from injector import Binder, CallError, ClassProvider, Scope +from injector import inject as raw_inject +from injector import reraise from zsl._state import get_current_app @@ -30,62 +27,14 @@ def instantiate(cls): return task -def inject(**bindings): - """ - Decorator for injecting parameters for ASL objects. - """ - - def outer_wrapper(f): - def function_wrapper(ff): - for key, value in viewitems(bindings): - bindings[key] = BindingKey(value) - - @functools.wraps(ff) - def _inject(*args, **kwargs): - inj = get_current_app().injector - dependencies = inj.args_to_inject( - function=ff, - bindings=bindings, - owner_key=ff - ) - dependencies.update(kwargs) - try: - return ff(*args, **dependencies) - except TypeError as e: - reraise(e, CallError(ff, args, dependencies, e)) - - return _inject - - ''' - Just a convenience method - delegates everything to wrapper. - ''' - - def method_or_class_wrapper(*a, **kwargs): - """ - Properly installs the injector into the object so that the injection can be performed. - """ - inj = get_current_app().injector - # Install injector into the self instance if this is a method call. - inj.install_into(a[0]) - - # Use the generic wrapper. - inject_f = injector.inject(**bindings) - # Call the method. - return inject_f(f)(*a, **kwargs) - - if inspect.ismethod(f): - return method_or_class_wrapper - else: - return function_wrapper(f) - - return outer_wrapper - +def inject(f): + @functools.wraps(f) + def wrapper(*args, **kwargs): + # todo read_lock for injector + return get_current_app().injector.call_with_injection(raw_inject(f), args=args, kwargs=kwargs) -def bind(interface, to=None, scope=None): - logging.getLogger(__name__).warning("Using this style of binding is deprecated. Use Modules instead.") - get_current_app().injector.binder.bind(interface, to, scope) + return wrapper -def simple_bind(binder, cls, scope): - # type: (Binder, Type, Scope) -> None +def simple_bind(binder: Binder, cls: Type, scope: Scope) -> None: binder.bind(interface=cls, to=ClassProvider(cls).get(binder.injector), scope=scope) diff --git a/src/zsl/utils/redis_helper.py b/src/zsl/utils/redis_helper.py index 1bcf41fa..80c01fd0 100644 --- a/src/zsl/utils/redis_helper.py +++ b/src/zsl/utils/redis_helper.py @@ -6,12 +6,11 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals - -from builtins import object from functools import partial +from typing import Dict, Optional from future.utils import viewitems +from injector import noninjectable from zsl import Config, inject @@ -43,9 +42,9 @@ class Keymaker(object): # TODO I think this should be done in proper OOP - @inject(config=Config) - def __init__(self, keys, prefix=None, config=None): + @inject + @noninjectable('keys', 'prefix') + def __init__(self, keys: Dict[str, str], prefix: Optional[str] = None, config: Config = None) -> None: project_specific_prefix = config.get('REDIS', {}).get('prefix') - for method, key in viewitems(keys): setattr(self, method, partial(redis_key, project_specific_prefix, prefix, key)) diff --git a/src/zsl/utils/resource_helper.py b/src/zsl/utils/resource_helper.py index 258b71aa..527b8315 100644 --- a/src/zsl/utils/resource_helper.py +++ b/src/zsl/utils/resource_helper.py @@ -14,7 +14,7 @@ from flask import request -from zsl import Config, Injected, Zsl, inject +from zsl import Config, Zsl, inject from zsl.db.model import AppModel from zsl.interface.resource import ResourceResult from zsl.resource.model_resource import ModelResource @@ -40,7 +40,7 @@ def parse_resource_path(path): return splits[0], splits[1:] -def get_method(resource, method): +def get_method(resource: ModelResource, method: str) -> Callable: """Test and return a method by name on resource. :param resource: resource object @@ -53,17 +53,19 @@ def get_method(resource, method): if hasattr(resource, method) and callable(getattr(resource, method)): return getattr(resource, method) else: - raise MethodNotImplementedException() + raise MethodNotImplementedException(f"Method {method} of resource {resource} is not implemented.") -@inject(config=Config) -def get_resource_task(resource_path, config=Injected): - # type: (str, Config) -> Callable[[str, Dict, Dict], Union[List[AppModel], AppModel, ResourceResult]] +ResourceTask = Callable[[str, Dict, Dict], Union[List[AppModel], AppModel, ResourceResult]] + + +@inject +def get_resource_task(resource_path: str, config: Config) -> ResourceTask: """Search and return a bounded method for given path. :param resource_path: resource path :type resource_path: str - :param config: current configuration, injected + :param config: current configuration :type config: Config :return: bounded method or None when not found :raises NameError: when can't find given resource by path @@ -127,15 +129,15 @@ def get_resource_task(resource_path, config=Injected): return None -@inject(app=Zsl) -def create_model_resource(resource_map, name, app=Injected): +@inject +def create_model_resource(resource_map, name, app: Zsl): """Create a model resource from a dict ``resource_map`` {'resource name': ('model package', 'model class')} :param resource_map: dict with resource descriptions :type resource_map: dict(str, tuple(str)) :param name: name of the concrete resource - :param app: current application, injected + :param app: current application :type app: Zsl :return: instantiated model resource """ diff --git a/src/zsl/utils/security_helper.py b/src/zsl/utils/security_helper.py index 38029127..f0987157 100644 --- a/src/zsl/utils/security_helper.py +++ b/src/zsl/utils/security_helper.py @@ -41,8 +41,8 @@ def wrap_plain_data_as_secure(data): return {'security': generate_security_data(), 'data': data} -@inject(config=Config) -def compute_token(random_token, config): +@inject +def compute_token(random_token: str, config: Config) -> str: """Compute a hash of the given token with a preconfigured secret. :param random_token: random token diff --git a/src/zsl/utils/testing.py b/src/zsl/utils/testing.py index b9e55029..4db37a9f 100644 --- a/src/zsl/utils/testing.py +++ b/src/zsl/utils/testing.py @@ -23,11 +23,11 @@ def set_test_responder(): set_default_responder(identity_responder) -@inject(config=Config) -def load_and_run_tests(config): +@inject +def load_and_run_tests(config: Config): """Load application test package and run it using testing util. - :param config: application configuration, injected + :param config: application configuration :type config: Config """ set_test_responder() diff --git a/tests/application/error_handling_test.py b/tests/application/error_handling_test.py index fd008195..0bcea96e 100644 --- a/tests/application/error_handling_test.py +++ b/tests/application/error_handling_test.py @@ -37,8 +37,8 @@ class ErrorHandlingTestCase(ZslTestCase, TestCase): app_name='ErrorHandlingTestCase', container=WebContainer, config_object=CONFIG) - @inject(app=Zsl) - def testErrorTaskExecution(self, app): + @inject + def testErrorTaskExecution(self, app: Zsl): with app.test_request_context('/'): with self.assertRaises(Exception): jc = WebJobContext(None, None, None, None, None) diff --git a/tests/application/version_test.py b/tests/application/version_test.py index 0b51f619..bbf63b99 100644 --- a/tests/application/version_test.py +++ b/tests/application/version_test.py @@ -18,7 +18,7 @@ class AppVersionTestCase(ZslTestCase, TestCase): version="1234.1111.4321", config_object=CONFIG) - @inject(app=Zsl) + @inject def testErrorTaskExecution(self, app: Zsl) -> None: self.assertEqual(Zsl.VERSION, app.zsl_version) self.assertEqual("1234.1111.4321", app.app_version) @@ -35,7 +35,7 @@ class NoAppVersionTestCase(ZslTestCase, TestCase): app_name='ErrorHandlingTestCase', container=CoreContainer, config_object=CONFIG) - @inject(app=Zsl) + @inject def testErrorTaskExecution(self, app: Zsl) -> None: self.assertEqual(Zsl.VERSION, app.zsl_version) self.assertIsNone(app.app_version) diff --git a/tests/db/helpers/pagination_parsing_test.py b/tests/db/helpers/pagination_parsing_test.py index 9b1f0971..05d9bdf4 100644 --- a/tests/db/helpers/pagination_parsing_test.py +++ b/tests/db/helpers/pagination_parsing_test.py @@ -66,8 +66,8 @@ class PaginationParsingTestCase(ZslTestCase, HTTPTestCase, TestCase): } ) - @inject(app=Zsl) - def testErrorTaskExecution(self, app): + @inject + def testErrorTaskExecution(self, app: Zsl) -> None: with self.getHTTPClient() as client: random_token = '1' diff --git a/tests/interface/cli/exec_task_from_cli_test.py b/tests/interface/cli/exec_task_from_cli_test.py index 09ed5d98..6d68ace2 100644 --- a/tests/interface/cli/exec_task_from_cli_test.py +++ b/tests/interface/cli/exec_task_from_cli_test.py @@ -33,17 +33,16 @@ class ExecTaskFromCliTestCase(ZslTestCase, TestCase): container=TestCliContainer, ) - @inject(zsl_cli=ZslCli) - def testRunningTestTask(self, zsl_cli): + @inject + def testRunningTestTask(self, zsl_cli: ZslCli) -> None: # type:(ZslCli)->None runner = CliRunner() result = runner.invoke(zsl_cli.cli, ["task", "task/zsl/test_task"]) self.assertEqual(0, result.exit_code, "No error is expected.") self.assertEqual("ok", result.output.strip(), "Valid task output must be shown") - @inject(zsl_cli=ZslCli) - def testRunningTaskWithListInput(self, zsl_cli): - # type:(ZslCli)->None + @inject + def testRunningTaskWithListInput(self, zsl_cli: ZslCli) -> None: runner = CliRunner() result = runner.invoke( zsl_cli.cli, diff --git a/tests/interface/web/cors_test.py b/tests/interface/web/cors_test.py index eddad25f..79f0a924 100644 --- a/tests/interface/web/cors_test.py +++ b/tests/interface/web/cors_test.py @@ -36,8 +36,8 @@ class CorsTestCase(ZslTestCase, TestCase): app_name='CorsTestCase', container=WebContainer, config_object=CONFIG) - @inject(app=Zsl) - def testNotFound(self, app): + @inject + def testNotFound(self, app: Zsl) -> None: with app.test_request_context('/'): response = perform_web_task('nn', 'r') @@ -51,8 +51,8 @@ def testNotFound(self, app): self.assertEqual('42', response.headers['Access-Control-Max-Age']) - @inject(app=Zsl) - def testCustom(self, app): + @inject + def testCustom(self, app: Zsl): with app.test_request_context('/'): response = perform_web_task('n', 'r') diff --git a/tests/interface/web/task_router_test.py b/tests/interface/web/task_router_test.py index 2877ace6..677d6b8a 100644 --- a/tests/interface/web/task_router_test.py +++ b/tests/interface/web/task_router_test.py @@ -30,8 +30,8 @@ class TaskRouterTestCase(ZslTestCase, TestCase): app_name='ErrorHandlingTestCase', container=WebContainer, config_object=CONFIG) - @inject(app=Zsl) - def testRoutingRoutes(self, app): + @inject + def testRoutingRoutes(self, app: Zsl): with app.test_request_context('/'): response = perform_web_task('n', 'r') @@ -40,8 +40,8 @@ def testRoutingRoutes(self, app): "Status code must be ok.") self.assertEqual('ok', response.data.decode('utf-8')) - @inject(app=Zsl) - def testNotFound(self, app): + @inject + def testNotFound(self, app: Zsl): with app.test_request_context('/'): response = perform_web_task('nn', 'r') @@ -49,8 +49,8 @@ def testNotFound(self, app): response.status_code, "Status code must be NOT_FOUND.") - @inject(app=Zsl) - def testRoutingPackages(self, app): + @inject + def testRoutingPackages(self, app: Zsl): with app.test_request_context('/'): response = perform_web_task('z', 'test_task') print(response.status_code) diff --git a/tests/interface/worker/run_dummy_worker_with_params_test.py b/tests/interface/worker/run_dummy_worker_with_params_test.py index 42d5dac6..3305d639 100644 --- a/tests/interface/worker/run_dummy_worker_with_params_test.py +++ b/tests/interface/worker/run_dummy_worker_with_params_test.py @@ -46,8 +46,8 @@ class RunDummyWorkerWithParamsTestCase(ZslTestCase, TestCase): _PARAM_1_TEST_VALUE = 1 _PARAM_2_TEST_VALUE = 'Dont forget to read some good books!' - @inject(worker=TaskQueueWorker) - def testWorkerRunCountAndRunParams(self, worker): + @inject + def testWorkerRunCountAndRunParams(self, worker: TaskQueueWorker) -> None: self.assertEqual(worker.run_count, 0, "worker.run() shouldn't be called yet.") run_worker(self._PARAM_1_TEST_VALUE, param_2=self._PARAM_2_TEST_VALUE) diff --git a/tests/interface/worker/run_dummy_worker_without_params_test.py b/tests/interface/worker/run_dummy_worker_without_params_test.py index 6062d817..9ddda3bd 100644 --- a/tests/interface/worker/run_dummy_worker_without_params_test.py +++ b/tests/interface/worker/run_dummy_worker_without_params_test.py @@ -39,8 +39,8 @@ class RunDummyWorkerWithoutParamsTestCase(ZslTestCase, TestCase): app_name='RunDummyWorkerWithoutParamsTestCase', container=DummyWorkerContainer, config_object=CONFIG) - @inject(worker=TaskQueueWorker) - def testWorkerRunCount(self, worker): + @inject + def testWorkerRunCount(self, worker: TaskQueueWorker) -> None: self.assertEqual(worker.run_count, 0, "worker.run() shouldn't be called yet.") run_worker() diff --git a/tests/mocks.py b/tests/mocks.py index 23d88761..57ea4c56 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -1,14 +1,13 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * import unittest.mock as mock +from zsl import Zsl +from zsl import inject as zsl_inject from zsl.service.service import SessionFactory from zsl.testing.db import TestSessionFactory as DbTestTestSessionFactory -from zsl.utils.injection_helper import bind -def mock_db_session(): +@zsl_inject +def mock_db_session(app: Zsl): mock_sess = mock.MagicMock() def session_holder(): @@ -16,9 +15,10 @@ def session_holder(): class TestSessionFactory(DbTestTestSessionFactory): def __init__(self): - super(TestSessionFactory, self).__init__() + app.injector.call_with_injection(super(TestSessionFactory, self).__init__) self._session_holder = session_holder - bind(SessionFactory, to=TestSessionFactory) - bind(DbTestTestSessionFactory, to=TestSessionFactory) + tsf = app.injector.create_object(TestSessionFactory) + app.injector.binder.bind(SessionFactory, to=tsf) + app.injector.binder.bind(DbTestTestSessionFactory, to=tsf) return mock_sess diff --git a/tests/resource/read_only_resource.py b/tests/resource/read_only_resource.py index 054c685b..5e61fa49 100644 --- a/tests/resource/read_only_resource.py +++ b/tests/resource/read_only_resource.py @@ -6,7 +6,6 @@ from resource.resource_test_helper import UserModel, create_resource_test_data from unittest.case import TestCase -from injector import inject from sqlalchemy.ext.declarative import declarative_base from zsl import Zsl, inject @@ -24,23 +23,23 @@ class ReadOnlyModelResource(ReadOnlyResourceMixin, ModelResource): class TestReadOnlyModelResource(TestCase, DbTestCase): @classmethod - def setUpClass(cls): + def setUpClass(cls) -> None: create_resource_test_data() - @inject(app=Zsl) - def setUp(self, app): + @inject + def setUp(self, app: Zsl) -> None: self.resource = app._injector.create_object(ReadOnlyModelResource, {'model_cls': UserModel}) - def testRead(self): + def testRead(self) -> None: m = self.resource.read([9]) self.assertEqual('nine', m.val, "Read one") self.assertEqual(10, len(self.resource.read([])), 'Expecting 10 dummy models.') - def testCreate(self): + def testCreate(self) -> None: self.assertRaises(ReadOnlyResourceUpdateOperationException, lambda: self.resource.create({}, {}, {})) - def testDelete(self): + def testDelete(self) -> None: self.assertRaises(ReadOnlyResourceUpdateOperationException, lambda: self.resource.delete({}, {}, {})) - def testUpdate(self): + def testUpdate(self) -> None: self.assertRaises(ReadOnlyResourceUpdateOperationException, lambda: self.resource.update({}, {}, {})) diff --git a/tests/resource/resource_test_helper.py b/tests/resource/resource_test_helper.py index 961bd813..83ad5fb9 100644 --- a/tests/resource/resource_test_helper.py +++ b/tests/resource/resource_test_helper.py @@ -94,8 +94,8 @@ def get_non_existent_id(): return len(users) + 10 -@inject(engine=Engine, session_holder=SessionHolder) -def create_resource_test_data(engine, session_holder): +@inject +def create_resource_test_data(engine: Engine, session_holder: SessionHolder) -> None: Base.metadata.create_all(engine) session = session_holder() diff --git a/tests/resource/transactional_guard_test.py b/tests/resource/transactional_guard_test.py index 32a70d3a..4035cb2e 100644 --- a/tests/resource/transactional_guard_test.py +++ b/tests/resource/transactional_guard_test.py @@ -2,11 +2,9 @@ Test the `transactional_guard` decorator in terms of creating the transaction before checking the policies and calling rollback if policy is broken. """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from resource.resource_test_helper import UserModel, create_resource_test_data, users from unittest import TestCase +import unittest.mock as mock from mocks import mock_db_session @@ -17,11 +15,6 @@ from zsl.resource.model_resource import ModelResource from zsl.testing.db import IN_MEMORY_DB_SETTINGS, DbTestCase -try: - import unittest.mock as mock -except ImportError: - import mock - class UserResource(ModelResource): __model__ = UserModel diff --git a/tests/task_decorators/forbid_web_access_test.py b/tests/task_decorators/forbid_web_access_test.py index 432d853a..162202b5 100644 --- a/tests/task_decorators/forbid_web_access_test.py +++ b/tests/task_decorators/forbid_web_access_test.py @@ -28,8 +28,8 @@ class ForbidWebAccessTestCase(ZslTestCase, TestCase): app_name='ForbidWebAccessTestCase', container=WebContainer, config_object=CONFIG) - @inject(app=Zsl) - def testHttpAccessToWebForbiddenTask(self, app): + @inject + def testHttpAccessToWebForbiddenTask(self, app: Zsl) -> None: """ :param app: Zsl diff --git a/tests/task_decorators/secured_task_test.py b/tests/task_decorators/secured_task_test.py index 02d90155..b6dd0513 100644 --- a/tests/task_decorators/secured_task_test.py +++ b/tests/task_decorators/secured_task_test.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * import http.client from unittest.case import TestCase diff --git a/tests/testing/db_test.py b/tests/testing/db_test.py index 72debd18..7071c69e 100644 --- a/tests/testing/db_test.py +++ b/tests/testing/db_test.py @@ -1,12 +1,9 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from unittest.case import TestCase from mocks import mock, mock_db_session from sqlalchemy.orm.session import Session -from zsl import inject +from zsl import Zsl, inject from zsl.application.containers.container import IoCContainer from zsl.application.modules.alchemy_module import AlchemyModule from zsl.application.modules.context_module import DefaultContextModule @@ -16,7 +13,6 @@ from zsl.testing.db import IN_MEMORY_DB_SETTINGS, DbTestCase, DbTestModule from zsl.testing.db import TestSessionFactory as SessionFactoryForTesting from zsl.testing.zsl import ZslTestCase, ZslTestConfiguration -from zsl.utils.injection_helper import bind class TestContainerNoTestSessionFactory(IoCContainer): @@ -52,10 +48,11 @@ def test_db_test_case(self): mock_sess.rollback.assert_called_once_with() mock_sess.close.assert_called_once_with() - def test_db_test_case_session(self): + @inject + def test_db_test_case_session(self, app: Zsl): test = DbTestCaseTest.DbTest() mock_tsf = mock.MagicMock() - bind(SessionFactoryForTesting, to=mock_tsf) + app.injector.binder.bind(SessionFactoryForTesting, to=mock_tsf) test.setUp() mock_tsf.create_test_session.assert_called_once_with() mock_tsf.create_session.assert_not_called() @@ -68,11 +65,14 @@ class TestSessionFactoryTest(ZslTestCase, TestCase): container=TestContainerNoTestSessionFactory ) - def test_single_session(self): - self.assertNotEqual(SessionFactoryForTesting(), SessionFactoryForTesting(), + @inject + def test_single_session(self, app: Zsl) -> None: + # This tests that two calls do constructor create different objects and no unexpected singletons are crated. + self.assertNotEqual(app.injector.create_object(SessionFactoryForTesting), + app.injector.create_object(SessionFactoryForTesting), "Two different instances may not be equal.") - f = SessionFactoryForTesting() + f = app.injector.create_object(SessionFactoryForTesting) f.create_test_session() self.assertEqual(f.create_session(), f.create_session(), "Two results must be equal.") @@ -87,14 +87,13 @@ class DbTestModuleTest(ZslTestCase, TestCase): container=TestContainerTestSessionFactory ) - def test_test_session_factory_is_injected(self): - @inject(session_factory=SessionFactory) - def get_session_factory(session_factory): - return session_factory + @inject + def test_test_session_factory_is_injected(self, app: Zsl): + def get_session_factory() -> SessionFactory: + return app.injector.get(SessionFactory) - @inject(test_session_factory=SessionFactoryForTesting) - def get_test_session_factory(test_session_factory): - return test_session_factory + def get_test_session_factory() -> SessionFactoryForTesting: + return app.injector.get(SessionFactoryForTesting) self.assertIsInstance(get_session_factory(), SessionFactoryForTesting, "Correct session factory must be returned") diff --git a/tests/utils/redis_helper_test.py b/tests/utils/redis_helper_test.py index 3b52e374..c0d1a3c5 100644 --- a/tests/utils/redis_helper_test.py +++ b/tests/utils/redis_helper_test.py @@ -45,9 +45,8 @@ def test_with_prefix(self): self.assertEqual(keymaker.a(), 'testing:AAA', "Pure method with prefix") self.assertEqual(keymaker.b('x', 'y'), 'testing:XX:x:y', "Method with arguments and prefix") - @inject(config=Config) - def test_with_global_prefix(self, config): - # type: (Config)->None + @inject + def test_with_global_prefix(self, config: Config) -> None: config.setdefault('REDIS', {'prefix': 'global_prefix'}) keymaker = Keymaker({'a': 'AAA', 'b': 'XX'}, prefix="testing") From 1ad9b37447bcb29c4a4b5172c5d7c9413835a6e2 Mon Sep 17 00:00:00 2001 From: Martin Babka Date: Tue, 26 Jan 2021 23:16:09 +0000 Subject: [PATCH 2/2] Drop Python 2 support --- requirements.txt | 4 +-- src/zsl/__main__.py | 2 -- .../containers/celery_container.py | 1 - src/zsl/application/containers/container.py | 4 +-- .../application/containers/core_container.py | 2 -- .../containers/gearman_container.py | 1 - src/zsl/application/error_handler.py | 8 +++--- src/zsl/application/initialization_context.py | 2 +- .../initializers/library_initializer.py | 4 +-- .../initializers/service_initializer.py | 6 ++--- .../initializers/unittest_initializer.py | 2 +- src/zsl/application/modules/alchemy_module.py | 8 +++--- src/zsl/application/modules/cache_module.py | 1 - src/zsl/application/modules/celery_module.py | 3 +-- src/zsl/application/modules/cli_module.py | 13 ++++------ src/zsl/application/modules/context_module.py | 1 - .../modules/error_handler_module.py | 2 +- src/zsl/application/modules/gearman_module.py | 2 +- src/zsl/application/modules/logger_module.py | 2 +- .../application/modules/web/configuration.py | 6 +---- src/zsl/application/modules/web/cors.py | 2 +- .../modules/web/web_context_module.py | 8 +++--- src/zsl/application/service_application.py | 10 +++---- src/zsl/cache/cache_module.py | 2 -- src/zsl/cache/id_helper.py | 8 +++--- src/zsl/cache/redis_cache_module.py | 6 ++--- src/zsl/cache/redis_id_helper.py | 2 +- src/zsl/configuration/__init__.py | 5 ---- src/zsl/constants.py | 3 --- src/zsl/contrib/alembic/alembic_config.py | 3 --- src/zsl/contrib/alembic/alembic_module.py | 2 +- src/zsl/contrib/sentry/sentry_config.py | 7 ++--- src/zsl/contrib/sentry/sentry_module.py | 6 ++--- src/zsl/db/helpers/__init__.py | 2 -- src/zsl/db/helpers/nested.py | 2 -- src/zsl/db/helpers/pagination.py | 8 +++--- src/zsl/db/helpers/query_filter.py | 26 +++++++++---------- src/zsl/db/helpers/query_helper.py | 4 +-- src/zsl/db/helpers/sorter.py | 4 +-- src/zsl/db/model/__init__.py | 2 -- src/zsl/db/model/app_model.py | 6 ++--- src/zsl/db/model/app_model_json_decoder.py | 1 - src/zsl/db/model/app_model_json_encoder.py | 1 - src/zsl/db/model/raw_model.py | 4 +-- src/zsl/db/model/sql_alchemy.py | 1 - src/zsl/errors.py | 6 ++--- src/zsl/interface/celery/worker.py | 2 +- src/zsl/interface/cli.py | 2 -- .../interface/gearman/json_data_encoder.py | 1 - src/zsl/interface/gearman/task_filler.py | 7 +++-- src/zsl/interface/gearman/worker.py | 13 +++++----- src/zsl/interface/resource.py | 2 -- src/zsl/interface/task.py | 6 ++--- src/zsl/interface/task_queue.py | 6 ++--- src/zsl/interface/web/importer.py | 1 - src/zsl/interface/web/performers/default.py | 1 - src/zsl/interface/web/performers/method.py | 1 - src/zsl/interface/web/performers/resource.py | 4 +-- src/zsl/interface/web/performers/task.py | 8 +++--- src/zsl/interface/web/utils/execution.py | 3 --- src/zsl/interface/web/utils/request_data.py | 5 ---- src/zsl/resource/guard.py | 12 ++++----- src/zsl/resource/json_server_resource.py | 10 +++---- src/zsl/resource/model_resource.py | 26 +++++++++---------- src/zsl/resource/resource_helper.py | 8 +++--- src/zsl/router/method.py | 6 ++--- src/zsl/router/task.py | 18 ++++++------- src/zsl/service/__init__.py | 2 -- src/zsl/service/service.py | 6 ++--- src/zsl/task/__init__.py | 2 -- src/zsl/task/job_context.py | 12 ++++----- src/zsl/task/task_data.py | 2 +- src/zsl/task/task_decorator.py | 8 +++--- src/zsl/task/task_status.py | 1 - src/zsl/tasks/zsl/cors_test_task.py | 4 +-- src/zsl/tasks/zsl/db_test_task.py | 6 ++--- src/zsl/tasks/zsl/kill_worker_task.py | 4 +-- src/zsl/tasks/zsl/schedule_celery_task.py | 4 +-- src/zsl/tasks/zsl/schedule_gearman_task.py | 4 +-- .../tasks/zsl/schedule_kill_worker_task.py | 4 +-- src/zsl/tasks/zsl/sum_task.py | 6 ++--- src/zsl/tasks/zsl/test_task.py | 4 +-- src/zsl/tasks/zsl/version_task.py | 4 +-- src/zsl/tasks/zsl/with_request_task.py | 4 +-- src/zsl/testing/__init__.py | 1 - src/zsl/testing/db.py | 6 ++--- src/zsl/testing/http.py | 4 +-- src/zsl/testing/test_utils.py | 4 +-- src/zsl/testing/zsl.py | 10 +++---- src/zsl/utils/background_task.py | 3 +-- src/zsl/utils/cache_helper.py | 10 +++---- src/zsl/utils/command_dispatcher.py | 4 +-- src/zsl/utils/date_helper.py | 2 -- src/zsl/utils/deploy/apiari_doc_generator.py | 5 ++-- src/zsl/utils/deploy/integrator.py | 3 +-- src/zsl/utils/deploy/js_model_generator.py | 8 +++--- src/zsl/utils/dict_to_object_conversion.py | 3 --- src/zsl/utils/documentation.py | 6 +---- src/zsl/utils/email_helper.py | 1 - src/zsl/utils/file_helper.py | 2 -- src/zsl/utils/gearman_helper.py | 2 +- src/zsl/utils/http.py | 3 --- src/zsl/utils/import_helper.py | 1 - src/zsl/utils/model_helper.py | 9 +++---- src/zsl/utils/nginx_push_helper.py | 6 ++--- src/zsl/utils/params_helper.py | 1 - src/zsl/utils/php_helper.py | 1 - src/zsl/utils/redis_helper.py | 2 +- src/zsl/utils/reflection_helper.py | 6 ++--- src/zsl/utils/request_helper.py | 1 - src/zsl/utils/resource_helper.py | 13 +++++----- src/zsl/utils/rss.py | 1 - src/zsl/utils/security_helper.py | 2 -- src/zsl/utils/string_helper.py | 6 ++--- src/zsl/utils/task_helper.py | 3 --- src/zsl/utils/testing.py | 1 - src/zsl/utils/type_helper.py | 1 - src/zsl/utils/url_helper.py | 5 +--- src/zsl/utils/warnings.py | 2 -- src/zsl/utils/xml_helper.py | 5 ++-- src/zsl/utils/xml_to_json.py | 5 ++-- 121 files changed, 197 insertions(+), 375 deletions(-) diff --git a/requirements.txt b/requirements.txt index 21c99e66..758ede4e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,11 +5,11 @@ click>=7.0 enum34 future Flask>=1.1 -injector==0.18.4 +injector>=0.18 redis>=3.2 requests>=2.22 SQLAlchemy>=1.3,<1.4 tox -Werkzeug>=0.16,<0.17 +Werkzeug>=0.16 zsl_client pytest diff --git a/src/zsl/__main__.py b/src/zsl/__main__.py index cf2db2c9..a7fd407d 100644 --- a/src/zsl/__main__.py +++ b/src/zsl/__main__.py @@ -4,8 +4,6 @@ Main service module. """ -from __future__ import unicode_literals - from zsl.interface.cli import cli if __name__ == "__main__": diff --git a/src/zsl/application/containers/celery_container.py b/src/zsl/application/containers/celery_container.py index fcb3bf43..0302fa71 100644 --- a/src/zsl/application/containers/celery_container.py +++ b/src/zsl/application/containers/celery_container.py @@ -2,7 +2,6 @@ :mod:`zsl.application.containers.celery_container` -------------------------------------------------- """ -from __future__ import unicode_literals from zsl.application.containers.core_container import CoreContainer from zsl.application.modules.celery_module import (CeleryCliModule, CeleryTaskQueueMainWorkerModule, diff --git a/src/zsl/application/containers/container.py b/src/zsl/application/containers/container.py index 608d5f87..f23d8ca2 100644 --- a/src/zsl/application/containers/container.py +++ b/src/zsl/application/containers/container.py @@ -2,12 +2,10 @@ :mod:`zsl.application.containers.container` ------------------------------------------- """ -from __future__ import unicode_literals - import inspect -class IoCContainer(object): +class IoCContainer: """Collection of DI modules set as class attributes. This is used for declarative DI configuration, which can be easily diff --git a/src/zsl/application/containers/core_container.py b/src/zsl/application/containers/core_container.py index e1b1fa6b..d99a9540 100644 --- a/src/zsl/application/containers/core_container.py +++ b/src/zsl/application/containers/core_container.py @@ -2,8 +2,6 @@ :mod:`zsl.application.containers.core_container` ------------------------------------------------ """ -from __future__ import unicode_literals - from zsl.application.containers.container import IoCContainer from zsl.application.modules.alchemy_module import AlchemyModule from zsl.application.modules.cache_module import RedisCacheInjectionModule diff --git a/src/zsl/application/containers/gearman_container.py b/src/zsl/application/containers/gearman_container.py index a92bc154..2e53e509 100644 --- a/src/zsl/application/containers/gearman_container.py +++ b/src/zsl/application/containers/gearman_container.py @@ -2,7 +2,6 @@ :mod:`zsl.application.containers.gearman_container` --------------------------------------------------- """ -from __future__ import unicode_literals from zsl.application.containers.core_container import CoreContainer from zsl.application.modules.gearman_module import GearmanModule diff --git a/src/zsl/application/error_handler.py b/src/zsl/application/error_handler.py index fe2859f7..bdf7c120 100644 --- a/src/zsl/application/error_handler.py +++ b/src/zsl/application/error_handler.py @@ -27,7 +27,7 @@ class ErrorResponse(AppModel): def __init__(self, code, message): - super(ErrorResponse, self).__init__({}) + super().__init__({}) self.code = code self.message = message @@ -51,11 +51,11 @@ def can_handle(self, e): def handle(self, ex): logger = logging.getLogger(__name__) logger.error(str(ex) + "\n" + traceback.format_exc()) - logger.error("Request:\n{0}\n{1}\n".format(request.headers, - request.data)) + logger.error("Request:\n{}\n{}\n".format(request.headers, + request.data)) link = documentation_link('error_handling') logger.info("Provide your own error handler so that " - "a better error is produced, check {0}.".format(link)) + "a better error is produced, check {}.".format(link)) add_responder(StatusCodeResponder(http.client.INTERNAL_SERVER_ERROR)) return ErrorResponse(self.ERROR_CODE, self.ERROR_MESSAGE) diff --git a/src/zsl/application/initialization_context.py b/src/zsl/application/initialization_context.py index 9eda111f..df169777 100644 --- a/src/zsl/application/initialization_context.py +++ b/src/zsl/application/initialization_context.py @@ -10,7 +10,7 @@ from typing import Any, List -class InitializationContext(object): +class InitializationContext: def __init__(self, unit_test: bool = False, initializers: List[Any] = None): if not initializers: diff --git a/src/zsl/application/initializers/library_initializer.py b/src/zsl/application/initializers/library_initializer.py index 2a001090..e0d9e173 100644 --- a/src/zsl/application/initializers/library_initializer.py +++ b/src/zsl/application/initializers/library_initializer.py @@ -16,7 +16,7 @@ def append_paths(path, vendor_modules): sys.path = new_path + sys.path -class LibraryInitializer(object): +class LibraryInitializer: # TODO: Change to iface """Add vendor modules to current path.""" @@ -32,5 +32,5 @@ def initialize(config: Config) -> None: vendor_path = external_libraries['vendor_path'] append_paths(vendor_path, external_libraries['libs']) - logging.info("Current PYTHON_PATH={0}.".format(sys.path)) + logging.info("Current PYTHON_PATH={}.".format(sys.path)) logging.debug("Project external libraries initialized.") diff --git a/src/zsl/application/initializers/service_initializer.py b/src/zsl/application/initializers/service_initializer.py index 718d558c..a1a41b5c 100644 --- a/src/zsl/application/initializers/service_initializer.py +++ b/src/zsl/application/initializers/service_initializer.py @@ -11,7 +11,7 @@ from zsl.utils.string_helper import camelcase_to_underscore -class ServiceInitializer(object): +class ServiceInitializer: # TODO: change to iface """Add outside services to application injector.""" @@ -35,7 +35,7 @@ def _bind_service(package_name: str, cls_name: str, binder: Binder) -> None: to=binder.injector.create_object(cls), scope=singleton ) - logging.debug("Created {0} binding.".format(cls)) + logging.debug("Created {} binding.".format(cls)) @staticmethod @inject @@ -60,5 +60,5 @@ def initialize(config: Config) -> None: for cls_name in services: module_name = camelcase_to_underscore(cls_name) - package_name = "{0}.{1}".format(service_package, module_name) + package_name = "{}.{}".format(service_package, module_name) ServiceInitializer._bind_service(package_name, cls_name) diff --git a/src/zsl/application/initializers/unittest_initializer.py b/src/zsl/application/initializers/unittest_initializer.py index 1ac7308b..a5c37f68 100644 --- a/src/zsl/application/initializers/unittest_initializer.py +++ b/src/zsl/application/initializers/unittest_initializer.py @@ -5,7 +5,7 @@ from zsl import ApplicationContext, Config, inject -class UnitTestInitializer(object): +class UnitTestInitializer: # TODO: Change to iface """Initializer handling the unit test settings.""" diff --git a/src/zsl/application/modules/alchemy_module.py b/src/zsl/application/modules/alchemy_module.py index e6430692..daa689d5 100644 --- a/src/zsl/application/modules/alchemy_module.py +++ b/src/zsl/application/modules/alchemy_module.py @@ -14,7 +14,7 @@ from zsl import Config -class SessionHolder(object): +class SessionHolder: def __init__(self, sess_cls: Type[Session]) -> None: self._sess_cls = sess_cls @@ -22,7 +22,7 @@ def __call__(self) -> Session: return self._sess_cls() -class SessionFactory(object): +class SessionFactory: """Creates a db session with an open transaction.""" @inject @@ -33,7 +33,7 @@ def create_session(self) -> Session: return self._session_holder() -class TransactionHolder(object): +class TransactionHolder: def __init__(self) -> None: self._orm = None self._transaction_callback = [] @@ -82,7 +82,7 @@ def run_transaction_callbacks(self) -> None: c() -class EmptyTransactionalHolder(object): +class EmptyTransactionalHolder: def __init__(self) -> None: self._session = None diff --git a/src/zsl/application/modules/cache_module.py b/src/zsl/application/modules/cache_module.py index c6ddcb65..41a02bc2 100644 --- a/src/zsl/application/modules/cache_module.py +++ b/src/zsl/application/modules/cache_module.py @@ -2,7 +2,6 @@ :mod:`zsl.application.modules.cache_module` ------------------------------------------- """ -from __future__ import unicode_literals import logging diff --git a/src/zsl/application/modules/celery_module.py b/src/zsl/application/modules/celery_module.py index 2dbb6634..68a1174f 100644 --- a/src/zsl/application/modules/celery_module.py +++ b/src/zsl/application/modules/celery_module.py @@ -2,7 +2,6 @@ :mod:`zsl.application.modules.celery_module` -------------------------------------------- """ -from __future__ import unicode_literals import click from injector import Module, singleton @@ -36,7 +35,7 @@ def configure(self, binder): binder.bind(CeleryTaskQueueWorkerBase, to=worker, scope=singleton) -class CeleryCli(object): +class CeleryCli: @inject def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help='Celery related tasks.') diff --git a/src/zsl/application/modules/cli_module.py b/src/zsl/application/modules/cli_module.py index 410dddd4..185600f7 100644 --- a/src/zsl/application/modules/cli_module.py +++ b/src/zsl/application/modules/cli_module.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * import logging import traceback @@ -13,7 +10,7 @@ from zsl.utils.testing import load_and_run_tests -class ZslCli(object): +class ZslCli: def __init__(self): # type: () -> ZslCli logging.getLogger(__name__).debug("Creating ZSL CLI.") @@ -29,7 +26,7 @@ def __call__(self, *args, **kwargs): cli(**kwargs) -class ZslTaskCli(object): +class ZslTaskCli: @inject def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.command(help="Execute a single task.") @@ -42,14 +39,14 @@ def task(task_path, data=None): result = exec_task(task_path, data) click.echo(result) except: # NOQA - msg = "Error when calling task '{0}'\n\n{1}.".format( + msg = "Error when calling task '{}'\n\n{}.".format( task_path, traceback.format_exc()) logging.getLogger(__name__).error(msg) click.echo(msg) exit(1) -class ZslTestCli(object): +class ZslTestCli: @inject def __init__(self, zsl_cli: ZslCli) -> ZslTaskCli: @zsl_cli.cli.group(help="Perform unit tests.") @@ -61,7 +58,7 @@ def run(): load_and_run_tests() -class ZslGenerateCli(object): +class ZslGenerateCli: @inject def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help="Perform unit tests.") diff --git a/src/zsl/application/modules/context_module.py b/src/zsl/application/modules/context_module.py index a284de6a..96aa93b0 100644 --- a/src/zsl/application/modules/context_module.py +++ b/src/zsl/application/modules/context_module.py @@ -5,7 +5,6 @@ Application context describes the environment in which the application is run\ and is responsible for specific initializations on it. """ -from __future__ import unicode_literals import logging diff --git a/src/zsl/application/modules/error_handler_module.py b/src/zsl/application/modules/error_handler_module.py index 78da51e3..05e67f9d 100644 --- a/src/zsl/application/modules/error_handler_module.py +++ b/src/zsl/application/modules/error_handler_module.py @@ -15,6 +15,6 @@ def provide_error_config(self, config: Config) -> ErrorConfiguration: def configure(self, binder: Binder) -> None: error_config = binder.injector.get(ErrorConfiguration) - super(ErrorHandlerModule, self).configure(binder) + super().configure(binder) for handler in error_config.handlers: register(handler) diff --git a/src/zsl/application/modules/gearman_module.py b/src/zsl/application/modules/gearman_module.py index 0af18721..a1225fa7 100644 --- a/src/zsl/application/modules/gearman_module.py +++ b/src/zsl/application/modules/gearman_module.py @@ -13,7 +13,7 @@ from zsl.utils.injection_helper import simple_bind -class GearmanCli(object): +class GearmanCli: @inject def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help="Gearman related tasks.") diff --git a/src/zsl/application/modules/logger_module.py b/src/zsl/application/modules/logger_module.py index 4e0455a2..5ff7e1cd 100644 --- a/src/zsl/application/modules/logger_module.py +++ b/src/zsl/application/modules/logger_module.py @@ -15,7 +15,7 @@ class LoggerModule(Module): LOGGING_CONFIG_NAME = 'LOGGING' def configure(self, binder: Binder) -> None: - super(LoggerModule, self).configure(binder) + super().configure(binder) binder.injector.call_with_injection(self.configure_logging) @inject diff --git a/src/zsl/application/modules/web/configuration.py b/src/zsl/application/modules/web/configuration.py index 4667aac7..b2ee98ad 100644 --- a/src/zsl/application/modules/web/configuration.py +++ b/src/zsl/application/modules/web/configuration.py @@ -1,11 +1,7 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * - from zsl.configuration import InvalidConfigurationException -class MethodConfiguration(object): +class MethodConfiguration: def __init__(self, package=None, packages=None, url_prefix='method'): if packages is not None and package is not None: raise InvalidConfigurationException("Can not take both packages and package in method configuration.") diff --git a/src/zsl/application/modules/web/cors.py b/src/zsl/application/modules/web/cors.py index c5c003d7..9db8dcdb 100644 --- a/src/zsl/application/modules/web/cors.py +++ b/src/zsl/application/modules/web/cors.py @@ -3,7 +3,7 @@ CORS_CONFIGURATION_NAME = 'CORS' -class CORSConfiguration(object): +class CORSConfiguration: DEFAULT_ALLOW_HEADERS = ['Accept', 'Origin', 'Content-Type', 'Authorization'] DEFAULT_EXPOSE_HEADERS = ['Location', 'X-Total-Count', 'Link'] diff --git a/src/zsl/application/modules/web/web_context_module.py b/src/zsl/application/modules/web/web_context_module.py index c028050f..c23d005e 100644 --- a/src/zsl/application/modules/web/web_context_module.py +++ b/src/zsl/application/modules/web/web_context_module.py @@ -13,7 +13,7 @@ from zsl.utils.injection_helper import simple_bind -class WebInitializer(object): +class WebInitializer: """Initialize the web application.""" @staticmethod @@ -34,7 +34,7 @@ def initialize(): web_initializers = default_initializers + (WebInitializer,) -class WebCli(object): +class WebCli: @inject def __init__(self, zsl_cli: ZslCli) -> None: @zsl_cli.cli.group(help='Web related tasks.') @@ -55,7 +55,7 @@ def web(self): return self._web -class WebHandler(object): +class WebHandler: @inject def run_web(self, flask: Zsl, host: str = '127.0.0.1', port: int = 5000, **options: Any): # type: (Zsl, str, int, **Any)->None @@ -92,6 +92,6 @@ def provide_cors_configuration(self, config: Config) -> CORSConfiguration: return config.get(CORS_CONFIGURATION_NAME, CORSConfiguration()) def configure(self, binder: Binder) -> None: - super(WebContextModule, self).configure(binder) + super().configure(binder) simple_bind(binder, WebCli, singleton) create_task_mapping() diff --git a/src/zsl/application/service_application.py b/src/zsl/application/service_application.py index 9e2270a8..b09e1353 100644 --- a/src/zsl/application/service_application.py +++ b/src/zsl/application/service_application.py @@ -19,8 +19,6 @@ file. """ -from __future__ import unicode_literals - import os from typing import Any, Callable @@ -52,7 +50,7 @@ def get_settings_from_profile(profile, profile_dir=None): if hasattr(profile_dir, '__file__'): profile_dir = os.path.dirname(profile_dir.__file__) - return os.path.join(profile_dir, '{0}.cfg'.format(profile)) + return os.path.join(profile_dir, '{}.cfg'.format(profile)) def set_profile(profile): @@ -85,7 +83,7 @@ def __init__( config_object=None, default_settings_module='settings.default_settings' ): - super(ServiceApplication, self).__init__( + super().__init__( import_name, static_url_path, static_folder, @@ -112,7 +110,7 @@ def __init__( self._dependencies_initialized = True def __str__(self): - return "ZSL(application={0}, zsl_version={1}, app_version={2})".format( + return "ZSL(application={}, zsl_version={}, app_version={})".format( self.name, self.VERSION, self._app_version) @@ -176,7 +174,7 @@ def _configure_injector(self, modules): self._create_injector() self._bind_core() self._bind_modules(modules) - self.logger.debug("Injector configuration with modules {0}.".format(modules)) + self.logger.debug("Injector configuration with modules {}.".format(modules)) self._dependencies_initialized = True @deprecated diff --git a/src/zsl/cache/cache_module.py b/src/zsl/cache/cache_module.py index d2f65629..94e96d47 100644 --- a/src/zsl/cache/cache_module.py +++ b/src/zsl/cache/cache_module.py @@ -4,10 +4,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals import abc -from builtins import object from future.utils import with_metaclass diff --git a/src/zsl/cache/id_helper.py b/src/zsl/cache/id_helper.py index 832d6e66..013cf522 100644 --- a/src/zsl/cache/id_helper.py +++ b/src/zsl/cache/id_helper.py @@ -4,10 +4,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals import abc -from builtins import object import json from future.utils import with_metaclass @@ -38,7 +36,7 @@ def app_model_encoder_fn(x): def create_key_class_prefix(cls): - return "{0}.{1}".format(cls.__module__, cls.__name__) + return "{}.{}".format(cls.__module__, cls.__name__) def create_key_object_prefix(obj): @@ -69,9 +67,9 @@ def get_app_model_related_fields(app_model, prefix=''): related = sorted(get_app_model_related_fields(model)) if len(related): - return "{0}:{1}:+{2}".format(create_key_object_prefix(model), model.get_id(), "+".join(related)) + return "{}:{}:+{}".format(create_key_object_prefix(model), model.get_id(), "+".join(related)) else: - return "{0}:{1}".format(create_key_object_prefix(model), model.get_id()) + return "{}:{}".format(create_key_object_prefix(model), model.get_id()) # TODO: Test diff --git a/src/zsl/cache/redis_cache_module.py b/src/zsl/cache/redis_cache_module.py index b569fd7b..5286fcb3 100644 --- a/src/zsl/cache/redis_cache_module.py +++ b/src/zsl/cache/redis_cache_module.py @@ -43,12 +43,12 @@ def set_key(self, key, value, timeout): def invalidate_key(self, key): pkey = self._prefix_key(key) - self.logger.debug("Key invalidation '{0}'.".format(key)) + self.logger.debug("Key invalidation '{}'.".format(key)) self._client.delete(pkey) def set_key_expiration(self, key, timeout): pkey = self._prefix_key(key) - self.logger.debug("Key expiration '{0}' = {1}.".format(key, timeout)) + self.logger.debug("Key expiration '{}' = {}.".format(key, timeout)) self._client.expire(pkey, timeout) def contains_key(self, key): @@ -82,5 +82,5 @@ def invalidate_by_glob(self, glob): for key in keylist: # This does not need to prefixed. - self.logger.debug('Invalidating key {0}.'.format(key)) + self.logger.debug('Invalidating key {}.'.format(key)) self._client.delete(key) diff --git a/src/zsl/cache/redis_id_helper.py b/src/zsl/cache/redis_id_helper.py index 4e8bf628..4bc9e216 100644 --- a/src/zsl/cache/redis_id_helper.py +++ b/src/zsl/cache/redis_id_helper.py @@ -39,7 +39,7 @@ def get_timeout(self, key, value, timeout): def gather_page(self, page_key, decoder=decoder_identity): page_keys = self._redis_cache_module.get_list(page_key) - logging.debug("Fetching page {0} from redis using keys {1}.".format(page_key, page_keys)) + logging.debug("Fetching page {} from redis using keys {}.".format(page_key, page_keys)) p = [] for k in page_keys: diff --git a/src/zsl/configuration/__init__.py b/src/zsl/configuration/__init__.py index 22dc77fe..af9896ba 100644 --- a/src/zsl/configuration/__init__.py +++ b/src/zsl/configuration/__init__.py @@ -1,7 +1,2 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * - - class InvalidConfigurationException(Exception): pass diff --git a/src/zsl/constants.py b/src/zsl/constants.py index cd6e6eae..3b6f19fc 100644 --- a/src/zsl/constants.py +++ b/src/zsl/constants.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from enum import Enum diff --git a/src/zsl/contrib/alembic/alembic_config.py b/src/zsl/contrib/alembic/alembic_config.py index 8047930e..ffc37c61 100644 --- a/src/zsl/contrib/alembic/alembic_config.py +++ b/src/zsl/contrib/alembic/alembic_config.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from collections import namedtuple AlembicConfiguration = namedtuple('AlembicConfiguration', ['alembic_directory']) diff --git a/src/zsl/contrib/alembic/alembic_module.py b/src/zsl/contrib/alembic/alembic_module.py index 4c357c8f..d176958b 100644 --- a/src/zsl/contrib/alembic/alembic_module.py +++ b/src/zsl/contrib/alembic/alembic_module.py @@ -110,7 +110,7 @@ def run_migrations_online(engine: Engine): raise -class AlembicCli(object): +class AlembicCli: """Alembic Cli interface support.""" @inject diff --git a/src/zsl/contrib/sentry/sentry_config.py b/src/zsl/contrib/sentry/sentry_config.py index b004207f..8086d9fd 100644 --- a/src/zsl/contrib/sentry/sentry_config.py +++ b/src/zsl/contrib/sentry/sentry_config.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from collections import namedtuple import logging @@ -12,5 +9,5 @@ class SentryConfiguration(namedtuple('SentryConfiguration', ['dsn', 'environment def __new__(cls, dsn, environment=None, tags=None, register_logging_handler=True, sentry_logging_handler_level=logging.ERROR): tags = [] if tags is None else tags - return super(SentryConfiguration, cls).__new__(cls, dsn, environment, tags, register_logging_handler, - sentry_logging_handler_level) + return super().__new__(cls, dsn, environment, tags, register_logging_handler, + sentry_logging_handler_level) diff --git a/src/zsl/contrib/sentry/sentry_module.py b/src/zsl/contrib/sentry/sentry_module.py index e43aeb06..3d405e09 100644 --- a/src/zsl/contrib/sentry/sentry_module.py +++ b/src/zsl/contrib/sentry/sentry_module.py @@ -37,9 +37,7 @@ class MyApplicationContainer(WebContainer): python app.py sentry test """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import logging from injector import Binder, inject, provider, singleton @@ -64,7 +62,7 @@ class MyApplicationContainer(WebContainer): raise -class SentryCli(object): +class SentryCli: pass """Sentry CLI interface support.""" @@ -117,7 +115,7 @@ def _register_logging_handler(self, config): return handler def handle(self, e): - logging.getLogger(__name__).info('Sending error message for {0}.'.format(e)) + logging.getLogger(__name__).info('Sending error message for {}.'.format(e)) self._client.captureException() diff --git a/src/zsl/db/helpers/__init__.py b/src/zsl/db/helpers/__init__.py index 9bd747ad..e45814e4 100644 --- a/src/zsl/db/helpers/__init__.py +++ b/src/zsl/db/helpers/__init__.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import types diff --git a/src/zsl/db/helpers/nested.py b/src/zsl/db/helpers/nested.py index bb0ca5e1..fd920c72 100644 --- a/src/zsl/db/helpers/nested.py +++ b/src/zsl/db/helpers/nested.py @@ -4,9 +4,7 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals -from builtins import * # NOQA from typing import Any, Optional from sqlalchemy.orm.attributes import InstrumentedAttribute diff --git a/src/zsl/db/helpers/pagination.py b/src/zsl/db/helpers/pagination.py index 128f27bb..0f3b5dd5 100644 --- a/src/zsl/db/helpers/pagination.py +++ b/src/zsl/db/helpers/pagination.py @@ -2,9 +2,7 @@ :mod:`zsl.db.helpers.pagination` -------------------------------- """ -from __future__ import unicode_literals -from builtins import object from typing import Dict, Union from sqlalchemy.orm.query import Query @@ -15,7 +13,7 @@ DEFAULT_PAGE_SIZE = 25 -class Pagination(object): +class Pagination: """ Pagination support. Allows to paginate a query. There are two choices. #. :meth:`.paginate` - paginates a query and obtains the count @@ -107,7 +105,7 @@ def __init__(self, raw=None, id_name='id', hints=None): if raw is None: raw = {'page_no': FIRST_PAGE, 'page_size': DEFAULT_PAGE_SIZE} - super(PaginationRequest, self).__init__(raw, id_name, hints) + super().__init__(raw, id_name, hints) self.page_no = int(self.page_no) if self.page_no else FIRST_PAGE self.page_size = int(self.page_size) if self.page_size else DEFAULT_PAGE_SIZE @@ -119,7 +117,7 @@ def create(page_no: int = FIRST_PAGE, page_size: int = DEFAULT_PAGE_SIZE) -> 'Pa class PaginationResponse(AppModel): def __init__(self, record_count, page_size, pagination): # type: (int, int, PaginationRequest)->None - super(PaginationResponse, self).__init__({}) + super().__init__({}) self.record_count = record_count max_page_size = pagination.page_size self.page_count = (record_count + max_page_size - 1) // max_page_size diff --git a/src/zsl/db/helpers/query_filter.py b/src/zsl/db/helpers/query_filter.py index 999b30a4..a26a6965 100644 --- a/src/zsl/db/helpers/query_filter.py +++ b/src/zsl/db/helpers/query_filter.py @@ -2,59 +2,57 @@ :mod:`zsl.db.helpers.query_filter` ---------------------------------- """ -from __future__ import unicode_literals -from builtins import object from future.utils import viewitems from sqlalchemy.sql import func -class OperatorEq(object): +class OperatorEq: @staticmethod def apply(q, attr, v): return q.filter(attr == v) -class OperatorNeq(object): +class OperatorNeq: @staticmethod def apply(q, attr, v): return q.filter(attr != v) -class OperatorLike(object): +class OperatorLike: @staticmethod def apply(q, attr, v): - return q.filter(attr.like('%{0}%'.format(v))) + return q.filter(attr.like('%{}%'.format(v))) -class OperatorLeftLike(object): +class OperatorLeftLike: """ Left side of string is like ... """ @staticmethod def apply(q, attr, v): - return q.filter(attr.like('{0}%'.format(v))) + return q.filter(attr.like('{}%'.format(v))) -class OperatorRightLike(object): +class OperatorRightLike: """ Right side of string is like ... """ @staticmethod def apply(q, attr, v): - return q.filter(attr.like('%{0}'.format(v))) + return q.filter(attr.like('%{}'.format(v))) -class OperatorBetween(object): +class OperatorBetween: @staticmethod def apply(q, attr, v): return q.filter(attr.between(v[0], v[1])) -class OperatorCompareDates(object): +class OperatorCompareDates: """ Compares only dates, year is not taken into account. Compared date value must be string in format '%m-%d' @@ -65,7 +63,7 @@ def apply(q, attr, v): return q.filter(func.date_format(attr, '%m-%d') == v) -class RelationshipOperatorContains(object): +class RelationshipOperatorContains: @staticmethod def apply(q, attr, v): return q.filter(attr.contains(v)) @@ -75,7 +73,7 @@ def apply(q, attr, v): FILTER_VALUES = 'values' -class QueryFilter(object): +class QueryFilter: """ Helper class for applying filter criteria to query. """ diff --git a/src/zsl/db/helpers/query_helper.py b/src/zsl/db/helpers/query_helper.py index d64ba162..00778a6a 100644 --- a/src/zsl/db/helpers/query_helper.py +++ b/src/zsl/db/helpers/query_helper.py @@ -4,16 +4,14 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object from zsl.db.helpers.pagination import Pagination from zsl.db.helpers.query_filter import QueryFilter from zsl.db.helpers.sorter import Sorter -class QueryHelper(object): +class QueryHelper: def __init__(self, cls, query_filter, pagination, sorter): self._cls = cls diff --git a/src/zsl/db/helpers/sorter.py b/src/zsl/db/helpers/sorter.py index 4ceb591f..549e3322 100644 --- a/src/zsl/db/helpers/sorter.py +++ b/src/zsl/db/helpers/sorter.py @@ -2,16 +2,14 @@ :mod:`zsl.db.helpers.sorter` ---------------------------- """ -from __future__ import unicode_literals -from builtins import object, zip from sqlalchemy import asc, desc DEFAULT_SORT_ORDER = 'ASC' # If changed, look at the condition in apply_sorter if self.get_order() == "DESC":. -class Sorter(object): +class Sorter: """ Helper class for applying ordering criteria to query. """ diff --git a/src/zsl/db/model/__init__.py b/src/zsl/db/model/__init__.py index 441c476a..c6984ca3 100644 --- a/src/zsl/db/model/__init__.py +++ b/src/zsl/db/model/__init__.py @@ -1,4 +1,2 @@ -from __future__ import unicode_literals - from zsl.db.model.app_model import AppModel from zsl.db.model.app_model_json_encoder import AppModelJSONEncoder diff --git a/src/zsl/db/model/app_model.py b/src/zsl/db/model/app_model.py index 9c532e97..e0d05e7d 100644 --- a/src/zsl/db/model/app_model.py +++ b/src/zsl/db/model/app_model.py @@ -4,10 +4,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals # noinspection PyCompatibility -from builtins import map, object from future.utils import viewitems @@ -22,7 +20,7 @@ ISO_8601_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ' -class AppModel(object): +class AppModel: """AppModel's are used as a thin and simple communication objects. Also they can be saved into cache. Basically they are known as Data Transfer Objects or DTOs. @@ -92,4 +90,4 @@ def get_attributes(self): return d def __str__(self): - return "{0}: {1}".format(self.__class__, self.__dict__) + return "{}: {}".format(self.__class__, self.__dict__) diff --git a/src/zsl/db/model/app_model_json_decoder.py b/src/zsl/db/model/app_model_json_decoder.py index e31486ce..6a5b4bfd 100644 --- a/src/zsl/db/model/app_model_json_decoder.py +++ b/src/zsl/db/model/app_model_json_decoder.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from json.decoder import WHITESPACE, JSONDecoder diff --git a/src/zsl/db/model/app_model_json_encoder.py b/src/zsl/db/model/app_model_json_encoder.py index 0d176460..9d20812d 100644 --- a/src/zsl/db/model/app_model_json_encoder.py +++ b/src/zsl/db/model/app_model_json_encoder.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from json.encoder import JSONEncoder diff --git a/src/zsl/db/model/raw_model.py b/src/zsl/db/model/raw_model.py index 5ccda90f..547e4d04 100644 --- a/src/zsl/db/model/raw_model.py +++ b/src/zsl/db/model/raw_model.py @@ -4,14 +4,12 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object from zsl.utils.model_helper import update_model -class ModelBase(object): +class ModelBase: def update(self, app_model, forbidden_keys=None, inverse=False): """ Updates the raw model. Consult `zsl.utils.model_helper.update_model`. diff --git a/src/zsl/db/model/sql_alchemy.py b/src/zsl/db/model/sql_alchemy.py index 14877e04..db4c4c16 100644 --- a/src/zsl/db/model/sql_alchemy.py +++ b/src/zsl/db/model/sql_alchemy.py @@ -6,7 +6,6 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals from sqlalchemy.ext.declarative import declarative_base diff --git a/src/zsl/errors.py b/src/zsl/errors.py index 28f8c655..f0f6c8be 100644 --- a/src/zsl/errors.py +++ b/src/zsl/errors.py @@ -12,7 +12,7 @@ class ZslError(Exception): ERROR_CONFIG_NAME = 'ERRORS' -class ErrorHandler(object): +class ErrorHandler: """ Custom error handler providing a response on a particular error. """ @@ -41,7 +41,7 @@ def handle(self, e): pass -class ErrorProcessor(object): +class ErrorProcessor: """ Custom error processor handling an error state caused by an error. For example an error processor may only log an error, send an email, etc. The main difference between an `ErrorProcessor` and an `ErrorHandler` is that the latter @@ -56,7 +56,7 @@ def handle(self, e): pass -class ErrorConfiguration(object): +class ErrorConfiguration: """ The configuration object for error handling. """ diff --git a/src/zsl/interface/celery/worker.py b/src/zsl/interface/celery/worker.py index 0f6f3f44..86eaf34f 100644 --- a/src/zsl/interface/celery/worker.py +++ b/src/zsl/interface/celery/worker.py @@ -60,7 +60,7 @@ class CeleryTaskQueueMainWorker(CeleryTaskQueueWorkerBase): """Worker implementation for Celery task queue.""" def __init__(self, ): - super(CeleryTaskQueueMainWorker, self).__init__() + super().__init__() self.celery_app = create_celery_app() self.celery_worker = None diff --git a/src/zsl/interface/cli.py b/src/zsl/interface/cli.py index 5d6df58c..8d486ac5 100644 --- a/src/zsl/interface/cli.py +++ b/src/zsl/interface/cli.py @@ -7,9 +7,7 @@ The module is responsible for creating the CLI interface of ZSL and provides the main `click` group for all the CLI commands and groups. If any command is to be added add it to this group `cli` defined in this module. """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import sys import click diff --git a/src/zsl/interface/gearman/json_data_encoder.py b/src/zsl/interface/gearman/json_data_encoder.py index b42a6480..44f4a08b 100644 --- a/src/zsl/interface/gearman/json_data_encoder.py +++ b/src/zsl/interface/gearman/json_data_encoder.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals import json diff --git a/src/zsl/interface/gearman/task_filler.py b/src/zsl/interface/gearman/task_filler.py index 3ef6edd5..ca250987 100644 --- a/src/zsl/interface/gearman/task_filler.py +++ b/src/zsl/interface/gearman/task_filler.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import print_function, unicode_literals import json @@ -15,7 +14,7 @@ @inject def exec_task_filler(task_path: str, json_data: str, config: Config) -> None: print("Initializing client.") - gm_client = gearman.GearmanClient(["{0}:{1}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) + gm_client = gearman.GearmanClient(["{}:{}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) print("Client initialized.") if json_data.startswith("file://"): @@ -25,6 +24,6 @@ def exec_task_filler(task_path: str, json_data: str, config: Config) -> None: ret_val = gm_client.submit_job(config['GEARMAN_TASK_NAME'], json.dumps( {'path': task_path, 'data': json.loads(json_data)})) - print("Returned value '{0}'.".format(ret_val)) + print("Returned value '{}'.".format(ret_val)) output = json.loads(ret_val.result)['data'] - print("Returned data '{0}' of type '{1}'.".format(output, type(output))) + print("Returned data '{}' of type '{}'.".format(output, type(output))) diff --git a/src/zsl/interface/gearman/worker.py b/src/zsl/interface/gearman/worker.py index 1866a336..b570604a 100644 --- a/src/zsl/interface/gearman/worker.py +++ b/src/zsl/interface/gearman/worker.py @@ -2,9 +2,8 @@ :mod:`zsl.interface.gearman.worker` ----------------------------------- -.. moduleauthor:: Martin +.. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from zsl.gearman import gearman from zsl.interface.gearman.json_data_encoder import JSONDataEncoder @@ -14,11 +13,11 @@ class ReloadingWorker(gearman.GearmanWorker): def __init__(self, host_list=None): - super(ReloadingWorker, self).__init__(host_list) + super().__init__(host_list) self._should_stop = False def on_job_complete(self, current_job, job_result): - super(ReloadingWorker, self).on_job_complete(current_job, job_result) + super().on_job_complete(current_job, job_result) if self._should_stop: quit() return True @@ -39,11 +38,11 @@ def job_from_gearman_job(gearman_job): class GearmanTaskQueueWorker(TaskQueueWorker): def __init__(self): - super(GearmanTaskQueueWorker, self).__init__() + super().__init__() self.gearman_worker = ReloadingWorker( - ["{0}:{1}".format(self._config['GEARMAN']['host'], - self._config['GEARMAN']['port'])]) + ["{}:{}".format(self._config['GEARMAN']['host'], + self._config['GEARMAN']['port'])]) self.gearman_worker.set_client_id(self._get_client_id()) self.gearman_worker.data_encoder = JSONDataEncoder self.gearman_worker.register_task(self._config['GEARMAN_TASK_NAME'], self.execute_gearman_job) diff --git a/src/zsl/interface/resource.py b/src/zsl/interface/resource.py index be1918ec..5915beec 100644 --- a/src/zsl/interface/resource.py +++ b/src/zsl/interface/resource.py @@ -4,9 +4,7 @@ Helper types describing resource results for resource router. """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * from collections import namedtuple result_fields = ['body', 'count', 'links', 'status', 'location', 'expose_headers'] diff --git a/src/zsl/interface/task.py b/src/zsl/interface/task.py index 3073b525..9c2f81dc 100644 --- a/src/zsl/interface/task.py +++ b/src/zsl/interface/task.py @@ -5,9 +5,7 @@ .. moduleauthor:: Peter Morihladko , Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * # NOQA from functools import wraps import json from json.encoder import JSONEncoder @@ -28,7 +26,7 @@ class ModelConversionError(Exception): def __init__(self, obj, attribute): msg = "Can not fit dictionary into model '{0}' since the model " \ "does not have attribute '{1}'" - super(ModelConversionError, self).__init__(msg.format(obj, attribute)) + super().__init__(msg.format(obj, attribute)) self._obj = obj self._attribute = attribute @@ -146,6 +144,6 @@ def create_task(task_path: str, task_router: TaskRouter): task_callable = task.perform else: raise ZslError( - "Can not create task with path '{0}'.".format(task_path)) + "Can not create task with path '{}'.".format(task_path)) return task, task_callable diff --git a/src/zsl/interface/task_queue.py b/src/zsl/interface/task_queue.py index 9fc2c8d2..0d03237f 100644 --- a/src/zsl/interface/task_queue.py +++ b/src/zsl/interface/task_queue.py @@ -36,7 +36,7 @@ def execute_job(job: Job, app: Zsl, task_router: TaskRouter) -> Dict: :rtype: dict """ - app.logger.info("Job fetched, preparing the task '{0}'.".format(job.path)) + app.logger.info("Job fetched, preparing the task '{}'.".format(job.path)) task, task_callable = task_router.route(job.path) jc = JobContext(job, task, task_callable) @@ -44,7 +44,7 @@ def execute_job(job: Job, app: Zsl, task_router: TaskRouter) -> Dict: app.logger.info("Executing task.") result = jc.task_callable(jc.task_data) - app.logger.info("Task {0} executed successfully.".format(job.path)) + app.logger.info("Task {} executed successfully.".format(job.path)) return {'task_name': job.path, 'data': result} @@ -71,7 +71,7 @@ def _get_client_id(): :return: client id :rtype: str """ - return "zsl-client-{0}".format(socket.gethostname()) + return "zsl-client-{}".format(socket.gethostname()) def handle_exception(self, e, task_path): # type: (Exception, str) -> dict diff --git a/src/zsl/interface/web/importer.py b/src/zsl/interface/web/importer.py index fdbf15e1..020b4e45 100644 --- a/src/zsl/interface/web/importer.py +++ b/src/zsl/interface/web/importer.py @@ -2,7 +2,6 @@ :mod:`zsl.interface.webservice.importer` ---------------------------------------- """ -from __future__ import unicode_literals import os import sys diff --git a/src/zsl/interface/web/performers/default.py b/src/zsl/interface/web/performers/default.py index 5db0501f..49442399 100644 --- a/src/zsl/interface/web/performers/default.py +++ b/src/zsl/interface/web/performers/default.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from flask.globals import request diff --git a/src/zsl/interface/web/performers/method.py b/src/zsl/interface/web/performers/method.py index 5bac947c..63ed1969 100644 --- a/src/zsl/interface/web/performers/method.py +++ b/src/zsl/interface/web/performers/method.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from importlib import import_module import logging diff --git a/src/zsl/interface/web/performers/resource.py b/src/zsl/interface/web/performers/resource.py index 4f8f0b54..dc5f9954 100644 --- a/src/zsl/interface/web/performers/resource.py +++ b/src/zsl/interface/web/performers/resource.py @@ -2,9 +2,7 @@ :mod:`zsl.interface.webservice.performers.resource` --------------------------------------------------- """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import json import logging @@ -41,7 +39,7 @@ def perform_resource(path): (resource, params) = parse_resource_path(path) resource_task = get_resource_task(resource) if resource_task is None: - raise ImportError("No resource named '{0}'.".format(resource)) + raise ImportError("No resource named '{}'.".format(resource)) msg_format = "Fetched resource named '{0}' with data\n{1}." logging.debug(msg_format.format(resource, request.data)) diff --git a/src/zsl/interface/web/performers/task.py b/src/zsl/interface/web/performers/task.py index 17fc2feb..b402ed89 100644 --- a/src/zsl/interface/web/performers/task.py +++ b/src/zsl/interface/web/performers/task.py @@ -2,9 +2,7 @@ :mod:`zsl.interface.webservice.performers.task` ----------------------------------------------- """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * # NOQA from functools import partial import logging @@ -39,8 +37,8 @@ def create_task_mapping(app: Zsl, task_configuration: TaskConfiguration) -> None namespace = namespace_configuration.namespace.rstrip('/') f = partial(perform_web_task, namespace) - name = "perform-web-task-{0}".format(namespace) + name = "perform-web-task-{}".format(namespace) f.__name__ = name - logging.getLogger(__name__).debug("Registering {0} at /{1}".format(name, namespace)) - app.add_url_rule("/{0}/".format(namespace), name, f, methods=["POST", "GET", "OPTIONS"]) + logging.getLogger(__name__).debug("Registering {} at /{}".format(name, namespace)) + app.add_url_rule("/{}/".format(namespace), name, f, methods=["POST", "GET", "OPTIONS"]) diff --git a/src/zsl/interface/web/utils/execution.py b/src/zsl/interface/web/utils/execution.py index 4bf2d5ed..3099d4ab 100644 --- a/src/zsl/interface/web/utils/execution.py +++ b/src/zsl/interface/web/utils/execution.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * # NOQA from functools import wraps from typing import Any, Callable diff --git a/src/zsl/interface/web/utils/request_data.py b/src/zsl/interface/web/utils/request_data.py index c13aac6a..d7f893e0 100644 --- a/src/zsl/interface/web/utils/request_data.py +++ b/src/zsl/interface/web/utils/request_data.py @@ -1,8 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * - - def extract_data(request): data = request.data json_data = request.get_json() diff --git a/src/zsl/resource/guard.py b/src/zsl/resource/guard.py index a53c3c9c..f0251837 100644 --- a/src/zsl/resource/guard.py +++ b/src/zsl/resource/guard.py @@ -7,9 +7,7 @@ policy class a complex security resource behaviour can be achieved. """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * from enum import Enum from functools import wraps import http.client @@ -30,7 +28,7 @@ class Access(Enum): CONTINUE = 3 -class ResourcePolicy(object): +class ResourcePolicy: """Declarative policy class. Every CRUD method has is corespondent *can_method__before* and @@ -127,10 +125,10 @@ class PolicyViolationError(Exception): def __init__(self, message, code=_HTTP_STATUS_FORBIDDEN): self.code = code - super(PolicyViolationError, self).__init__(message) + super().__init__(message) -class GuardedMixin(object): +class GuardedMixin: """Add guarded CRUD methods to resource. The ``guard`` replaces the CRUD guarded methods with a wrapper with @@ -169,7 +167,7 @@ def default_error_handler(e, *_): ) -class guard(object): +class guard: """Guard decorator. This decorator wraps the CRUD methods with security checks before and @@ -373,7 +371,7 @@ class _TransactionalPolicyViolationError(PolicyViolationError): def __init__(self, result): # type: (ResourceResult) -> None self.result = result - super(_TransactionalPolicyViolationError, self).__init__( + super().__init__( result.body, result.status ) diff --git a/src/zsl/resource/json_server_resource.py b/src/zsl/resource/json_server_resource.py index 925f97ab..6ba741e6 100644 --- a/src/zsl/resource/json_server_resource.py +++ b/src/zsl/resource/json_server_resource.py @@ -2,9 +2,7 @@ :mod:`zsl.resource.json_server_resource` ---------------------------------------- """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import http.client import logging import re @@ -32,7 +30,7 @@ ) # any other arguments from these are considered as `property_name(_operator)=some_vaule` filter -_SKIPPED_ARGUMENTS = set(['callback', '_', 'q', '_start', '_end', '_sort', '_order', '_limit', '_embed', '_expand']) +_SKIPPED_ARGUMENTS = {'callback', '_', 'q', '_start', '_end', '_sort', '_order', '_limit', '_embed', '_expand'} # first group is the column name, then it can have a . separator or an operator suffix _re_column_name = re.compile(r'^([^.]*?)(\..*?)?(_lte|_gte|_ne|_like)?$') @@ -84,7 +82,7 @@ def to_filter(self, query, arg): def create(self, *args, **kwargs): """Adds created http status response and location link.""" - resource = super(JsonServerResource, self).create(*args, **kwargs) + resource = super().create(*args, **kwargs) return ResourceResult( body=resource, @@ -223,7 +221,7 @@ def read(self, params, args, data): try: return ResourceResult( - body=super(JsonServerResource, self).read(params, args, data), + body=super().read(params, args, data), count=result_count, links=result_links ) @@ -239,7 +237,7 @@ def update(self, *args, **kwargs): if request.method == 'PUT': logging.warning("Called not implemented resource method PUT") - resource = super(JsonServerResource, self).update(*args, **kwargs) + resource = super().update(*args, **kwargs) if resource: return resource diff --git a/src/zsl/resource/model_resource.py b/src/zsl/resource/model_resource.py index 43f2ade3..f8dfe1bf 100644 --- a/src/zsl/resource/model_resource.py +++ b/src/zsl/resource/model_resource.py @@ -81,7 +81,7 @@ def _is_list(list_): return False -class ResourceQueryContext(object): +class ResourceQueryContext: """ The context of the resource query. - holds the parameters and arguments of the query, @@ -153,7 +153,7 @@ def __init__(self, model_cls=None): Create Model CRUD resource for ``model_cls`` """ - super(ModelResourceBase, self).__init__() + super().__init__() if not model_cls: self.model_cls = self.__model__ @@ -467,7 +467,7 @@ class CachedModelResource(ModelResource): def __init__(self, model_cls: Type, cache_module: CacheModule, id_helper: IdHelper, logger: logging.Logger, timeout: str = 'short'): - super(CachedModelResource, self).__init__(model_cls) + super().__init__(model_cls) self._cache_module = cache_module self._id_helper = id_helper self._logger = logger @@ -475,7 +475,7 @@ def __init__(self, model_cls: Type, cache_module: CacheModule, def _create_key(self, arghash): key_prefix = create_key_class_prefix(self.model_cls) - return "cached-resource:{0}:{1}".format(key_prefix, arghash) + return "cached-resource:{}:{}".format(key_prefix, arghash) def _create_key_from_context(self, ctx): arghash = sha256(json.dumps({'params': ctx.params, 'args': ctx.args, 'data': ctx.data})).hexdigest() @@ -484,13 +484,13 @@ def _create_key_from_context(self, ctx): def _get_one(self, row_id, ctx): # Make hash of params, args and data and ache using the hash in the key. key = self._create_key_from_context(ctx) - self._logger.debug("CachedModelResource - get one, key: {0}.".format(key)) + self._logger.debug("CachedModelResource - get one, key: {}.".format(key)) if self._id_helper.check_key(key): result = json.loads(self._id_helper.get_key(key)) else: self._logger.debug("CachedModelResource - get one not cached, transferring to resource...") - result = super(CachedModelResource, self)._get_one(row_id, ctx) + result = super()._get_one(row_id, ctx) # serialize as model self._id_helper.set_key(key, app_model_encoder_fn(result), self._timeout) @@ -501,13 +501,13 @@ def _get_one(self, row_id, ctx): def _get_collection_count(self, ctx): # Make hash of params, args and data and ache using the hash in the key. key = self._create_key_from_context(ctx) - self._logger.debug("CachedModelResource - get one, key: {0}.".format(key)) + self._logger.debug("CachedModelResource - get one, key: {}.".format(key)) if self._id_helper.check_key(key): result = int(self._id_helper.get_key(key)) else: self._logger.debug("CachedModelResource - get one not cached, transferring to resource...") - result = super(CachedModelResource, self)._get_collection_count(ctx) + result = super()._get_collection_count(ctx) self._id_helper.set_key(key, app_model_encoder_fn(result), self._timeout) return result @@ -515,13 +515,13 @@ def _get_collection_count(self, ctx): def _get_collection(self, ctx): # Make hash of params, args and data and ache using the hash in the key. key = self._create_key_from_context(ctx) - self._logger.debug("CachedModelResource - collection, key: {0}.".format(key)) + self._logger.debug("CachedModelResource - collection, key: {}.".format(key)) if self._id_helper.check_key(key): result = self._id_helper.gather_page(key, app_model_decoder_fn) else: self._logger.debug("CachedModelResource - collection not cached, transferring to resource...") - result = super(CachedModelResource, self)._get_collection(ctx) + result = super()._get_collection(ctx) self._id_helper.fill_page(key, result, self._timeout, app_model_encoder_fn) return result @@ -552,8 +552,8 @@ def delete(self, params, args, data): class ReadOnlyResourceUpdateOperationException(Exception): def __init__(self, operation): self._operation = operation - super(ReadOnlyResourceUpdateOperationException, self).__init__( - "Can not perform operation '{0}' on ReadOnlyResource.".format(operation)) + super().__init__( + "Can not perform operation '{}' on ReadOnlyResource.".format(operation)) def get_operation(self): return self._operation @@ -561,7 +561,7 @@ def get_operation(self): operation = property(get_operation) -class ReadOnlyResourceMixin(object): +class ReadOnlyResourceMixin: """ The mixin to be used to forbid the update/delete and create operations. Remember the Python's MRO and place this mixin at the right place in the inheritance declaration. diff --git a/src/zsl/resource/resource_helper.py b/src/zsl/resource/resource_helper.py index 1bcd549e..33225bf5 100644 --- a/src/zsl/resource/resource_helper.py +++ b/src/zsl/resource/resource_helper.py @@ -4,9 +4,7 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * from future.utils import viewitems, viewvalues from sqlalchemy import and_, asc, desc, or_ @@ -45,7 +43,7 @@ def filter_from_url_arg(model_cls, query, arg, query_operator=and_, break if operator is None: - raise Exception('No operator in expression "{0}".'.format(expr)) + raise Exception('No operator in expression "{}".'.format(expr)) (column_names, value) = expr.split(operator) @@ -67,7 +65,7 @@ def filter_from_url_arg(model_cls, query, arg, query_operator=and_, column = getattr(e_model_cls, column_name) exprs.append(getattr(column, method)(typed_value)) else: - raise Exception('Invalid property {0} in class {1}.'.format(column_name, e_model_cls)) + raise Exception('Invalid property {} in class {}.'.format(column_name, e_model_cls)) exprs = _join_equal_columns_to_or(exprs) return query.join(*joins).filter(query_operator(*exprs)) @@ -140,7 +138,7 @@ def order_from_url_arg(model_cls, query, arg): order_by = asc(column) if direction == 'asc' else desc(column) orderings.append(order_by) else: - raise Exception('Invalid property {0} in class {1}.'.format(column_name, model_cls)) + raise Exception('Invalid property {} in class {}.'.format(column_name, model_cls)) return query.join(*joins).order_by(*orderings) diff --git a/src/zsl/router/method.py b/src/zsl/router/method.py index 2b480eb0..9b6fc0ba 100644 --- a/src/zsl/router/method.py +++ b/src/zsl/router/method.py @@ -44,7 +44,7 @@ def set_default_responder(responder): _default_responder_method = default_web_responder -class Performer(object): +class Performer: def __init__(self, f): global _default_responder_method self._f = f @@ -81,9 +81,9 @@ def _get_method_configuration(config): def route(path: str, app: Zsl, config: Config, **options: Any) -> Callable: def _decorator(f): method_config = _get_method_configuration(config) - url = "/{0}{1}".format(method_config.url_prefix, path) + url = "/{}{}".format(method_config.url_prefix, path) logging.getLogger(__name__).info( - "Mapping url '{0}' as a method.".format(url)) + "Mapping url '{}' as a method.".format(url)) routed_function = app.route(url, **options) return routed_function(Performer(f)) diff --git a/src/zsl/router/task.py b/src/zsl/router/task.py index fd76026f..073e7c54 100644 --- a/src/zsl/router/task.py +++ b/src/zsl/router/task.py @@ -2,10 +2,8 @@ :mod:`zsl.router.task` ------------------------ """ -from __future__ import absolute_import, division, print_function, unicode_literals from abc import ABCMeta -from builtins import * import importlib import logging from typing import Any, Callable, Dict, List, Tuple @@ -18,7 +16,7 @@ TASK_CONFIGURATION_NAME = 'TASKS' -class TaskNamespace(object): +class TaskNamespace: def __init__(self, namespace: str, task_configuration: 'TaskConfiguration') -> None: self._task_packages = [] self._routes = {} @@ -65,7 +63,7 @@ def namespace(self): return self._namespace -class TaskConfiguration(object): +class TaskConfiguration: def __init__(self): self._namespaces = [] # type: List[TaskNamespace] @@ -82,8 +80,8 @@ def namespaces(self): class RoutingError(ZslError): def __init__(self, path): - msg = "Can not find task at path '{0}'.".format(path) - super(RoutingError, self).__init__(msg) + msg = "Can not find task at path '{}'.".format(path) + super().__init__(msg) self._path = path @property @@ -91,7 +89,7 @@ def path(self): return self._path -class RouterStrategy(object): +class RouterStrategy: __metaclass__ = ABCMeta def can_route(self, path): @@ -186,7 +184,7 @@ def _find_task_in_namespace(self, task_packages, package_path, class_name): exceptions = [] module_ = None for task_package in task_packages: - module_name = "{0}.{1}".format(task_package, ".".join(package_path)) + module_name = "{}.{}".format(task_package, ".".join(package_path)) try: logger.debug("Trying to load module with name '%s' and class name '%s'.", @@ -203,7 +201,7 @@ def _find_task_in_namespace(self, task_packages, package_path, class_name): return module_, exceptions -class TaskRouter(object): +class TaskRouter: @inject def __init__(self, config: Config, task_configuration: TaskConfiguration) -> None: self._config = config @@ -239,5 +237,5 @@ def _create_result(self, cls): :return: """ task = instantiate(cls) - logging.getLogger(__name__).debug("Task object {0} created [{1}].".format(cls.__name__, task)) + logging.getLogger(__name__).debug("Task object {} created [{}].".format(cls.__name__, task)) return task, get_callable(task) diff --git a/src/zsl/service/__init__.py b/src/zsl/service/__init__.py index ef8b22a4..73860e4d 100644 --- a/src/zsl/service/__init__.py +++ b/src/zsl/service/__init__.py @@ -1,3 +1 @@ -from __future__ import unicode_literals - from .service import Service, transactional diff --git a/src/zsl/service/service.py b/src/zsl/service/service.py index 2e9ae7b8..ed5fd07f 100644 --- a/src/zsl/service/service.py +++ b/src/zsl/service/service.py @@ -5,9 +5,7 @@ .. moduleauthor:: Martin Babka , Peter Morihladko """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * # NOQA from functools import wraps import logging @@ -20,7 +18,7 @@ _EMPTY_TX_HOLDER = EmptyTransactionalHolder() -class TransactionalSupportMixin(object): +class TransactionalSupportMixin: """This mixin allows the objects to access transactional holder.""" @property @@ -37,7 +35,7 @@ class Service(TransactionalSupportMixin): @inject def __init__(self, app: Zsl, engine: Engine) -> None: """Constructor - initializes and injects the needed libraries.""" - super(Service, self).__init__() + super().__init__() self._app = app self._engine = engine diff --git a/src/zsl/task/__init__.py b/src/zsl/task/__init__.py index edc5fdfa..bb110714 100644 --- a/src/zsl/task/__init__.py +++ b/src/zsl/task/__init__.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - from wsgiref.handlers import CGIHandler from flask import Flask diff --git a/src/zsl/task/job_context.py b/src/zsl/task/job_context.py index f2fcd6b4..ab69ef7b 100644 --- a/src/zsl/task/job_context.py +++ b/src/zsl/task/job_context.py @@ -4,10 +4,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals from abc import abstractmethod -from builtins import * import contextlib from threading import current_thread from typing import Any, Callable, Dict @@ -18,7 +16,7 @@ from zsl.utils.reflection_helper import proxy_object_to_delegate -class Job(object): +class Job: def __init__(self, data): # type: (dict) -> None @@ -52,7 +50,7 @@ def is_valid(self): return self.data and 'path' in self.data and 'data' in self.data -class JobContext(object): +class JobContext: """Job Context""" def __init__(self, job, task, task_callable): @@ -92,7 +90,7 @@ def _set_current_context(cls, context): current_thread()._current_job_context = context -class Responder(object): +class Responder: @abstractmethod def respond(self, r): pass @@ -127,7 +125,7 @@ def create_job(path, data): class WebJobContext(JobContext): def __init__(self, path, data, task, task_callable, request): """Constructor""" - super(WebJobContext, self).__init__(create_job(path, data), task, task_callable) + super().__init__(create_job(path, data), task, task_callable) self._request = request self._responders = [] @@ -148,7 +146,7 @@ def notify_responders(self, response): class DelegatingJobContext(JobContext): def __init__(self, job, task, task_callable): wrapped_job_context = JobContext.get_current_context() - super(DelegatingJobContext, self).__init__(job, task, task_callable) + super().__init__(job, task, task_callable) self._wrapped_job_context = wrapped_job_context proxy_object_to_delegate(self, wrapped_job_context) diff --git a/src/zsl/task/task_data.py b/src/zsl/task/task_data.py index 8d68958d..d3650fd3 100644 --- a/src/zsl/task/task_data.py +++ b/src/zsl/task/task_data.py @@ -12,7 +12,7 @@ from zsl.utils.warnings import deprecated -class TaskData(object): +class TaskData: @inject @noninjectable('payload_type') diff --git a/src/zsl/task/task_decorator.py b/src/zsl/task/task_decorator.py index 7f0f552d..184bc8a1 100644 --- a/src/zsl/task/task_decorator.py +++ b/src/zsl/task/task_decorator.py @@ -143,7 +143,7 @@ def jsonp_output_decorator(*args, **kwargs): if callback_key not in data: raise KeyError( - 'Missing required parameter "{0}" for task.'.format( + 'Missing required parameter "{}" for task.'.format( callback_key)) callback = data[callback_key] @@ -238,7 +238,7 @@ def error_and_result_decorator_inner_fn(f, web_only, *args, **kwargs): except: # NOQA exc = traceback.format_exc() logging.error(exc) - rv = {'error': "{0}".format(exc)} + rv = {'error': "{}".format(exc)} return json.dumps(rv) @@ -257,7 +257,7 @@ def required_data_decorator(*args, **kwargs): for i in data: if i not in task_data: raise KeyError( - 'Missing required parameter "{0}" for task.'.format(i)) + 'Missing required parameter "{}" for task.'.format(i)) return f(*args, **kwargs) @@ -387,7 +387,7 @@ def _get_data_from_args(args): class SecurityException(Exception): def __init__(self, hashed_token): Exception.__init__(self, - "Invalid hashed token '{0}'.".format(hashed_token)) + "Invalid hashed token '{}'.".format(hashed_token)) self._hashed_token = hashed_token def get_hashed_token(self): diff --git a/src/zsl/task/task_status.py b/src/zsl/task/task_status.py index ccd07973..03a3babd 100644 --- a/src/zsl/task/task_status.py +++ b/src/zsl/task/task_status.py @@ -4,7 +4,6 @@ Status codes for REST services """ -from __future__ import unicode_literals OK = {'status': 'ok'} diff --git a/src/zsl/tasks/zsl/cors_test_task.py b/src/zsl/tasks/zsl/cors_test_task.py index b17fcfbb..0f2016d6 100644 --- a/src/zsl/tasks/zsl/cors_test_task.py +++ b/src/zsl/tasks/zsl/cors_test_task.py @@ -6,9 +6,7 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object from injector import inject @@ -16,7 +14,7 @@ from zsl.task.task_decorator import crossdomain, json_input, json_output -class CorsTestTask(object): +class CorsTestTask: @inject def __init__(self, app: Zsl) -> None: diff --git a/src/zsl/tasks/zsl/db_test_task.py b/src/zsl/tasks/zsl/db_test_task.py index c6902a25..0fa73b1a 100644 --- a/src/zsl/tasks/zsl/db_test_task.py +++ b/src/zsl/tasks/zsl/db_test_task.py @@ -13,7 +13,7 @@ from zsl import Zsl -class DbTestTask(object): +class DbTestTask: """ Connects to a database and executes a simple query. The result of the query should be 6. @@ -30,7 +30,7 @@ class DbTestTask(object): def __init__(self, db: sqlalchemy.engine.Engine, app: Zsl) -> None: self._db = db self._app = app - self._app.logger.debug("Call from DbTestTesk.__init__, db {0}".format(db)) + self._app.logger.debug("Call from DbTestTesk.__init__, db {}".format(db)) def perform(self, data): - return Response("{0}".format(self._db.execute("select 1 * 2 * 3").scalar()), mimetype='text/plain') + return Response("{}".format(self._db.execute("select 1 * 2 * 3").scalar()), mimetype='text/plain') diff --git a/src/zsl/tasks/zsl/kill_worker_task.py b/src/zsl/tasks/zsl/kill_worker_task.py index 0f77270d..d8e74353 100644 --- a/src/zsl/tasks/zsl/kill_worker_task.py +++ b/src/zsl/tasks/zsl/kill_worker_task.py @@ -6,9 +6,7 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object from injector import inject @@ -17,7 +15,7 @@ from zsl.task.job_context import JobContext, WebJobContext -class KillWorkerTask(object): +class KillWorkerTask: @inject def __init__(self, app: Zsl) -> None: self._app = app diff --git a/src/zsl/tasks/zsl/schedule_celery_task.py b/src/zsl/tasks/zsl/schedule_celery_task.py index 95c77102..711a6acf 100644 --- a/src/zsl/tasks/zsl/schedule_celery_task.py +++ b/src/zsl/tasks/zsl/schedule_celery_task.py @@ -2,9 +2,7 @@ :mod:`zsl.tasks.asl.schedule_gearman_task` ------------------------------------------ """ -from __future__ import unicode_literals -from builtins import object from zsl.interface.celery.worker import create_celery_app, zsl_task from zsl.task.task_data import TaskData @@ -16,7 +14,7 @@ def schedule_celery_task(path, data): zsl_task.delay({"path": path, "data": data}) -class ScheduleCeleryTask(object): +class ScheduleCeleryTask: @json_input def perform(self, data): # type: (TaskData)->str diff --git a/src/zsl/tasks/zsl/schedule_gearman_task.py b/src/zsl/tasks/zsl/schedule_gearman_task.py index 6dd18f29..c583d38a 100644 --- a/src/zsl/tasks/zsl/schedule_gearman_task.py +++ b/src/zsl/tasks/zsl/schedule_gearman_task.py @@ -2,15 +2,13 @@ :mod:`zsl.tasks.asl.schedule_gearman_task` ------------------------------------------ """ -from __future__ import unicode_literals -from builtins import object from zsl.task.task_data import TaskData from zsl.utils.gearman_helper import schedule_gearman_task -class ScheduleGearmanTask(object): +class ScheduleGearmanTask: def perform(self, data): # type: (TaskData)->str diff --git a/src/zsl/tasks/zsl/schedule_kill_worker_task.py b/src/zsl/tasks/zsl/schedule_kill_worker_task.py index b3707f72..8db2dd92 100644 --- a/src/zsl/tasks/zsl/schedule_kill_worker_task.py +++ b/src/zsl/tasks/zsl/schedule_kill_worker_task.py @@ -2,14 +2,12 @@ :mod:`zsl.tasks.asl.schedule_kill_worker_task` ---------------------------------------------- """ -from __future__ import unicode_literals -from builtins import object from zsl.utils.gearman_helper import schedule_gearman_task -class ScheduleKillWorkerTask(object): +class ScheduleKillWorkerTask: def perform(self, _): # Create gearman. schedule_gearman_task('zsl/kill_worker_task', {}) diff --git a/src/zsl/tasks/zsl/sum_task.py b/src/zsl/tasks/zsl/sum_task.py index 7f53e551..cb936272 100644 --- a/src/zsl/tasks/zsl/sum_task.py +++ b/src/zsl/tasks/zsl/sum_task.py @@ -6,9 +6,7 @@ ..moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object from injector import inject @@ -17,7 +15,7 @@ from zsl.task.task_decorator import json_input, json_output -class SumTask(object): +class SumTask: @inject def __init__(self, app: Zsl) -> None: self._app = app @@ -27,5 +25,5 @@ def __init__(self, app: Zsl) -> None: def perform(self, data): # type: (TaskData)->str payload = data.payload - self._app.logger.debug("Sum task with data '{0}'.".format(payload)) + self._app.logger.debug("Sum task with data '{}'.".format(payload)) return {"input": payload, "result": sum(payload)} diff --git a/src/zsl/tasks/zsl/test_task.py b/src/zsl/tasks/zsl/test_task.py index efa34f6f..00ae29d6 100644 --- a/src/zsl/tasks/zsl/test_task.py +++ b/src/zsl/tasks/zsl/test_task.py @@ -6,13 +6,11 @@ ..moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import logging -class TestTask(object): +class TestTask: def perform(self, _data): logging.getLogger(__name__).debug("Running zsl.tasks.zsl.TestTask") return "ok" diff --git a/src/zsl/tasks/zsl/version_task.py b/src/zsl/tasks/zsl/version_task.py index f898960e..9727494e 100644 --- a/src/zsl/tasks/zsl/version_task.py +++ b/src/zsl/tasks/zsl/version_task.py @@ -6,9 +6,7 @@ ..moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import object import sqlalchemy @@ -16,7 +14,7 @@ from zsl.task.task_decorator import json_output -class VersionTask(object): +class VersionTask: """ Shows the versions of ASL and the various used libraries. """ diff --git a/src/zsl/tasks/zsl/with_request_task.py b/src/zsl/tasks/zsl/with_request_task.py index 4e188b64..cab8956a 100644 --- a/src/zsl/tasks/zsl/with_request_task.py +++ b/src/zsl/tasks/zsl/with_request_task.py @@ -6,16 +6,14 @@ ..moduleauthor:: Julius Flimmel """ -from __future__ import unicode_literals -from builtins import object from zsl import Zsl, inject from zsl.interface.task import payload_into_model from zsl.task.task_decorator import json_input, json_output -class WithRequestTask(object): +class WithRequestTask: class Request: def __init__(self): diff --git a/src/zsl/testing/__init__.py b/src/zsl/testing/__init__.py index cdd13840..c39d3189 100644 --- a/src/zsl/testing/__init__.py +++ b/src/zsl/testing/__init__.py @@ -2,4 +2,3 @@ :mod:`zsl.testing` ------------------ """ -from __future__ import absolute_import, unicode_literals diff --git a/src/zsl/testing/db.py b/src/zsl/testing/db.py index 83fbf84d..5981f299 100644 --- a/src/zsl/testing/db.py +++ b/src/zsl/testing/db.py @@ -99,7 +99,7 @@ def provide_transaction_holder_factory(self) -> TransactionHolderFactory: return TestTransactionHolderFactory() -class DbTestCase(object): +class DbTestCase: """:class:`.DbTestCase` is a mixin to be used when testing with a database.""" @@ -107,7 +107,7 @@ class DbTestCase(object): @zsl_inject def setUp(self, session_factory: TestSessionFactory) -> None: - super(DbTestCase, self).setUp() + super().setUp() logging.getLogger(__name__).debug("DbTestCase.setUp") session_factory.create_test_session() @@ -117,7 +117,7 @@ def tearDown(self, session_factory: TestSessionFactory) -> None: # as the one used in setUp. logging.getLogger(__name__).debug("DbTestCase.tearDown") session_factory.close_test_session() - super(DbTestCase, self).tearDown() + super().tearDown() IN_MEMORY_DB_SETTINGS = { diff --git a/src/zsl/testing/http.py b/src/zsl/testing/http.py index d2b3f214..249d5f83 100644 --- a/src/zsl/testing/http.py +++ b/src/zsl/testing/http.py @@ -3,9 +3,7 @@ ----------------------- This module allows for querying the API via HTTP. """ -from __future__ import absolute_import, unicode_literals -from builtins import * from http import HTTPStatus import json from typing import Any, AnyStr, Dict, Union @@ -27,7 +25,7 @@ def json_loads(str_): return json.loads(str_) -class HTTPTestCase(object): +class HTTPTestCase: """Extends TestCase with methods for easier testing of HTTP requests.""" _DEFAULT_REQUEST_TASK_HEADERS = { diff --git a/src/zsl/testing/test_utils.py b/src/zsl/testing/test_utils.py index b95eff57..7ddb5d02 100644 --- a/src/zsl/testing/test_utils.py +++ b/src/zsl/testing/test_utils.py @@ -3,9 +3,7 @@ ----------------------------- Test utilities. """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import json from typing import Any @@ -32,4 +30,4 @@ class TestTaskData(TaskData): def __init__(self, payload): # type: (Any)->None - super(TestTaskData, self).__init__(json.dumps(payload)) + super().__init__(json.dumps(payload)) diff --git a/src/zsl/testing/zsl.py b/src/zsl/testing/zsl.py index 4595a49a..36a3c52f 100644 --- a/src/zsl/testing/zsl.py +++ b/src/zsl/testing/zsl.py @@ -6,9 +6,7 @@ while testing. Refer to unit testing section :ref:`unit-testing-zsl-instance` for an example. """ -from __future__ import absolute_import, unicode_literals -from builtins import * from collections import namedtuple from functools import partial import logging @@ -27,7 +25,7 @@ profile=None) -class ZslTestCase(object): +class ZslTestCase: ZSL_TEST_CONFIGURATION = None # type: ZslTestConfiguration @classmethod @@ -50,13 +48,13 @@ def setUpClass(cls): app.debug = True logging.getLogger(config.app_name).debug( - "ZSL test app created {0}.".format(app)) + "ZSL test app created {}.".format(app)) - super(ZslTestCase, cls).setUpClass() + super().setUpClass() @classmethod def tearDownClass(cls): config = cls.ZSL_TEST_CONFIGURATION # type: ZslTestConfiguration logging.getLogger(config.app_name).debug( - "ZSL test app tear down {0}.".format(config.app_name)) + "ZSL test app tear down {}.".format(config.app_name)) set_current_app(None) diff --git a/src/zsl/utils/background_task.py b/src/zsl/utils/background_task.py index 3ab83ff0..c539e2c1 100644 --- a/src/zsl/utils/background_task.py +++ b/src/zsl/utils/background_task.py @@ -2,7 +2,6 @@ :mod:`zsl.utils.background_task` -------------------------------- """ -from __future__ import unicode_literals from functools import wraps from typing import Callable @@ -78,7 +77,7 @@ def get_background_method(obj, params): method = getattr(obj, method_name) if not callable(method) or not hasattr(method, '_background_fn'): - raise Exception('{0} is not a background task'.format(params['method'])) + raise Exception('{} is not a background task'.format(params['method'])) method = getattr(method, '_background_fn') diff --git a/src/zsl/utils/cache_helper.py b/src/zsl/utils/cache_helper.py index a0bdb0c3..d6b0b5f3 100644 --- a/src/zsl/utils/cache_helper.py +++ b/src/zsl/utils/cache_helper.py @@ -4,10 +4,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals import abc -from builtins import object import json import logging @@ -18,7 +16,7 @@ from zsl.task.job_context import JobContext -class CacheDecorator(object): +class CacheDecorator: @inject # TODO: Redo as module @@ -187,9 +185,9 @@ def create_key_for_data(prefix, data, key_params): values = [] for k in key_params: if k in d and type(d[k]) is list: - values.append("{0}:{1}".format(k, " -".join(d[k]))) + values.append("{}:{}".format(k, " -".join(d[k]))) else: value = d[k] if k in d else '' - values.append("{0}:{1}".format(k, value)) + values.append("{}:{}".format(k, value)) - return "{0}-{1}".format(prefix, "-".join(values)) + return "{}-{}".format(prefix, "-".join(values)) diff --git a/src/zsl/utils/command_dispatcher.py b/src/zsl/utils/command_dispatcher.py index acd66419..766f7bf2 100644 --- a/src/zsl/utils/command_dispatcher.py +++ b/src/zsl/utils/command_dispatcher.py @@ -4,13 +4,11 @@ .. moduleauthor:: peter morihladko """ -from __future__ import unicode_literals -from builtins import object import inspect -class CommandDispatcher(object): +class CommandDispatcher: """ A simple class for command dictionary. A command is a function which can take named parameters. diff --git a/src/zsl/utils/date_helper.py b/src/zsl/utils/date_helper.py index 3b50aa77..b304ee31 100644 --- a/src/zsl/utils/date_helper.py +++ b/src/zsl/utils/date_helper.py @@ -1,9 +1,7 @@ -# -*- coding: utf-8 -*- """ :mod:`zsl.utils.date_helper` ---------------------------- """ -from __future__ import unicode_literals from datetime import date, timedelta diff --git a/src/zsl/utils/deploy/apiari_doc_generator.py b/src/zsl/utils/deploy/apiari_doc_generator.py index 5445d59e..ec68e479 100644 --- a/src/zsl/utils/deploy/apiari_doc_generator.py +++ b/src/zsl/utils/deploy/apiari_doc_generator.py @@ -7,7 +7,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, print_function, unicode_literals import importlib import inspect @@ -44,7 +43,7 @@ def _add_doc(self, obj): obj_id = self._get_obj_id(obj) if obj_id in self._done: return - logging.debug('Adding {0}.'.format(obj_id)) + logging.debug('Adding {}.'.format(obj_id)) self._done.add(obj_id) apistr = "" @@ -105,7 +104,7 @@ def docmodule(self, obj, name=None, *args): return for loader, module_name, _ispkg in pkgutil.iter_modules(obj.__path__): - logging.debug("Loading module {0} in {1}.".format(module_name, obj.__path__)) + logging.debug("Loading module {} in {}.".format(module_name, obj.__path__)) try: module = loader.find_module(module_name).load_module(module_name) self.docmodule(module) diff --git a/src/zsl/utils/deploy/integrator.py b/src/zsl/utils/deploy/integrator.py index 415cc1a2..c661ace3 100644 --- a/src/zsl/utils/deploy/integrator.py +++ b/src/zsl/utils/deploy/integrator.py @@ -3,7 +3,6 @@ ---------------------------------- """ -from __future__ import unicode_literals import os import tempfile @@ -17,7 +16,7 @@ def integrate_to_file(what, filename, start_line, end_line): try: with open(filename) as f: lines = f.readlines() - except IOError: + except OSError: lines = [] tmp_file = tempfile.NamedTemporaryFile(delete=False) diff --git a/src/zsl/utils/deploy/js_model_generator.py b/src/zsl/utils/deploy/js_model_generator.py index e3b36a20..1dc28dcf 100644 --- a/src/zsl/utils/deploy/js_model_generator.py +++ b/src/zsl/utils/deploy/js_model_generator.py @@ -4,9 +4,7 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals -from builtins import object, range import hashlib import importlib import json @@ -35,7 +33,7 @@ }}""" -class ModelGenerator(object): +class ModelGenerator: def __init__(self, module, model_prefix="", collection_prefix="", model_fn="Atteq.bb.Model", collection_fn="Atteq.bb.Collection"): self.model_prefix = model_prefix @@ -100,7 +98,7 @@ def generate_model(self, model_name, model_plural=None): fk_table = list(column.foreign_keys)[0].target_fullname.split('.')[0] if fk_table in self.table_to_class: - attrs['foreign_model'] = '%s%s' % (self.model_prefix, self.table_to_class[fk_table]) + attrs['foreign_model'] = '{}{}'.format(self.model_prefix, self.table_to_class[fk_table]) except sqlalchemy.exc.NoReferencedTableError: attrs['type'] = 'Text' @@ -196,7 +194,7 @@ def generate_js_models(module, models, collection_prefix, model_prefix, sys.stderr.write("Integrate is really experimental") if not marker: - marker = hashlib.md5("{0}{1}".format(module, models)).hexdigest() + marker = hashlib.md5("{}{}".format(module, models)).hexdigest() start = "// * -- START AUTOGENERATED %s -- * //\n" % marker end = "// * -- END AUTOGENERATED %s -- * //\n" % marker diff --git a/src/zsl/utils/dict_to_object_conversion.py b/src/zsl/utils/dict_to_object_conversion.py index fb748712..53242ad3 100644 --- a/src/zsl/utils/dict_to_object_conversion.py +++ b/src/zsl/utils/dict_to_object_conversion.py @@ -1,7 +1,4 @@ -from __future__ import unicode_literals - # noinspection PyCompatibility -from builtins import int, str from datetime import date, datetime from typing import Any, Dict, Union diff --git a/src/zsl/utils/documentation.py b/src/zsl/utils/documentation.py index 7d486e44..325a8a05 100644 --- a/src/zsl/utils/documentation.py +++ b/src/zsl/utils/documentation.py @@ -1,11 +1,7 @@ -# -*- coding: utf-8 -*- """ :mod:`zsl.utils.date_helper` ---------------------------- """ -from __future__ import unicode_literals - -from builtins import * def documentation_link(chapter): @@ -17,4 +13,4 @@ def documentation_link(chapter): :param chapter: Chapter name in to which the link points. Use underscores instead of spaces. :return: The absolute link to the documentation. """ - return "http://zsl.readthedocs.io/en/latest/{0}.html".format(chapter) + return "http://zsl.readthedocs.io/en/latest/{}.html".format(chapter) diff --git a/src/zsl/utils/email_helper.py b/src/zsl/utils/email_helper.py index 83e38b7a..727f3027 100644 --- a/src/zsl/utils/email_helper.py +++ b/src/zsl/utils/email_helper.py @@ -3,7 +3,6 @@ ----------------------------- """ -from __future__ import unicode_literals from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText diff --git a/src/zsl/utils/file_helper.py b/src/zsl/utils/file_helper.py index d174775a..450fe497 100644 --- a/src/zsl/utils/file_helper.py +++ b/src/zsl/utils/file_helper.py @@ -4,9 +4,7 @@ .. moduleauthor:: Peter Morihladko , Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals -from builtins import * import errno import os diff --git a/src/zsl/utils/gearman_helper.py b/src/zsl/utils/gearman_helper.py index 2c20acd2..3815595a 100644 --- a/src/zsl/utils/gearman_helper.py +++ b/src/zsl/utils/gearman_helper.py @@ -13,6 +13,6 @@ @inject def schedule_gearman_task(path: str, data: Any, config: Config) -> None: - gm_client = gearman.GearmanClient(["{0}:{1}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) + gm_client = gearman.GearmanClient(["{}:{}".format(config['GEARMAN']['host'], config['GEARMAN']['port'])]) gm_client.submit_job(config['GEARMAN_TASK_NAME'], json.dumps( {'path': path, 'data': json.dumps(data)}), wait_until_complete=False, background=True) diff --git a/src/zsl/utils/http.py b/src/zsl/utils/http.py index b6057a15..be027f91 100644 --- a/src/zsl/utils/http.py +++ b/src/zsl/utils/http.py @@ -1,6 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from http import HTTPStatus from typing import Union diff --git a/src/zsl/utils/import_helper.py b/src/zsl/utils/import_helper.py index 40f79224..bc7bec20 100644 --- a/src/zsl/utils/import_helper.py +++ b/src/zsl/utils/import_helper.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals import importlib diff --git a/src/zsl/utils/model_helper.py b/src/zsl/utils/model_helper.py index c88594de..1164d259 100644 --- a/src/zsl/utils/model_helper.py +++ b/src/zsl/utils/model_helper.py @@ -5,7 +5,6 @@ Helper module for working with models. """ -from __future__ import unicode_literals import logging @@ -29,13 +28,13 @@ def update_model(raw_model, app_model, forbidden_keys=None, inverse=False): if inverse: for k in app_model: - logging.debug("Considering property {0}.".format(k)) + logging.debug("Considering property {}.".format(k)) if (hasattr(raw_model, k)) and (k not in forbidden_keys): - logging.debug("Setting property {0} to value '{1}'.".format(k, app_model[k])) + logging.debug("Setting property {} to value '{}'.".format(k, app_model[k])) setattr(raw_model, k, app_model[k]) else: for k in raw_model.__dict__: - logging.debug("Considering property {0}.".format(k)) + logging.debug("Considering property {}.".format(k)) if (k in app_model) and (k not in forbidden_keys): - logging.debug("Setting property {0} to value '{1}'.".format(k, app_model[k])) + logging.debug("Setting property {} to value '{}'.".format(k, app_model[k])) setattr(raw_model, k, app_model[k]) diff --git a/src/zsl/utils/nginx_push_helper.py b/src/zsl/utils/nginx_push_helper.py index b3360aa8..fba96a40 100644 --- a/src/zsl/utils/nginx_push_helper.py +++ b/src/zsl/utils/nginx_push_helper.py @@ -5,9 +5,7 @@ Helper for nginx push stream module https://github.com/wandenberg/nginx-push-stream-module """ -from __future__ import unicode_literals -from builtins import object import json import requests @@ -15,13 +13,13 @@ from zsl.utils.url_helper import urlencode -class NginxPusher(object): +class NginxPusher: def __init__(self, server_path, channel_prefix=None): self._server_path = server_path self._channel_prefix = (channel_prefix + '.') if channel_prefix is not None else '' def channel_path(self, channel_id): - return '{0}?id={1}{2}'.format(self._server_path, self._channel_prefix, channel_id) + return '{}?id={}{}'.format(self._server_path, self._channel_prefix, channel_id) def push_msg(self, channel_id, msg): """Push ``msg`` for given ``channel_id``. If ``msg`` is not string, it diff --git a/src/zsl/utils/params_helper.py b/src/zsl/utils/params_helper.py index eab032e7..e2289eb4 100644 --- a/src/zsl/utils/params_helper.py +++ b/src/zsl/utils/params_helper.py @@ -6,7 +6,6 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals from functools import reduce import inspect diff --git a/src/zsl/utils/php_helper.py b/src/zsl/utils/php_helper.py index 91ed1c7e..f7a54c84 100644 --- a/src/zsl/utils/php_helper.py +++ b/src/zsl/utils/php_helper.py @@ -6,7 +6,6 @@ Module with functions to help with dealing with code written in PHP. """ -from __future__ import unicode_literals def bool_to_str(boolean): diff --git a/src/zsl/utils/redis_helper.py b/src/zsl/utils/redis_helper.py index 80c01fd0..823fe114 100644 --- a/src/zsl/utils/redis_helper.py +++ b/src/zsl/utils/redis_helper.py @@ -31,7 +31,7 @@ def redis_key(*args): return ':'.join(str(a) for a in args if a is not None) -class Keymaker(object): +class Keymaker: """Keymaker is a class to generate an object to generate Redis keys. :Example: diff --git a/src/zsl/utils/reflection_helper.py b/src/zsl/utils/reflection_helper.py index 7db1baff..ee4254f4 100644 --- a/src/zsl/utils/reflection_helper.py +++ b/src/zsl/utils/reflection_helper.py @@ -6,10 +6,8 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals oldstr = str -from builtins import * # NOQA def extend(instance, new_class): @@ -19,7 +17,7 @@ def extend(instance, new_class): :param new_class: Ancestor. """ instance.__class__ = type( - '%s_extended_with_%s' % (instance.__class__.__name__, new_class.__name__), + '{}_extended_with_{}'.format(instance.__class__.__name__, new_class.__name__), (new_class, instance.__class__,), {} ) @@ -39,7 +37,7 @@ def is_list(v): def proxy_object_to_delegate(proxy_object, delegate_object): - proxy_class_name = 'Proxy{0}To{1}'.format( + proxy_class_name = 'Proxy{}To{}'.format( proxy_object.__class__.__name__, delegate_object.__class__.__name__) proxy_object.__class__ = type(oldstr(proxy_class_name), diff --git a/src/zsl/utils/request_helper.py b/src/zsl/utils/request_helper.py index 6e58b089..c927ea12 100644 --- a/src/zsl/utils/request_helper.py +++ b/src/zsl/utils/request_helper.py @@ -5,7 +5,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals from future.utils import viewitems diff --git a/src/zsl/utils/resource_helper.py b/src/zsl/utils/resource_helper.py index 527b8315..82298751 100644 --- a/src/zsl/utils/resource_helper.py +++ b/src/zsl/utils/resource_helper.py @@ -6,7 +6,6 @@ """ # TODO describe what model resource is and use cases -from __future__ import unicode_literals import importlib import logging @@ -76,7 +75,7 @@ def get_resource_task(resource_path: str, config: Config) -> ResourceTask: resource = None for resource_package in resource_packages: - module_name = "{0}.{1}".format(resource_package, resource_path) + module_name = "{}.{}".format(resource_package, resource_path) try: module = importlib.import_module(module_name) @@ -87,7 +86,7 @@ def get_resource_task(resource_path: str, config: Config) -> ResourceTask: resource = instantiate(cls) break except AttributeError: - raise NameError("Can't find resource [{0}]".format(resource_path)) + raise NameError("Can't find resource [{}]".format(resource_path)) except ImportError: @@ -98,11 +97,11 @@ def get_resource_task(resource_path: str, config: Config) -> ResourceTask: resource = exposer.get_resource(class_name) break except Exception as e: - logging.error("Can not load resource {0} [{1}].".format(resource_path, e)) + logging.error("Can not load resource {} [{}].".format(resource_path, e)) pass if resource is None: - raise NameError("Can't find resource [{0}]".format(resource_path)) + raise NameError("Can't find resource [{}]".format(resource_path)) try: if request.method == 'POST': @@ -149,9 +148,9 @@ def create_model_resource(resource_map, name, app: Zsl): elif len(resource_description) == 3: module_name, model_name, resource_class = resource_map[name] else: - raise ImportError("Invalid resource description for resource '{0}'".format(name)) + raise ImportError("Invalid resource description for resource '{}'".format(name)) except KeyError: - raise ImportError("Missing resource description for resource '{0}'".format(name)) + raise ImportError("Missing resource description for resource '{}'".format(name)) module = importlib.import_module(module_name) model_cls = getattr(module, model_name) diff --git a/src/zsl/utils/rss.py b/src/zsl/utils/rss.py index 8c4dc8af..9ed03d78 100644 --- a/src/zsl/utils/rss.py +++ b/src/zsl/utils/rss.py @@ -6,7 +6,6 @@ .. moduleauthor:: Peter Morihladko """ -from __future__ import unicode_literals import xml.etree.ElementTree as ET diff --git a/src/zsl/utils/security_helper.py b/src/zsl/utils/security_helper.py index f0987157..bc7ba014 100644 --- a/src/zsl/utils/security_helper.py +++ b/src/zsl/utils/security_helper.py @@ -5,9 +5,7 @@ Helper module with function dealing with security. """ -from __future__ import unicode_literals -from builtins import str import hashlib import sys diff --git a/src/zsl/utils/string_helper.py b/src/zsl/utils/string_helper.py index 4ef5b55c..c34dd2c4 100644 --- a/src/zsl/utils/string_helper.py +++ b/src/zsl/utils/string_helper.py @@ -4,9 +4,7 @@ .. moduleauthor:: Martin Babka """ -from __future__ import unicode_literals -from builtins import range, str import random import re import string @@ -83,10 +81,10 @@ def generate_random_string(size=6, chars=string.ascii_uppercase + string.digits) def addslashes(s, escaped_chars=None): - """Add slashes for given characters. Default is for ``\`` and ``'``. + """Add slashes for given characters. Default is for ``\\`` and ``'``. :param s: string - :param escaped_chars: list of characters to prefix with a slash ``\`` + :param escaped_chars: list of characters to prefix with a slash ``\\`` :return: string with slashed characters :rtype: str diff --git a/src/zsl/utils/task_helper.py b/src/zsl/utils/task_helper.py index fa5f70dc..b0a83744 100644 --- a/src/zsl/utils/task_helper.py +++ b/src/zsl/utils/task_helper.py @@ -7,9 +7,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, division, print_function, unicode_literals - -from builtins import * from zsl.task.task_data import TaskData from zsl.utils.injection_helper import instantiate diff --git a/src/zsl/utils/testing.py b/src/zsl/utils/testing.py index 4db37a9f..230fc3ed 100644 --- a/src/zsl/utils/testing.py +++ b/src/zsl/utils/testing.py @@ -4,7 +4,6 @@ .. moduleauthor:: Martin Babka """ -from __future__ import absolute_import, unicode_literals import importlib import unittest diff --git a/src/zsl/utils/type_helper.py b/src/zsl/utils/type_helper.py index 786c0294..5405d2a8 100644 --- a/src/zsl/utils/type_helper.py +++ b/src/zsl/utils/type_helper.py @@ -6,7 +6,6 @@ .. moduleauthor:: peter """ -from __future__ import unicode_literals def not_empty_list(l): diff --git a/src/zsl/utils/url_helper.py b/src/zsl/utils/url_helper.py index 580c1a2d..366c1ef5 100644 --- a/src/zsl/utils/url_helper.py +++ b/src/zsl/utils/url_helper.py @@ -1,13 +1,10 @@ -# coding: utf-8 """ :mod:`zsl.utils.url_helper` --------------------------- Helper module for URL handling. """ -from __future__ import unicode_literals -from builtins import str import re import unicodedata import urllib @@ -36,7 +33,7 @@ def slugify(value, allow_unicode=False): else: value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub(r'[^\w\s-]', '', value).strip().lower() - return re.sub('[-\s]+', '-', value) + return re.sub(r'[-\s]+', '-', value) def urlencode(query): diff --git a/src/zsl/utils/warnings.py b/src/zsl/utils/warnings.py index 51176e40..a807afd8 100644 --- a/src/zsl/utils/warnings.py +++ b/src/zsl/utils/warnings.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import - import functools import warnings diff --git a/src/zsl/utils/xml_helper.py b/src/zsl/utils/xml_helper.py index 52ea6689..12a8dd8a 100644 --- a/src/zsl/utils/xml_helper.py +++ b/src/zsl/utils/xml_helper.py @@ -4,7 +4,6 @@ Helper functions for working with XML and ElementTree. """ -from __future__ import unicode_literals from functools import reduce import xml.etree.ElementTree as ET @@ -64,7 +63,7 @@ def msg_err_missing_attributes(tag, *attributes): :return: message :rtype: str """ - return "Missing one or more required attributes (%s) in xml tag %s" % ('|'.join(attributes), tag) + return "Missing one or more required attributes ({}) in xml tag {}".format('|'.join(attributes), tag) def msg_err_missing_children(tag, *children): @@ -75,7 +74,7 @@ def msg_err_missing_children(tag, *children): :return: message :rtype: str """ - return "Missing one or more required children (%s) in xml tag %s" % ('|'.join(children), tag) + return "Missing one or more required children ({}) in xml tag {}".format('|'.join(children), tag) def attrib_to_dict(element, *args, **kwargs): diff --git a/src/zsl/utils/xml_to_json.py b/src/zsl/utils/xml_to_json.py index a4124f21..170f765d 100644 --- a/src/zsl/utils/xml_to_json.py +++ b/src/zsl/utils/xml_to_json.py @@ -4,7 +4,6 @@ Helper functions for simpler parsing xml into object with schemas. """ -from __future__ import unicode_literals from future.utils import viewitems @@ -56,7 +55,7 @@ def xml_to_json(element, definition, required=False): if sub_element is None: if required: - raise NotCompleteXmlException('Expecting {0} in element {1}'.format(definition, element.tag)) + raise NotCompleteXmlException('Expecting {} in element {}'.format(definition, element.tag)) return None return sub_element.text.strip() if sub_element.text else None @@ -135,7 +134,7 @@ def _parse_tuple(element, definition, required): if sub_elem is None: if required: - raise NotCompleteXmlException('Expecting {0} in element {1}'.format(first, element.tag)) + raise NotCompleteXmlException('Expecting {} in element {}'.format(first, element.tag)) return None