Skip to content

Commit

Permalink
update to use .json extension file, but fallback to legacy .conf
Browse files Browse the repository at this point in the history
update to use .json extension file, but fallback to legacy .conf

fix variable value

store config file keys in config.py

properly detect and import legacy config
  • Loading branch information
n8marti committed Jan 2, 2024
1 parent a113043 commit 034cdbb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
10 changes: 6 additions & 4 deletions LogosLinuxInstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from utils import set_appimage
from utils import set_default_config
from utils import setDebug
from utils import write_config
from wine import run_indexing
from wine import run_logos
from wine import run_winetricks
Expand Down Expand Up @@ -130,10 +131,11 @@ def main():
# Update config from CONFIG_FILE.
# FIXME: This means that values in CONFIG_FILE take precedence over env variables.
# Is this preferred, or should env variables take precedence over CONFIG_FILE?
config_file = config.CONFIG_FILE
if file_exists(config.LEGACY_CONFIG_FILE):
config_file = config.LEGACY_CONFIG_FILE
config.set_config_env(config_file)
if not file_exists(config.CONFIG_FILE) and file_exists(config.LEGACY_CONFIG_FILE):
config.set_config_env(config.LEGACY_CONFIG_FILE)
write_config(config.CONFIG_FILE)
else:
config.set_config_env(config.CONFIG_FILE)

# Parse CLI args and update affected config vars.
parse_args(cli_args)
Expand Down
49 changes: 31 additions & 18 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
DEBUG = os.getenv('DEBUG', False)
DELETE_INSTALL_LOG = os.getenv('DELETE_INSTALL_LOG', False)
DIALOG = os.getenv('DIALOG')
LEGACY_CONFIG_FILE = os.path.expanduser("~/.config/Logos_on_Linux/Logos_on_Linux.conf")
LOGOS_LOG = os.getenv('LOGOS_LOG', os.path.expanduser("~/.local/state/Logos_on_Linux/install.log"))
LOGOS_VERSION = os.getenv('LOGOS_VERSION')
LOGOS64_MSI = os.getenv('LOGOS64_MSI')
Expand All @@ -48,7 +49,7 @@
# Other run-time variables.
ACTION = 'app'
APPIMAGE_LINK_SELECTION_NAME = "selected_wine.AppImage"
DEFAULT_CONFIG_PATH = os.path.expanduser("~/.config/Logos_on_Linux/Logos_on_Linux.conf")
DEFAULT_CONFIG_PATH = os.path.expanduser("~/.config/Logos_on_Linux/Logos_on_Linux.json")
EXTRA_INFO = "The following packages are usually necessary: winbind cabextract libjpeg8."
GUI = None
LOGOS_FORCE_ROOT = False
Expand Down Expand Up @@ -79,26 +80,38 @@
PACKAGES = None
SUPERUSER_COMMAND = None

persistent_config_keys = [
"FLPRODUCT", "FLPRODUCTi", "TARGETVERSION", "INSTALLDIR", "APPDIR",
"APPDIR_BINDIR", "WINETRICKSBIN", "WINEPREFIX", "WINEBIN_CODE", "WINE_EXE",
"WINESERVER_EXE", "WINE64_APPIMAGE_FULL_URL", "WINECMD_ENCODING",
"WINE64_APPIMAGE_FULL_FILENAME", "APPIMAGE_LINK_SELECTION_NAME",
"LOGOS_EXECUTABLE", "LOGOS_EXE", "LOGOS_DIR", "LOGS", "BACKUPDIR"
]

def get_config_file_dict(config_file_path):
config_dict = {}
try:
with open(config_file_path, 'r') as config_file:
cfg = json.load(config_file)
if config_file_path.endswith('.json'):
try:
with open(config_file_path, 'r') as config_file:
cfg = json.load(config_file)

