Skip to content

Commit

Permalink
✨🚧Supported requirements & notification
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed May 8, 2023
1 parent 54c9821 commit b295e03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
34 changes: 28 additions & 6 deletions cfdraw/app/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Dict
import sys
import subprocess

from typing import List
from typing import Type
from typing import AsyncGenerator
from aiohttp import ClientSession
from fastapi import FastAPI
Expand All @@ -9,9 +12,11 @@
from fastapi.middleware import cors

from cfdraw import constants
from cfdraw.utils import console
from cfdraw.config import get_config
from cfdraw.app.schema import IApp
from cfdraw.app.endpoints import *
from cfdraw.schema.plugins import IPlugin
from cfdraw.plugins.factory import Plugins
from cfdraw.plugins.factory import PluginFactory

Expand All @@ -34,9 +39,26 @@ def info(msg: str) -> None:
self.hash = random_hash()
info(f"🚀 Starting Backend Server at {self.config.api_url} ...")
info("🔨 Compiling Plugins & Endpoints...")
for plugin_type in self.plugins.values():
plugin_type.hash = self.hash
plugin_type.http_session = self.http_session
requirements = []
tplugin_with_notification: List[Type[IPlugin]] = []
for tplugin in self.plugins.values():
tplugin.hash = self.hash
tplugin.http_session = self.http_session
if tplugin.requirements is not None:
requirements += tplugin.requirements
if tplugin.notification is not None:
tplugin_with_notification.append(tplugin)
if requirements:
info("📦 Installing Requirements...")
cmd = f"{sys.executable} -m pip install {' '.join(requirements)}"
subprocess.run(cmd, shell=True)
if tplugin_with_notification:
console.rule("")
info(f"📣 Notifications:")
for tplugin in tplugin_with_notification:
console.rule(f"[bold green][ {tplugin.identifier} ]")
console.print(tplugin.notification)
console.rule("")
for endpoint in self.endpoints:
await endpoint.on_startup()
upload_root_path = self.config.upload_root_path
Expand All @@ -48,8 +70,8 @@ def info(msg: str) -> None:
# shutdown

await self.http_session.close()
for plugin_type in self.plugins.values():
plugin_type.http_session = None
for tplugin in self.plugins.values():
tplugin.http_session = None
for endpoint in self.endpoints:
await endpoint.on_shutdown()
self.http_session = None
Expand Down
9 changes: 9 additions & 0 deletions cfdraw/schema/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ class IPlugin(ABC):
# internal
_in_group: bool = False

# abstract

@property
@abstractmethod
def type(self) -> PluginType:
Expand Down Expand Up @@ -424,6 +426,13 @@ def send_progress(
) -> bool:
pass

# optional

## List of python package requirements of the plugin
requirements: Optional[List[str]] = None
## The notification (introductions, hardware requirements, etc.) you want to print out
notification: Optional[str] = None


class Subscription(str, Enum):
ALL = "__all__"
Expand Down

0 comments on commit b295e03

Please sign in to comment.