From ab17491ab45202c4f6b7a67e0cd719913be3afbc Mon Sep 17 00:00:00 2001 From: Artem30801 Date: Tue, 6 Oct 2020 00:35:59 +0300 Subject: [PATCH] server: fixed running from any working directory --- server/modules/server_core.py | 11 +++++------ server/server.py | 9 +++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/server/modules/server_core.py b/server/modules/server_core.py index 68c2bf0..d793bb6 100644 --- a/server/modules/server_core.py +++ b/server/modules/server_core.py @@ -11,9 +11,8 @@ import collections import traceback # Add parent dir to PATH to import messaging_lib and config_lib -current_dir = (os.path.dirname(os.path.realpath(__file__))) -lib_dir = os.path.realpath(os.path.join(current_dir, '../../lib')) -sys.path.insert(0, lib_dir) +current_dir = os.path.dirname(os.path.realpath(__file__)) +sys.path.insert(0, os.path.realpath(os.path.join(current_dir, os.pardir, os.pardir, 'lib'))) # Import modules from lib dir import messaging @@ -23,7 +22,7 @@ random.seed() now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") -log_path = 'server_logs' +log_path = os.path.join(current_dir, os.pardir, "server_logs") if not os.path.exists(log_path): try: os.mkdir(log_path) @@ -38,7 +37,7 @@ ConfigOption = collections.namedtuple("ConfigOption", ["section", "option", "val class Server(messaging.Singleton): - def __init__(self, config_path="../config/server.ini", server_id=None): + def __init__(self, config_path=os.path.join(current_dir, os.pardir, "config", "server.ini"), server_id=None): self.id = server_id if server_id else str(random.randint(0, 9999)).zfill(4) self.time_started = 0 @@ -373,7 +372,7 @@ if __name__ == '__main__': level=logging.DEBUG, format="%(asctime)s [%(name)-7.7s] [%(threadName)-19.19s] [%(levelname)-7.7s] %(message)s", handlers=[ - logging.FileHandler("server_logs/{}.log".format(now)), + logging.FileHandler(os.path.join(log_path, "{}.log".format(now))), logging.StreamHandler() ]) diff --git a/server/server.py b/server/server.py index cd1c637..d7a0b6f 100644 --- a/server/server.py +++ b/server/server.py @@ -648,6 +648,7 @@ def set_taskbar_icon(): if __name__ == "__main__": + current_dir = os.path.dirname(os.path.realpath(__file__)) msgbox_handler = ExitMsgbox() msgbox_handler.setLevel(logging.CRITICAL) @@ -655,7 +656,7 @@ if __name__ == "__main__": level=logging.INFO, format="%(asctime)s [%(name)-7.7s] [%(threadName)-19.19s] [%(levelname)-7.7s] %(message)s", handlers=[ - logging.FileHandler("server_logs/{}.log".format(now)), + logging.FileHandler(os.path.join(current_dir, "server_logs", "{}.log".format(now))), logging.StreamHandler(), msgbox_handler ]) @@ -663,7 +664,7 @@ if __name__ == "__main__": sys.excepthook = except_hook # for debugging (exceptions traceback) app = QApplication(sys.argv) - splash_pix = QPixmap('icons/coex_splash.jpg') + splash_pix = QPixmap(os.path.join(current_dir, "icons", "coex_splash.jpg")) splash = QSplashScreen(splash_pix) splash.setEnabled(False) @@ -677,7 +678,7 @@ if __name__ == "__main__": # time.sleep(3) app_icon = QIcon() - app_icon.addFile('icons/image.ico', QtCore.QSize(256, 256)) + app_icon.addFile(os.path.join(current_dir, "icons", "image.ico"), QtCore.QSize(256, 256)) app.setWindowIcon(app_icon) if sys.platform == 'win32': @@ -688,7 +689,7 @@ if __name__ == "__main__": # app.exec_() with loop: - server = ServerQt(config_path="config/server.ini") + server = ServerQt(config_path=os.path.join(current_dir, "config", "server.ini")) window = MainWindow(server) Client.on_first_connect = window.new_client_connected