Skip to content

Commit

Permalink
start commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PPLev committed Apr 21, 2024
1 parent 638ee16 commit 4e45b90
Show file tree
Hide file tree
Showing 41 changed files with 2,787 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Byte-compiled / optimized / DLL files
__pycache__/

/venv/
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

.idea/

*.db
115 changes: 115 additions & 0 deletions core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import asyncio
import os
import re
import sys

from magic_filter import MagicFilter
from termcolor import cprint, colored
import logging

import packages
from jaa import JaaCore

F = MagicFilter()
version = "0.0.1"

logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",
level=logging.INFO)


class NotFoundFilerTextError(BaseException):
pass


class MetaSingleton(type):
_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class EventObserver:
def __init__(self):
self.callbacks = {}

async def _run_callback(self, callback, package: None | packages.TextPackage = None):
try:
await callback(package=package)
except Exception as exc:
logging.exception(f'Сопрограмма {callback.__module__}.{callback.__name__}() вызвала исключение: {exc}')

def register(self, filt: MagicFilter = None):
def wrapper(func: callable):
if not asyncio.iscoroutinefunction(func):
raise ValueError("function needs to be async which takes one parameter")

if filt:
async def wrapper_(package=None):
if filt.resolve(package.for_filter):
asyncio.run_coroutine_threadsafe(
coro=self._run_callback(func, package=package),
loop=asyncio.get_event_loop()
)

else:
async def wrapper_(package=None):
asyncio.run_coroutine_threadsafe(
coro=self._run_callback(func, package=package),
loop=asyncio.get_event_loop()
)

self.callbacks[f"{func.__module__}.{func.__name__}"] = wrapper_

return wrapper

async def __call__(self, package=None):
# TODO: Сделать контекст
for callback in self.callbacks.values():
await callback(package)


class Core(JaaCore, metaclass=MetaSingleton):
def __init__(self, observer_list=["on_input", "on_output"]):
super().__init__()

self.sound_playing = None
self.ws_server = None
self.gpt_talk = None
for observer in observer_list:
self.add_observer(observer)

def add_observer(self, observer_name):
if not hasattr(self, observer_name):
setattr(self, observer_name, EventObserver())

@staticmethod
async def start_loop():
while True:
await asyncio.sleep(0)

@staticmethod
async def _reboot():
# No recommend for use
python = sys.executable
os.execl(python, python, *sys.argv)

@staticmethod
def get_manifest(plugin_name: str):
manifest_re = r"manifest\s=\s(\{[\s\S]*?\})(?=\s*return manifest)"

if plugin_name.endswith(".py"):
with open(f"plugins/{plugin_name}", "r", encoding="utf-8") as file:
plugin_content = file.read()
else:
with open(f"plugins/{plugin_name}/__init__.py", "r", encoding="utf-8") as file:
plugin_content = file.read()

find_data = re.findall(manifest_re, plugin_content)[0]
data = eval(find_data)
return data


if __name__ == '__main__':
core = Core()
Loading

0 comments on commit 4e45b90

Please sign in to comment.