ebuild_rasp2/ebuild/reloader.py

96 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
""" ebuild/reloader.py
İki ayrı konfigürasyon modülünü (config_statics, config_variables)
izleyen ve dosya değişikliklerinde importlib.reload çağıran yardımcı sınıf.
"""
import os
import importlib
from . import config_statics as cfg_s
from . import config_variables as cfg_v
class ConfigReloader(object):
""" Statik ve dinamik config dosyalarını izler.
maybe_reload() çağrıldığında:
- Değişmiş dosyaları yeniden yükler
- Hangi grubun değiştiğini dict olarak döner.
"""
def __init__(self):
self._statics_path = None
self._statics_last = None
self._vars_path = None
self._vars_last = None
# Statics
try:
p = getattr(cfg_s, "__file__", None)
if p and p.endswith((".pyc", ".pyo")):
p = p[:-1]
self._statics_path = os.path.abspath(p) if p else None
self._statics_last = (
os.path.getmtime(self._statics_path)
if self._statics_path else None
)
except Exception:
self._statics_path, self._statics_last = None, None
# Variables
try:
p = getattr(cfg_v, "__file__", None)
if p and p.endswith((".pyc", ".pyo")):
p = p[:-1]
self._vars_path = os.path.abspath(p) if p else None
self._vars_last = (
os.path.getmtime(self._vars_path)
if self._vars_path else None
)
except Exception:
self._vars_path, self._vars_last = None, None
def _check_and_reload(self, path_attr, last_attr, module):
path = getattr(self, path_attr)
if not path:
return False
try:
cur = os.path.getmtime(path)
except Exception:
return False
last = getattr(self, last_attr)
if last is None or cur > last:
try:
importlib.reload(module)
setattr(self, last_attr, os.path.getmtime(path))
return True
except Exception:
return False
return False
def maybe_reload(self):
""" Değişiklik algılanırsa ilgili config'i yeniden yükler.
return:
{
"statics_changed": bool,
"variables_changed": bool,
}
"""
statics_changed = self._check_and_reload(
"_statics_path", "_statics_last", cfg_s
)
variables_changed = self._check_and_reload(
"_vars_path", "_vars_last", cfg_v
)
return {
"statics_changed": statics_changed,
"variables_changed": variables_changed,
}