From b306d1f3134ddc08ad35a0e99b326c927311f9ea Mon Sep 17 00:00:00 2001 From: Zedifus Date: Tue, 8 Mar 2022 04:40:44 +0000 Subject: [PATCH] Tidy imports & dep auto-installer --- .../controllers/crafty_perms_controller.py | 2 +- app/classes/controllers/roles_controller.py | 1 - .../controllers/server_perms_controller.py | 1 - app/classes/controllers/servers_controller.py | 4 +- app/classes/controllers/users_controller.py | 5 +-- app/classes/minecraft/mc_ping.py | 3 +- app/classes/minecraft/serverjars.py | 11 ++---- app/classes/minecraft/stats.py | 8 ++-- app/classes/models/crafty_permissions.py | 16 +++----- app/classes/models/management.py | 21 ++++------- app/classes/models/roles.py | 15 +++----- app/classes/models/server_permissions.py | 22 ++++------- app/classes/models/servers.py | 15 +++----- app/classes/models/users.py | 18 +++------ app/classes/shared/authentication.py | 11 ++++-- app/classes/shared/command.py | 2 +- app/classes/shared/console.py | 10 ++--- app/classes/shared/file_helpers.py | 12 +----- app/classes/shared/helpers.py | 20 ++++++---- app/classes/shared/main_controller.py | 11 +++--- app/classes/shared/main_models.py | 19 ++++------ app/classes/shared/migration.py | 12 ++---- app/classes/shared/server.py | 21 ++++------- app/classes/shared/tasks.py | 20 ++++------ app/classes/web/ajax_handler.py | 17 +++++---- app/classes/web/api_handler.py | 13 +++---- app/classes/web/base_handler.py | 16 +++++--- app/classes/web/file_handler.py | 15 +++++--- app/classes/web/http_handler.py | 7 +++- app/classes/web/panel_handler.py | 27 +++++++------- app/classes/web/public_handler.py | 13 ++----- app/classes/web/server_handler.py | 18 +++------ app/classes/web/static_handler.py | 8 +++- app/classes/web/tornado_handler.py | 37 +++++++++---------- app/classes/web/upload_handler.py | 15 ++++---- app/classes/web/websocket_handler.py | 9 +---- 36 files changed, 203 insertions(+), 272 deletions(-) diff --git a/app/classes/controllers/crafty_perms_controller.py b/app/classes/controllers/crafty_perms_controller.py index d73774c7..75cf2601 100644 --- a/app/classes/controllers/crafty_perms_controller.py +++ b/app/classes/controllers/crafty_perms_controller.py @@ -1,6 +1,6 @@ import logging -from app.classes.models.crafty_permissions import crafty_permissions, Enum_Permissions_Crafty +from app.classes.models.crafty_permissions import crafty_permissions, Enum_Permissions_Crafty from app.classes.models.users import ApiKeys logger = logging.getLogger(__name__) diff --git a/app/classes/controllers/roles_controller.py b/app/classes/controllers/roles_controller.py index 4071ba7e..bded6e23 100644 --- a/app/classes/controllers/roles_controller.py +++ b/app/classes/controllers/roles_controller.py @@ -3,7 +3,6 @@ import logging from app.classes.models.roles import roles_helper from app.classes.models.server_permissions import server_permissions from app.classes.models.users import users_helper - from app.classes.shared.helpers import helper logger = logging.getLogger(__name__) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index 9afa5bd2..bd0ae36d 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -4,7 +4,6 @@ from app.classes.models.server_permissions import server_permissions, Enum_Perm from app.classes.models.users import users_helper, ApiKeys from app.classes.models.roles import roles_helper from app.classes.models.servers import servers_helper - from app.classes.shared.main_models import db_helper logger = logging.getLogger(__name__) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 95ed242c..4a596428 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -2,15 +2,13 @@ import os import logging import json +from app.classes.controllers.roles_controller import Roles_Controller from app.classes.models.servers import servers_helper from app.classes.models.users import users_helper, ApiKeys from app.classes.models.server_permissions import server_permissions, Enum_Permissions_Server - from app.classes.shared.helpers import helper from app.classes.shared.main_models import db_helper -from app.classes.controllers.roles_controller import Roles_Controller - logger = logging.getLogger(__name__) class Servers_Controller: diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index fc5b8492..e63ede37 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -1,11 +1,10 @@ import logging from typing import Optional -from app.classes.shared.helpers import helper -from app.classes.shared.authentication import authentication - from app.classes.models.users import users_helper from app.classes.models.crafty_permissions import crafty_permissions, Enum_Permissions_Crafty +from app.classes.shared.helpers import helper +from app.classes.shared.authentication import authentication logger = logging.getLogger(__name__) diff --git a/app/classes/minecraft/mc_ping.py b/app/classes/minecraft/mc_ping.py index b8f9caa0..1e4e6b7e 100644 --- a/app/classes/minecraft/mc_ping.py +++ b/app/classes/minecraft/mc_ping.py @@ -8,12 +8,11 @@ import logging.config import uuid import random -from app.classes.shared.console import console from app.classes.minecraft.bedrock_ping import BedrockPing +from app.classes.shared.console import console logger = logging.getLogger(__name__) - class Server: def __init__(self, data): self.description = data.get('description') diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index 8a2a7dda..46e040a5 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -1,4 +1,3 @@ -import sys import json import threading import time @@ -6,11 +5,10 @@ import shutil import logging from datetime import datetime -from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.controllers.servers_controller import Servers_Controller -from app.classes.web.websocket_helper import websocket_helper from app.classes.models.server_permissions import server_permissions +from app.classes.shared.helpers import helper +from app.classes.web.websocket_helper import websocket_helper logger = logging.getLogger(__name__) @@ -18,10 +16,7 @@ try: import requests except ModuleNotFoundError as err: - logger.critical(f"Import Error: Unable to load {err.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {err.name} module") - sys.exit(1) - + helper.auto_installer_fix(err) class ServerJars: diff --git a/app/classes/minecraft/stats.py b/app/classes/minecraft/stats.py index 93f56053..456213ce 100644 --- a/app/classes/minecraft/stats.py +++ b/app/classes/minecraft/stats.py @@ -4,15 +4,13 @@ import datetime import base64 import psutil -from app.classes.models.management import Host_Stats - -from app.classes.shared.helpers import helper -from app.classes.models.servers import servers_helper from app.classes.minecraft.mc_ping import ping +from app.classes.models.management import Host_Stats +from app.classes.models.servers import servers_helper +from app.classes.shared.helpers import helper logger = logging.getLogger(__name__) - class Stats: def __init__(self, controller): diff --git a/app/classes/models/crafty_permissions.py b/app/classes/models/crafty_permissions.py index 0a3644ed..13a655e5 100644 --- a/app/classes/models/crafty_permissions.py +++ b/app/classes/models/crafty_permissions.py @@ -1,26 +1,20 @@ -import sys import logging from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.shared.permission_helper import permission_helper - from app.classes.models.users import Users, ApiKeys -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) - try: from peewee import SqliteDatabase, Model, ForeignKeyField, CharField, IntegerField, DoesNotExist from enum import Enum except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/models/management.py b/app/classes/models/management.py index fa905d88..927f97b4 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -1,31 +1,24 @@ -import sys import logging import datetime -from app.classes.shared.helpers import helper -from app.classes.shared.console import console -from app.classes.shared.main_models import db_helper - from app.classes.models.users import Users, users_helper from app.classes.models.servers import Servers from app.classes.models.server_permissions import server_permissions - +from app.classes.shared.helpers import helper +from app.classes.shared.main_models import db_helper from app.classes.web.websocket_helper import websocket_helper -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) - try: from peewee import SqliteDatabase, Model, ForeignKeyField, CharField, IntegerField, DateTimeField, FloatField, TextField, AutoField, BooleanField from playhouse.shortcuts import model_to_dict except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/models/roles.py b/app/classes/models/roles.py index ddecd798..782e0af5 100644 --- a/app/classes/models/roles.py +++ b/app/classes/models/roles.py @@ -1,24 +1,19 @@ -import sys import logging import datetime from app.classes.shared.helpers import helper -from app.classes.shared.console import console - -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) try: from peewee import SqliteDatabase, Model, CharField, DoesNotExist, AutoField, DateTimeField from playhouse.shortcuts import model_to_dict except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/models/server_permissions.py b/app/classes/models/server_permissions.py index fcf966ef..9ec9c0cd 100644 --- a/app/classes/models/server_permissions.py +++ b/app/classes/models/server_permissions.py @@ -1,33 +1,25 @@ -import sys import logging -from app.classes.shared.helpers import helper -from app.classes.shared.console import console -from app.classes.shared.permission_helper import permission_helper - from app.classes.models.servers import Servers from app.classes.models.roles import Roles from app.classes.models.users import User_Roles, users_helper, ApiKeys, Users - - -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) +from app.classes.shared.helpers import helper +from app.classes.shared.permission_helper import permission_helper try: from peewee import SqliteDatabase, Model, ForeignKeyField, CharField, CompositeKey, JOIN from enum import Enum except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) - #************************************************************************************************ # Role Servers Class #************************************************************************************************ diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index c391bb65..07b87d31 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -1,24 +1,19 @@ -import sys import logging import datetime from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.shared.main_models import db_helper -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) - try: from peewee import SqliteDatabase, Model, ForeignKeyField, CharField, AutoField, DateTimeField, BooleanField, IntegerField, FloatField except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index 75dc4b97..78966556 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -1,27 +1,21 @@ -import sys import logging import datetime from typing import Optional, Union -from app.classes.shared.helpers import helper -from app.classes.shared.console import console - from app.classes.models.roles import Roles, roles_helper - -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) +from app.classes.shared.helpers import helper try: from peewee import SqliteDatabase, Model, ForeignKeyField, CharField, AutoField, DateTimeField, BooleanField, CompositeKey, DoesNotExist, JOIN from playhouse.shortcuts import model_to_dict except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/shared/authentication.py b/app/classes/shared/authentication.py index c0c6a693..25053d0b 100644 --- a/app/classes/shared/authentication.py +++ b/app/classes/shared/authentication.py @@ -2,14 +2,17 @@ import logging import time from typing import Optional, Dict, Any, Tuple -import jwt -from jwt import PyJWTError - from app.classes.models.users import users_helper, ApiKeys from app.classes.shared.helpers import helper -logger = logging.getLogger(__name__) +try: + import jwt + from jwt import PyJWTError +except ModuleNotFoundError as e: + helper.auto_installer_fix(e) + +logger = logging.getLogger(__name__) class Authentication: def __init__(self): diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 001d8421..18b4f868 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -6,10 +6,10 @@ import logging from app.classes.shared.console import console from app.classes.shared.helpers import helper - from app.classes.web.websocket_helper import websocket_helper logger = logging.getLogger(__name__) + class MainPrompt(cmd.Cmd): def __init__(self, tasks_manager, migration_manager): diff --git a/app/classes/shared/console.py b/app/classes/shared/console.py index 43b8cd92..942e0301 100644 --- a/app/classes/shared/console.py +++ b/app/classes/shared/console.py @@ -2,18 +2,16 @@ import datetime import logging import sys -logger = logging.getLogger(__name__) +from app.classes.shared.helpers import helper try: from colorama import init from termcolor import colored except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - print(f"Import Error: Unable to load {e.name} module") - from app.classes.shared.installer import installer - installer.do_install() - sys.exit(1) + helper.auto_installer_fix(e) + +logger = logging.getLogger(__name__) class Console: diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index 39d23b04..205c9066 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -1,21 +1,11 @@ import os import shutil -import sys import logging import pathlib - -from app.classes.shared.console import console +from zipfile import ZipFile, ZIP_DEFLATED logger = logging.getLogger(__name__) -try: - from zipfile import ZipFile, ZIP_DEFLATED - -except ModuleNotFoundError as err: - logger.critical(f"Import Error: Unable to load {err.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {err.name} module") - sys.exit(1) - class FileHelpers: allowed_quotes = [ "\"", diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 8b0a9a84..111b22f8 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -18,23 +18,23 @@ from datetime import datetime from socket import gethostname from contextlib import suppress import psutil -from requests import get -from app.classes.web.websocket_helper import websocket_helper from app.classes.shared.console import console +from app.classes.shared.installer import installer from app.classes.shared.file_helpers import file_helper - -logger = logging.getLogger(__name__) +from app.classes.shared.helpers import helper +from app.classes.web.websocket_helper import websocket_helper try: import requests + from requests import get from OpenSSL import crypto from argon2 import PasswordHasher except ModuleNotFoundError as err: - logger.critical(f"Import Error: Unable to load {err.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {err.name} module") - sys.exit(1) + helper.auto_installer_fix(err) + +logger = logging.getLogger(__name__) class Helpers: allowed_quotes = [ @@ -61,6 +61,12 @@ class Helpers: self.passhasher = PasswordHasher() self.exiting = False + @staticmethod + def auto_installer_fix(ex): + logger.critical(f"Import Error: Unable to load {ex.name} module", exc_info=True) + print(f"Import Error: Unable to load {ex.name} module") + installer.do_install() + def float_to_string(self, gbs: int): s = str(float(gbs) * 1000).rstrip("0").rstrip(".") return s diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 25d21a9e..c3d5f0a1 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -6,7 +6,6 @@ import time import logging import tempfile from typing import Union -from peewee import DoesNotExist from app.classes.controllers.crafty_perms_controller import Crafty_Perms_Controller from app.classes.controllers.management_controller import Management_Controller @@ -14,23 +13,25 @@ from app.classes.controllers.users_controller import Users_Controller from app.classes.controllers.roles_controller import Roles_Controller from app.classes.controllers.server_perms_controller import Server_Perms_Controller from app.classes.controllers.servers_controller import Servers_Controller - from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.models.users import helper_users from app.classes.models.management import helpers_management from app.classes.models.servers import servers_helper - from app.classes.shared.console import console from app.classes.shared.helpers import helper from app.classes.shared.server import Server from app.classes.shared.file_helpers import file_helper - from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.serverjars import server_jar_obj from app.classes.minecraft.stats import Stats - from app.classes.web.websocket_helper import websocket_helper +try: + from peewee import DoesNotExist + +except ModuleNotFoundError as err: + helper.auto_installer_fix(err) + logger = logging.getLogger(__name__) class Controller: diff --git a/app/classes/shared/main_models.py b/app/classes/shared/main_models.py index e96fc07f..eee69f82 100644 --- a/app/classes/shared/main_models.py +++ b/app/classes/shared/main_models.py @@ -1,30 +1,25 @@ -import sys import logging +from app.classes.models.users import Users, users_helper from app.classes.shared.helpers import helper from app.classes.shared.console import console -from app.classes.models.users import Users, users_helper - # To disable warning about unused import ; Users is imported from here in other places -#pylint: disable=self-assigning-variable +# pylint: disable=self-assigning-variable Users = Users -logger = logging.getLogger(__name__) -peewee_logger = logging.getLogger('peewee') -peewee_logger.setLevel(logging.INFO) - try: # pylint: disable=unused-import from peewee import SqliteDatabase, fn from playhouse.shortcuts import model_to_dict except ModuleNotFoundError as err: - logger.critical(f"Import Error: Unable to load {err.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {err.name} module") - sys.exit(1) + helper.auto_installer_fix(err) -database = SqliteDatabase(helper.db_path, pragmas={ +logger = logging.getLogger(__name__) +peewee_logger = logging.getLogger('peewee') +peewee_logger.setLevel(logging.INFO) +database = SqliteDatabase(helper.db_path, pragmas = { 'journal_mode': 'wal', 'cache_size': -1024 * 10}) diff --git a/app/classes/shared/migration.py b/app/classes/shared/migration.py index f200f1c7..4105b833 100644 --- a/app/classes/shared/migration.py +++ b/app/classes/shared/migration.py @@ -6,14 +6,11 @@ import sys import os import re from functools import wraps - from functools import cached_property from app.classes.shared.helpers import helper from app.classes.shared.console import console -logger = logging.getLogger(__name__) - try: import peewee from playhouse.migrate import ( @@ -23,10 +20,9 @@ try: ) except ModuleNotFoundError as e: - logger.critical("Import Error: Unable to load {} module".format( - e.name), exc_info=True) - console.critical("Import Error: Unable to load {} module".format(e.name)) - sys.exit(1) + helper.auto_installer_fix(e) + +logger = logging.getLogger(__name__) MIGRATE_TABLE = 'migratehistory' MIGRATE_TEMPLATE = '''# Generated by database migrator @@ -448,4 +444,4 @@ class MigrationManager(object): migrator = self.migrator self.up_one(name, migrator, False, True) - logger.warning('Rolled back migration: {}'.format(name)) \ No newline at end of file + logger.warning('Rolled back migration: {}'.format(name)) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 57dce27c..7ebafdb3 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -1,5 +1,4 @@ import os -import sys import re import time import datetime @@ -9,35 +8,29 @@ import logging.config import subprocess import html import tempfile -from apscheduler.schedulers.background import BackgroundScheduler -#TZLocal is set as a hidden import on win pipeline -from tzlocal import get_localzone +from app.classes.minecraft.stats import Stats +from app.classes.minecraft.mc_ping import ping, ping_bedrock from app.classes.models.servers import Server_Stats, servers_helper from app.classes.models.management import management_helper from app.classes.models.users import users_helper from app.classes.models.server_permissions import server_permissions - -from app.classes.minecraft.stats import Stats -from app.classes.minecraft.mc_ping import ping, ping_bedrock - from app.classes.shared.helpers import helper from app.classes.shared.console import console from app.classes.shared.translation import translation from app.classes.shared.file_helpers import file_helper - from app.classes.web.websocket_helper import websocket_helper -logger = logging.getLogger(__name__) - try: import psutil + #TZLocal is set as a hidden import on win pipeline + from tzlocal import get_localzone + from apscheduler.schedulers.background import BackgroundScheduler except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) +logger = logging.getLogger(__name__) class ServerOutBuf: lines = {} diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index d448b644..bad0c79f 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -1,35 +1,29 @@ import os -import sys import time import logging import threading import asyncio import datetime -from tzlocal import get_localzone +from app.classes.controllers.users_controller import Users_Controller +from app.classes.minecraft.serverjars import server_jar_obj +from app.classes.models.management import management_helper +from app.classes.models.users import users_helper from app.classes.shared.helpers import helper from app.classes.shared.console import console - from app.classes.web.tornado_handler import Webserver from app.classes.web.websocket_helper import websocket_helper -from app.classes.minecraft.serverjars import server_jar_obj -from app.classes.models.management import management_helper -from app.classes.controllers.users_controller import Users_Controller -from app.classes.models.users import users_helper - -logger = logging.getLogger('apscheduler') - try: + from tzlocal import get_localzone from apscheduler.events import EVENT_JOB_EXECUTED from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger except ModuleNotFoundError as err: - logger.critical(f"Import Error: Unable to load {err.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {err.name} module") - sys.exit(1) + helper.auto_installer_fix(err) +logger = logging.getLogger('apscheduler') scheduler_intervals = { 'seconds', 'minutes', 'hours', diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index c103bab7..16d6b130 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -3,22 +3,25 @@ import html import re import logging import time -import tornado.web -import tornado.escape -import bleach +from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.shared.console import console from app.classes.shared.helpers import helper -from app.classes.web.websocket_helper import websocket_helper from app.classes.shared.translation import translation from app.classes.shared.server import ServerOutBuf - +from app.classes.web.websocket_helper import websocket_helper from app.classes.web.base_handler import BaseHandler -from app.classes.models.server_permissions import Enum_Permissions_Server + +try: + import bleach + import tornado.web + import tornado.escape + +except ModuleNotFoundError as ex: + helper.auto_installer_fix(ex) logger = logging.getLogger(__name__) - class AjaxHandler(BaseHandler): def render_page(self, template, page_data): diff --git a/app/classes/web/api_handler.py b/app/classes/web/api_handler.py index b4371694..1374d929 100644 --- a/app/classes/web/api_handler.py +++ b/app/classes/web/api_handler.py @@ -3,8 +3,7 @@ import re from app.classes.web.base_handler import BaseHandler -log = logging.getLogger(__name__) - +logger = logging.getLogger(__name__) bearer_pattern = re.compile(r'^Bearer', flags=re.IGNORECASE) class ApiHandler(BaseHandler): @@ -17,7 +16,7 @@ class ApiHandler(BaseHandler): def access_denied(self, user, reason=''): if reason: reason = ' because ' + reason - log.info("User %s from IP %s was denied access to the API route " + self.request.path + reason, user, self.get_remote_ip()) + logger.info("User %s from IP %s was denied access to the API route " + self.request.path + reason, user, self.get_remote_ip()) self.finish(self.return_response(403, { 'error':'ACCESS_DENIED', 'info':'You were denied access to the requested resource' @@ -25,7 +24,7 @@ class ApiHandler(BaseHandler): def authenticate_user(self) -> bool: try: - log.debug("Searching for specified token") + logger.debug("Searching for specified token") api_token = self.get_argument('token', '') if api_token is None and self.request.headers.get('Authorization'): @@ -34,10 +33,10 @@ class ApiHandler(BaseHandler): api_token = self.get_cookie('token') user_data = self.controller.users.get_user_by_api_token(api_token) - log.debug("Checking results") + logger.debug("Checking results") if user_data: # Login successful! Check perms - log.info(f"User {user_data['username']} has authenticated to API") + logger.info(f"User {user_data['username']} has authenticated to API") # TODO: Role check return True # This is to set the "authenticated" @@ -46,7 +45,7 @@ class ApiHandler(BaseHandler): self.access_denied("unknown", "the user provided an invalid token") return False except Exception as e: - log.warning("An error occured while authenticating an API user: %s", e) + logger.warning("An error occured while authenticating an API user: %s", e) self.finish(self.return_response(403, { 'error':'ACCESS_DENIED', 'info':'An error occured while authenticating the user' diff --git a/app/classes/web/base_handler.py b/app/classes/web/base_handler.py index 9c7831cf..3ac7d879 100644 --- a/app/classes/web/base_handler.py +++ b/app/classes/web/base_handler.py @@ -4,17 +4,21 @@ from typing import ( List, Optional, Tuple, Dict, Any ) -import tornado.web -import bleach - -from app.classes.shared.authentication import authentication -from app.classes.shared.main_controller import Controller from app.classes.models.users import ApiKeys +from app.classes.shared.authentication import authentication +from app.classes.shared.main_controller import Controller +from app.classes.shared.helpers import helper + +try: + import tornado.web + import bleach + +except ModuleNotFoundError as e: + helper.auto_installer_fix(e) logger = logging.getLogger(__name__) - class BaseHandler(tornado.web.RequestHandler): nobleach = {bool, type(None)} diff --git a/app/classes/web/file_handler.py b/app/classes/web/file_handler.py index 558b064d..3f84fc51 100644 --- a/app/classes/web/file_handler.py +++ b/app/classes/web/file_handler.py @@ -1,19 +1,22 @@ import os import logging -import tornado.web -import tornado.escape -import bleach +from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.shared.console import console from app.classes.shared.helpers import helper from app.classes.shared.file_helpers import file_helper - from app.classes.web.base_handler import BaseHandler -from app.classes.models.server_permissions import Enum_Permissions_Server + +try: + import bleach + import tornado.web + import tornado.escape + +except ModuleNotFoundError as e: + helper.auto_installer_fix(e) logger = logging.getLogger(__name__) - class FileHandler(BaseHandler): def render_page(self, template, page_data): diff --git a/app/classes/web/http_handler.py b/app/classes/web/http_handler.py index 668faf37..e3b03f57 100644 --- a/app/classes/web/http_handler.py +++ b/app/classes/web/http_handler.py @@ -1,9 +1,14 @@ import logging -import requests from app.classes.shared.helpers import helper from app.classes.web.base_handler import BaseHandler +try: + import requests + +except ModuleNotFoundError as e: + helper.auto_installer_fix(e) + logger = logging.getLogger(__name__) class HTTPHandler(BaseHandler): diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 2082006d..636ed36b 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -5,27 +5,28 @@ from typing import Dict, Any, Tuple import json import logging import threading -import bleach -import libgravatar -import requests - -import tornado.web -import tornado.escape -from tornado import iostream - -#TZLocal is set as a hidden import on win pipeline -from tzlocal import get_localzone -from cron_validator import CronValidator from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.models.crafty_permissions import Enum_Permissions_Crafty from app.classes.models.management import management_helper - from app.classes.shared.authentication import authentication from app.classes.shared.helpers import helper - from app.classes.web.base_handler import BaseHandler +try: + import bleach + import libgravatar + import requests + import tornado.web + import tornado.escape + from tornado import iostream + #TZLocal is set as a hidden import on win pipeline + from tzlocal import get_localzone + from cron_validator import CronValidator + +except ModuleNotFoundError as ex: + helper.auto_installer_fix(ex) + logger = logging.getLogger(__name__) class PanelHandler(BaseHandler): diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index 95672130..242b7da7 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -1,23 +1,18 @@ -import sys import logging +from app.classes.models.users import Users from app.classes.shared.authentication import authentication from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.shared.main_models import fn - -from app.classes.models.users import Users from app.classes.web.base_handler import BaseHandler -logger = logging.getLogger(__name__) - try: import bleach except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) + +logger = logging.getLogger(__name__) class PublicHandler(BaseHandler): diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index d8855efa..de900c07 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -1,30 +1,24 @@ -import sys import json import logging import os -import libgravatar -import requests +from app.classes.minecraft.serverjars import server_jar_obj +from app.classes.models.crafty_permissions import Enum_Permissions_Crafty from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.shared.file_helpers import file_helper from app.classes.web.base_handler import BaseHandler -from app.classes.models.crafty_permissions import Enum_Permissions_Crafty -from app.classes.minecraft.serverjars import server_jar_obj - - -logger = logging.getLogger(__name__) try: import tornado.web import tornado.escape import bleach + import libgravatar + import requests except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) +logger = logging.getLogger(__name__) class ServerHandler(BaseHandler): diff --git a/app/classes/web/static_handler.py b/app/classes/web/static_handler.py index 993dc3b0..4d401ddb 100644 --- a/app/classes/web/static_handler.py +++ b/app/classes/web/static_handler.py @@ -1,5 +1,11 @@ from typing import ( Optional ) -import tornado.web + +try: + import tornado.web + +except ModuleNotFoundError as e: + from app.classes.shared.helpers import helper + helper.auto_installer_fix(e) class CustomStaticHandler(tornado.web.StaticFileHandler): def validate_absolute_path(self, root: str, absolute_path: str) -> Optional[str]: diff --git a/app/classes/web/tornado_handler.py b/app/classes/web/tornado_handler.py index 42313ead..ca411751 100644 --- a/app/classes/web/tornado_handler.py +++ b/app/classes/web/tornado_handler.py @@ -4,7 +4,21 @@ import json import asyncio import logging -logger = logging.getLogger(__name__) +from app.classes.shared.translation import translation +from app.classes.shared.console import console +from app.classes.shared.helpers import helper +from app.classes.web.file_handler import FileHandler +from app.classes.web.public_handler import PublicHandler +from app.classes.web.panel_handler import PanelHandler +from app.classes.web.default_handler import DefaultHandler +from app.classes.web.server_handler import ServerHandler +from app.classes.web.ajax_handler import AjaxHandler +from app.classes.web.api_handler import ServersStats, NodeStats +from app.classes.web.websocket_handler import SocketHandler +from app.classes.web.static_handler import CustomStaticHandler +from app.classes.web.upload_handler import UploadHandler +from app.classes.web.http_handler import HTTPHandler, HTTPHandlerPage +from app.classes.web.status_handler import StatusHandler try: import tornado.web @@ -14,26 +28,11 @@ try: import tornado.escape import tornado.locale import tornado.httpserver - from app.classes.web.file_handler import FileHandler - from app.classes.web.public_handler import PublicHandler - from app.classes.web.panel_handler import PanelHandler - from app.classes.web.default_handler import DefaultHandler - from app.classes.web.server_handler import ServerHandler - from app.classes.web.ajax_handler import AjaxHandler - from app.classes.web.api_handler import ServersStats, NodeStats - from app.classes.web.websocket_handler import SocketHandler - from app.classes.web.static_handler import CustomStaticHandler - from app.classes.web.upload_handler import UploadHandler - from app.classes.web.http_handler import HTTPHandler, HTTPHandlerPage - from app.classes.web.status_handler import StatusHandler - from app.classes.shared.translation import translation - from app.classes.shared.console import console - from app.classes.shared.helpers import helper except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) + +logger = logging.getLogger(__name__) class Webserver: diff --git a/app/classes/web/upload_handler.py b/app/classes/web/upload_handler.py index 478a042c..2702593e 100644 --- a/app/classes/web/upload_handler.py +++ b/app/classes/web/upload_handler.py @@ -2,23 +2,24 @@ import logging import os import time -import tornado.options -import tornado.web -import tornado.httpserver - +from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.shared.helpers import helper from app.classes.shared.console import console from app.classes.shared.main_controller import Controller - from app.classes.web.websocket_helper import websocket_helper from app.classes.web.base_handler import BaseHandler -from app.classes.models.server_permissions import Enum_Permissions_Server +try: + import tornado.web + import tornado.options + import tornado.httpserver +except ModuleNotFoundError as ex: + helper.auto_installer_fix(ex) logger = logging.getLogger(__name__) -# Class&Function Defination +# Class & Function Defination MAX_STREAMED_SIZE = 1024 * 1024 * 1024 @tornado.web.stream_request_body diff --git a/app/classes/web/websocket_handler.py b/app/classes/web/websocket_handler.py index bc90a5c0..d8146fd3 100644 --- a/app/classes/web/websocket_handler.py +++ b/app/classes/web/websocket_handler.py @@ -1,24 +1,19 @@ import json import logging import asyncio -import sys from urllib.parse import parse_qsl from app.classes.shared.authentication import authentication from app.classes.shared.helpers import helper -from app.classes.shared.console import console from app.classes.web.websocket_helper import websocket_helper -logger = logging.getLogger(__name__) - try: import tornado.websocket except ModuleNotFoundError as e: - logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) - console.critical(f"Import Error: Unable to load {e.name} module") - sys.exit(1) + helper.auto_installer_fix(e) +logger = logging.getLogger(__name__) class SocketHandler(tornado.websocket.WebSocketHandler): page = None