def walk_package(package: str, path: str, ignores: Set[str]) -> Iterator[ContentSrc]: logger.info(f"Loading {package}/{path}") if not path.endswith("/"): path = path + "/" packagepath = importlib_resources.files(package) # type: ignore root = packagepath / path if not root.is_dir(): return for srcpath in _iter_package_files(root, ignores): destname = posixpath.relpath(srcpath, root) dirname, fname = posixpath.split(posixpath.relpath(srcpath, packagepath)) metadatapath = ( srcpath.parent / f"{srcpath.name}{miyadaiku.METADATA_FILE_SUFFIX}" ) if metadatapath.exists(): text = metadatapath.read_bytes() metadata = yaml.load(text, Loader=yaml.FullLoader) or {} else: metadata = {} yield ContentSrc( package=package, srcpath=str(posixpath.relpath(srcpath, packagepath)), metadata=metadata, contentpath=to_contentpath(destname), mtime=None, )
def add_bytes(self, type: str, path: str, body: bytes) -> Content: cpath = to_contentpath(path) contentsrc = ContentSrc( package=None, srcpath=None, metadata={"type": type}, contentpath=cpath, mtime=0, ) content = contents.build_content(contentsrc, body) self._contentfiles[content.src.contentpath] = content return content
def walk_directory(path: Path, ignores: Set[str]) -> Iterator[ContentSrc]: logger.info(f"Loading {path}") path = path.expanduser().resolve() if not path.is_dir(): return for root, dirs, files in os.walk(path): rootpath = Path(root) if rootpath.stem.startswith("."): continue dirs[:] = (dirname for dirname in dirs if not is_ignored(ignores, dirname)) filenames = ( filename for filename in files if not is_ignored(ignores, filename) ) for name in filenames: filename = (rootpath / name).resolve() dirname, fname = os.path.split(filename) metadatafile = os.path.join( dirname, f"{fname}{miyadaiku.METADATA_FILE_SUFFIX}" ) metadata: Dict[Any, Any] = {} if os.path.isfile(metadatafile): text = open(metadatafile, encoding=miyadaiku.YAML_ENCODING).read() metadata = yaml.load(text, Loader=yaml.FullLoader) or {} mtime = filename.stat().st_mtime yield ContentSrc( package="", srcpath=str(filename), metadata=metadata, contentpath=to_contentpath(str(filename.relative_to(path))), mtime=mtime, )
def create_contexts( siteroot: SiteRoot, srcs: Sequence[Tuple[str, str]], config: Optional[Dict[Any, Any]] = None, ) -> List[context.OutputContext]: siteroot.clear() for path, src in srcs: siteroot.write_text(siteroot.contents / path, src) if config is None: config = {} site = siteroot.load(config, {}) ret = [] jinjaenv = site.build_jinjaenv() for path, src in srcs: contentpath = to_contentpath(path) if site.files.has_content(contentpath): content = site.files.get_content(contentpath) builders = builder.create_builders(site, content) for b in builders: ctx = b.build_context(site, jinjaenv) ret.append(ctx) return ret
def test_to_contentpath() -> None: assert ((), "filename.txt") == to_contentpath("filename.txt")