Skip to content

Commit

Permalink
feat(config)!: add batch mode choice to config file
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Add a new "other_settings" folder to the
"config_files" to keep other configuration files there.
  • Loading branch information
Cyber-Syntax committed Jan 13, 2025
1 parent 4ca69ca commit 6d49803
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
23 changes: 11 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
from babel.support import Translations

_ = gettext.gettext
file_handler = FileHandler()


def get_locale_config(file_path):
"""Load the locale configuration from the config file."""
config_path = os.path.join(file_path, "locale.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as file:
if os.path.exists(file_handler.config_path):
with open(file_handler.config_path, "r", encoding="utf-8") as file:
config = json.load(file)
return config.get("locale") # Return None if no locale is set
return None # Return None if no config file exists


def save_locale_config(file_path, locale):
"""Save the selected locale to the config file."""
config_path = os.path.join(file_path, "locale.json")
print(f"Saving locale config to {config_path}") # Debug statement
os.makedirs(os.path.dirname(config_path), exist_ok=True)
with open(config_path, "w", encoding="utf-8") as file:
print(f"Saving locale config to {file_handler.config_path}") # Debug statement
os.makedirs(os.path.dirname(file_handler.config_path), exist_ok=True)
with open(file_handler.config_path, "w", encoding="utf-8") as file:
json.dump({"locale": locale}, file, indent=4)
print(f"Locale saved as: {locale}") # Debug statement

Expand Down Expand Up @@ -78,10 +77,11 @@ def update_locale(file_handler):
choice = int(input("Enter your choice: "))
if choice in languages:
language = languages[choice]
config_path = os.path.join(file_handler.file_path, "locale.json")
print(f"Updating locale config to {config_path}") # Debug statement
os.makedirs(os.path.dirname(config_path), exist_ok=True)
with open(config_path, "w", encoding="utf-8") as file:
print(
f"Updating locale config to {file_handler.config_path}"
) # Debug statement
os.makedirs(os.path.dirname(file_handler.config_path), exist_ok=True)
with open(file_handler.config_path, "w", encoding="utf-8") as file:
json.dump({"locale": language}, file, indent=4)
print(f"Locale updated to: {language}") # Debug statement
else:
Expand Down Expand Up @@ -173,7 +173,6 @@ def main():
9. Make executable, delete version from appimage_name and move to directory
"""
configure_logging()
file_handler = FileHandler()

if not os.path.isfile(os.path.join(file_handler.file_path, "locale.json")):
select_language(file_handler.file_path)
Expand Down
7 changes: 7 additions & 0 deletions src/app_image_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ class AppImageDownloader:

def __post_init__(self):
self.appimage_folder = os.path.expanduser(self.appimage_folder)

self.file_path = os.path.join(self.appimage_folder, "config_files/")
os.makedirs(self.file_path, exist_ok=True)

other_settings_folder = os.path.join(self.file_path, "other_settings")
os.makedirs(other_settings_folder, exist_ok=True)

self.config_batch_path = os.path.join(other_settings_folder, "batch_mode.json")
self.config_path = os.path.join(other_settings_folder, "locale.json")

@handle_common_errors
def ask_user(self):
"""New appimage installation options"""
Expand Down
45 changes: 33 additions & 12 deletions src/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ def update_version(self):
def check_updates_json_all(self):
"""Check for updates for all JSON files"""
json_files = [
file
for file in os.listdir(self.file_path)
if file.endswith(".json") and file != "locale.json"
file for file in os.listdir(self.file_path) if file.endswith(".json")
]

# Output the list of JSON files found
Expand Down Expand Up @@ -499,15 +497,22 @@ def check_updates_json_all(self):
@handle_common_errors
def update_selected_appimages(self, appimages_to_update):
"""Update all appimages"""
if (
input(
_("Enable batch mode to continue without asking for approval? (y/n): ")
).lower()
!= "y"
):
batch_mode = False
else:
batch_mode = True
batch_mode = self.load_batch_mode()

if batch_mode is None: # If no saved value is found, prompt for it
if (
input(
_(
"Enable batch mode to continue without asking for approval? (y/n): "
)
).lower()
!= "y"
):
batch_mode = False
else:
batch_mode = True
# Save the batch_mode value to a file
self.save_batch_mode(batch_mode)

if batch_mode:
print(
Expand Down Expand Up @@ -540,3 +545,19 @@ def update_selected_appimages(self, appimages_to_update):
self.handle_file_operations(batch_mode=batch_mode)

print(_("Update process completed for all selected appimages."))

def save_batch_mode(self, batch_mode):
"""Save batch_mode to a JSON file"""

data = {"batch_mode": batch_mode}
with open(self.config_batch_path, "w") as json_file:
json.dump(data, json_file)

def load_batch_mode(self):
"""Load batch_mode from a JSON file"""
try:
with open(self.config_batch_path, "r") as json_file:
data = json.load(json_file)
return data.get("batch_mode", None) # Return None if not found
except FileNotFoundError:
return None # Return None if the file doesn't exist

0 comments on commit 6d49803

Please sign in to comment.