Fix formatting to comply with Black

This commit is contained in:
Zedifus 2022-04-11 11:08:36 +01:00
parent 42ca4bb973
commit 19f2429a4c
24 changed files with 160 additions and 55 deletions

View File

@ -10,6 +10,7 @@ logger = logging.getLogger(__name__)
class Roles_Controller:
def __init__(self, users_helper):
self.users_helper = users_helper
@staticmethod
def get_all_roles():
return helper_roles.get_all_roles()
@ -45,7 +46,9 @@ class Roles_Controller:
for server in added_servers:
Permissions_Servers.get_or_create(role_id, server, permissions_mask)
for server in base_data["servers"]:
Permissions_Servers.update_role_permission(role_id, server, permissions_mask)
Permissions_Servers.update_role_permission(
role_id, server, permissions_mask
)
# TODO: This is horribly inefficient and we should be using bulk queries
# but im going for functionality at this point
Permissions_Servers.delete_roles_permissions(role_id, removed_servers)

View File

@ -11,6 +11,7 @@ from app.classes.models.server_permissions import Permissions_Servers
logger = logging.getLogger(__name__)
class ServerJars:
def __init__(self, helper):
self.helper = helper
@ -177,7 +178,9 @@ class ServerJars:
try:
Servers_Controller.set_download(server_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
)
break
except Exception as ex:
@ -195,7 +198,9 @@ class ServerJars:
user, "notification", "Executable download finished"
)
time.sleep(3)
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
)
return True
except Exception as e:
logger.error(f"Unable to save jar to {path} due to error:{e}")
@ -206,6 +211,8 @@ class ServerJars:
user, "notification", "Executable download finished"
)
time.sleep(3)
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
)
return False

View File

@ -61,7 +61,9 @@ class Stats:
with p.oneshot():
process_stats = {
"cpu_usage": real_cpu,
"memory_usage": Helpers.human_readable_file_size(p.memory_info()[0]),
"memory_usage": Helpers.human_readable_file_size(
p.memory_info()[0]
),
"mem_percentage": round(p.memory_percent(), 0),
}
return process_stats

View File

@ -2,6 +2,7 @@ import peewee
database_proxy = peewee.DatabaseProxy()
class BaseModel(peewee.Model):
class Meta:
database = database_proxy

View File

@ -181,9 +181,7 @@ class Permissions_Servers:
@staticmethod
def remove_roles_of_server(server_id):
return (
Role_Servers.delete()
.where(Role_Servers.server_id == server_id)
.execute()
Role_Servers.delete().where(Role_Servers.server_id == server_id).execute()
)
@staticmethod

View File

@ -112,4 +112,4 @@ class FileHelpers:
f" - Error was: {e}"
)
return True
return True

View File

@ -630,7 +630,10 @@ class Helpers:
sizes = []
for p in paths:
sizes.append(
{"path": p, "size": Helpers.human_readable_file_size(os.stat(p).st_size)}
{
"path": p,
"size": Helpers.human_readable_file_size(os.stat(p).st_size),
}
)
return sizes
@ -913,7 +916,9 @@ class Helpers:
def backup_select(self, path, user_id):
if user_id:
self.websocket_helper.broadcast_user(user_id, "send_temp_path", {"path": path})
self.websocket_helper.broadcast_user(
user_id, "send_temp_path", {"path": path}
)
@staticmethod
def unzip_backup_archive(backup_path, zip_name):

View File

