forked from uniborg/uniborg
Compare commits
No commits in common. "3178eb5fbafd214dbd06d60213ae41e7d0d1b8c0" and "5dd06471dbec179d3692ebf228934bedbde52862" have entirely different histories.
3178eb5fba
...
5dd06471db
|
@ -4,11 +4,13 @@
|
|||
|
||||
from telethon import events
|
||||
from telethon.utils import add_surrogate
|
||||
from telethon.tl.types import MessageEntityPre, DocumentAttributeFilename
|
||||
from telethon.tl.types import MessageEntityPre
|
||||
from telethon.tl.tlobject import TLObject
|
||||
from telethon.errors import MessageTooLongError
|
||||
import datetime
|
||||
|
||||
STR_LEN_MAX = 256
|
||||
BYTE_LEN_MAX = 64
|
||||
|
||||
|
||||
def parse_pre(text):
|
||||
text = text.strip()
|
||||
|
@ -18,7 +20,7 @@ def parse_pre(text):
|
|||
)
|
||||
|
||||
|
||||
def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
||||
def yaml_format(obj, indent=0):
|
||||
"""
|
||||
Pretty formats the given object as a YAML string which is returned.
|
||||
(based on TLObject.pretty_format)
|
||||
|
@ -53,8 +55,8 @@ def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
|||
indent -= 2
|
||||
elif isinstance(obj, str):
|
||||
# truncate long strings and display elipsis
|
||||
result = repr(obj[:max_str_len])
|
||||
if len(obj) > max_str_len:
|
||||
result = repr(obj[:STR_LEN_MAX])
|
||||
if len(obj) > STR_LEN_MAX:
|
||||
result += '…'
|
||||
return result
|
||||
elif isinstance(obj, bytes):
|
||||
|
@ -62,7 +64,7 @@ def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
|||
if all(0x20 <= c < 0x7f for c in obj):
|
||||
return repr(obj)
|
||||
else:
|
||||
return ('<…>' if len(obj) > max_byte_len else
|
||||
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
||||
' '.join(f'{b:02X}' for b in obj))
|
||||
elif isinstance(obj, datetime.datetime):
|
||||
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
||||
|
@ -72,6 +74,7 @@ def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
|||
result.append('\n')
|
||||
indent += 2
|
||||
for x in obj:
|
||||
|
||||
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
||||
result.append('\n')
|
||||
result.pop()
|
||||
|
@ -88,21 +91,4 @@ async def _(event):
|
|||
return
|
||||
msg = await event.message.get_reply_message()
|
||||
yaml_text = yaml_format(msg)
|
||||
try:
|
||||
await event.edit(yaml_text, parse_mode=parse_pre)
|
||||
except MessageTooLongError:
|
||||
await event.delete()
|
||||
yaml_text = yaml_format(
|
||||
msg,
|
||||
max_str_len=9999,
|
||||
max_byte_len=9999
|
||||
)
|
||||
await borg.send_file(
|
||||
await event.get_input_chat(),
|
||||
f'<pre>{yaml_text}</pre>'.encode('utf-8'),
|
||||
reply_to=msg,
|
||||
attributes=[
|
||||
DocumentAttributeFilename('info.html')
|
||||
],
|
||||
allow_cache=False
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue