Skip to content

Commit

Permalink
Merge branch 'lorcalhost:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MasaiasuOse authored Nov 25, 2021
2 parents 08b1f61 + c03ee35 commit 14e1fd4
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 490 deletions.
192 changes: 99 additions & 93 deletions btb_manager_telegram/buttons.py

Large diffs are not rendered by default.

58 changes: 31 additions & 27 deletions btb_manager_telegram/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ def menu(update: Update, _: CallbackContext) -> int:
logger.info("Start bot button pressed.")

reply_text_escape_fun(
i18n_format("bot_starting"),
i18n_format("btb.starting"),
reply_markup=reply_markup_config,
parse_mode="MarkdownV2",
)
status = buttons.start_bot()
message = [
i18n_format("bot_already_running"),
i18n_format("bot_started"),
i18n_format("bot_start_error"),
f"{i18n_format('installation_path_error', path=settings.ROOT_PATH)}\n{i18n_format('directory_hint')}",
f"{i18n_format('python_lib_error', path=settings.PYTHON_PATH)}\n",
i18n_format("btb.already_running"),
i18n_format("btb.started"),
i18n_format("btb.start_error"),
f"{i18n_format('btb.installation_path_error', path=settings.ROOT_PATH)}\n{i18n_format('btb.directory_hint')}",
f"{i18n_format('btb.lib_error', path=settings.PYTHON_PATH)}\n",
][status]
reply_text_escape_fun(
message,
Expand Down Expand Up @@ -345,7 +345,7 @@ def edit_coin(update: Update, _: CallbackContext) -> int:

if update.message.text != "/stop":
message = (
f"{i18n_format('edited_coin_list')}\n\n"
f"{i18n_format('coin_list.success')}\n\n"
f"```\n"
f"{update.message.text}\n"
f"```"
Expand All @@ -357,11 +357,11 @@ def edit_coin(update: Update, _: CallbackContext) -> int:
f.write(update.message.text + "\n")
except Exception as e:
logger.error(f"❌ Unable to edit coin list file: {e}", exc_info=True)
message = i18n_format("coin_edit_error")
message = i18n_format("coin_list.error")
else:
message = (
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('coin_list_not_modified')}"
f"{i18n_format('coin_list.not_modified')}"
)

keyboard = [[i18n_format("keyboard.go_back")]]
Expand All @@ -379,7 +379,7 @@ def edit_user_config(update: Update, _: CallbackContext) -> int:

if update.message.text != "/stop":
message = (
f"{i18n_format('edited_user_config')}\n\n"
f"{i18n_format('config.success')}\n\n"
f"```\n"
f"{update.message.text}\n"
f"```"
Expand All @@ -393,15 +393,15 @@ def edit_user_config(update: Update, _: CallbackContext) -> int:
logger.error(
f"❌ Unable to edit user configuration file: {e}", exc_info=True
)
message = i18n_format("user_config_error")
message = i18n_format("config.error")
try:
shutil.copymode(user_cfg_file_path, f"{user_cfg_file_path}.backup")
except:
pass
else:
message = (
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('user_config_not_modified')}"
f"{i18n_format('config.not_modified')}"
)

keyboard = [[i18n_format("keyboard.go_back")]]
Expand All @@ -420,7 +420,7 @@ def delete_db(update: Update, _: CallbackContext) -> int:
reply_text_escape_fun = reply_text_escape(update.message.reply_text)

if update.message.text != i18n_format("keyboard.go_back"):
message = i18n_format("deleted_db")
message = i18n_format("db.delete.success")
db_file_path = os.path.join(settings.ROOT_PATH, "data/crypto_trading.db")
pw_file_path = os.path.join(settings.ROOT_PATH, "data/paper_wallet.json")
log_file_path = os.path.join(settings.ROOT_PATH, "logs/crypto_trading.log")
Expand All @@ -432,17 +432,18 @@ def delete_db(update: Update, _: CallbackContext) -> int:
os.remove(pw_file_path)
except Exception as e:
logger.error(f"❌ Unable to delete database file: {e}", exc_info=True)
message = i18n_format("delete_db_error")
message = i18n_format("db.delete.error")
try:
with open(log_file_path, "w") as f:
f.truncate()
except Exception as e:
logger.error(f"❌ Unable to clear log file: {e}", exc_info=True)
message = i18n_format("clear_log_error")
message = i18n_format("db.delete.clear_log_error")

else:
message = (
f"{i18n_format('exited_no_change')}\n" f"{i18n_format('db_not_deleted')}"
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('db.delete.not_deleted')}"
)