@ -13,7 +13,6 @@ class import3:
self.console = self.helper.console
self.controller = controller
def start_import(self):
folder = os.path.normpath(
input(
@ -26,7 +25,9 @@ class import3:
"Crafty cannot find the path you entered. "
"Does Crafty's user have permission to access it?"
)
self.console.info("Please run the import3 command again and enter a valid path.")
self.console.info(
"Please run the import3 command again and enter a valid path."
)
else:
with open(os.path.join(folder, "users.json"), encoding="utf-8") as f:
user_json = json.loads(f.read())

View File

@ -7,6 +7,7 @@ import logging
import tempfile
from typing import Union
from peewee import DoesNotExist
# TZLocal is set as a hidden import on win pipeline
from tzlocal import get_localzone
from apscheduler.schedulers.background import BackgroundScheduler
@ -31,6 +32,7 @@ from app.classes.minecraft.stats import Stats
logger = logging.getLogger(__name__)
class Controller:
def __init__(self, database, helper):
self.helper = helper
@ -47,7 +49,9 @@ class Controller:
self.roles = Roles_Controller(self.users_helper)
self.server_perms = Server_Perms_Controller()
self.servers = Servers_Controller(self.servers_helper)
self.users = Users_Controller(self.helper, self.users_helper, self.authentication)
self.users = Users_Controller(
self.helper, self.users_helper, self.authentication
)
tz = get_localzone()
self.support_scheduler = BackgroundScheduler(timezone=str(tz))
self.support_scheduler.start()
@ -103,7 +107,9 @@ class Controller:
# if the properties file isn't there, let's warn
if not Helpers.check_file_exists(settings_file):
logger.error(f"Unable to find {settings_file}. Skipping this server.")
self.console.error(f"Unable to find {settings_file}. Skipping this server.")
self.console.error(
f"Unable to find {settings_file}. Skipping this server."
)
continue
settings = ServerProps(settings_file)
@ -217,7 +223,9 @@ class Controller:
)
tempZipStorage += ".zip"
self.helper.websocket_helper.broadcast_user(exec_user["user_id"], "send_logs_bootbox", {})
self.helper.websocket_helper.broadcast_user(
exec_user["user_id"], "send_logs_bootbox", {}
)
self.users.set_support_path(exec_user["user_id"], tempZipStorage)
@ -717,10 +725,13 @@ class Controller:
old_bu_path = server_data["backup_path"]
Server_Perms_Controller.backup_role_swap(old_server_id, new_server_id)
if not Helpers.is_os_windows():
backup_path = Helpers.validate_traversal(self.helper.backup_path, old_bu_path)
backup_path = Helpers.validate_traversal(
self.helper.backup_path, old_bu_path
)
if Helpers.is_os_windows():
backup_path = Helpers.validate_traversal(
Helpers.wtol_path(self.helper.backup_path), Helpers.wtol_path(old_bu_path)
Helpers.wtol_path(self.helper.backup_path),
Helpers.wtol_path(old_bu_path),
)
backup_path = Helpers.wtol_path(str(backup_path))
backup_path.replace(" ", "^ ")
@ -761,7 +772,9 @@ class Controller:
server_port,
)
if not Helpers.check_file_exists(os.path.join(server_dir, "crafty_managed.txt")):
if not Helpers.check_file_exists(
os.path.join(server_dir, "crafty_managed.txt")
):
try:
# place a file in the dir saying it's owned by crafty
with open(
@ -794,7 +807,9 @@ class Controller:
server_name = server_data["server_name"]
logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
self.console.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
self.console.info(
f"Deleting Server: ID {server_id} | Name: {server_name} "
)
srv_obj = s["server_obj"]
running = srv_obj.check_running()

View File

@ -6,6 +6,7 @@ from app.classes.shared.helpers import Helpers
logger = logging.getLogger(__name__)
class db_builder:
def __init__(self, database, helper, users_helper):
self.database = database

View File

@ -331,11 +331,15 @@ class MigrationManager(object):
"""
if not os.path.exists(self.helper.migration_dir):
logger.warning(
"Migration directory: {} does not exist.".format(self.helper.migration_dir)
"Migration directory: {} does not exist.".format(
self.helper.migration_dir
)
)
os.makedirs(self.helper.migration_dir)
return sorted(
f[:-3] for f in os.listdir(self.helper.migration_dir) if self.filemask.match(f)
f[:-3]
for f in os.listdir(self.helper.migration_dir)
if self.filemask.match(f)
)
@property
@ -419,7 +423,9 @@ class MigrationManager(object):
if Helpers.is_os_windows() and sys.version_info >= (3, 0):
# if system is windows - force utf-8 encoding
call_params["encoding"] = "utf-8"
with open(os.path.join(self.helper.migration_dir, name + ".py"), **call_params) as f:
with open(
os.path.join(self.helper.migration_dir, name + ".py"), **call_params
) as f:
code = f.read()
scope = {}
code = compile(code, "<string>", "exec", dont_inherit=True)

View File

@ -9,6 +9,7 @@ import subprocess
import html
import tempfile
import psutil
# TZLocal is set as a hidden import on win pipeline
from tzlocal import get_localzone
from apscheduler.schedulers.background import BackgroundScheduler
@ -148,7 +149,9 @@ class Server:
delay = int(self.settings["auto_start_delay"])
logger.info(f"Scheduling server {self.name} to start in {delay} seconds")
self.console.info(f"Scheduling server {self.name} to start in {delay} seconds")
self.console.info(
f"Scheduling server {self.name} to start in {delay} seconds"
)
self.server_scheduler.add_job(
self.run_scheduled_server,
@ -216,7 +219,9 @@ class Server:
if not Helpers.check_path_exists(self.server_path):
logger.critical(f"Server path: {self.server_path} does not seem to exits")
self.console.critical(f"Server path: {self.server_path} does not seem to exits")
self.console.critical(
f"Server path: {self.server_path} does not seem to exits"
)
if not Helpers.check_writeable(self.server_path):
logger.critical(f"Unable to write/access {self.server_path}")
@ -255,7 +260,9 @@ class Server:
return False
logger.info(f"Launching Server {self.name} with command {self.server_command}")
self.console.info(f"Launching Server {self.name} with command {self.server_command}")
self.console.info(
f"Launching Server {self.name} with command {self.server_command}"
)
# Checks for eula. Creates one if none detected.
# If EULA is detected and not set to true we offer to set it true.
@ -450,11 +457,15 @@ class Server:
server_users = Permissions_Servers.get_server_user_list(self.server_id)
for user in server_users:
if user != user_id:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
)
else:
server_users = Permissions_Servers.get_server_user_list(self.server_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
)
else:
logger.warning(
f"Server PID {self.process.pid} died right after starting "
@ -485,7 +496,11 @@ class Server:
self.helper.websocket_helper.broadcast_user(
user_id,
"send_start_error",
{"error": self.helper.translation.translate("error", "internet", user_lang)},
{
"error": self.helper.translation.translate(
"error", "internet", user_lang
)
},
)
def stop_crash_detection(self):
@ -998,7 +1013,9 @@ class Server:
"string": message,
},
)
self.helper.websocket_helper.broadcast_page("/panel/dashboard", "send_start_reload", {})
self.helper.websocket_helper.broadcast_page(
"/panel/dashboard", "send_start_reload", {}
)
backup_dir = os.path.join(
Helpers.get_os_understandable_path(self.settings["path"]),
"crafty_executable_backups",

View File

@ -177,12 +177,16 @@ class TasksManager:
],
)
except Exception as e:
self.helper.console.error(f"Failed to schedule task with error: {e}.")
self.helper.console.error(
f"Failed to schedule task with error: {e}."
)
self.helper.console.warning("Removing failed task from DB.")
logger.error(f"Failed to schedule task with error: {e}.")
logger.warning("Removing failed task from DB.")
# remove items from DB if task fails to add to apscheduler
self.controller.management_helper.delete_scheduled_task(schedule.schedule_id)
self.controller.management_helper.delete_scheduled_task(
schedule.schedule_id
)
else:
if schedule.interval_type == "hours":
self.scheduler.add_job(
@ -270,7 +274,9 @@ class TasksManager:
],
)
except Exception as e:
self.helper.console.error(f"Failed to schedule task with error: {e}.")
self.helper.console.error(
f"Failed to schedule task with error: {e}."
)
self.helper.console.warning("Removing failed task from DB.")
logger.error(f"Failed to schedule task with error: {e}.")
logger.warning("Removing failed task from DB.")
@ -381,7 +387,9 @@ class TasksManager:
],
)
except Exception as e:
self.helper.console.error(f"Failed to schedule task with error: {e}.")
self.helper.console.error(
f"Failed to schedule task with error: {e}."
)
self.helper.console.info("Removing failed task from DB.")
self.controller.management_helper.delete_scheduled_task(sch_id)
else:
@ -511,7 +519,10 @@ class TasksManager:
logger.info("Scheduling Serverjars.com cache refresh service every 12 hours")
self.scheduler.add_job(
self.controller.server_jars.refresh_cache, "interval", hours=12, id="serverjars"
self.controller.server_jars.refresh_cache,
"interval",
hours=12,
id="serverjars",
)
def realtime(self):

View File

@ -9,7 +9,9 @@ logger = logging.getLogger(__name__)
class Translation:
def __init__(self, helper):
self.helper = helper
self.translations_path = os.path.join(self.helper.root_dir, "app", "translations")
self.translations_path = os.path.join(
self.helper.root_dir, "app", "translations"
)
self.cached_translation = None
self.cached_translation_lang = None

View File

@ -283,7 +283,9 @@ class AjaxHandler(BaseHandler):
if server_id is None:
logger.warning("Server ID not found in send_command ajax call")
self.helper.console.warning("Server ID not found in send_command ajax call")
self.helper.console.warning(
"Server ID not found in send_command ajax call"
)
srv_obj = self.controller.get_server_obj(server_id)
@ -430,7 +432,11 @@ class AjaxHandler(BaseHandler):
self.helper.websocket_helper.broadcast_user(
user_id,
"send_start_error",
{"error": self.helper.translation.translate("error", "no-file", user_lang)},
{
"error": self.helper.translation.translate(
"error", "no-file", user_lang
)
},
)
return
@ -496,12 +502,15 @@ class AjaxHandler(BaseHandler):
)
) or not Helpers.check_file_exists(os.path.abspath(file_path)):
logger.warning(f"Invalid path in del_backup ajax call ({file_path})")
self.helper.console.warning(f"Invalid path in del_backup ajax call ({file_path})")
self.helper.console.warning(
f"Invalid path in del_backup ajax call ({file_path})"
)
return
# Delete the file
if Helpers.validate_traversal(
Helpers.get_os_understandable_path(server_info["backup_path"]), file_path
Helpers.get_os_understandable_path(server_info["backup_path"]),
file_path,
):
os.remove(file_path)

View File

@ -7,6 +7,7 @@ from app.classes.models.users import ApiKeys
logger = logging.getLogger(__name__)
class BaseHandler(tornado.web.RequestHandler):
nobleach = {bool, type(None)}

View File

@ -8,9 +8,10 @@ from app.classes.models.server_permissions import Enum_Permissions_Server
from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers
from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__)
class FileHandler(BaseHandler):
def render_page(self, template, page_data):
self.render(
@ -256,7 +257,9 @@ class FileHandler(BaseHandler):
self.get_body_argument("file_path", default=None, strip=True)
)
self.helper.self.helper.console.warning(f"Delete {file_path} for server {server_id}")
self.helper.self.helper.console.warning(
f"Delete {file_path} for server {server_id}"
)
if not self.check_server_id(server_id, "del_file"):
return
@ -303,7 +306,9 @@ class FileHandler(BaseHandler):
Helpers.get_os_understandable_path(server_info["path"]), dir_path
) or not Helpers.check_path_exists(os.path.abspath(dir_path)):
logger.warning(f"Invalid path in del_file file ajax call ({dir_path})")
self.helper.console.warning(f"Invalid path in del_file file ajax call ({dir_path})")
self.helper.console.warning(
f"Invalid path in del_file file ajax call ({dir_path})"
)
return
# Delete the directory
@ -389,7 +394,9 @@ class FileHandler(BaseHandler):
if item_path is None or new_item_name is None:
logger.warning("Invalid path(s) in rename_file file ajax call")
self.helper.console.warning("Invalid path(s) in rename_file file ajax call")
self.helper.console.warning(
"Invalid path(s) in rename_file file ajax call"
)
return
if not Helpers.in_path(
@ -465,7 +472,9 @@ class FileHandler(BaseHandler):
if item_path is None or new_item_name is None:
logger.warning("Invalid path(s) in rename_file file ajax call")
self.helper.console.warning("Invalid path(s) in rename_file file ajax call")
self.helper.console.warning(
"Invalid path(s) in rename_file file ajax call"
)
return
if not Helpers.in_path(

View File

@ -335,7 +335,9 @@ class PanelHandler(BaseHandler):
template = "public/error.html"
elif page == "credits":
with open(self.helper.credits_cache, encoding="utf-8") as credits_default_local:
with open(
self.helper.credits_cache, encoding="utf-8"
) as credits_default_local:
try:
remote = requests.get(
"https://craftycontrol.com/credits", allow_redirects=True
@ -1311,7 +1313,9 @@ class PanelHandler(BaseHandler):
):
server_obj.path = server_path
server_obj.log_path = log_path
if Helpers.validate_traversal(self.helper.get_servers_root_dir(), executable):
if Helpers.validate_traversal(
self.helper.get_servers_root_dir(), executable
):
server_obj.executable = executable
server_obj.execution_command = execution_command
server_obj.server_ip = server_ip
@ -1889,7 +1893,9 @@ class PanelHandler(BaseHandler):
)
self.write(
self.controller.authentication.generate(key.user_id.user_id, {"token_id": key.token_id})
self.controller.authentication.generate(
key.user_id.user_id, {"token_id": key.token_id}
)
)
self.finish()

View File

@ -19,7 +19,9 @@ class PublicHandler(BaseHandler):
if user_id is not None:
self.set_cookie(
"token", self.controller.authentication.generate(user_id), expires_days=int(expire_days)
"token",
self.controller.authentication.generate(user_id),
expires_days=int(expire_days),
)
else:
self.clear_cookie("token")

View File

@ -203,7 +203,9 @@ class ServerHandler(BaseHandler):
os.path.join(self.helper.servers_dir, new_server_uuid)
):
new_server_uuid = Helpers.create_uuid()
new_server_path = os.path.join(self.helper.servers_dir, new_server_uuid)
new_server_path = os.path.join(
self.helper.servers_dir, new_server_uuid
)
# copy the old server
FileHelpers.copy_dir(server_data.get("path"), new_server_path)

View File

@ -9,7 +9,9 @@ class StatusHandler(BaseHandler):
def get(self):
page_data = {}
page_data["lang"] = self.helper.get_setting("language")
page_data["lang_page"] = self.helper.getLangPage(self.helper.get_setting("language"))
page_data["lang_page"] = self.helper.getLangPage(
self.helper.get_setting("language")
)
page_data["servers"] = self.controller.servers.get_all_servers_stats()
running = 0
for srv in page_data["servers"]:

View File

@ -18,7 +18,11 @@ class UploadHandler(BaseHandler):
# noinspection PyAttributeOutsideInit
def initialize(
self, helper: Helpers = None, controller: Controller = None, tasks_manager=None, translator=None
self,
helper: Helpers = None,
controller: Controller = None,
tasks_manager=None,
translator=None,
):
self.helper = helper
self.controller = controller

View File

@ -17,7 +17,9 @@ class SocketHandler(tornado.websocket.WebSocketHandler):
translator = None
io_loop = None
def initialize(self, helper=None, controller=None, tasks_manager=None, translator=None):
def initialize(
self, helper=None, controller=None, tasks_manager=None, translator=None
):
self.helper = helper
self.controller = controller
self.tasks_manager = tasks_manager

View File

@ -106,8 +106,7 @@ if __name__ == "__main__":
# start the database
database = peewee.SqliteDatabase(
helper.db_path,
pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
helper.db_path, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
)
database_proxy.initialize(database)