Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
Update json.py
Browse files Browse the repository at this point in the history
  • Loading branch information
code-rgb authored Oct 19, 2020
1 parent 4b47f9c commit 683b76c
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions userge/plugins/tools/json.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
"""Json / Yaml"""

# by - @DeletedUser420

import re
import json
import re
import sys

import yaml
from userge import userge, Message
from userge import Message, userge


@userge.on_cmd("json", about={
'header': "message object to json",
'usage': "reply {tr}json to any message"})
async def jsonify(message: Message):
msg = str(message.reply_to_message) if message.reply_to_message else str(message)
await message.edit_or_send_as_file(text=msg, filename="json.txt", caption="Too Large")
"""Json-ify"""
if message.reply_to_message:
msg = str(message.reply_to_message)
else:
msg = str(message)
await message.edit_or_send_as_file(
text=msg,
filename="json.txt",
caption="Too Large"
)


@userge.on_cmd("yaml", about={
'header': "message object to yaml",
'usage': "reply {tr}yaml to any message"})
async def yamlify(message: Message):
"""get yaml"""
msg = str(message.reply_to_message) if message.reply_to_message else str(message)
yaml_ify = yaml.dump(json.loads(msg), allow_unicode=True)
"""yaml-ify"""
if message.reply_to_message:
msg = str(message.reply_to_message)
else:
msg = str(message)
json_file = json.loads(msg)
yaml_ify = yaml.dump(convert(json_file), allow_unicode=True)
regex = r"(\s+|)(?:- _:|_:)[\s]"
result = re.sub(regex, " ", yaml_ify, re.MULTILINE)
if result:
Expand All @@ -28,3 +45,17 @@ async def yamlify(message: Message):
filename="yaml.txt",
caption="Too Large"
)


def convert(obj):
if isinstance(obj, bool):
return bool_emoji(obj)
if isinstance(obj, (list, tuple)):
return [convert(item) for item in obj]
if isinstance(obj, dict):
return {convert(key): convert(value) for key, value in obj.items()}
return obj


def bool_emoji(choice: bool) -> str:
return "✔️" if choice else "✖️"

1 comment on commit 683b76c

@vercel
Copy link

@vercel vercel bot commented on 683b76c Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.