keyboard = [[i18n_format("keyboard.ok")]]
Expand All @@ -459,7 +460,7 @@ def update_tg_bot(update: Update, _: CallbackContext) -> int:
reply_text_escape_fun = reply_text_escape(update.message.reply_text)

if update.message.text != i18n_format("keyboard.cancel_update"):
message = i18n_format("tgb_updating")
message = i18n_format("update.tgb.updating")
keyboard = [["/start"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
reply_text_escape_fun(
Expand All @@ -475,13 +476,14 @@ def update_tg_bot(update: Update, _: CallbackContext) -> int:
kill_btb_manager_telegram_process()
except Exception as e:
logger.error(f"❌ Unable to update BTB Manager Telegram: {e}", exc_info=True)
message = i18n_format("tgb_update_error")
message = i18n_format("update.tgb.error")
reply_text_escape_fun(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
else:
message = (
f"{i18n_format('exited_no_change')}\n" f"{i18n_format('tgb_not_updated')}"
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('update.tgb.not_updated')}"
)
keyboard = [[i18n_format("keyboard.ok_s")]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
Expand All @@ -503,8 +505,8 @@ def update_btb(update: Update, _: CallbackContext) -> int:

if update.message.text != i18n_format("keyboard.cancel_update"):
message = (
f"{i18n_format('btb_updating')}\n"
f"{i18n_format('wait_then_start_manually')}"
f"{i18n_format('update.btb.updating')}\n"
f"{i18n_format('update.btb.start_manually')}"
)
reply_text_escape_fun(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
Expand All @@ -526,7 +528,8 @@ def update_btb(update: Update, _: CallbackContext) -> int:
)
else:
message = (
f"{i18n_format('exited_no_change')}\n" f"{i18n_format('btb_bot_updated')}"
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('update.btb.not_updated')}"
)
reply_text_escape_fun(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
Expand Down Expand Up @@ -596,7 +599,8 @@ def panic(update: Update, _: CallbackContext) -> int:
message = i18n_format("killed_bot")
else:
message = (
f"{i18n_format('exited_no_change')}\n" f"{i18n_format('btb_not_updated')}"
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('update.btb.not_updated')}"
)

reply_text_escape_fun(message, reply_markup=reply_markup, parse_mode="MarkdownV2")
Expand Down Expand Up @@ -624,7 +628,7 @@ def execute_custom_script(update: Update, _: CallbackContext) -> int:
f"Unable to find script named {update.message.text} in custom_scripts.json file: {e}",
exc_info=True,
)
message = i18n_format("script_not_found", name=update.message.text)
message = i18n_format("script.not_found", name=update.message.text)
reply_text_escape_fun(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
Expand All @@ -646,7 +650,7 @@ def execute_custom_script(update: Update, _: CallbackContext) -> int:
)
except Exception as e:
logger.error(f"Error during script execution: {e}", exc_info=True)
message = i18n_format("script_error")
message = i18n_format("script.error")
reply_text_escape_fun(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
Expand Down Expand Up @@ -693,14 +697,14 @@ def cancel(update: Update, _: CallbackContext) -> int:

UPDATE_TG_HANDLER = MessageHandler(
Filters.regex(
f"^({i18n_format('keyboard.update')}|{i18n_format('keyboard.cancel_confirm')})$"
f"^({i18n_format('keyboard.update')}|{i18n_format('keyboard.cancel_update')})$"
),
update_tg_bot,
)

UPDATE_BTB_HANDLER = MessageHandler(
Filters.regex(
f"^({i18n_format('keyboard.update')}|{i18n_format('keyboard.cancel_confirm')})$"
f"^({i18n_format('keyboard.update')}|{i18n_format('keyboard.cancel_update')})$"
),
update_btb,
)
Expand Down
21 changes: 9 additions & 12 deletions btb_manager_telegram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def i18n_format(key, **kwargs):


def escape_tg(message):
escape_char = (".", "-", "?", "!", ">")
escape_char = (".", "-", "?", "!", ">", "{", "}")
escaped_message = ""
is_escaped = False
for cur_char in message:
Expand Down Expand Up @@ -201,8 +201,8 @@ def update_checker():
logger.info("BTB Manager Telegram update found.")

message = (
f"{i18n_format('tg_bot_update_availabe')}\n\n"
f"{i18n_format('tg_bot_update_instruction')}"
f"{i18n_format('update.tgb.available')}\n\n"
f"{i18n_format('update.tgb.instruction')}"
)
settings.TG_UPDATE_BROADCASTED_BEFORE = True
settings.CHAT.send_message(escape_tg(message), parse_mode="MarkdownV2")
Expand All @@ -218,14 +218,11 @@ def update_checker():
logger.info("Binance Trade Bot update found.")

message = (
f"{i18n_format('btb_update_availabe')}\n\n"
f"{i18n_format('btb_bot_update_instruction')}"
f"{i18n_format('update.btb.available')}\n\n"
f"{i18n_format('update.btb.instruction')}"
)
settings.BTB_UPDATE_BROADCASTED_BEFORE = True
settings.CHAT.send_message(escape_tg(message), parse_mode="MarkdownV2")
sleep(1)
bot.close()
sleep(1)
scheduler.enter(
60 * 60 * 12,
1,
Expand Down Expand Up @@ -262,7 +259,7 @@ def get_custom_scripts_keyboard():
custom_scripts_path = "./config/custom_scripts.json"
keyboard = []
custom_script_exist = False
message = i18n_format("script_not_found_in_file_error")
message = i18n_format("script.no_script")

if os.path.exists(custom_scripts_path):
with open(custom_scripts_path) as f:
Expand All @@ -272,12 +269,12 @@ def get_custom_scripts_keyboard():

if len(keyboard) >= 1:
custom_script_exist = True
message = i18n_format("select_script")
message = i18n_format("script.select")
else:
logger.warning(
"Unable to find custom_scripts.json file inside BTB-manager-telegram's config/ directory."
)
message = i18n_format("script_not_found_in_folder_error")
message = i18n_format("script.no_config")

keyboard.append([i18n_format("cancel")])
keyboard.append([i18n_format("keyboard.cancel")])
return keyboard, custom_script_exist, message
2 changes: 2 additions & 0 deletions i18n/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tmp.yml
*.old.yml
66 changes: 66 additions & 0 deletions i18n/check_translation_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import re

import click
import yaml


@click.command()
@click.argument("translation_file")
def main(translation_file):
"""
Check the translation file you want by
reading all the translation keys used
in the program and checking their exitence
in the given file
"""

print("Running...\r", end="")

with open(translation_file, "r") as f:
translation = yaml.safe_load(f)

def getkeys(pre, d):
keys = []
for k in d:
new_pre = k if pre == "" else pre + "." + k
if type(d[k]) == dict:
keys += getkeys(new_pre, d[k])
else:
keys.append(new_pre)
return keys

translation_keys = getkeys("", translation)

btb_dir = "../btb_manager_telegram"
main_text_output = ""
for filename in os.listdir(btb_dir):
full_path = os.path.join(btb_dir, filename)
if not os.path.isfile(full_path):
continue
with open(full_path, "r") as f:
content = f.read()

finds = [
i[4]
for i in re.findall(
"(i18n_format|i18n.t)\(((\n| )*)('|\")(([a-z]|_|\n|\.)*)('|\")", content
)
]

text_output = ""
for elt in finds:
if elt not in translation_keys:
text_output += f"\t\t{elt}\n"

if text_output != "":
main_text_output += f"\t{filename} :\n{text_output}\n"

if main_text_output != "":
print(f"Missing keys in '{translation_file}' from :\n{main_text_output}")
else:
print("No missing keys!")


if __name__ == "__main__":
main()
Loading

0 comments on commit 14e1fd4

Please sign in to comment.