refactored some attributes as cached properties
This commit is contained in:
parent
15707ada59
commit
e7558d996b
@ -10,7 +10,7 @@ dependencies = [
|
||||
dynamic = []
|
||||
authors = []
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
requires-python = ">=3.8"
|
||||
classifiers = []
|
||||
version = "0.0.0"
|
||||
|
||||
|
@ -10,6 +10,8 @@ from collections import defaultdict
|
||||
import toml
|
||||
from datetime import datetime
|
||||
import jinja2
|
||||
from functools import cached_property
|
||||
|
||||
|
||||
from . import config
|
||||
|
||||
@ -147,9 +149,9 @@ class ArticleTranslation:
|
||||
|
||||
# initializing the location of the article translation
|
||||
self.language_code = stem_to_language_code(self.file.stem)
|
||||
self.location_in_tree = [self.language_code, *self.article.location_in_tree]
|
||||
self.url = "/" + "/".join(self.location_in_tree)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||
self.slug_path = [self.language_code, *self.article.slug_path[1:]]
|
||||
self.url = "/" + "/".join(self.slug_path)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.slug_path)
|
||||
self.cross_article_context = TRANSLATED_CROSS_ARTICLE_CONTEXT[self.language_code][self.article.slug] = {}
|
||||
|
||||
|
||||
@ -212,32 +214,61 @@ class ArticleTranslation:
|
||||
f.write(TEMPLATE["article_translation"].render(self.context))
|
||||
|
||||
|
||||
class ArticleConfig(TypedDict):
|
||||
name: str
|
||||
datetime: str
|
||||
author: str
|
||||
|
||||
|
||||
class Article:
|
||||
def __init__(self, directory: Path, location_in_tree: Optional[List[str]] = None, is_root: bool = False, parent: Optional[Article] = None):
|
||||
directory: Path
|
||||
|
||||
@cached_property
|
||||
def config(self) -> ArticleConfig:
|
||||
config_file = self.directory / "index.toml"
|
||||
return toml.load(config_file) if config_file.exists() else {}
|
||||
|
||||
@cached_property
|
||||
def slug(self) -> str:
|
||||
slug = self.config.get("name", self.directory.name)
|
||||
if slug in ARTICLE_LAKE:
|
||||
logger.error("two articles have the same name at %s and %r", ARTICLE_LAKE[slug].directory, self.directory)
|
||||
exit(1)
|
||||
return slug
|
||||
|
||||
article_path: List[Article]
|
||||
|
||||
@cached_property
|
||||
def slug_path(self) -> List[str]:
|
||||
return [a.slug for a in self.article_path]
|
||||
|
||||
@cached_property
|
||||
def url(self) -> str:
|
||||
return "/" + "/".join(self.slug_path[1:])
|
||||
|
||||
@cached_property
|
||||
def dist_path(self) -> Path:
|
||||
return Path(config.setup.dist_directory, *self.slug_path[1:])
|
||||
|
||||
|
||||
def __init__(self, directory: Path, article_path: Optional[List[str]] = None, is_root: bool = False, parent: Optional[Article] = None):
|
||||
self.directory = directory
|
||||
|
||||
self.article_path: List[Article] = article_path or []
|
||||
self.article_path.append(self)
|
||||
|
||||
self.context: Dict[str, Any] = {}
|
||||
self.context_shared: Dict[str, Any] = {}
|
||||
if parent is not None:
|
||||
self.context["parent"] = parent.context_shared
|
||||
|
||||
# initializing the config values of the article
|
||||
config_file = self.directory / "index.toml"
|
||||
self.config = toml.load(config_file) if config_file.exists() else {}
|
||||
|
||||
# initializing the location and slug of the article
|
||||
self.slug = self.config.get("name", self.directory.name)
|
||||
if self.slug in ARTICLE_LAKE:
|
||||
logger.error("two articles have the same name at %s and %r", ARTICLE_LAKE[self.slug].directory, self.directory)
|
||||
exit(1)
|
||||
self.cross_article_context = CROSS_ARTICLE_CONTEXT[self.slug] = {}
|
||||
ARTICLE_LAKE[self.slug] = self
|
||||
|
||||
self.location_in_tree: List[str] = location_in_tree or []
|
||||
if not is_root:
|
||||
self.location_in_tree.append(self.slug)
|
||||
self.url = "/" + "/".join(self.location_in_tree)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||
|
||||
print()
|
||||
print(self.slug_path)
|
||||
print(self.url)
|
||||
print(self.dist_path)
|
||||
|
||||
# build the tree
|
||||
self.child_articles: List[Article] = []
|
||||
@ -255,13 +286,13 @@ class Article:
|
||||
elif c.is_dir():
|
||||
self.child_articles.append(Article(
|
||||
directory=c,
|
||||
location_in_tree=self.location_in_tree.copy(),
|
||||
article_path=self.article_path.copy(),
|
||||
parent=self,
|
||||
))
|
||||
|
||||
self.article_translations_list.sort(key=lambda a: a.priority, reverse=True)
|
||||
|
||||
logger.info("found %s at %s with the translations %s", self.slug, ".".join(list(self.location_in_tree)), ",".join(self.article_translations_map.keys()))
|
||||
logger.info("found %s at %s with the translations %s", self.slug, ".".join(list(self.slug_path)), ",".join(self.article_translations_map.keys()))
|
||||
|
||||
def __init_context__(self):
|
||||
self.context_shared["url"] = self.url
|
||||
|
Loading…
x
Reference in New Issue
Block a user