diff --git a/.prospector.yaml b/.prospector.yaml index 4458f4bf4..7709ca98f 100644 --- a/.prospector.yaml +++ b/.prospector.yaml @@ -21,6 +21,7 @@ pylint: - ungrouped-imports - unused-argument - use-symbolic-message-instead + - wrong-import-order pep8: options: diff --git a/c2cwsgiutils/auth.py b/c2cwsgiutils/auth.py index e768e2320..7ed3f8e5c 100644 --- a/c2cwsgiutils/auth.py +++ b/c2cwsgiutils/auth.py @@ -5,7 +5,7 @@ from pyramid.httpexceptions import HTTPForbidden # noinspection PyProtectedMember -from c2cwsgiutils._utils import env_or_settings, env_or_config, config_bool +from c2cwsgiutils.config_utils import config_bool, env_or_config, env_or_settings COOKIE_AGE = 7 * 24 * 3600 SECRET_PROP = "c2c.secret" # nosec # noqa diff --git a/c2cwsgiutils/broadcast/__init__.py b/c2cwsgiutils/broadcast/__init__.py index 03a5095e8..ad7a0bd27 100644 --- a/c2cwsgiutils/broadcast/__init__.py +++ b/c2cwsgiutils/broadcast/__init__.py @@ -6,7 +6,8 @@ from typing import Any, Callable, Dict, List, Optional import pyramid.config -from c2cwsgiutils import _utils, redis_utils + +from c2cwsgiutils import config_utils, redis_utils from c2cwsgiutils.broadcast import interface, local, redis LOG = logging.getLogger(__name__) @@ -21,8 +22,10 @@ def init(config: Optional[pyramid.config.Configurator] = None) -> None: Initialize the broadcaster with Redis, if configured. Otherwise, fall back to a fake local implementation. """ global _broadcaster - broadcast_prefix = _utils.env_or_config(config, BROADCAST_ENV_KEY, BROADCAST_CONFIG_KEY, "broadcast_api_") - master, slave, _ = redis_utils.get() + broadcast_prefix = config_utils.env_or_config( + config, BROADCAST_ENV_KEY, BROADCAST_CONFIG_KEY, "broadcast_api_" + ) + master, slave, _ = redis_utils.get(config.get_settings() if config else None) if _broadcaster is None: if master is not None and slave is not None: _broadcaster = redis.RedisBroadcaster(broadcast_prefix, master, slave) diff --git a/c2cwsgiutils/_utils.py b/c2cwsgiutils/config_utils.py similarity index 100% rename from c2cwsgiutils/_utils.py rename to c2cwsgiutils/config_utils.py diff --git a/c2cwsgiutils/db_maintenance_view.py b/c2cwsgiutils/db_maintenance_view.py index 7af95dc3b..e3670f0f6 100644 --- a/c2cwsgiutils/db_maintenance_view.py +++ b/c2cwsgiutils/db_maintenance_view.py @@ -2,7 +2,8 @@ from typing import Any, Mapping, Optional, cast import pyramid.request -from c2cwsgiutils import _utils, auth, broadcast, db, redis_utils + +from c2cwsgiutils import auth, broadcast, config_utils, db, redis_utils LOG = logging.getLogger(__name__) CONFIG_KEY = "c2c.db_maintenance_view_enabled" @@ -16,7 +17,9 @@ def install_subscriber(config: pyramid.config.Configurator) -> None: """ if auth.is_enabled(config, ENV_KEY, CONFIG_KEY): config.add_route( - "c2c_db_maintenance", _utils.get_base_path(config) + r"/db/maintenance", request_method="GET" + "c2c_db_maintenance", + config_utils.get_base_path(config) + r"/db/maintenance", + request_method="GET", ) config.add_view(_db_maintenance, route_name="c2c_db_maintenance", renderer="fast_json", http_cache=0) _restore(config) @@ -60,13 +63,13 @@ def _restore(config: pyramid.config.Configurator) -> None: def _store(settings: Mapping[str, Any], readonly: bool) -> None: - master, _, _ = redis_utils.get() + master, _, _ = redis_utils.get(settings) if master is not None: master.set(REDIS_PREFIX + "force_readonly", "true" if readonly else "false") def _get_redis_value(settings: Mapping[str, Any]) -> Optional[str]: - _, slave, _ = redis_utils.get() + _, slave, _ = redis_utils.get(settings) if slave is not None: value = slave.get(REDIS_PREFIX + "force_readonly") return str(value) if value else None diff --git a/c2cwsgiutils/debug/__init__.py b/c2cwsgiutils/debug/__init__.py index b6b172a07..d11a08d2f 100644 --- a/c2cwsgiutils/debug/__init__.py +++ b/c2cwsgiutils/debug/__init__.py @@ -2,7 +2,7 @@ import pyramid.config -from c2cwsgiutils import _utils, auth +from c2cwsgiutils import auth, config_utils from c2cwsgiutils.debug import utils CONFIG_KEY = "c2c.debug_view_enabled" @@ -26,7 +26,7 @@ def init_daemon(config: Optional[pyramid.config.Configurator] = None) -> None: Initialize the debug broadcast listeners. Used mostly for headless processes that depend on a master providing a normal REST API and broadcasting those requests. """ - if _utils.env_or_config(config, ENV_KEY, CONFIG_KEY, type_=_utils.config_bool): + if config_utils.env_or_config(config, ENV_KEY, CONFIG_KEY, type_=config_utils.config_bool): from c2cwsgiutils.debug import _listeners _listeners.init() diff --git a/c2cwsgiutils/debug/_views.py b/c2cwsgiutils/debug/_views.py index 31c0efa85..a12bbe732 100644 --- a/c2cwsgiutils/debug/_views.py +++ b/c2cwsgiutils/debug/_views.py @@ -1,18 +1,18 @@ -from datetime import datetime import gc -from io import StringIO import logging import re import time +from datetime import datetime +from io import StringIO from typing import Any, Callable, Dict, List, Mapping import objgraph import pyramid.config -from pyramid.httpexceptions import HTTPException, exception_response import pyramid.request import pyramid.response +from pyramid.httpexceptions import HTTPException, exception_response -from c2cwsgiutils import _utils, auth, broadcast +from c2cwsgiutils import auth, broadcast, config_utils from c2cwsgiutils.debug.utils import dump_memory_maps, get_size LOG = logging.getLogger(__name__) @@ -155,7 +155,7 @@ def _add_view( config: pyramid.config.Configurator, name: str, path: str, view: Callable[[pyramid.request.Request], Any] ) -> None: config.add_route( - "c2c_debug_" + name, _utils.get_base_path(config) + r"/debug/" + path, request_method="GET" + "c2c_debug_" + name, config_utils.get_base_path(config) + r"/debug/" + path, request_method="GET" ) config.add_view(view, route_name="c2c_debug_" + name, renderer="fast_json", http_cache=0) diff --git a/c2cwsgiutils/errors.py b/c2cwsgiutils/errors.py index 89bb31196..98ef33e6e 100644 --- a/c2cwsgiutils/errors.py +++ b/c2cwsgiutils/errors.py @@ -12,7 +12,7 @@ from pyramid.httpexceptions import HTTPError, HTTPException, HTTPRedirection, HTTPSuccessful from webob.request import DisconnectionError -from c2cwsgiutils import _utils, auth +from c2cwsgiutils import auth, config_utils DEVELOPMENT = os.environ.get("DEVELOPMENT", "0") != "0" DEPRECATED_CONFIG_KEY = "c2c.error_details_secret" @@ -153,7 +153,9 @@ def _passthrough(exception: HTTPException, request: pyramid.request.Request) -> def init(config: pyramid.config.Configurator) -> None: if ( - _utils.env_or_config(config, "C2C_ENABLE_EXCEPTION_HANDLING", "c2c.enable_exception_handling", "0") + config_utils.env_or_config( + config, "C2C_ENABLE_EXCEPTION_HANDLING", "c2c.enable_exception_handling", "0" + ) != "0" ): for exception in (HTTPSuccessful, HTTPRedirection): diff --git a/c2cwsgiutils/health_check.py b/c2cwsgiutils/health_check.py index fd06dddd0..900fa6f01 100644 --- a/c2cwsgiutils/health_check.py +++ b/c2cwsgiutils/health_check.py @@ -13,9 +13,8 @@ import time import traceback from collections import Counter -from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union - from enum import Enum +from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union import pyramid.config import pyramid.request @@ -24,7 +23,7 @@ import sqlalchemy.orm from pyramid.httpexceptions import HTTPNotFound -from c2cwsgiutils import _utils, auth, broadcast, redis_utils, stats, version +from c2cwsgiutils import auth, broadcast, config_utils, redis_utils, stats, version LOG = logging.getLogger(__name__) ALEMBIC_HEAD_RE = re.compile(r"^([a-f0-9]+) \(head\)\n$") @@ -93,13 +92,18 @@ class HealthCheck: def __init__(self, config: pyramid.config.Configurator) -> None: config.add_route( - "c2c_health_check", _utils.get_base_path(config) + r"/health_check", request_method="GET" + "c2c_health_check", config_utils.get_base_path(config) + r"/health_check", request_method="GET" ) config.add_view(self._view, route_name="c2c_health_check", renderer="fast_json", http_cache=0) self._checks: List[Tuple[str, Callable[[pyramid.request.Request], Any], int]] = [] - redis = os.environ.get(redis_utils.REDIS_SENTINELS_KEY, os.environ.get(redis_utils.REDIS_URL_KEY)) - if redis: + self.name = config_utils.env_or_config( + config, + redis_utils.REDIS_SENTINELS_KEY, + redis_utils.REDIS_SENTINELS_KEY_PROP, + config_utils.env_or_config(config, redis_utils.REDIS_URL_KEY, redis_utils.REDIS_URL_KEY_PROP), + ) + if self.name: self.add_redis_check(level=2) if version.get_version() is not None: self.add_version_check(level=2) @@ -248,7 +252,7 @@ def add_redis_check(self, name: Optional[str] = None, level: int = 1) -> None: """ def check(request: pyramid.request.Request) -> Any: - master, slave, sentinel = redis_utils.get() + master, slave, sentinel = redis_utils.get(request.registry.settings) result = {} @@ -264,7 +268,12 @@ def add(name: str, func: Callable[..., Any], *args: Any) -> None: if slave is not None: add("slave_info", slave.info) if sentinel is not None: - service_name = os.environ.get(redis_utils.REDIS_SERVICENAME_KEY, "mymaster") + service_name = config_utils.env_or_settings( + request.registry.settings, + redis_utils.REDIS_SERVICENAME_KEY, + redis_utils.REDIS_SERVICENAME_KEY_PROP, + "mymaster", + ) add("sentinel", sentinel.sentinels[0].sentinel) add("sentinel_masters", sentinel.sentinels[0].sentinel_masters) add("sentinel_master", sentinel.sentinels[0].sentinel_master, service_name) @@ -274,8 +283,11 @@ def add(name: str, func: Callable[..., Any], *args: Any) -> None: return result if name is None: - name = os.environ.get(redis_utils.REDIS_SENTINELS_KEY, os.environ.get(redis_utils.REDIS_URL_KEY)) - assert name + name = self.name + + if name is None: + raise RuntimeError("Redis should be confgured") + self._checks.append((name, check, level)) def add_version_check(self, name: str = "version", level: int = 2) -> None: diff --git a/c2cwsgiutils/index.py b/c2cwsgiutils/index.py index 15a32ad1e..941b0e5fa 100644 --- a/c2cwsgiutils/index.py +++ b/c2cwsgiutils/index.py @@ -4,7 +4,7 @@ import pyramid.request import pyramid.response -from c2cwsgiutils import _utils, profiler +from c2cwsgiutils import profiler, config_utils from c2cwsgiutils.auth import is_auth additional_title: Optional[str] = None @@ -308,7 +308,7 @@ def _health_check(request: pyramid.request.Request) -> str: def init(config: pyramid.config.Configurator) -> None: - base_path = _utils.get_base_path(config) + base_path = config_utils.get_base_path(config) if base_path != "": config.add_route("c2c_index", base_path, request_method=("GET", "POST")) config.add_view(_index, route_name="c2c_index", http_cache=0) diff --git a/c2cwsgiutils/logging_view.py b/c2cwsgiutils/logging_view.py index c62eeb0c8..db2041505 100644 --- a/c2cwsgiutils/logging_view.py +++ b/c2cwsgiutils/logging_view.py @@ -2,7 +2,8 @@ from typing import Any, Generator, Mapping, Tuple import pyramid.request -from c2cwsgiutils import _utils, auth, broadcast, redis_utils + +from c2cwsgiutils import auth, broadcast, redis_utils, config_utils LOG = logging.getLogger(__name__) CONFIG_KEY = "c2c.log_view_enabled" @@ -16,7 +17,7 @@ def install_subscriber(config: pyramid.config.Configurator) -> None: """ if auth.is_enabled(config, ENV_KEY, CONFIG_KEY): config.add_route( - "c2c_logging_level", _utils.get_base_path(config) + r"/logging/level", request_method="GET" + "c2c_logging_level", config_utils.get_base_path(config) + r"/logging/level", request_method="GET" ) config.add_view( _logging_change_level, route_name="c2c_logging_level", renderer="fast_json", http_cache=0 @@ -67,7 +68,7 @@ def _restore_overrides(config: pyramid.config.Configurator) -> None: def _store_override(settings: Mapping[str, Any], name: str, level: str) -> None: try: - master, _, _ = redis_utils.get() + master, _, _ = redis_utils.get(settings) if master: master.set(REDIS_PREFIX + name, level) except ImportError: @@ -75,7 +76,7 @@ def _store_override(settings: Mapping[str, Any], name: str, level: str) -> None: def _list_overrides(settings: Mapping[str, Any]) -> Generator[Tuple[str, str], None, None]: - _, slave, _ = redis_utils.get() + _, slave, _ = redis_utils.get(settings) if slave is not None: for key in slave.scan_iter(REDIS_PREFIX + "*"): level = slave.get(key) diff --git a/c2cwsgiutils/pyramid_logging.py b/c2cwsgiutils/pyramid_logging.py index a8444cfa6..ea2058f0f 100644 --- a/c2cwsgiutils/pyramid_logging.py +++ b/c2cwsgiutils/pyramid_logging.py @@ -13,7 +13,7 @@ import os import socket import sys -from typing import Any, MutableMapping, Mapping, IO, Optional +from typing import IO, Any, Mapping, MutableMapping, Optional import cee_syslog_handler from pyramid.threadlocal import get_current_request diff --git a/c2cwsgiutils/redis_stats.py b/c2cwsgiutils/redis_stats.py index c4ea1ef7d..b5e01c685 100644 --- a/c2cwsgiutils/redis_stats.py +++ b/c2cwsgiutils/redis_stats.py @@ -1,9 +1,9 @@ import logging -from typing import Optional, Callable, Any, Dict # noqa # pylint: disable=unused-import +from typing import Any, Callable, Dict, Optional # noqa # pylint: disable=unused-import import pyramid.config -from c2cwsgiutils import stats, _utils +from c2cwsgiutils import config_utils, stats LOG = logging.getLogger(__name__) ORIG: Optional[Callable[..., Any]] = None @@ -23,7 +23,9 @@ def _execute_command_patch(self: Any, *args: Any, **options: Any) -> Any: def init(config: Optional[pyramid.config.Configurator] = None) -> None: global ORIG - if _utils.env_or_config(config, "C2C_TRACK_REDIS", "c2c.track_redis", True, _utils.config_bool): + if config_utils.env_or_config( + config, "C2C_TRACK_REDIS", "c2c.track_redis", True, config_utils.config_bool + ): try: import redis.client diff --git a/c2cwsgiutils/redis_utils.py b/c2cwsgiutils/redis_utils.py index 31da31076..43fee60a3 100644 --- a/c2cwsgiutils/redis_utils.py +++ b/c2cwsgiutils/redis_utils.py @@ -1,14 +1,15 @@ import logging -import os import threading import time -from typing import Optional, Tuple +from typing import Any, Mapping, Optional, Tuple import redis.client import redis.exceptions import redis.sentinel import yaml +import c2cwsgiutils.config_utils + LOG = logging.getLogger(__name__) REDIS_URL_KEY = "C2C_REDIS_URL" @@ -17,24 +18,39 @@ REDIS_SERVICENAME_KEY = "C2C_REDIS_SERVICENAME" REDIS_DB_KEY = "C2C_REDIS_DB" +REDIS_URL_KEY_PROP = "c2c.redis_url" +REDIS_OPTIONS_KEY_PROP = "c2c.redis_options" +REDIS_SENTINELS_KEY_PROP = "c2c.redis_sentinels" +REDIS_SERVICENAME_KEY_PROP = "c2c.redis_servicename" +REDIS_DB_KEY_PROP = "c2c.redis_db" + _master: Optional[redis.Redis] = None _slave: Optional[redis.Redis] = None _sentinel: Optional[redis.sentinel.Sentinel] = None -def get() -> Tuple[Optional[redis.Redis], Optional[redis.Redis], Optional[redis.sentinel.Sentinel]]: +def get( + settings: Optional[Mapping[str, Any]] = None, +) -> Tuple[Optional[redis.Redis], Optional[redis.Redis], Optional[redis.sentinel.Sentinel]]: if _master is None: - _init() + _init(settings) return _master, _slave, _sentinel -def _init() -> None: +def _init(settings: Optional[Mapping[str, Any]]) -> None: global _master, _slave, _sentinel - sentinels = os.environ.get(REDIS_SENTINELS_KEY) - service_name = os.environ.get(REDIS_SERVICENAME_KEY) - db = os.environ.get(REDIS_DB_KEY) - url = os.environ.get(REDIS_URL_KEY) - redis_options_ = os.environ.get(REDIS_OPTIONS_KEY) + sentinels = c2cwsgiutils.config_utils.env_or_settings( + settings, REDIS_SENTINELS_KEY, REDIS_SENTINELS_KEY_PROP + ) + service_name = c2cwsgiutils.config_utils.env_or_settings( + settings, REDIS_SERVICENAME_KEY, REDIS_SERVICENAME_KEY_PROP + ) + db = c2cwsgiutils.config_utils.env_or_settings(settings, REDIS_DB_KEY, REDIS_DB_KEY_PROP) + url = c2cwsgiutils.config_utils.env_or_settings(settings, REDIS_URL_KEY, REDIS_URL_KEY_PROP) + redis_options_ = c2cwsgiutils.config_utils.env_or_settings( + settings, REDIS_OPTIONS_KEY, REDIS_OPTIONS_KEY_PROP + ) + redis_options = ( {} if redis_options_ is None diff --git a/c2cwsgiutils/request_tracking/__init__.py b/c2cwsgiutils/request_tracking/__init__.py index f913fe55c..14db07e09 100644 --- a/c2cwsgiutils/request_tracking/__init__.py +++ b/c2cwsgiutils/request_tracking/__init__.py @@ -12,7 +12,7 @@ import requests.models from pyramid.threadlocal import get_current_request -from c2cwsgiutils import _utils, stats +from c2cwsgiutils import stats, config_utils ID_HEADERS: List[str] = [] _HTTPAdapter_send = requests.adapters.HTTPAdapter.send @@ -76,17 +76,17 @@ def init(config: Optional[pyramid.config.Configurator] = None) -> None: global ID_HEADERS, DEFAULT_TIMEOUT ID_HEADERS = ["X-Request-ID", "X-Correlation-ID", "Request-ID", "X-Varnish", "X-Amzn-Trace-Id"] if config is not None: - extra_header = _utils.env_or_config(config, "C2C_REQUEST_ID_HEADER", "c2c.request_id_header") + extra_header = config_utils.env_or_config(config, "C2C_REQUEST_ID_HEADER", "c2c.request_id_header") if extra_header: ID_HEADERS.insert(0, extra_header) config.add_request_method(_gen_request_id, "c2c_request_id", reify=True) - DEFAULT_TIMEOUT = _utils.env_or_config( + DEFAULT_TIMEOUT = config_utils.env_or_config( config, "C2C_REQUESTS_DEFAULT_TIMEOUT", "c2c.requests_default_timeout", type_=float ) _patch_requests() - if _utils.env_or_config(config, "C2C_SQL_REQUEST_ID", "c2c.sql_request_id", False): + if config_utils.env_or_config(config, "C2C_SQL_REQUEST_ID", "c2c.sql_request_id", False): from . import _sql _sql.init() diff --git a/c2cwsgiutils/sentry.py b/c2cwsgiutils/sentry.py index 260d1a476..761fad3c7 100644 --- a/c2cwsgiutils/sentry.py +++ b/c2cwsgiutils/sentry.py @@ -1,7 +1,7 @@ import contextlib import logging import os -from typing import MutableMapping, Any, Generator, Optional, Callable # noqa # pylint: disable=unused-import +from typing import Any, Callable, Generator, MutableMapping, Optional # noqa # pylint: disable=unused-import import pyramid.config import sentry_sdk @@ -10,7 +10,7 @@ from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware -from c2cwsgiutils import _utils +from c2cwsgiutils import config_utils LOG = logging.getLogger(__name__) _client_setup = False @@ -30,12 +30,12 @@ def do_filter(event: Any, hint: Any) -> Any: def init(config: Optional[pyramid.config.Configurator] = None) -> None: global _client_setup - sentry_url = _utils.env_or_config(config, "SENTRY_URL", "c2c.sentry.url") + sentry_url = config_utils.env_or_config(config, "SENTRY_URL", "c2c.sentry.url") if sentry_url is not None and not _client_setup: client_info: MutableMapping[str, Any] = { key[14:].lower(): value for key, value in os.environ.items() if key.startswith("SENTRY_CLIENT_") } - git_hash = _utils.env_or_config(config, "GIT_HASH", "c2c.git_hash") + git_hash = config_utils.env_or_config(config, "GIT_HASH", "c2c.git_hash") if git_hash is not None and not ("release" in client_info and client_info["release"] != "latest"): client_info["release"] = git_hash client_info["ignore_errors"] = client_info.pop("ignore_exceptions", "SystemExit").split(",") @@ -43,7 +43,9 @@ def init(config: Optional[pyramid.config.Configurator] = None) -> None: sentry_logging = LoggingIntegration( level=logging.DEBUG, - event_level=_utils.env_or_config(config, "SENTRY_LEVEL", "c2c.sentry_level", "ERROR").upper(), + event_level=config_utils.env_or_config( + config, "SENTRY_LEVEL", "c2c.sentry_level", "ERROR" + ).upper(), ) sentry_sdk.init( # type: ignore dsn=sentry_url, @@ -53,9 +55,9 @@ def init(config: Optional[pyramid.config.Configurator] = None) -> None: ) _client_setup = True - excludes = _utils.env_or_config(config, "SENTRY_EXCLUDES", "c2c.sentry.excludes", "sentry_sdk").split( - "," - ) + excludes = config_utils.env_or_config( + config, "SENTRY_EXCLUDES", "c2c.sentry.excludes", "sentry_sdk" + ).split(",") for exclude in excludes: ignore_logger(exclude) diff --git a/c2cwsgiutils/sql_profiler/_impl.py b/c2cwsgiutils/sql_profiler/_impl.py index 969c04b3a..ca0323e70 100644 --- a/c2cwsgiutils/sql_profiler/_impl.py +++ b/c2cwsgiutils/sql_profiler/_impl.py @@ -11,7 +11,7 @@ import sqlalchemy.engine import sqlalchemy.event -from c2cwsgiutils import _utils, auth, broadcast +from c2cwsgiutils import auth, broadcast, config_utils LOG = logging.getLogger(__name__) repository = None @@ -61,7 +61,7 @@ def _sql_profiler_view(request: pyramid.request.Request) -> Mapping[str, Any]: def _setup_profiler(enable: str) -> None: global repository - if _utils.config_bool(enable): + if config_utils.config_bool(enable): if repository is None: LOG.warning("Enabling the SQL profiler") repository = _Repository() @@ -93,7 +93,7 @@ def init(config: pyramid.config.Configurator) -> None: broadcast.subscribe("c2c_sql_profiler", _setup_profiler) config.add_route( - "c2c_sql_profiler", _utils.get_base_path(config) + r"/sql_profiler", request_method="GET" + "c2c_sql_profiler", config_utils.get_base_path(config) + r"/sql_profiler", request_method="GET" ) config.add_view(_sql_profiler_view, route_name="c2c_sql_profiler", renderer="fast_json", http_cache=0) LOG.info("Enabled the /sql_profiler API") diff --git a/c2cwsgiutils/stats.py b/c2cwsgiutils/stats.py index 90a4b75a9..6c52218f1 100644 --- a/c2cwsgiutils/stats.py +++ b/c2cwsgiutils/stats.py @@ -9,19 +9,28 @@ import socket import threading import time -from abc import abstractmethod, ABCMeta -from typing import Mapping, Sequence, List, Generator, Any, Optional, Callable, Dict -from typing import Tuple, MutableMapping # noqa # pylint: disable=unused-import +from abc import ABCMeta, abstractmethod +from typing import ( # noqa # pylint: disable=unused-import + Any, + Callable, + Dict, + Generator, + List, + Mapping, + MutableMapping, + Optional, + Sequence, + Tuple, +) import pyramid.request -from c2cwsgiutils import _utils - +from c2cwsgiutils import config_utils LOG = logging.getLogger(__name__) USE_TAGS_ENV = "STATSD_USE_TAGS" TAG_PREFIX_ENV = "STATSD_TAG_" -USE_TAGS = _utils.config_bool(os.environ.get(USE_TAGS_ENV, "0")) +USE_TAGS = config_utils.config_bool(os.environ.get(USE_TAGS_ENV, "0")) TagType = Optional[Mapping[str, Any]] @@ -296,12 +305,12 @@ def init_backends(settings: Optional[Mapping[str, str]] = None) -> None: :param settings: The Pyramid config """ - if _utils.env_or_settings(settings, "STATS_VIEW", "c2c.stats_view", False): # pragma: nocover + if config_utils.env_or_settings(settings, "STATS_VIEW", "c2c.stats_view", False): # pragma: nocover BACKENDS["memory"] = MemoryBackend() - statsd_address = _utils.env_or_settings(settings, "STATSD_ADDRESS", "c2c.statsd_address", None) + statsd_address = config_utils.env_or_settings(settings, "STATSD_ADDRESS", "c2c.statsd_address", None) if statsd_address is not None: # pragma: nocover - statsd_prefix = _utils.env_or_settings(settings, "STATSD_PREFIX", "c2c.statsd_prefix", "") + statsd_prefix = config_utils.env_or_settings(settings, "STATSD_PREFIX", "c2c.statsd_prefix", "") statsd_tags = get_env_tags() try: BACKENDS["statsd"] = StatsDBackend(statsd_address, statsd_prefix, statsd_tags) diff --git a/c2cwsgiutils/stats_pyramid/_views.py b/c2cwsgiutils/stats_pyramid/_views.py index a16f0042d..3cbdbcbf8 100644 --- a/c2cwsgiutils/stats_pyramid/_views.py +++ b/c2cwsgiutils/stats_pyramid/_views.py @@ -2,12 +2,12 @@ import pyramid.config -from c2cwsgiutils import _utils, stats +from c2cwsgiutils import stats, config_utils def init(config: pyramid.config.Configurator) -> None: config.add_route( - "c2c_read_stats_json", _utils.get_base_path(config) + r"/stats.json", request_method="GET" + "c2c_read_stats_json", config_utils.get_base_path(config) + r"/stats.json", request_method="GET" ) memory_backend = cast(stats.MemoryBackend, stats.BACKENDS["memory"]) config.add_view( diff --git a/c2cwsgiutils/version.py b/c2cwsgiutils/version.py index 3f194f96c..e30a5b7a5 100644 --- a/c2cwsgiutils/version.py +++ b/c2cwsgiutils/version.py @@ -1,11 +1,11 @@ import json import logging import os -from typing import Optional, Dict, cast +from typing import Dict, Optional, cast import pyramid.config -from c2cwsgiutils import _utils, stats +from c2cwsgiutils import stats, config_utils VERSIONS_PATH = "/app/versions.json" LOG = logging.getLogger(__name__) @@ -15,7 +15,7 @@ def init(config: pyramid.config.Configurator) -> None: if os.path.isfile(VERSIONS_PATH): versions = _read_versions() config.add_route( - "c2c_versions", _utils.get_base_path(config) + r"/versions.json", request_method="GET" + "c2c_versions", config_utils.get_base_path(config) + r"/versions.json", request_method="GET" ) config.add_view( lambda request: versions, route_name="c2c_versions", renderer="fast_json", http_cache=0 diff --git a/setup.py b/setup.py index 9a6c37b43..1414c0b30 100644 --- a/setup.py +++ b/setup.py @@ -52,7 +52,6 @@ def long_description() -> str: "c2cwsgiutils-stats-db = c2cwsgiutils.scripts.stats_db:main", "c2cwsgiutils-test-print = c2cwsgiutils.scripts.test_print:main", "c2cwsgiutils-check-es = c2cwsgiutils.scripts.check_es:main", - # deprecated scripts "c2cwsgiutils_genversion.py = c2cwsgiutils.scripts.genversion:deprecated", "c2cwsgiutils_coverage_report.py = c2cwsgiutils.scripts.coverage_report:deprecated",