70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
||
|
||
import re
|
||
import os
|
||
import logging
|
||
|
||
|
||
class ConfigUpdater:
|
||
def __init__(self):
|
||
self.logger = logging.getLogger(__name__)
|
||
self.keys_file = 'keys.py'
|
||
|
||
def update_start_topic_id(self, new_topic_id):
|
||
"""Обновление start_topic_id в файле keys.py"""
|
||
try:
|
||
# Проверяем существование файла
|
||
if not os.path.exists(self.keys_file):
|
||
self.logger.error(f"Файл {self.keys_file} не найден")
|
||
return False
|
||
|
||
# Читаем содержимое файла
|
||
with open(self.keys_file, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# Ищем строку с start_topic_id и заменяем значение
|
||
pattern = r'^(start_topic_id\s*=\s*)(.*)$'
|
||
replacement = rf'\g<1>{new_topic_id}'
|
||
|
||
new_content, count = re.subn(pattern, replacement, content, flags=re.MULTILINE)
|
||
|
||
if count == 0:
|
||
self.logger.error("Не найдена переменная start_topic_id в keys.py")
|
||
return False
|
||
|
||
# Записываем обновленное содержимое
|
||
with open(self.keys_file, 'w', encoding='utf-8') as f:
|
||
f.write(new_content)
|
||
|
||
self.logger.info(f"Успешно обновлен start_topic_id на {new_topic_id} в {self.keys_file}")
|
||
return True
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"Ошибка при обновлении start_topic_id: {e}")
|
||
return False
|
||
|
||
def get_current_start_topic_id(self):
|
||
"""Получение текущего значения start_topic_id из keys.py"""
|
||
try:
|
||
if not os.path.exists(self.keys_file):
|
||
self.logger.error(f"Файл {self.keys_file} не найден")
|
||
return None
|
||
|
||
with open(self.keys_file, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# Ищем значение start_topic_id
|
||
match = re.search(r'^start_topic_id\s*=\s*(.*)$', content, flags=re.MULTILINE)
|
||
if match:
|
||
value = match.group(1).strip()
|
||
# Пытаемся преобразовать в число
|
||
try:
|
||
return int(value) if value and value != 'None' else None
|
||
except ValueError:
|
||
return None
|
||
|
||
return None
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"Ошибка при чтении start_topic_id: {e}")
|
||
return None |