diff --git a/addon_examples/local_anim.py b/addon_examples/local_anim.py index b582dec..c7247b1 100644 --- a/addon_examples/local_anim.py +++ b/addon_examples/local_anim.py @@ -20,13 +20,13 @@ bulk upload, like changing priority or removing a joint. """ import asyncio -import os import pathlib from abc import abstractmethod from typing import * from hippolyzer.lib.base import serialization as se from hippolyzer.lib.base.datatypes import UUID +from hippolyzer.lib.base.helpers import get_mtime from hippolyzer.lib.base.llanim import Animation from hippolyzer.lib.base.message.message import Block, Message from hippolyzer.lib.proxy import addon_ctx @@ -39,13 +39,6 @@ from hippolyzer.lib.proxy.region import ProxiedRegion from hippolyzer.lib.proxy.sessions import Session, SessionManager -def _get_mtime(path: str): - try: - return os.stat(path).st_mtime - except: - return None - - class LocalAnimAddon(BaseAddon): # name -> path, only for anims actually from files local_anim_paths: Dict[str, str] = SessionProperty(dict) @@ -176,7 +169,7 @@ class LocalAnimAddon(BaseAddon): anim_data = None if anim_path: old_mtime = cls.local_anim_mtimes.get(anim_name) - mtime = _get_mtime(anim_path) + mtime = get_mtime(anim_path) if only_if_changed and old_mtime == mtime: return diff --git a/hippolyzer/lib/base/helpers.py b/hippolyzer/lib/base/helpers.py index 28afcfd..4848c4f 100644 --- a/hippolyzer/lib/base/helpers.py +++ b/hippolyzer/lib/base/helpers.py @@ -2,6 +2,8 @@ from __future__ import annotations import codecs import functools +import os + import pkg_resources import re import weakref @@ -145,3 +147,10 @@ def to_chunks(chunkable: Sequence[_T], chunk_size: int) -> Generator[_T, None, N while chunkable: yield chunkable[:chunk_size] chunkable = chunkable[chunk_size:] + + +def get_mtime(path): + try: + return os.stat(path).st_mtime + except: + return None diff --git a/hippolyzer/lib/proxy/addons.py b/hippolyzer/lib/proxy/addons.py index aa16a0a..dcfdeb2 100644 --- a/hippolyzer/lib/proxy/addons.py +++ b/hippolyzer/lib/proxy/addons.py @@ -16,6 +16,7 @@ from types import ModuleType from typing import * from hippolyzer.lib.base.datatypes import UUID +from hippolyzer.lib.base.helpers import get_mtime from hippolyzer.lib.base.message.message import Message from hippolyzer.lib.base.network.transport import UDPPacket from hippolyzer.lib.proxy import addon_ctx @@ -31,13 +32,6 @@ if TYPE_CHECKING: LOG = logging.getLogger(__name__) -def _get_mtime(path): - try: - return os.stat(path).st_mtime - except: - return None - - class BaseInteractionManager: @abc.abstractmethod async def open_dir(self, caption: str = '', directory: str = '', filter_str: str = '') -> Optional[str]: @@ -187,7 +181,7 @@ class AddonManager: def _check_hotreloads(cls): """Mark addons that rely on changed files for reloading""" for filename, importers in cls.HOTRELOAD_IMPORTERS.items(): - mtime = _get_mtime(filename) + mtime = get_mtime(filename) if not mtime or mtime == cls.FILE_MTIMES.get(filename, None): continue @@ -216,7 +210,7 @@ class AddonManager: # Mark the caller as having imported (and being dependent on) `module` stack = inspect.stack()[1] cls.HOTRELOAD_IMPORTERS[imported_file].add(stack.filename) - cls.FILE_MTIMES[imported_file] = _get_mtime(imported_file) + cls.FILE_MTIMES[imported_file] = get_mtime(imported_file) importing_spec = next((s for s in cls.BASE_ADDON_SPECS if s.origin == stack.filename), None) imported_spec = next((s for s in cls.BASE_ADDON_SPECS if s.origin == imported_file), None) @@ -264,7 +258,7 @@ class AddonManager: for spec in cls.BASE_ADDON_SPECS[:]: had_mod = spec.name in cls.FRESH_ADDON_MODULES try: - mtime = _get_mtime(spec.origin) + mtime = get_mtime(spec.origin) mtime_changed = mtime != cls.FILE_MTIMES.get(spec.origin, None) if not mtime_changed and had_mod: continue