Files
devplacepy/devplacepy/services/community/files.py
T
retoor 6f80a820da
DevPlace CI / test (push) Failing after 3m13s
Zo doen wijdat in oost
2026-10-16 07:20:44 +02:00

41 lines
1.2 KiB
Python

# retoor <retoor@molodetz.nl>
from __future__ import annotations
import importlib.util
from pathlib import Path
from types import ModuleType
from devplacepy import config
from devplacepy.services.community.generator import CodeGenerator
class CommunityFiles:
def __init__(self, root: Path | None = None):
self.root = root
def directory(self, slug: str) -> Path:
return (self.root or config.COMMUNITIES_DIR) / slug
def collectors_path(self, slug: str) -> Path:
return self.directory(slug) / "collectors.py"
def write(self, slug: str, schema: dict) -> Path:
return CodeGenerator(schema).write(self.directory(slug))
def load(self, slug: str, schema: dict) -> ModuleType:
path = self.collectors_path(slug)
if not path.exists():
self.write(slug, schema)
spec = importlib.util.spec_from_file_location(f"community_collectors_{slug.replace('-', '_')}", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def read(self, slug: str) -> str:
path = self.collectors_path(slug)
return path.read_text() if path.exists() else ""
files = CommunityFiles()