mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2025-11-01 22:11:23 +03:00
Squashed commit of the following:
commit 70890bbec6a4eedb47a75499a13de70fb8189137 Author: Maxim Slipenko <maxim@slipenko.com> Date: Tue Aug 20 11:49:18 2024 +0300 wip: фиксит импорт commit 1ee4c92ca0e4886b445397aa139d321919fd85da Author: Maxim Slipenko <maxim@slipenko.com> Date: Tue Aug 20 11:48:55 2024 +0300 wip: добавляет message_db_logger для проверки commit a4c43fe5607e27103f03496f197292c4f942eb92 Author: Maxim Slipenko <maxim@slipenko.com> Date: Tue Aug 20 10:46:54 2024 +0300 wip: piccolo test
This commit is contained in:
0
src/karkas_piccolo/README.md
Normal file
0
src/karkas_piccolo/README.md
Normal file
3
src/karkas_piccolo/karkas_piccolo/__main__.py
Normal file
3
src/karkas_piccolo/karkas_piccolo/__main__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .main import main
|
||||
|
||||
main()
|
||||
6
src/karkas_piccolo/karkas_piccolo/conf/apps/AppConfig.py
Normal file
6
src/karkas_piccolo/karkas_piccolo/conf/apps/AppConfig.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from piccolo.conf.apps import AppConfig as OrigAppConfig
|
||||
|
||||
|
||||
class AppConfig(OrigAppConfig):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -0,0 +1,6 @@
|
||||
from piccolo.conf.apps import AppRegistry as OrigAppRegistry
|
||||
|
||||
|
||||
class AppRegistry(OrigAppRegistry):
|
||||
def __init__(self, apps_configs):
|
||||
self.app_configs = apps_configs
|
||||
2
src/karkas_piccolo/karkas_piccolo/conf/apps/__init__.py
Normal file
2
src/karkas_piccolo/karkas_piccolo/conf/apps/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .AppConfig import AppConfig
|
||||
from .AppRegistry import AppRegistry
|
||||
2
src/karkas_piccolo/karkas_piccolo/karkas/__init__.py
Normal file
2
src/karkas_piccolo/karkas_piccolo/karkas/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .get_info_path import get_info_path
|
||||
from .migrate_forward import migrate_forward
|
||||
@@ -0,0 +1,9 @@
|
||||
import inspect
|
||||
import os
|
||||
|
||||
|
||||
def get_info_path():
|
||||
caller_frame = inspect.currentframe()
|
||||
caller_file = caller_frame.f_code.co_filename
|
||||
caller_directory = os.path.dirname(os.path.abspath(caller_file))
|
||||
return os.path.join(caller_directory, "info.json")
|
||||
@@ -0,0 +1,7 @@
|
||||
from piccolo.apps.migrations.commands.forwards import run_forwards
|
||||
|
||||
import karkas_piccolo.patches.app.finder # noqa: F401
|
||||
|
||||
|
||||
async def migrate_forward():
|
||||
await run_forwards("all")
|
||||
8
src/karkas_piccolo/karkas_piccolo/main.py
Normal file
8
src/karkas_piccolo/karkas_piccolo/main.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import karkas_piccolo.patches.cli.finder # noqa: F401
|
||||
import karkas_piccolo.patches.migrations # noqa: F401
|
||||
|
||||
|
||||
def main():
|
||||
from piccolo.main import main as piccolo_main
|
||||
|
||||
piccolo_main()
|
||||
28
src/karkas_piccolo/karkas_piccolo/patches/app/finder.py
Normal file
28
src/karkas_piccolo/karkas_piccolo/patches/app/finder.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import typing as t
|
||||
|
||||
import piccolo.conf
|
||||
import piccolo.conf.apps
|
||||
from piccolo.conf.apps import PiccoloAppModule, PiccoloConfModule
|
||||
|
||||
from karkas_core.singleton import Singleton
|
||||
|
||||
|
||||
class Finder(piccolo.conf.apps.Finder):
|
||||
def get_app_modules(self):
|
||||
apps = []
|
||||
|
||||
for k, m in self.get_app_registry().app_configs.items():
|
||||
module = PiccoloAppModule(name=k)
|
||||
module.APP_CONFIG = m
|
||||
apps.append(module)
|
||||
|
||||
return apps
|
||||
|
||||
def get_piccolo_conf_module(
|
||||
self, module_name: t.Optional[str] = None
|
||||
) -> t.Optional[PiccoloConfModule]:
|
||||
singleton = Singleton()
|
||||
return singleton.storage["_database"]["conf"]
|
||||
|
||||
|
||||
piccolo.conf.apps.Finder = Finder
|
||||
97
src/karkas_piccolo/karkas_piccolo/patches/cli/finder.py
Normal file
97
src/karkas_piccolo/karkas_piccolo/patches/cli/finder.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
import typing as t
|
||||
from pathlib import Path
|
||||
|
||||
import piccolo.conf.apps
|
||||
from piccolo.conf.apps import PiccoloAppModule, PiccoloConfModule
|
||||
from piccolo.engine.sqlite import SQLiteEngine
|
||||
|
||||
from karkas_piccolo.conf.apps import AppRegistry
|
||||
from karkas_piccolo.utils import get_info_json
|
||||
|
||||
|
||||
class UnsafeFSLoader:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def _resolve_module_from_path(self, module_name: str):
|
||||
path = Path(self.path)
|
||||
|
||||
if module_name != ".":
|
||||
path = path.joinpath(module_name.replace(".", "/"))
|
||||
|
||||
if path.is_dir():
|
||||
init_file_path = path / "__init__.py"
|
||||
if not init_file_path.exists():
|
||||
raise FileNotFoundError(f"File {init_file_path} does not exist.")
|
||||
file_path = init_file_path
|
||||
else:
|
||||
path = path.with_suffix(".py")
|
||||
if path.is_file():
|
||||
file_path = path
|
||||
else:
|
||||
raise ValueError(f"Module not found: {module_name}")
|
||||
|
||||
return file_path
|
||||
|
||||
def load(self):
|
||||
full_path = self._resolve_module_from_path("db")
|
||||
|
||||
if full_path.name == "__init__.py":
|
||||
module_name = full_path.parent.name
|
||||
path = full_path.parent.parent.absolute()
|
||||
else:
|
||||
module_name = full_path.stem
|
||||
path = full_path.parent.absolute()
|
||||
|
||||
sys.path.insert(0, str(path))
|
||||
|
||||
# Загружаем спецификацию модуля
|
||||
spec = importlib.util.spec_from_file_location(module_name, full_path)
|
||||
|
||||
# Создаем модуль
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
|
||||
# Выполняем модуль
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
return module
|
||||
|
||||
|
||||
class Finder(piccolo.conf.apps.Finder):
|
||||
def get_app_modules(self):
|
||||
apps = []
|
||||
|
||||
for k, m in self.get_app_registry().app_configs.items():
|
||||
module = PiccoloAppModule(name=k)
|
||||
module.APP_CONFIG = m
|
||||
apps.append(module)
|
||||
|
||||
return apps
|
||||
|
||||
def get_piccolo_conf_module(
|
||||
self, module_name: t.Optional[str] = None
|
||||
) -> t.Optional[PiccoloConfModule]:
|
||||
info = get_info_json()
|
||||
|
||||
fallback_module = PiccoloConfModule(
|
||||
name=info["id"],
|
||||
)
|
||||
|
||||
module_dir = os.getcwd()
|
||||
|
||||
print(module_dir)
|
||||
|
||||
module = UnsafeFSLoader(module_dir).load()
|
||||
|
||||
fallback_module.DB = SQLiteEngine(config={})
|
||||
fallback_module.APP_REGISTRY = AppRegistry(
|
||||
apps_configs={info["id"]: module.APP_CONFIG}
|
||||
)
|
||||
|
||||
return fallback_module
|
||||
|
||||
|
||||
piccolo.conf.apps.Finder = Finder
|
||||
42
src/karkas_piccolo/karkas_piccolo/patches/migrations.py
Normal file
42
src/karkas_piccolo/karkas_piccolo/patches/migrations.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import typing as t
|
||||
|
||||
import piccolo.apps.migrations.piccolo_app
|
||||
from piccolo.apps.migrations.commands.backwards import backwards
|
||||
from piccolo.apps.migrations.commands.check import check
|
||||
from piccolo.apps.migrations.commands.clean import clean
|
||||
from piccolo.apps.migrations.commands.forwards import forwards
|
||||
from piccolo.apps.migrations.commands.new import new as orig_new
|
||||
from piccolo.conf.apps import AppConfig, Command
|
||||
|
||||
from karkas_piccolo.utils import get_info_json
|
||||
|
||||
|
||||
async def new(
|
||||
auto: bool = False,
|
||||
desc: str = "",
|
||||
auto_input: t.Optional[str] = None,
|
||||
):
|
||||
info = get_info_json()
|
||||
|
||||
return await orig_new(
|
||||
info["id"],
|
||||
auto=auto,
|
||||
desc=desc,
|
||||
auto_input=auto_input,
|
||||
)
|
||||
|
||||
|
||||
APP_CONFIG = AppConfig(
|
||||
app_name="migrations",
|
||||
migrations_folder_path="",
|
||||
commands=[
|
||||
Command(callable=backwards, aliases=["b", "back", "backward"]),
|
||||
Command(callable=check),
|
||||
Command(callable=clean),
|
||||
Command(callable=forwards, aliases=["f", "forward"]),
|
||||
Command(callable=new, aliases=["n", "create"]),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
piccolo.apps.migrations.piccolo_app.APP_CONFIG = APP_CONFIG
|
||||
8
src/karkas_piccolo/karkas_piccolo/utils.py
Normal file
8
src/karkas_piccolo/karkas_piccolo/utils.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import json
|
||||
|
||||
|
||||
def get_info_json(path="info.json"):
|
||||
with open(path, "r", encoding="utf-8") as file:
|
||||
data = json.load(file)
|
||||
|
||||
return data
|
||||
1131
src/karkas_piccolo/poetry.lock
generated
Normal file
1131
src/karkas_piccolo/poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
2
src/karkas_piccolo/poetry.toml
Normal file
2
src/karkas_piccolo/poetry.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[virtualenvs]
|
||||
in-project = true
|
||||
19
src/karkas_piccolo/pyproject.toml
Normal file
19
src/karkas_piccolo/pyproject.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[tool.poetry]
|
||||
name = "karkas-piccolo"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Maxim Slipenko <maxim@slipenko.com>"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
karkas-piccolo = 'karkas_piccolo.main:main'
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.10,<3.13"
|
||||
piccolo = { extras=["sqlite"], version = "^1.16.0" }
|
||||
karkas-core = { path = "../karkas_core", develop = true }
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
Reference in New Issue
Block a user