for key, value in cfg.items():
config_dict[key] = value
return config_dict
except TypeError as e:
logging.error(f"Error opening Config file: {e}")
return None
except FileNotFoundError:
# Most likely a new install with no saved config file yet.
logging.info(f"No config file not found at {config_file_path}")
return None
except json.JSONDecodeError:
# Probably legacy config for bash script.
logging.info("Converting from legacy config file.")
for key, value in cfg.items():
config_dict[key] = value
return config_dict
except TypeError as e:
logging.error(f"Error opening Config file.")
logging.error(e)
return None
except FileNotFoundError:
logging.info(f"No config file not found at {config_file_path}")
return None
except json.JSONDecodeError as e:
logging.error(f"Config file could not be read.")
logging.error(e)
return None
elif config_file_path.endswith('.conf'):
# Legacy config from bash script.
logging.info("Reading from legacy config file.")
with open(config_file_path, 'r') as config_file:
for line in config_file:
line = line.strip()
Expand All @@ -113,12 +126,12 @@ def get_config_file_dict(config_file_path):
if len(vparts) > 1:
value = vparts[0].strip().strip('"').strip("'")
config_dict[parts[0]] = value

return config_dict

def set_config_env(config_file_path):
config_dict = get_config_file_dict(config_file_path)
if config_dict is None:
logos_error(f"Error: Unable to get config at {config_file_path}")
logging.info(f"Setting {len(config_dict)} variables from config file.")
for key, value in config_dict.items():
globals()[key] = value
13 changes: 3 additions & 10 deletions installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,9 @@ def postInstall(app):
app.root.event_generate("<<UpdateInstallText>>")

HOME = os.environ.get('HOME')
config_keys = [
"FLPRODUCT", "FLPRODUCTi", "TARGETVERSION", "INSTALLDIR", "APPDIR",
"APPDIR_BINDIR", "WINETRICKSBIN", "WINEPREFIX", "WINEBIN_CODE", "WINE_EXE",
"WINESERVER_EXE", "WINE64_APPIMAGE_FULL_URL", "WINECMD_ENCODING",
"WINE64_APPIMAGE_FULL_FILENAME", "APPIMAGE_LINK_SELECTION_NAME",
"LOGOS_EXECUTABLE", "LOGOS_EXE", "LOGOS_DIR", "LOGS", "BACKUPDIR"
]

logging.debug("post-install config:")
for k in config_keys:
for k in config.persistent_config_keys:
logging.debug(f"{k}: {config.__dict__.get(k)}")

if os.path.isfile(config.LOGOS_EXE):
Expand All @@ -410,7 +403,7 @@ def postInstall(app):
logging.info(f"No config file at {config.CONFIG_FILE}")
os.makedirs(os.path.join(HOME, ".config", "Logos_on_Linux"), exist_ok=True)
if os.path.isdir(os.path.join(HOME, ".config", "Logos_on_Linux")):
write_config(config.CONFIG_FILE, config_keys)
write_config(config.CONFIG_FILE)
logging.info(f"A config file was created at {config.CONFIG_FILE}.")
else:
logos_warn(f"{HOME}/.config/Logos_on_Linux does not exist. Failed to create config file.")
Expand All @@ -426,7 +419,7 @@ def postInstall(app):
break
if different is True and logos_acknowledge_question(f"Update config file at {config.CONFIG_FILE}?", "The existing config file was not overwritten."):
logging.info(f"Updating config file.")
write_config(config.CONFIG_FILE, config_keys)
write_config(config.CONFIG_FILE)
else:
# Script was run with a config file. Skip modifying the config.
pass
Expand Down
5 changes: 3 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ def set_default_config():
config.MYDOWNLOADS = get_user_downloads_dir()
os.makedirs(os.path.dirname(config.LOGOS_LOG), exist_ok=True)

def write_config(config_file_path, config_keys=None):
def write_config(config_file_path):
logging.info(f"Writing config to {config_file_path}")
os.makedirs(os.path.dirname(config_file_path), exist_ok=True)

config_data = {key: config.__dict__.get(key) for key in config_keys}
config_data = {key: config.__dict__.get(key) for key in config.persistent_config_keys}

try:
with open(config_file_path, 'w') as config_file:
Expand Down

0 comments on commit 034cdbb

Please sign in to comment.