Compare commits
4 Commits
5faea8f69f
...
releases/v
| Author | SHA1 | Date | |
|---|---|---|---|
| ffd758d9a4 | |||
| 526661e498 | |||
| b0c87a427c | |||
| 7b74e0b0b8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
__pycache__
|
||||
venv/
|
||||
.venv/
|
||||
.vscode/
|
||||
log*.log
|
||||
config_manager.egg-info
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "config_manager"
|
||||
version = "1.1.0"
|
||||
version = "1.2.0"
|
||||
description = "Config manager for building applications"
|
||||
authors = [
|
||||
{ name = "Aleksei Zosimov", email = "lesha.spb@gmail.com" }
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
from config_manager import ConfigManager
|
||||
@@ -1,2 +0,0 @@
|
||||
from .config_manager import ConfigManager
|
||||
from .log_manager import LogManager
|
||||
@@ -1,150 +0,0 @@
|
||||
import logging
|
||||
import logging.config
|
||||
import asyncio
|
||||
import json
|
||||
import yaml
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
========
|
||||
from .log_manager import LogManager
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
|
||||
class ConfigManager:
|
||||
DEFAULT_UPDATE_INTERVAL = 5.0
|
||||
DEFAULT_WORK_INTERVAL = 2.0
|
||||
|
||||
def __init__(self, path: str, log_manager: Optional[LogManager] = None):
|
||||
self.path = path
|
||||
self.config: Any = None
|
||||
self._last_hash = None
|
||||
self.update_interval = self.DEFAULT_UPDATE_INTERVAL
|
||||
self.work_interval = self.DEFAULT_WORK_INTERVAL
|
||||
self._halt = asyncio.Event()
|
||||
self._task: Optional[asyncio.Task] = None
|
||||
self._loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
|
||||
self._log_manager = log_manager or LogManager()
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def _read_file_sync(self) -> str:
|
||||
with open(self.path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
async def _read_file_async(self) -> str:
|
||||
return await asyncio.to_thread(self._read_file_sync)
|
||||
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
def _parse_config(self, data) -> Any:
|
||||
========
|
||||
def _parse_config(self, data) -> Any:
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
extension = os.path.splitext(self.path)[1].lower()
|
||||
if extension in (".yaml", ".yml"):
|
||||
return yaml.safe_load(data)
|
||||
else:
|
||||
return json.loads(data)
|
||||
|
||||
def _update_intervals_from_config(self) -> None:
|
||||
if not self.config:
|
||||
return
|
||||
upd = self.config.get("update_interval")
|
||||
wrk = self.config.get("work_interval")
|
||||
|
||||
if isinstance(upd, (int, float)) and upd > 0:
|
||||
self.update_interval = float(upd)
|
||||
self.logger.info(f"Update interval set to {self.update_interval} seconds")
|
||||
else:
|
||||
self.update_interval = self.DEFAULT_UPDATE_INTERVAL
|
||||
|
||||
if isinstance(wrk, (int, float)) and wrk > 0:
|
||||
self.work_interval = float(wrk)
|
||||
self.logger.info(f"Work interval set to {self.work_interval} seconds")
|
||||
else:
|
||||
self.work_interval = self.DEFAULT_WORK_INTERVAL
|
||||
|
||||
async def _update_config(self) -> None:
|
||||
try:
|
||||
data = await self._read_file_async()
|
||||
current_hash = hash(data)
|
||||
if current_hash != self._last_hash:
|
||||
new_config = self._parse_config(data)
|
||||
self.config = new_config
|
||||
self._last_hash = current_hash
|
||||
|
||||
self._log_manager.apply_config(new_config)
|
||||
self._update_intervals_from_config()
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error reading/parsing config file: {e}")
|
||||
|
||||
def execute(self) -> None:
|
||||
"""
|
||||
Метод для переопределения в подклассах.
|
||||
Здесь может быть блокирующая работа.
|
||||
Запускается в отдельном потоке.
|
||||
"""
|
||||
pass
|
||||
|
||||
async def _worker_loop(self) -> None:
|
||||
while not self._halt.is_set():
|
||||
await asyncio.to_thread(self.execute)
|
||||
await asyncio.sleep(self.work_interval)
|
||||
|
||||
async def _periodic_update_loop(self) -> None:
|
||||
while not self._halt.is_set():
|
||||
await self._update_config()
|
||||
await asyncio.sleep(self.update_interval)
|
||||
|
||||
async def _run(self) -> None:
|
||||
"""Внутренняя корутина, запускающая все циклы"""
|
||||
self._halt.clear()
|
||||
self.logger.info("ConfigManager started")
|
||||
try:
|
||||
await asyncio.gather(
|
||||
self._worker_loop(),
|
||||
self._periodic_update_loop()
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
self.logger.info("ConfigManager tasks cancelled")
|
||||
finally:
|
||||
self.logger.info("ConfigManager stopped")
|
||||
|
||||
def start(self) -> None:
|
||||
"""Запускает менеджер конфигурации в текущем event loop"""
|
||||
if self._task is not None and not self._task.done():
|
||||
self.logger.warning("ConfigManager is already running")
|
||||
return
|
||||
|
||||
try:
|
||||
self._loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
self.logger.error("start() must be called from within an async context")
|
||||
raise
|
||||
|
||||
self._task = self._loop.create_task(self._run())
|
||||
self.logger.info("ConfigManager task created")
|
||||
|
||||
async def stop(self) -> None:
|
||||
"""Останавливает менеджер конфигурации и ожидает завершения"""
|
||||
if self._task is None:
|
||||
self.logger.warning("ConfigManager is not running")
|
||||
return
|
||||
|
||||
self.logger.info("ConfigManager stopping...")
|
||||
self._halt.set()
|
||||
|
||||
try:
|
||||
await self._task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
self._task = None
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
logger.info("ConfigManager stopped successfully")
|
||||
========
|
||||
self.logger.info("ConfigManager stopped successfully")
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
2
src/config_manager/__init__.py
Normal file
2
src/config_manager/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .cfg_manager import ConfigManager
|
||||
from .log_manager import LogManager
|
||||
@@ -6,12 +6,7 @@ import yaml
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
========
|
||||
from .log_manager import LogManager
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
|
||||
class ConfigManager:
|
||||
DEFAULT_UPDATE_INTERVAL = 5.0
|
||||
@@ -37,11 +32,7 @@ class ConfigManager:
|
||||
async def _read_file_async(self) -> str:
|
||||
return await asyncio.to_thread(self._read_file_sync)
|
||||
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
def _parse_config(self, data) -> Any:
|
||||
========
|
||||
def _parse_config(self, data) -> Any:
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
extension = os.path.splitext(self.path)[1].lower()
|
||||
if extension in (".yaml", ".yml"):
|
||||
return yaml.safe_load(data)
|
||||
@@ -143,8 +134,4 @@ class ConfigManager:
|
||||
pass
|
||||
|
||||
self._task = None
|
||||
<<<<<<<< HEAD:src/config_manager.py
|
||||
logger.info("ConfigManager stopped successfully")
|
||||
========
|
||||
self.logger.info("ConfigManager stopped successfully")
|
||||
>>>>>>>> develop:src/basic_application/config_manager.py
|
||||
@@ -1,26 +0,0 @@
|
||||
import os
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
|
||||
import logging
|
||||
import asyncio
|
||||
|
||||
from config_manager.config_manager import ConfigManager
|
||||
logger = logging.getLogger()
|
||||
|
||||
# Пример наследования и переопределения execute
|
||||
class MyApp(ConfigManager):
|
||||
def execute(self) -> None:
|
||||
logger.info("Executing blocking work with config: %s", self.config)
|
||||
|
||||
|
||||
async def main():
|
||||
app = MyApp("config.yaml")
|
||||
app.start()
|
||||
await asyncio.sleep(20)
|
||||
await app.stop()
|
||||
logger.info("Work finished.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -1,5 +1,5 @@
|
||||
# === Раздел с общими конфигурационными параметрами ===
|
||||
param: 5
|
||||
runtime: 5
|
||||
|
||||
# === Логирование ===
|
||||
log:
|
||||
131
tests/test.py
131
tests/test.py
@@ -1,131 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch, mock_open, AsyncMock
|
||||
import asyncio
|
||||
import logging
|
||||
import io
|
||||
import json
|
||||
import yaml
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from basic_application.basic_application import ConfigManager
|
||||
|
||||
|
||||
class TestConfigManager(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
self.json_data = json.dumps({
|
||||
"work_interval": 1,
|
||||
"update_interval": 1,
|
||||
"logging": {
|
||||
"version": 1,
|
||||
"handlers": {"console": {"class": "logging.StreamHandler", "level": "DEBUG"}},
|
||||
"root": {"handlers": ["console"], "level": "DEBUG"}
|
||||
},
|
||||
"some_key": "some_value"
|
||||
})
|
||||
self.yaml_data = """
|
||||
work_interval: 1
|
||||
update_interval: 1
|
||||
logging:
|
||||
version: 1
|
||||
handlers:
|
||||
console:
|
||||
class: logging.StreamHandler
|
||||
level: DEBUG
|
||||
root:
|
||||
handlers: [console]
|
||||
level: DEBUG
|
||||
some_key: some_value
|
||||
"""
|
||||
|
||||
@patch("builtins.open", new_callable=mock_open, read_data="")
|
||||
async def test_read_file_async_json(self, mock_file):
|
||||
mock_file.return_value.read = lambda: self.json_data
|
||||
cm = ConfigManager("config.json")
|
||||
content = await cm._read_file_async()
|
||||
self.assertEqual(content, self.json_data)
|
||||
|
||||
@patch("builtins.open", new_callable=mock_open, read_data="")
|
||||
async def test_read_file_async_yaml(self, mock_file):
|
||||
mock_file.return_value.read = lambda: self.yaml_data
|
||||
cm = ConfigManager("config.yaml")
|
||||
content = await cm._read_file_async()
|
||||
self.assertEqual(content, self.yaml_data)
|
||||
|
||||
def test_parse_json(self):
|
||||
cm = ConfigManager("config.json")
|
||||
parsed = cm._parse_config(self.json_data)
|
||||
self.assertIsInstance(parsed, dict)
|
||||
self.assertEqual(parsed["some_key"], "some_value")
|
||||
|
||||
def test_parse_yaml(self):
|
||||
cm = ConfigManager("config.yaml")
|
||||
parsed = cm._parse_config(self.yaml_data)
|
||||
self.assertIsInstance(parsed, dict)
|
||||
self.assertEqual(parsed["some_key"], "some_value")
|
||||
|
||||
@patch("basic_application.basic_application.logging.config.dictConfig")
|
||||
def test_apply_logging_config(self, mock_dict_config):
|
||||
cm = ConfigManager("config.json")
|
||||
cm._apply_logging_config({"logging": {"version": 1}})
|
||||
mock_dict_config.assert_called_once()
|
||||
|
||||
async def test_update_config_changes_config_and_intervals(self):
|
||||
# Мокаем чтение файла
|
||||
m = mock_open(read_data=self.json_data)
|
||||
with patch("builtins.open", m):
|
||||
cm = ConfigManager("config.json")
|
||||
|
||||
# Проверяем исходные интервалы
|
||||
self.assertEqual(cm.update_interval, cm.DEFAULT_UPDATE_INTERVAL)
|
||||
self.assertEqual(cm.work_interval, cm.DEFAULT_WORK_INTERVAL)
|
||||
|
||||
await cm._update_config()
|
||||
|
||||
# После обновления данные заполнены
|
||||
self.assertIsInstance(cm.config, dict)
|
||||
self.assertEqual(cm.update_interval, 1.0)
|
||||
self.assertEqual(cm.work_interval, 1.0)
|
||||
|
||||
async def test_execute_called_in_worker_loop(self):
|
||||
called = False
|
||||
|
||||
class TestCM(ConfigManager):
|
||||
def execute(self2):
|
||||
nonlocal called
|
||||
called = True
|
||||
|
||||
cm = TestCM("config.json")
|
||||
|
||||
async def stop_after_delay():
|
||||
await asyncio.sleep(0.1)
|
||||
cm.stop()
|
||||
|
||||
# Запускаем worker_loop и через 0.1 сек останавливаем
|
||||
await asyncio.gather(cm._worker_loop(), stop_after_delay())
|
||||
|
||||
self.assertTrue(called)
|
||||
|
||||
async def test_periodic_update_loop_runs(self):
|
||||
count = 0
|
||||
|
||||
class TestCM(ConfigManager):
|
||||
async def _update_config(self2):
|
||||
nonlocal count
|
||||
count += 1
|
||||
if count >= 2:
|
||||
self2.stop()
|
||||
|
||||
cm = TestCM("config.json")
|
||||
|
||||
await cm._periodic_update_loop()
|
||||
|
||||
self.assertGreaterEqual(count, 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.WARNING) # отключаем логи во время тестов
|
||||
unittest.main()
|
||||
@@ -1,24 +1,24 @@
|
||||
from basic_application import ConfigManager
|
||||
#import os
|
||||
#os.chdir(os.path.dirname(__file__))
|
||||
|
||||
from config_manager import ConfigManager
|
||||
import logging
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
import os
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
class MyApp(ConfigManager):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.iter = 0
|
||||
|
||||
|
||||
def execute(self) -> None:
|
||||
logger.info(f"current iteration {self.iter}")
|
||||
self.iter += 1
|
||||
|
||||
|
||||
async def main():
|
||||
app = MyApp("config.yaml")
|
||||
app.start()
|
||||
@@ -29,7 +29,3 @@ async def main():
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user