-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
77 lines (66 loc) · 2.06 KB
/
utils.py
File metadata and controls
77 lines (66 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import asyncio
import datetime
import json
import os
from dotenv import load_dotenv
load_dotenv()
LANGUAGE = os.environ.get("LANGUAGE", "en")
DATA_PATH = "data"
STATIC_PATH = "static"
COMMANDS_FILE = os.path.join(DATA_PATH, "commands.json")
def reformat_lang_dict(lang_dict: dict[str, dict[str, str]]) -> dict[str, dict[str, str]]:
"""
Reformat the language dictionary to have the language as first keys.
"""
reformatted_dict = {}
for message, translations in lang_dict.items():
for lang, text in translations.items():
if lang not in reformatted_dict:
reformatted_dict[lang] = {}
reformatted_dict[lang][message] = text
return reformatted_dict
# Load messages in the selected language
with open(os.path.join(STATIC_PATH, "lang.json"), "r") as f:
lang_dict = json.load(f)
msg: dict[str, str] = reformat_lang_dict(lang_dict).get(LANGUAGE, "en")
def read_from_file_sync(filename: str) -> str:
with open(filename, "r") as f:
return f.read()
async def read_from_file(filename: str) -> str:
loop = asyncio.get_event_loop()
content = await loop.run_in_executor(None, read_from_file_sync, filename)
return content
def write_to_file_sync(
filename: str,
content: str
):
with open(filename, "w") as f:
f.write(content)
async def write_to_file(
filename: str,
content: str
):
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, write_to_file_sync, filename, content)
def load_commands() -> list[dict[str, str]]:
if os.path.exists(COMMANDS_FILE):
with open(COMMANDS_FILE, "r") as file:
return json.load(file)
return []
def save_commands(commands: list[dict[str, str]]):
with open(COMMANDS_FILE, "w") as file:
json.dump(commands, file, indent=4)
def get_greeting() -> str:
"""
Returns a greeting based on the current time of day.
"""
now = datetime.datetime.now()
current_hour = now.hour
if current_hour < 5:
return msg.get("greeting_night")
elif current_hour < 15:
return msg.get("greeting_morning")
elif current_hour < 21:
return msg.get("greeting_afternoon")
else:
return msg.get("greeting_night")