Fix bugs with encoding of providers

* Made ConfigurationStore a dataclass so that it gets encoded correctly
  when using asdict
* Simplified the code around encoding providers

Fixes #325
Fixes #320
This commit is contained in:
Sumner Evans
2021-11-10 23:29:59 -07:00
parent cf46fe8b5e
commit 8e59702c99
2 changed files with 14 additions and 19 deletions

View File

@@ -170,6 +170,7 @@ class CacheMissError(Exception):
KEYRING_APP_NAME = "app.sublimemusic.SublimeMusic" KEYRING_APP_NAME = "app.sublimemusic.SublimeMusic"
@dataclass
class ConfigurationStore(dict): class ConfigurationStore(dict):
""" """
This defines an abstract store for all configuration parameters for a given Adapter. This defines an abstract store for all configuration parameters for a given Adapter.

View File

@@ -3,7 +3,7 @@ import os
import pickle import pickle
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from pathlib import Path from pathlib import Path
from typing import Any, cast, Dict, Optional, Tuple, Type, Union from typing import Any, Dict, Optional, Tuple, Type, Union
import dataclasses_json import dataclasses_json
from dataclasses_json import config, DataClassJsonMixin from dataclasses_json import config, DataClassJsonMixin
@@ -68,27 +68,21 @@ class ProviderConfiguration:
def encode_providers( def encode_providers(
providers_dict: Dict[str, Union[ProviderConfiguration, Dict[str, Any]]] providers_dict: Dict[str, Union[ProviderConfiguration, Dict[str, Any]]]
) -> Dict[str, Dict[str, Any]]: ) -> Dict[str, Dict[str, Any]]:
def get_typename(
config: Union[ProviderConfiguration, Dict[str, Any]],
key: str,
) -> Optional[str]:
key += "_type"
if isinstance(config, dict):
return type_.__name__ if (type_ := config.get(key)) else None
else:
return type_.__name__ if (type_ := getattr(config, key)) else None
return { return {
id_: { id_: {
**(config if isinstance(config, dict) else asdict(config)), **(config if isinstance(config, dict) else asdict(config)),
"ground_truth_adapter_type": ( "ground_truth_adapter_type": get_typename(config, "ground_truth_adapter"),
config["ground_truth_adapter_type"] "caching_adapter_type": get_typename(config, "caching_adapter"),
if isinstance(config, dict)
else config.ground_truth_adapter_type
).__name__,
"caching_adapter_type": (
(
cast(type, config.get("caching_adapter_type")).__name__
if config.get("caching_adapter_type") is not None
else None
)
if isinstance(config, dict)
else (
config.caching_adapter_type.__name__
if config.caching_adapter_type is not None
else None
)
),
} }
for id_, config in providers_dict.items() for id_, config in providers_dict.items()
} }