Use Protocol for proper config documentation

This commit is contained in:
Dima Gerasimov 2020-05-09 21:37:34 +01:00
parent c75747f371
commit 90b9d1d9c1

View file

@ -1,36 +1,43 @@
""" """
Reddit data: saved items/comments/upvotes/etc. Reddit data: saved items/comments/upvotes/etc.
""" """
from pathlib import Path from typing import TYPE_CHECKING
from typing import List, Sequence, Mapping, Iterator, Optional, TYPE_CHECKING if TYPE_CHECKING:
from typing_extensions import Protocol
from .kython.kompress import CPath class reddit(Protocol):
from .core.common import mcachew, get_files, LazyLogger, make_dict, PathIsh, classproperty '''
Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output
'''
export_path: PathIsh # path to the exported data
rexport : Optional[PathIsh] # path to a local clone of rexport
# todo extract this in documentation...
cfg = reddit
else:
from my.config import reddit as cfg from my.config import reddit as cfg
###
# TODO hmm, optional attribute and Optional type are quite different...
from typing import Optional
from types import ModuleType from types import ModuleType
from .core.common import classproperty, PathIsh
# todo would be nice to inherit from cfg to get defaults.. but mypy says it's incompatible -- because of classproperty??
class config: class config:
''' @classproperty
Reddit module uses [[rexport][https://github.com/karlicoss/rexport]] output. def export_path(cls) -> PathIsh:
legacy: Optional[PathIsh] = getattr(cfg, 'export_dir', None)
config reddit: if legacy is not None: # todo warn?
export_dir: Path | str # path to the exported data return legacy
rexport : Optional[Path | str] # path to a local clone of rexport return cfg.export_path
'''
# TODO config could load export.. is it too mad?
@classproperty @classproperty
def export_dir(cls) -> PathIsh: def rexport_module(cls) -> ModuleType:
# todo migrate this to export_path?
# TODO here we can defensively extract export_path/export_dir?
return cfg.export_dir
# TODO use export_path
@classproperty
def rexport(cls) -> ModuleType:
# todo return Type[rexport]?? # todo return Type[rexport]??
# todo ModuleIsh? # todo ModuleIsh?
rexport: Optional[PathIsh] = getattr(cfg, 'rexport', None) rexport: Optional[PathIsh] = getattr(cfg, 'rexport', None)
@ -42,23 +49,29 @@ class config:
import my.config.repos.rexport.dal as m import my.config.repos.rexport.dal as m
return m return m
# TODO maybe make the whole thing lazy?
if TYPE_CHECKING: if TYPE_CHECKING:
# TODO not sure what is the right way to handle this.. # TODO not sure what is the right way to handle this..
import my.config.repos.rexport.dal as rexport import my.config.repos.rexport.dal as rexport
else:
# interesting... if I move this before TYPE_CHECKING, it fails # TODO ugh. this would import too early
# apparently the former wins in case of mypy? rexport = config.rexport_module
rexport = config.rexport
### ###
from typing import List, Sequence, Mapping, Iterator
from .core.common import mcachew, get_files, LazyLogger, make_dict
logger = LazyLogger(__name__, level='debug') logger = LazyLogger(__name__, level='debug')
from pathlib import Path
from .kython.kompress import CPath
def inputs() -> Sequence[Path]: def inputs() -> Sequence[Path]:
# TODO rename to export_path? files = get_files(config.export_path)
files = get_files(config.export_dir)
# TODO Cpath better be automatic by get_files... # TODO Cpath better be automatic by get_files...
res = list(map(CPath, files)); assert len(res) > 0 res = list(map(CPath, files)); assert len(res) > 0
# todo move the assert to get_files? # todo move the assert to get_files?
@ -107,10 +120,6 @@ from multiprocessing import Pool
# TODO hmm. apparently decompressing takes quite a bit of time... # TODO hmm. apparently decompressing takes quite a bit of time...
def reddit(suffix: str) -> str:
return 'https://reddit.com' + suffix
class SaveWithDt(NamedTuple): class SaveWithDt(NamedTuple):
save: Save save: Save
backup_dt: datetime backup_dt: datetime