feat: Dynamically resolve configuration file paths to support frozen executables.
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
|||||||
import uuid
|
import uuid
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
CONFIG_FILE = "config.dat" # Obfuscated file
|
import sys
|
||||||
|
|
||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
DEFAULT_JOB = {
|
DEFAULT_JOB = {
|
||||||
@@ -30,6 +30,16 @@ class ConfigManager:
|
|||||||
"schedule_unit": "Menit"
|
"schedule_unit": "Menit"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_base_dir():
|
||||||
|
if getattr(sys, 'frozen', False):
|
||||||
|
return os.path.dirname(sys.executable)
|
||||||
|
return os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_config_path(filename="config.dat"):
|
||||||
|
return os.path.join(ConfigManager._get_base_dir(), filename)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_new_job():
|
def create_new_job():
|
||||||
job = ConfigManager.DEFAULT_JOB.copy()
|
job = ConfigManager.DEFAULT_JOB.copy()
|
||||||
@@ -38,24 +48,27 @@ class ConfigManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_config():
|
def load_config():
|
||||||
|
config_dat = ConfigManager._get_config_path("config.dat")
|
||||||
|
config_json = ConfigManager._get_config_path("config.json")
|
||||||
|
|
||||||
# Check for old unencrypted config.json and migrate
|
# Check for old unencrypted config.json and migrate
|
||||||
if os.path.exists("config.json"):
|
if os.path.exists(config_json):
|
||||||
try:
|
try:
|
||||||
with open("config.json", 'r') as f:
|
with open(config_json, 'r') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
print("Migrasi config.json...")
|
print("Migrasi config.json...")
|
||||||
config = ConfigManager._migrate_old_config(data)
|
config = ConfigManager._migrate_old_config(data)
|
||||||
ConfigManager.save_config(config)
|
ConfigManager.save_config(config)
|
||||||
os.rename("config.json", "config.json.bak")
|
os.rename(config_json, config_json + ".bak")
|
||||||
return config
|
return config
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error migrasi config: {}".format(e))
|
print("Error migrasi config: {}".format(e))
|
||||||
|
|
||||||
if not os.path.exists(CONFIG_FILE):
|
if not os.path.exists(config_dat):
|
||||||
return ConfigManager.DEFAULT_CONFIG.copy()
|
return ConfigManager.DEFAULT_CONFIG.copy()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(CONFIG_FILE, 'rb') as f:
|
with open(config_dat, 'rb') as f:
|
||||||
encoded_data = f.read()
|
encoded_data = f.read()
|
||||||
|
|
||||||
# Simple Base64 Decode
|
# Simple Base64 Decode
|
||||||
@@ -113,7 +126,8 @@ class ConfigManager:
|
|||||||
# Simple Base64 Encode
|
# Simple Base64 Encode
|
||||||
encoded_data = base64.b64encode(json_data.encode('utf-8'))
|
encoded_data = base64.b64encode(json_data.encode('utf-8'))
|
||||||
|
|
||||||
with open(CONFIG_FILE, 'wb') as f:
|
config_path = ConfigManager._get_config_path("config.dat")
|
||||||
|
with open(config_path, 'wb') as f:
|
||||||
f.write(encoded_data)
|
f.write(encoded_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error saving config: {}".format(e))
|
print("Error saving config: {}".format(e))
|
||||||
|
|||||||
Reference in New Issue
Block a user