Skip to content

Commit

Permalink
add RawConfig type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
spladug committed Jun 7, 2019
1 parent ecff866 commit 03eb019
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
12 changes: 6 additions & 6 deletions baseplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys

from typing import Dict, TYPE_CHECKING
from typing import TYPE_CHECKING

from baseplate import config, metrics
from baseplate.core import Baseplate
Expand All @@ -13,7 +13,7 @@
import raven


def metrics_client_from_config(raw_config: Dict[str, str]) -> metrics.Client:
def metrics_client_from_config(raw_config: config.RawConfig) -> metrics.Client:
"""Configure and return a metrics client.
This expects two configuration options:
Expand All @@ -39,7 +39,7 @@ def metrics_client_from_config(raw_config: Dict[str, str]) -> metrics.Client:
return metrics.make_client(cfg.metrics.namespace, cfg.metrics.endpoint)


def make_metrics_client(raw_config: Dict[str, str]) -> metrics.Client:
def make_metrics_client(raw_config: config.RawConfig) -> metrics.Client:
warn_deprecated(
"make_metrics_client is deprecated in favor of the more "
"consistently named metrics_client_from_config"
Expand All @@ -48,7 +48,7 @@ def make_metrics_client(raw_config: Dict[str, str]) -> metrics.Client:


def tracing_client_from_config(
raw_config: Dict[str, str], log_if_unconfigured: bool = True
raw_config: config.RawConfig, log_if_unconfigured: bool = True
) -> tracing.TracingClient:
"""Configure and return a tracing client.
Expand Down Expand Up @@ -114,7 +114,7 @@ def tracing_client_from_config(


def make_tracing_client(
raw_config: Dict[str, str], log_if_unconfigured: bool = True
raw_config: config.RawConfig, log_if_unconfigured: bool = True
) -> tracing.TracingClient:
warn_deprecated(
"make_tracing_client is deprecated in favor of the more "
Expand All @@ -123,7 +123,7 @@ def make_tracing_client(
return tracing_client_from_config(raw_config, log_if_unconfigured)


def error_reporter_from_config(raw_config: Dict[str, str], module_name: str) -> "raven.Client":
def error_reporter_from_config(raw_config: config.RawConfig, module_name: str) -> "raven.Client":
"""Configure and return a error reporter.
This expects one configuration option and can take many optional ones:
Expand Down
11 changes: 6 additions & 5 deletions baseplate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def __getattr__(self, name: str) -> Any:

ConfigSpecItem = Union["Parser", Dict[str, Any], Callable[[str], T]]
ConfigSpec = Dict[str, ConfigSpecItem]
RawConfig = Dict[str, str]


class Parser(Generic[T]):
Expand All @@ -414,7 +415,7 @@ def from_spec(spec: ConfigSpecItem) -> "Parser":
return CallableParser(spec)
raise AssertionError("invalid specification: %r" % spec)

def parse(self, key_path: str, raw_config: Dict[str, str]) -> T:
def parse(self, key_path: str, raw_config: RawConfig) -> T:
"""Parse and return the relevant info for a given key.
:param key_path: The key this parser is looking for.
Expand All @@ -430,7 +431,7 @@ class SpecParser(Parser[ConfigNamespace]):
def __init__(self, spec: ConfigSpec):
self.spec = spec

def parse(self, key_path: str, raw_config: Dict[str, str]) -> ConfigNamespace:
def parse(self, key_path: str, raw_config: RawConfig) -> ConfigNamespace:
parsed = ConfigNamespace()
for key, spec in self.spec.items():
assert "." not in key, "dots are not allowed in keys"
Expand All @@ -451,7 +452,7 @@ class CallableParser(Parser[T]):
def __init__(self, callable_: Callable[[str], T]):
self.callable = callable_

def parse(self, key_path: str, raw_config: Dict[str, str]) -> T:
def parse(self, key_path: str, raw_config: RawConfig) -> T:
raw_value = raw_config.get(key_path, "")

try:
Expand Down Expand Up @@ -534,7 +535,7 @@ class DictOf(Parser[ConfigNamespace]):
def __init__(self, spec: ConfigSpecItem):
self.subparser = Parser.from_spec(spec)

def parse(self, key_path: str, raw_config: Dict[str, str]) -> ConfigNamespace:
def parse(self, key_path: str, raw_config: RawConfig) -> ConfigNamespace:
# match keys that start out with the prefix we expect (key_path) and
# extract the subkey from the.key.prefix.{subkey}.the.rest
if key_path:
Expand All @@ -560,7 +561,7 @@ def parse(self, key_path: str, raw_config: Dict[str, str]) -> ConfigNamespace:
return values


def parse_config(config: Dict[str, str], spec: ConfigSpec) -> ConfigNamespace:
def parse_config(config: RawConfig, spec: ConfigSpec) -> ConfigNamespace:
"""Parse options against a spec and return a structured representation.
:param config: The raw stringy configuration dictionary.
Expand Down
2 changes: 1 addition & 1 deletion baseplate/secrets/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _get_data(self) -> Dict:


def secrets_store_from_config(
app_config: Dict[str, str], timeout: Optional[int] = None, prefix: str = "secrets."
app_config: config.RawConfig, timeout: Optional[int] = None, prefix: str = "secrets."
) -> SecretsStore:
"""Configure and return a secrets store.
Expand Down

0 comments on commit 03eb019

Please sign in to comment.