Update configfile API
This commit is contained in:
parent
d29694b1a2
commit
f7c852951e
4 changed files with 36 additions and 22 deletions
|
|
@ -84,9 +84,11 @@ class LargeFileBase(ABC):
|
||||||
@classmethod
|
@classmethod
|
||||||
def configure_credentials_from_file(
|
def configure_credentials_from_file(
|
||||||
cls,
|
cls,
|
||||||
secrets_path: Union[Path, str],
|
secrets: Union[Path, str, ConfigFile],
|
||||||
) -> None:
|
) -> None:
|
||||||
cls.configure_credentials(**ConfigFile(secrets_path))
|
if not isinstance(secrets, ConfigFile):
|
||||||
|
secrets = ConfigFile(secrets)
|
||||||
|
cls.configure_credentials(**{k.lower(): v for k, v in secrets.items()})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def configure_credentials(cls, **kwargs: str) -> None:
|
def configure_credentials(cls, **kwargs: str) -> None:
|
||||||
|
|
|
||||||
|
|
@ -90,17 +90,8 @@ class MongodbDriver(TracingDatabaseDriver):
|
||||||
|
|
||||||
query: Dict[str, Any] = {
|
query: Dict[str, Any] = {
|
||||||
"filter": {},
|
"filter": {},
|
||||||
"sort": [
|
|
||||||
(col.column_id, 1 if col.direction == "asc" else -1) for col in sort_by
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if skip:
|
|
||||||
query["skip"] = skip
|
|
||||||
|
|
||||||
if take:
|
|
||||||
query["limit"] = take
|
|
||||||
|
|
||||||
and_query: List[Dict[str, Any]] = [{}]
|
and_query: List[Dict[str, Any]] = [{}]
|
||||||
and_query.extend({"tags": tag} for tag in conjunctive_tags)
|
and_query.extend({"tags": tag} for tag in conjunctive_tags)
|
||||||
and_query.extend(
|
and_query.extend(
|
||||||
|
|
@ -120,10 +111,21 @@ class MongodbDriver(TracingDatabaseDriver):
|
||||||
query["filter"]["$and"] = and_query
|
query["filter"]["$and"] = and_query
|
||||||
|
|
||||||
with MongoClient[Any](self.mongo_connection_string) as client:
|
with MongoClient[Any](self.mongo_connection_string) as client:
|
||||||
values = client[self.mongo_database].traces.find(**query)
|
count = client[self.mongo_database].traces.count_documents(**query)
|
||||||
documents = [Trace[Any].parse_obj(t) for t in values]
|
|
||||||
|
|
||||||
return documents, len(documents)
|
if skip:
|
||||||
|
query["skip"] = skip
|
||||||
|
|
||||||
|
if take:
|
||||||
|
query["limit"] = take
|
||||||
|
|
||||||
|
query["sort"] = [
|
||||||
|
(col.column_id, 1 if col.direction == "asc" else -1) for col in sort_by
|
||||||
|
]
|
||||||
|
|
||||||
|
with client[self.mongo_database].traces.find(**query) as cursor:
|
||||||
|
documents = [Trace[Any].parse_obj(t) for t in cursor]
|
||||||
|
return documents, count
|
||||||
|
|
||||||
def update(self, id: str, new_version: Trace) -> None:
|
def update(self, id: str, new_version: Trace) -> None:
|
||||||
serialized = new_version.to_flat_dict()
|
serialized = new_version.to_flat_dict()
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,11 @@ class TracingDatabaseDriver(ABC):
|
||||||
@classmethod
|
@classmethod
|
||||||
def configure_credentials_from_file(
|
def configure_credentials_from_file(
|
||||||
cls,
|
cls,
|
||||||
secrets_path: Union[Path, str],
|
secrets: Union[Path, str, ConfigFile],
|
||||||
) -> None:
|
) -> None:
|
||||||
cls.configure_credentials(**ConfigFile(secrets_path))
|
if not isinstance(secrets, ConfigFile):
|
||||||
|
secrets = ConfigFile(secrets)
|
||||||
|
cls.configure_credentials(**{k.lower(): v for k, v in secrets.items()})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def configure_credentials(
|
def configure_credentials(
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,30 @@ from ..logger import get_logger
|
||||||
from .parse_error import ParseError
|
from .parse_error import ParseError
|
||||||
from .pattern import pattern
|
from .pattern import pattern
|
||||||
|
|
||||||
ENVIRONMENT_VARIABLE_KEY_PREFIX = "ENV"
|
|
||||||
|
|
||||||
logger = get_logger("ConfigFile")
|
logger = get_logger("ConfigFile")
|
||||||
|
|
||||||
|
|
||||||
class ConfigFile(Mapping[str, str]):
|
class ConfigFile(Mapping[str, str]):
|
||||||
def __init__(self, path: Union[Path, str]) -> None:
|
ENVIRONMENT_VARIABLE_KEY_PREFIX = "ENV"
|
||||||
|
|
||||||
|
def __init__(self, path: Union[Path, str], *, ignore_missing: bool = False) -> None:
|
||||||
if not isinstance(path, Path):
|
if not isinstance(path, Path):
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
|
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
raise FileNotFoundError(path.absolute())
|
raise FileNotFoundError(path.absolute())
|
||||||
|
|
||||||
|
self._ignore_missing = ignore_missing
|
||||||
|
|
||||||
self._path = path
|
self._path = path
|
||||||
self._key_values: Dict[str, str] = {}
|
self._key_values: Dict[str, str] = {}
|
||||||
|
|
||||||
self._parse()
|
self._parse()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def path(self) -> Path:
|
||||||
|
return self._path
|
||||||
|
|
||||||
def _parse(self) -> None:
|
def _parse(self) -> None:
|
||||||
with open(self._path, encoding="utf-8") as f:
|
with open(self._path, encoding="utf-8") as f:
|
||||||
lines: str = f.read()
|
lines: str = f.read()
|
||||||
|
|
@ -39,21 +45,23 @@ class ConfigFile(Mapping[str, str]):
|
||||||
|
|
||||||
already_exists = key in self._key_values
|
already_exists = key in self._key_values
|
||||||
if already_exists and not value.startswith(
|
if already_exists and not value.startswith(
|
||||||
f"{ENVIRONMENT_VARIABLE_KEY_PREFIX}:"
|
f"{self.ENVIRONMENT_VARIABLE_KEY_PREFIX}:"
|
||||||
):
|
):
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
f"Key `{key}` has been already defined and its value is `{self._key_values[key]}`"
|
f"Key `{key}` has been already defined and its value is `{self._key_values[key]}`"
|
||||||
)
|
)
|
||||||
|
|
||||||
if value.startswith(f"{ENVIRONMENT_VARIABLE_KEY_PREFIX}:"):
|
if value.startswith(f"{self.ENVIRONMENT_VARIABLE_KEY_PREFIX}:"):
|
||||||
_, value = value.split(":")
|
_, value = value.split(":")
|
||||||
if value not in os.environ:
|
if value not in os.environ:
|
||||||
issue = f'The value of `{key}` contains the "{ENVIRONMENT_VARIABLE_KEY_PREFIX}` prefix but `{value}` is not defined as an environment variable'
|
issue = f'The value of `{key}` contains the "{self.ENVIRONMENT_VARIABLE_KEY_PREFIX}` prefix but `{value}` is not defined as an environment variable'
|
||||||
if already_exists:
|
if already_exists:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"{issue}, using the default value defined above (`{self._key_values[key]}`)"
|
f"{issue}, using the default value defined above (`{self._key_values[key]}`)"
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
elif self._ignore_missing:
|
||||||
|
logger.warning(issue)
|
||||||
else:
|
else:
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
f"{issue} and no default value has been provided"
|
f"{issue} and no default value has been provided"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue