avoid unconditional import of hypexport

This commit is contained in:
Dima Gerasimov 2019-11-17 14:10:05 +00:00
parent e72ad57f91
commit 9efe74aeb2

View file

@ -1,11 +1,20 @@
from typing import Dict, List, NamedTuple, Optional, Sequence from typing import Dict, List, NamedTuple, Optional, Sequence, Any
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from .common import group_by_key, the, cproperty, PathIsh from .common import group_by_key, the, cproperty, PathIsh
from my_configuration import paths
import my_configuration.repos.hypexport.model as hypexport try:
# TODO might be worth having a special mode for type checking with and without my_configuration?
# TODO could somehow use typing.TYPE_CHECKING for that?
import my_configuration.repos.hypexport.model as hypexport
Highlight = hypexport.Highlight
Model = hypexport.Model
except:
Model = Any # type: ignore
Highlight = Any # type: ignore
class Config(NamedTuple): class Config(NamedTuple):
export_path_: Optional[PathIsh]=None export_path_: Optional[PathIsh]=None
@ -26,7 +35,12 @@ class Config(NamedTuple):
if hp is not None: if hp is not None:
raise RuntimeError("TODO") raise RuntimeError("TODO")
else: else:
global Model
global Highlight
import my_configuration.repos.hypexport.model as hypexport import my_configuration.repos.hypexport.model as hypexport
# TODO a bit hacky.. not sure how to make it both mypy and runtime safe..
Model = hypexport.Model
Highlight = hypexport.Highlight
return hypexport return hypexport
config = Config() config = Config()
@ -41,16 +55,13 @@ def configure(*, export_path: Optional[PathIsh]=None, hypexport_path: Optional[P
# TODO for the purposes of mypy, try importing my_configuration anyway? # TODO for the purposes of mypy, try importing my_configuration anyway?
# return type for this method as well # return type for this method as well
# TODO check if it works at runtime.. # TODO check if it works at runtime..
def get_model() -> hypexport.Model: def get_model() -> Model:
export_path = config.export_path export_path = config.export_path
sources = list(sorted(export_path.glob('*.json'))) sources = list(sorted(export_path.glob('*.json')))
model = hypexport.Model(sources) model = Model(sources)
return model return model
Highlight = hypexport.Highlight
class Page(NamedTuple): class Page(NamedTuple):
""" """
Represents annotated page along with the highlights Represents annotated page along with the highlights
@ -58,11 +69,11 @@ class Page(NamedTuple):
highlights: Sequence[Highlight] highlights: Sequence[Highlight]
@cproperty @cproperty
def link(self): def link(self) -> str:
return the(h.page_link for h in self.highlights) return the(h.page_link for h in self.highlights)
@cproperty @cproperty
def title(self): def title(self) -> str:
return the(h.page_title for h in self.highlights) return the(h.page_title for h in self.highlights)
@cproperty @cproperty