modm_data.html.document
1# Copyright 2022, Niklas Hauser 2# SPDX-License-Identifier: MPL-2.0 3 4import re 5import logging 6from pathlib import Path 7from functools import cached_property 8from .chapter import Chapter 9 10LOGGER = logging.getLogger(__name__) 11 12 13class Document: 14 def __init__(self, path: str): 15 self.path = Path(path) 16 self.relpath = self.path.relative_to(Path().cwd()) 17 self.fullname = self.path.stem 18 self.name = self.fullname.split("-")[0] 19 self.version = self.fullname.split("-")[1] 20 21 @cached_property 22 def _chapters(self) -> dict[str, Chapter]: 23 chapters = {} 24 for path in self.path.glob("*.html"): 25 chapters[path.stem.replace("_", " ")] = Chapter(path) 26 return chapters 27 28 @cached_property 29 def path_pdf(self) -> str: 30 return Path(str(self.path).replace("/html/", "/pdf/") + ".pdf") 31 32 def chapters(self, pattern: str = None) -> list[Chapter]: 33 if pattern is None: 34 return list(self._chapters.values()) 35 return [c for name, c in self._chapters.items() 36 if re.search(pattern, name, re.IGNORECASE)] 37 38 def chapter(self, pattern: str) -> Chapter: 39 chapters = self.chapters(pattern) 40 if len(chapters) == 0: 41 LOGGER.error(f"Cannot find chapter with pattern '{pattern}'!") 42 if len(chapters) > 1: 43 LOGGER.error(f"Found multiple chapters with pattern '{pattern}'!") 44 assert len(chapters) == 1 45 return chapters[0] 46 47 def __repr__(self) -> str: 48 return f"Doc({self.fullname})"
LOGGER =
<Logger modm_data.html.document (WARNING)>
class
Document:
14class Document: 15 def __init__(self, path: str): 16 self.path = Path(path) 17 self.relpath = self.path.relative_to(Path().cwd()) 18 self.fullname = self.path.stem 19 self.name = self.fullname.split("-")[0] 20 self.version = self.fullname.split("-")[1] 21 22 @cached_property 23 def _chapters(self) -> dict[str, Chapter]: 24 chapters = {} 25 for path in self.path.glob("*.html"): 26 chapters[path.stem.replace("_", " ")] = Chapter(path) 27 return chapters 28 29 @cached_property 30 def path_pdf(self) -> str: 31 return Path(str(self.path).replace("/html/", "/pdf/") + ".pdf") 32 33 def chapters(self, pattern: str = None) -> list[Chapter]: 34 if pattern is None: 35 return list(self._chapters.values()) 36 return [c for name, c in self._chapters.items() 37 if re.search(pattern, name, re.IGNORECASE)] 38 39 def chapter(self, pattern: str) -> Chapter: 40 chapters = self.chapters(pattern) 41 if len(chapters) == 0: 42 LOGGER.error(f"Cannot find chapter with pattern '{pattern}'!") 43 if len(chapters) > 1: 44 LOGGER.error(f"Found multiple chapters with pattern '{pattern}'!") 45 assert len(chapters) == 1 46 return chapters[0] 47 48 def __repr__(self) -> str: 49 return f"Doc({self.fullname})"
39 def chapter(self, pattern: str) -> Chapter: 40 chapters = self.chapters(pattern) 41 if len(chapters) == 0: 42 LOGGER.error(f"Cannot find chapter with pattern '{pattern}'!") 43 if len(chapters) > 1: 44 LOGGER.error(f"Found multiple chapters with pattern '{pattern}'!") 45 assert len(chapters) == 1 46 return chapters[0]