Services - please help! #215
Replies: 2 comments 5 replies
-
here's a slightly more complete version note that the service is not registering any service base from sphinxcontrib.needs.logging import getLogger
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Type
Config = TypeVar("Config")
class BaseService(ABC, Generic[Config]):
def __init__(self, config: Config):
self.log = getLogger(__name__)
self.config = config
@abstractmethod
def request(self, *args, **kwargs):
pass
@staticmethod
@abstractmethod
def options() -> Type[Config]:
pass
@staticmethod
@abstractmethod
def name() -> str:
pass github service @dataclass
class GithubServiceConfig:
username: str
token: str
url: str = "https://api.github.com/"
max_amount: int = -1
max_content_lines: int = -1
id_prefix: str = "GITHUB_"
layout: str = "github"
download_avatars: bool = True
download_folder: Path = Path("github_images")
class GithubService(BaseService):
@staticmethod
def options() -> Type[GithubServiceConfig]:
return GithubServiceConfig
@staticmethod
def name() -> str:
return "github" service manager from sphinxcontrib.needs.logging import getLogger
from sphinxcontrib.needs.services.base import BaseService
from typing import Dict, Type, Any
Options = Dict[str, Any]
class ServiceManager:
def __init__(self, extra_options: Dict[str, Options]):
self.log = getLogger(__name__)
self.extra_options = extra_options
self.services: Dict[str, BaseService] = {}
def register(self, clazz: Type[BaseService]):
name = clazz.name()
raw_config = self.extra_options.get(name, {})
config = clazz.options().__init__(**raw_config)
self.services[name] = clazz(config) it's a bit tidier, no? |
Beta Was this translation helpful? Give feedback.
-
Yes, BaseService class does not contain much (only request func)
Not 100% what you mean.
Your dataclass approach looks promising. We could put most of the "registration" stuff to an other function, which is part of BaseService class and gets called by the manager after service initialization. from sphinxcontrib.needs.logging import getLogger
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Type
Config = TypeVar("Config")
class BaseService(ABC, Generic[Config]):
def __init__(self, config: Config):
self.log = getLogger(__name__)
self.config = config
@abstractmethod
def request(self, *args, **kwargs):
pass
@staticmethod
@abstractmethod
def options() -> Type[Config]:
pass
@staticmethod
@abstractmethod
def name() -> str:
pass
def register_need_type(name):
try:
add_need_type(name, ...)
except:
....
def register_layout(name, layout):
.... Or even better the "register" functions are part of the ServiceManager and each service gets access to it during |
Beta Was this translation helpful? Give feedback.
-
I have lots of questions about the 'services' module!
i think there's lots of room for improvement.
service
requires?__init__
method of the github service is working pretty hard here. it doesn't feel like the responsibility of checking the presence of config values, registering need_types, and registering layouts should live here.i've made a quick sketch of an alternative approach-
base class
github service
service_manager
what do you think of this as a possible direction?
Beta Was this translation helpful? Give feedback.
All reactions