You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
3.4 KiB
78 lines
3.4 KiB
1 year ago
|
# -*- coding: utf8 -*-
|
||
|
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
|
||
|
|
||
|
# Сообщения для работы с документами
|
||
|
|
||
|
from bot_sys import log, config, user_access
|
||
|
from bot_modules import groups_utils
|
||
|
from template import simple_message
|
||
|
import odf
|
||
|
|
||
|
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT):
|
||
|
async def DocFiles(a_Message):
|
||
|
user_id = str(a_Message.from_user.id)
|
||
|
user_groups= groups_utils.GetUserGroupData(a_Bot, user_id)
|
||
|
if not user_access.CheckAccess(a_Bot.GetRootIDs(), a_AccessFunc(), user_groups, access_mode):
|
||
|
return await simple_message.AccessDeniedMessage(a_Bot, a_GetButtonsFunc, user_id, a_Message, user_groups)
|
||
|
|
||
|
msg = a_CaptionMessage.GetDesc()
|
||
|
msg = msg.replace('@time', a_Bot.GetLog().GetTime())
|
||
|
|
||
|
files = a_FilesFunc(user_id)
|
||
|
if not files:
|
||
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
|
||
|
return
|
||
|
|
||
|
for file_path, dict_replace in files.values():
|
||
|
if not dict_replace:
|
||
|
continue
|
||
|
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id)
|
||
|
document = await GetFile(a_Bot, file_path)
|
||
|
if document is None:
|
||
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
|
||
|
else:
|
||
|
await a_Bot.SendDocument(
|
||
|
user_id,
|
||
|
document,
|
||
|
msg,
|
||
|
simple_message.ProxyGetButtonsTemplate(a_GetButtonsFunc)(a_Message, user_groups),
|
||
|
simple_message.ProxyGetButtonsTemplate(a_GetInlineButtonsFunc)(a_Message, user_groups)
|
||
|
)
|
||
|
return DocFiles
|
||
|
|
||
|
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
|
||
|
document = odf.opendocument.load(a_FilePath)
|
||
|
if document == None:
|
||
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.')
|
||
|
return None
|
||
|
|
||
|
for element in document.getElementsByType(text.Span):
|
||
|
extracted_text = odf.teletype.extractText(element)
|
||
|
|
||
|
for file_path, dict_replace in a_DictReplace.values():
|
||
|
if extracted_text.find('Replace this') != -1:
|
||
|
extracted_text = extracted_text.replace('Replace this', 'to this')
|
||
|
|
||
|
new_element = odf.text.Span()
|
||
|
new_element.setAttribute('stylename', element.getAttribute('stylename'))
|
||
|
new_element.addText(extracted_text)
|
||
|
|
||
|
element.parentNode.insertBefore(new_element, element)
|
||
|
element.parentNode.removeChild(element)
|
||
|
|
||
|
new_file_path = a_FilePath[:-4]
|
||
|
new_file_path += f"{a_user_id}.odt"
|
||
|
document.save(new_file_path)
|
||
|
return new_file_path
|
||
|
|
||
|
async def GetFile(a_Bot, a_Path):
|
||
|
if not a_Path:
|
||
|
return None
|
||
|
try:
|
||
|
document = open(a_Path, 'rb')
|
||
|
a_Bot.GetLog().Success(f'Загружен файл {a_Path}')
|
||
|
return document
|
||
|
except Exception as e:
|
||
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_Path}. Ошибка {str(e)}')
|
||
|
return None
|