fix: build queue and dependencies + add key_map_fn

This commit is contained in:
relikd
2022-11-22 10:58:14 +01:00
parent e7ae59fadf
commit 0891be06e2
9 changed files with 300 additions and 134 deletions

View File

@@ -1,20 +1,9 @@
from lektor.reporter import reporter, style
from typing import List, Dict, Optional, TypeVar
from typing import Callable, Any, Union, Generic
T = TypeVar('T')
def report_config_error(key: str, field: str, val: str, e: Exception) -> None:
''' Send error message to Lektor reporter. Indicate which field is bad. '''
msg = '[ERROR] invalid config for [{}.{}] = "{}", Error: {}'.format(
key, field, val, repr(e))
try:
reporter._write_line(style(msg, fg='red'))
except Exception:
print(msg) # fallback in case Lektor API changes
def most_used_key(keys: List[T]) -> Optional[T]:
''' Find string with most occurrences. '''
if len(keys) < 3:
@@ -58,3 +47,16 @@ def build_url(parts: List[str]) -> str:
if '.' not in url.rsplit('/', 1)[-1]:
url += '/'
return url or '/'
class cached_property(Generic[T]):
''' Calculate complex property only once. '''
def __init__(self, fn: Callable[[Any], T]) -> None:
self.fn = fn
def __get__(self, obj: object, typ: Union[type, None] = None) -> T:
if obj is None:
return self # type: ignore
ret = obj.__dict__[self.fn.__name__] = self.fn(obj)
return ret