Skip to content

Commit

Permalink
fix(hmr): fix hot module reloader
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Mar 11, 2024
1 parent ed91ac8 commit c6e83fe
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/dicergirl/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from nonebot.adapters import Bot
from pathlib import Path
from diceutils.status import StatusPool
from infini.core import Core
from infini.loader import Loader
from infini.output import Output

import importlib
import sys
import asyncio

status = StatusPool.register("dicergirl")
core: Core
Expand All @@ -20,23 +23,38 @@ def hmr(output: Output = None):

packages = status.get("bot", "packages") or []

for package in packages:
for name in [name for name in sys.modules.keys() if name.startswith(package)]:
sys.modules[name] = (
importlib.reload(sys.modules[name])
if name in sys.modules
else importlib.import_module(name)
)
sys.modules[package] = (
importlib.reload(sys.modules[package])
if package in sys.modules
else importlib.import_module(package)
)

with Loader() as loader:
for package in packages:
for name in [
name for name in sys.modules.keys() if name.startswith(package)
]:
sys.modules[name] = (
importlib.reload(sys.modules[name])
if name in sys.modules
else importlib.import_module(name)
)
sys.modules[package] = (
importlib.reload(sys.modules[package])
if package in sys.modules
else importlib.import_module(package)
)

loader.load(package)
core = loader.into_core()

if output:
output.status = 0


def file_upload(bot: Bot, filepath: Path, output: Output):
asyncio.run(
bot.call_api(
"upload_group_file",
**{
"group_id": output.variables["group_id"],
"file": str(filepath),
"name": filepath.name,
},
)
)
output.status = 0

0 comments on commit c6e83fe

Please sign in to comment.