2026-10-16 07:20:44 +02:00
|
|
|
# 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)
|
2026-10-17 02:23:43 +02:00
|
|
|
else:
|
|
|
|
|
source = path.read_text()
|
|
|
|
|
if not CodeGenerator.is_current(source):
|
|
|
|
|
self.write(slug, CodeGenerator.embedded_schema(source) or schema)
|
2026-10-16 07:20:44 +02:00
|
|
|
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()
|