41 lines
1.2 KiB
Python
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()
|