Download google_drive_export/pipeline/config.py from FaustDerLegende/split: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/datasets/FaustDerLegende/split/resolve/main/google_drive_export/pipeline/config.py
- Command line
-
hf download hf://datasets/FaustDerLegende/split/google_drive_export/pipeline/config.py
-
curl -L -o config.py https://huggingface.co/datasets/FaustDerLegende/split/resolve/main/google_drive_export/pipeline/config.py
3.52 kB
| """Configuration loading, filesystem layout and logging setup.""" | |
| from __future__ import annotations | |
| import copy | |
| import logging | |
| import sys | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any | |
| import yaml | |
| PROJECT_ROOT = Path(__file__).resolve().parent.parent | |
| def load_yaml(path: Path) -> dict[str, Any]: | |
| with open(path, "r", encoding="utf-8") as fh: | |
| return yaml.safe_load(fh) or {} | |
| def deep_update(base: dict, override: dict) -> dict: | |
| """Recursively merge ``override`` into ``base`` (in place) and return it.""" | |
| for key, value in override.items(): | |
| if isinstance(value, dict) and isinstance(base.get(key), dict): | |
| deep_update(base[key], value) | |
| else: | |
| base[key] = copy.deepcopy(value) | |
| return base | |
| def load_config(path: str | Path | None = None, overrides: dict | None = None) -> dict[str, Any]: | |
| cfg_path = Path(path) if path else PROJECT_ROOT / "config" / "config.yaml" | |
| cfg = load_yaml(cfg_path) | |
| cfg["_config_dir"] = str(cfg_path.resolve().parent) | |
| if overrides: | |
| deep_update(cfg, overrides) | |
| return cfg | |
| def load_taxonomy(cfg: dict) -> dict[str, Any]: | |
| return load_yaml(Path(cfg["_config_dir"]) / "taxonomy.yaml") | |
| def load_queries(cfg: dict) -> dict[str, Any]: | |
| return load_yaml(Path(cfg["_config_dir"]) / "queries.yaml") | |
| class Paths: | |
| """Filesystem layout. Manifest paths are stored relative to ``data``.""" | |
| base: Path | |
| data: Path | |
| originals: Path | |
| images: Path | |
| review: Path | |
| realworld: Path | |
| reports: Path | |
| splits: Path | |
| manifest_db: Path | |
| manifest_jsonl: Path | |
| def abs(self, rel: str | Path) -> Path: | |
| return self.data / rel | |
| def rel(self, path: str | Path) -> str: | |
| return str(Path(path).resolve().relative_to(self.data.resolve())) | |
| def ensure(self) -> None: | |
| for p in (self.data, self.originals, self.images, self.review, self.realworld, | |
| self.reports, self.splits): | |
| p.mkdir(parents=True, exist_ok=True) | |
| def get_paths(cfg: dict, base: str | Path | None = None) -> Paths: | |
| base_dir = Path(base).resolve() if base else PROJECT_ROOT | |
| pcfg = cfg.get("paths", {}) | |
| data = base_dir / pcfg.get("data_dir", "data") | |
| return Paths( | |
| base=base_dir, | |
| data=data, | |
| originals=data / "originals", | |
| images=data / "images", | |
| review=data / "review", | |
| realworld=data / "realworld", | |
| reports=base_dir / pcfg.get("reports_dir", "reports"), | |
| splits=base_dir / pcfg.get("splits_dir", "splits"), | |
| manifest_db=data / "manifest.db", | |
| manifest_jsonl=data / "manifest.jsonl", | |
| ) | |
| def setup_logging(verbose: bool = False) -> None: | |
| """Configure logging *before* icrawler is imported. | |
| icrawler calls ``logging.basicConfig`` when a crawler is constructed; that is a | |
| no-op once the root logger already has handlers, so configuring first keeps our | |
| format and level in charge. | |
| """ | |
| level = logging.DEBUG if verbose else logging.INFO | |
| root = logging.getLogger() | |
| if not root.handlers: | |
| handler = logging.StreamHandler(sys.stderr) | |
| handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)s: %(message)s", | |
| datefmt="%H:%M:%S")) | |
| root.addHandler(handler) | |
| root.setLevel(level) | |
| logging.getLogger("urllib3").setLevel(logging.WARNING) | |
| logging.getLogger("PIL").setLevel(logging.WARNING) | |