def _load_config(self, cli_config): name = self.import_name.replace(".", "_") key = f"{name}_config".upper() config_path = os.getenv(key, None) if config_path: config_path = self._normalize_config_path(config_path) print(f"[INFO] Load config file {config_path!r}") try: with open(config_path) as f: content = f.read() except FileNotFoundError: msg = f"config file {config_path!r} not found" raise ConfigError(msg) from None try: config = toml.loads(content) except toml.TomlDecodeError: msg = f"config file {config_path!r} is not valid TOML file" raise ConfigError(msg) from None config.update(cli_config) else: print(f"[INFO] No config file provided " f"by {key} environment variable") config = cli_config self.config_path = config_path try: self.config = self.config_class(**config) except Invalid as ex: raise ConfigError(ex.message) from None self._config_dict = {"config": self.config} for k, v in asdict(self.config).items(): self._config_dict[f"config.{k}"] = v
def print_config(self): table = [("Key", "Value", "Schema")] config_schema = self.config.__schema__.items for key, value in sorted(asdict(self.config).items()): schema = shorten_text(config_schema[key].repr()) value = shorten_text(str(value), 20) table.append((key, value, schema)) self._print_table(table, title="Configs")
def print_config(self): table = [('Key', 'Value', 'Schema')] config_schema = self.config.__schema__.items for key, value in sorted(asdict(self.config).items()): schema = config_schema[key] table.append((key, _shorten(str(value)), _shorten(schema.repr()))) table = SingleTable(table, title='Configs') print(table.table)
def run(self): if self.config.print_config: self.print_config() if self.config.print_plugin: self.print_plugin() if self.config.print_service: self.print_service() server_config = asdict(self.config, keys=fields(ServerConfig)) run(self, **server_config)
def __init__(self, import_name, **cli_config): self.import_name = import_name self._load_config_module() self._load_intro() self._load_plugins() self._load_schema_compiler() self._load_config_class() self._load_config(cli_config) self._config_dict = asdict(self.config) self._active_plugins() self._load_services() self.router = Router(self.services, self.config.url_prefix)
def set_user_marked(self, feed_id, offset, is_user_marked=True) -> Story: try: story = Story.get_by_offset(feed_id, offset) except Story.DoesNotExist: story = None if (not story) and is_user_marked: common_story = self.get_by_offset(feed_id, offset, detail=True) d = asdict(common_story) d.pop('content_length', None) story = Story(**d) story.is_user_marked = is_user_marked story.save() elif story: Story.set_user_marked_by_id(story.id, is_user_marked=is_user_marked) return story
def model(value): return asdict(Model(value))
def to_dict(self): return asdict(self)
def test_asdict(): user = User(id=123, name="test") assert asdict(user) == {"id": 123, "name": "test"} assert asdict(user, keys=["name"]) == {"name": "test"}