Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
now using the lang helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 8, 2020
1 parent ca45d0f commit a734600
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 44 deletions.
3 changes: 2 additions & 1 deletion cron/cachempgauntlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from conn.mysql import myconn
from objects.levels import Gauntlet, MapPack
from objects.misc import RGB
from helpers.lang import lang
import logging

map_packs = []
Expand All @@ -22,7 +23,7 @@ async def cron_cache_mappacks():
try:
colour = RGB(colour_list[0],colour_list[1],colour_list[2])
except IndexError:
logging.warn(f"Map pack '{pack[2]}' has an invalid colour! Setting to white.")
logging.warn(lang.warn("pack_invalid_colour", pack[1]))
colour = RGB(255,255,255)
map_packs.append(MapPack(
pack[0],
Expand Down
5 changes: 3 additions & 2 deletions cron/cpcalc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from conn.mysql import myconn
from helpers.generalhelper import create_offsets_from_page, dict_keys
from helpers.userhelper import user_helper
from helpers.lang import lang
import logging
import math

Expand All @@ -19,9 +20,9 @@ async def cron_calc_cp():
await mycursor.execute("SELECT COUNT(*) FROM levels WHERE starStars > 0 OR starFeatured > 0 OR starEpic > 0 OR awarded > 0 OR magic>0")
count = (await mycursor.fetchone())[0]
pages = math.ceil(count/PAGE_SIZE)
logging.debug(f"CP cron has {pages} pages.")
logging.debug(lang.debug("cron_cp_pages", pages))
for i in range(pages):
logging.debug(f"Running page {i+1}/{pages}")
logging.debug(lang.debug("cron_cp_page_running", i+1, pages))
offset = create_offsets_from_page(i, PAGE_SIZE)
await mycursor.execute("SELECT extID, starStars,starFeatured,starEpic,awarded,magic FROM levels WHERE starStars > 0 OR starFeatured > 0 OR starEpic > 0 OR awarded > 0 OR magic>0 LIMIT %s OFFSET %s", (PAGE_SIZE, offset))
levels_db = await mycursor.fetchall()
Expand Down
9 changes: 5 additions & 4 deletions cron/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .cachelb import cron_top_stars, cron_top_cp
from .cachempgauntlets import cron_cache_mappacks, cron_cache_gauntlets
from helpers.timehelper import Timer, time_str
from helpers.lang import lang
import logging
import traceback
import asyncio
Expand All @@ -21,21 +22,21 @@ async def run_cron():
total_t = Timer()
total_t.start()
for job in CRON_JOBS:
logging.debug(f"Running job {job.__name__}")
logging.debug(lang.debug("cron_job_running",job.__name__))
t = Timer()
t.start()
try:
await job()
except Exception as e:
logging.error(f"Job {job.__name__} failed with error {e}. Enable GDPyS debug mode for the full traceback.")
logging.error(lang.error("CRON_FAIL", job.__name__, e))
logging.debug(traceback.format_exc())
# So we dont gett 32846238746238ms or 0.0s
t_str = time_str(t)
logging.info(f"Finished job {job.__name__} in {time_str}")
logging.info(lang.info("CRON_FINISH", job.__name__, time_str))

# Don't copy paste code now watch me not follow my own advice. If I have to use this somewhere else, I will move this to timehelper.
t_str = time_str(total_t)
logging.info("Finished all cron jobs in " + t_str)
logging.info(lang.info("CRON_ALL_FINISH", t_str))

#async def cron_gather():
# """An experimental way of running all of the cron jobs simultaniously async style."""
Expand Down
4 changes: 2 additions & 2 deletions handlers/levelextras.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from helpers.filterhelper import check_comment
from helpers.priveliegehelper import priv_helper
from helpers.crypthelper import decode_base64
from helpers.lang import lang
from objects.comments import Comment, CommentBan
from gdpys.client import client
from objects.levels import Rating
Expand Down Expand Up @@ -38,10 +39,9 @@ async def level_comments_handler(request : aiohttp.web.Request):
try:
comment_user = await user_helper.get_object(await user_helper.userid_accid(comment.user_id))
except AssertionError: # The user does not exist
logging.debug(f"Failed searching for user {comment.user_id}. Should be skipped.")
logging.debug(lang.debug("comment_user_search_fail", comment.user_id))
else:
privilege = await priv_helper.get_privilege_from_privs(comment_user.privileges)
logging.debug(comment.user_id)
response += wave_string({
#1 : comment.level_id,
2 : comment.comment_base64,
Expand Down
5 changes: 3 additions & 2 deletions handlers/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from helpers.crypthelper import cipher_xor, hash_sha1
from helpers.auth import auth
from helpers.timehelper import time_since_midnight, get_timestamp
from helpers.lang import lang
from objects.levels import SearchQuery, Level, Gauntlet
from cron.cachempgauntlets import map_packs, gauntlets
from constants import XorKeys, ResponseCodes, CryptKeys
Expand Down Expand Up @@ -51,7 +52,7 @@ async def level_search_modular_hanlder(request : aiohttp.web.Request) -> aiohttp
# We are getting gauntlets
gauntlet : Gauntlet = select_obj_id(gauntlets, gauntlet_req) # Select gauntlet from known list.
if gauntlet is None: # Not Found
logging.debug(f"Gauntlet {gauntlet_req} not found.") # TODO: Incooperate this with the lang system.
logging.debug(lang.debug("gauntlet_not_found", gauntlet_req)) # TODO: Incooperate this with the lang system.
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
levels = await level_helper.level_list_objs(gauntlet.level_list())

Expand Down Expand Up @@ -133,7 +134,7 @@ async def download_level(request : aiohttp.web.Request) -> aiohttp.web.Response:
try:
level_str = await level.load_string()
except FileNotFoundError:
logging.error(f"Could not find file data for level {level.ID} at " + user_config["level_path"] + str(level.ID))
logging.error(lang.error("LEVEL_FILE_NOT_FOUND", level.ID,user_config["level_path"] + str(level.ID)))
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
response = joint_string({
1 : level.ID,
Expand Down
3 changes: 0 additions & 3 deletions handlers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ async def login_handler(request: aiohttp.web.Request):
try:
account_id = await user_helper.get_accountid_from_username(post_data["userName"])
except AssertionError:
logging.debug("Assert error, couldn't get an object from username.")
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
user_obj = await user_helper.get_object(account_id)
logging.debug(user_obj)
# Here, we are doing some checks such as whether the user has the correct privileges
if not await auth.check_password(post_data["userName"], post_data["password"]):
logging.debug("Failed at password check.")
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
if not user_helper.has_privilege(user_obj, Permissions.authenticate):
logging.debug("No authorization permissions.")
return aiohttp.web.Response(text=ResponseCodes.login_contact_rob)
return aiohttp.web.Response(text=f"{user_obj.account_id},{user_obj.user_id}")
9 changes: 5 additions & 4 deletions helpers/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from helpers.crypthelper import compare_bcrypt, decode_gjp
from helpers.generalhelper import dict_keys
from helpers.lang import lang
from conn.mysql import myconn
from dataclasses import dataclass
import logging
Expand All @@ -16,7 +17,7 @@ def __init__(self):

async def _cache_bcrypt(self, account_id: int):
"""Caches a person's bcrypt into an object."""
logging.debug(f"Caching bcrypt for person {account_id}")
logging.debug(lang.debug("cache_bcrypt", account_id))
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("SELECT password FROM accounts WHERE accountID = %s LIMIT 1", (account_id,))
response = await mycursor.fetchone()
Expand All @@ -32,11 +33,11 @@ async def check_gjp(self, account_id: int, gjp: str) -> bool:
if self.cached_credentials[account_id].known_gjp == gjp:
return True
elif self.cached_credentials[account_id].known_gjp == "":
logging.debug("No known GJP, checking")
logging.debug(lang.debug("no_gjp"))
if compare_bcrypt(decode_gjp(gjp), self.cached_credentials[account_id].bcrypt):
self.cached_credentials[account_id].known_gjp = gjp
return True
logging.debug("Failed check.")
logging.debug(lang.debug("bcrypt_fail"))
return False

async def check_password(self, username: str, password: str) -> bool:
Expand All @@ -46,7 +47,7 @@ async def check_password(self, username: str, password: str) -> bool:
await mycursor.execute("SELECT password FROM accounts WHERE userName LIKE %s LIMIT 1", (username,))
response = await mycursor.fetchone()
if response is None:
logging.debug("Didnt find user with that username")
logging.debug(lang.debug("user_not_found"))
return False
return compare_bcrypt(password, response[0])

Expand Down
3 changes: 1 addition & 2 deletions helpers/crypthelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def compare_bcrypt(text1: str, text2: str) -> bool:
check : bool = False
try:
check = bcrypt.checkpw(text1.encode(), text2.encode())
except ValueError as e:
logging.debug(f"Handled checkpw ValueError {e}")
except ValueError:
pass
return check

Expand Down
5 changes: 3 additions & 2 deletions helpers/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from constants import Paths
from exceptions import LangNotFound

class Lang:
class Lang():
def __init__(self, language : str = "en"):
"""Loads the given language."""
self.text_find_msg = "Failed to find translated text for {}|{}"
Expand Down Expand Up @@ -45,7 +45,8 @@ def _get_from_json(self, type : str, text : str) -> str:
def _get_full(self, type : str, text : str, format_args : tuple = ()) -> str:
"""Gets full formatted translation from lang pack."""
new_text = self._get_from_json(type, text)
new_text = self._format_string(new_text, format_args)
if len(format_args) > 0:
new_text = self._format_string(new_text, format_args)
return new_text

def warn(self, text : str, *args) -> str:
Expand Down
7 changes: 3 additions & 4 deletions helpers/levelhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from helpers.timehelper import get_timestamp
from helpers.crypthelper import hash_sha1
from helpers.userhelper import user_helper
from helpers.lang import lang
from conn.mysql import myconn
from objects.levels import Level, Rating, DailyLevel
from config import user_config
Expand Down Expand Up @@ -149,7 +150,6 @@ async def upload_level(self, level : Level) -> int:
await mycursor.execute("SELECT levelID FROM levels WHERE extID = %s AND levelName = %s", (level.account_id, level.name))
level_count = await mycursor.fetchone()
if level_count is not None:
logging.debug("The level is being updated.")
# We are currently updating an existing level.
await mycursor.execute("""UPDATE levels SET
gameVersion = %s,
Expand Down Expand Up @@ -194,7 +194,6 @@ async def upload_level(self, level : Level) -> int:
os.remove(user_config["level_path"] + str(level_id)) # Removing previous level version

else:
logging.debug("This is a new level.")
# It is a new level, insert it instead.
await mycursor.execute("""INSERT INTO levels
(
Expand Down Expand Up @@ -296,9 +295,9 @@ async def _daily_level_from_db(self) -> DailyLevel:
await mycursor.execute("SELECT feaID, levelID, timestamp, type FROM dailyfeatures WHERE timestamp < %s AND type = 0 ORDER BY timestamp DESC LIMIT 1", (timestamp,))
daily = await mycursor.fetchone()
if daily is None:
logging.warning("No daily level set! Please set one or else there won't be a daily level.")
logging.warning(lang.warn("no_daily"))
return None
logging.debug("Cached new daily level.")
logging.debug(lang.debug("new_daily_cache"))
return DailyLevel(
daily[0],
daily[1],
Expand Down
5 changes: 3 additions & 2 deletions helpers/priveliegehelper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from conn.mysql import myconn
from helpers.generalhelper import dict_keys
from helpers.lang import lang
from objects.misc import RGB, Privilege
from config import user_config
import logging
Expand Down Expand Up @@ -30,15 +31,15 @@ async def cache_privs(self) -> None:
try:
colour = RGB(colour_list[0],colour_list[1],colour_list[2])
except IndexError:
logging.warn(f"Privilege '{priv[1]}' has an invalid colour. Setting to white locally.")
logging.warn(lang.warn("privilege_invalid_colour", priv[1]))
colour = RGB(255,255,255)
self.privilege_cache[priv[2]] = Privilege(
priv[0],
priv[1],
priv[2],
colour
)
logging.debug(f"Privilege {priv[1]} ({priv[2]}) cached to memory.")
logging.debug(lang.debug("privelege_cached", priv[1], priv[0]))

async def get_privilege_from_privs(self, privilege_num : int) -> Privilege:
"""Returns privilege from priv num."""
Expand Down
15 changes: 8 additions & 7 deletions helpers/ratelimit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# My own rate limiting module. Probably not how it should be done but this suits my needs perfectly
from helpers.generalhelper import dict_keys
from helpers.timehelper import get_timestamp
from helpers.lang import lang
import logging

class RateLimit():
Expand All @@ -12,7 +13,7 @@ def __init__(self):

def update_struct(self):
"""Updates struct for all cached IPs (may be costly)."""
logging.debug("Updating all ip rate limits with new struct")
logging.debug(lang.debug("ip_new_struct"))
all_ips = dict_keys(self.ips)
structs_list = dict_keys(self.STTRUCT)
for ip in all_ips:
Expand All @@ -24,15 +25,15 @@ def add_to_struct(self, name : str, default : int = 0, limit : int = 0):
""""Appends onto the struct."""
self.STTRUCT[name] = default
self.limits[name] = limit
logging.debug(f"Added {name} to struct with default value of {default} and limit of {limit}")
logging.debug(lang.debug("added_to_struct", name,default,limit))
self.update_struct()

def bump_and_check(self, ip : str, name : str) -> bool:
"""Bumps a value for an ip and checks if it reached the limit. Returns bool."""
structure = self.STTRUCT
logging.debug("Bumping ip count")
logging.debug(lang.debug("bumping_ip"))
if ip not in dict_keys(self.ips):
logging.debug("New IP.")
logging.debug(lang.debug("bumping_ip"))
self.ips[ip] = structure
self.ips[ip]["timestamp"] = get_timestamp() # Messy
# I'm so done i just cant...
Expand All @@ -41,7 +42,7 @@ def bump_and_check(self, ip : str, name : str) -> bool:

# Checks to reset counts
if self.ips[ip]["timestamp"] + 86400 < get_timestamp():
logging.debug("Count expired for ip. Reseting.")
logging.debug(lang.debug("count_expired"))
self.ips[ip] = structure
self.ips[ip]["timestamp"] = get_timestamp() # Messy
# I'm so done i just cant...
Expand All @@ -52,9 +53,9 @@ def bump_and_check(self, ip : str, name : str) -> bool:

# Check if it passed the limit
if self.limits[name] < self.ips[ip][name]:
logging.debug(f"{ip} reached limit for {name} with {self.ips[ip][name]}/{self.limits[name]}")
logging.debug(lang.debug("reached_limit", ip,name,self.ips[ip][name],self.limits[name]))
return False
logging.debug(f"{ip} passed limit with {self.ips[ip][name]}/{self.limits[name]}")
logging.debug(lang.debug("passed_limit", ip, self.ips[ip][name],self.limits[name]))
return True

# Global rate limiter
Expand Down
5 changes: 3 additions & 2 deletions helpers/userhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from helpers.generalhelper import dict_keys
from helpers.timehelper import get_timestamp, Timer
from helpers.crypthelper import decode_base64, hash_bcrypt, encode_base64
from helpers.lang import lang
from objects.accounts import Account, AccountExtras
from objects.comments import AccountComment
from conn.mysql import myconn
Expand Down Expand Up @@ -117,7 +118,7 @@ async def get_object(self, account_id: int) -> Account:
"""Gets user object from cache or caches and returns it."""
account_id = int(account_id)
if account_id not in dict_keys(self.object_cache):
logging.debug("Object doesnt exist! Caching.")
logging.debug(lang.debug("obj_caching"))
await self.recache_object(account_id)
return self.object_cache[account_id]

Expand Down Expand Up @@ -303,7 +304,7 @@ async def update_profile_settings(self, account_id : int, youtube : str, twitter

async def give_cp(self, account_id : int, cp : int = 1):
"""Gives a user an amount of CP."""
logging.debug(f"{account_id} has gained {cp} CP")
logging.debug(lang.debug("cp_gain", account_id, cp))
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("UPDATE users SET creatorPoints = creatorPoints + %s WHERE extID = %s LIMIT 1", (cp, account_id,))
await myconn.conn.commit()
Expand Down
27 changes: 22 additions & 5 deletions languages/packs/en.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
{
"errors": {
"START_LVLPATH": "Level/Save path does not exist! Please create it before starting GDPyS."
"START_LVLPATH": "Level/Save path does not exist! Please create it before starting GDPyS.",
"CRON_FAIL" : "Job {0} failed with error {1}. Enable GDPyS debug mode for the full traceback.",
"LEVEL_FILE_NOT_FOUND" : "Could not find file data for level {0} at {1}."
},
"info": {
"SET_LVLPATH": "Set level path: {0}\nSet save path: {1}"
"SET_LVLPATH": "Set level path: {0}\nSet save path: {1}",
"CRON_FINISH" : "Finished cron job {0} in {1}",
"CRON_ALL_FINISH" : "Finished all cron jobs in {0}"
},
"warning": {
"no_daily": "No daily level set! Please set one."
"no_daily": "No daily level set! Please set one.",
"pack_invalid_colour" : "Map pack {0} has an invalid colour! Setting to white.",
"privilege_invalid_colour" : "Privilege '{0}' has an invalid colour. Setting to white locally."
},
"debug": {
"no_gjp": "No known GJP, checking...",
"user_not_found": "Couldn't find user with such name!",
"handled_checkpw_err": "Handled checkpw ValueError {0}",
"privelege_cached": "Privelege {0} {1} cached to memory.",
"privelege_cached": "Privilege {0} ({1}) cached to memory.",
"ip_new_struct": "Updating all IP rate limits with the new struct...",
"added_to_struct": "Added {0} to struct with default value of {1} and limit of {2}",
"bumping_ip": "Bumping IP count...",
"count_expired": "Count expired for IP. Resetting.",
"reached_limit": "{0} reached limit for {1} with {2}/{3}",
"passed_limit": "{0} passed limit with {1}/{2}"
"passed_limit": "{0} passed limit with {1}/{2}",
"new_ip" : "New IP added to the ratelimit list.",
"cron_cp_pages" : "CP cron has {0} pages.",
"cron_cp_page_running" : "Running page {0}/{1}",
"cron_job_running" : "Running cron job '{0}'.",
"comment_user_search_fail" : "Failed searching for user {0}. Should be skipped.",
"gauntlet_not_found" : "Gauntlet {0} could not be found!",
"cache_bcrypt" : "Caching bcrypt for person {0}",
"bcrypt_fail" : "Bcrypt check failed!",
"new_daily_cache" : "A new daily level has been cached!",
"cp_gain" : "{0} has gained {1} CP!",
"obj_caching" : "The object does not exist! Caching from database."
},
"runtime": {
"shutdown": "Shutting down! Bye!",
Expand Down

0 comments on commit a734600

Please sign in to comment.