Skip to content

Commit 06bbf3e

Browse files
committed
refactor: inline config file handling into Config
1 parent 472855d commit 06bbf3e

File tree

3 files changed

+90
-86
lines changed

3 files changed

+90
-86
lines changed

safeeyes/configuration.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"""
2323

2424
import copy
25+
import logging
2526
from packaging.version import parse
27+
import os
28+
import shutil
2629
import typing
2730

2831
from safeeyes import utility
@@ -44,11 +47,11 @@ def load(cls) -> "Config":
4447
force_upgrade_keys: list[str] = []
4548
# force_upgrade_keys = ['long_breaks', 'short_breaks']
4649

47-
# if create_startup_entry finds a broken autostart symlink, it will repair
50+
# if _create_startup_entry finds a broken autostart symlink, it will repair
4851
# it
49-
utility.create_startup_entry(force=False)
52+
cls._create_startup_entry(force=False)
5053
if user_config is None:
51-
utility.initialize_safeeyes()
54+
cls._initialize_config()
5255
user_config = copy.deepcopy(system_config)
5356
cfg = cls(user_config, system_config)
5457
cfg.save()
@@ -126,3 +129,86 @@ def __eq__(self, config):
126129

127130
def __ne__(self, config):
128131
return self.__user_config != config.__user_config
132+
133+
@classmethod
134+
def reset_config(cls) -> None:
135+
# Remove the ~/.config/safeeyes/safeeyes.json and safeeyes_style.css
136+
utility.delete(utility.CONFIG_FILE_PATH)
137+
138+
# Copy the safeeyes.json and safeeyes_style.css
139+
shutil.copy2(utility.SYSTEM_CONFIG_FILE_PATH, utility.CONFIG_FILE_PATH)
140+
141+
# Add write permission (e.g. if original file was stored in /nix/store)
142+
os.chmod(utility.CONFIG_FILE_PATH, 0o600)
143+
144+
cls._create_startup_entry()
145+
146+
@classmethod
147+
def _initialize_config(cls) -> None:
148+
"""Create the config file in XDG_CONFIG_HOME(or
149+
~/.config)/safeeyes directory.
150+
"""
151+
logging.info("Copy the config files to XDG_CONFIG_HOME(or ~/.config)/safeeyes")
152+
153+
# Remove the ~/.config/safeeyes/safeeyes.json file
154+
utility.delete(utility.CONFIG_FILE_PATH)
155+
156+
if not os.path.isdir(utility.CONFIG_DIRECTORY):
157+
utility.mkdir(utility.CONFIG_DIRECTORY)
158+
159+
# Copy the safeeyes.json
160+
shutil.copy2(utility.SYSTEM_CONFIG_FILE_PATH, utility.CONFIG_FILE_PATH)
161+
os.chmod(utility.CONFIG_FILE_PATH, 0o600)
162+
163+
# This method gets called when the configuration file is not present,
164+
# which happens just after installation or manual deletion of
165+
# .config/safeeyes/safeeyes.json file. In these cases, we want to force the
166+
# creation of a startup entry
167+
cls._create_startup_entry(force=True)
168+
169+
@classmethod
170+
def _create_startup_entry(cls, force: bool = False) -> None:
171+
"""Create start up entry."""
172+
startup_dir_path = os.path.join(utility.HOME_DIRECTORY, ".config/autostart")
173+
startup_entry = os.path.join(
174+
startup_dir_path, "io.github.slgobinath.SafeEyes.desktop"
175+
)
176+
# until Safe Eyes 2.1.5 the startup entry had another name
177+
# https://github.com/slgobinath/safeeyes/commit/684d16265a48794bb3fd670da67283fe4e2f591b#diff-0863348c2143a4928518a4d3661f150ba86d042bf5320b462ea2e960c36ed275L398
178+
obsolete_entry = os.path.join(startup_dir_path, "safeeyes.desktop")
179+
180+
create_link = False
181+
182+
if force:
183+
# if force is True, just create the link
184+
create_link = True
185+
else:
186+
# if force is False, we want to avoid creating the startup symlink if it was
187+
# manually deleted by the user, we want to create it only if a broken one is
188+
# found
189+
if os.path.islink(startup_entry):
190+
# if the link exists, check if it is broken
191+
try:
192+
os.stat(startup_entry)
193+
except FileNotFoundError:
194+
# a FileNotFoundError will get thrown if the startup symlink is
195+
# broken
196+
create_link = True
197+
198+
if os.path.islink(obsolete_entry):
199+
# if a link with the old naming exists, delete it and create a new one
200+
create_link = True
201+
utility.delete(obsolete_entry)
202+
203+
if create_link:
204+
# Create the folder if not exist
205+
utility.mkdir(startup_dir_path)
206+
207+
# Remove existing files
208+
utility.delete(startup_entry)
209+
210+
# Create the new startup entry
211+
try:
212+
os.symlink(utility.SYSTEM_DESKTOP_FILE, startup_entry)
213+
except OSError:
214+
logging.error("Failed to create startup entry at %s" % startup_entry)

safeeyes/ui/settings_dialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def on_reset_menu_clicked(self, button: Gtk.Button) -> None:
172172
def __confirmation_dialog_response(dialog, result) -> None:
173173
response_id = dialog.choose_finish(result)
174174
if response_id == 1:
175-
utility.reset_config()
175+
Config.reset_config()
176176
self.config = Config.load()
177177
# Remove breaks from the container
178178
self.__clear_children(self.box_short_breaks)

safeeyes/utility.py

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -415,29 +415,6 @@ def load_css_file(style_sheet_path, priority, required=True):
415415
Gtk.StyleContext.add_provider_for_display(display, css_provider, priority)
416416

417417

418-
def initialize_safeeyes():
419-
"""Create the config file in XDG_CONFIG_HOME(or
420-
~/.config)/safeeyes directory.
421-
"""
422-
logging.info("Copy the config files to XDG_CONFIG_HOME(or ~/.config)/safeeyes")
423-
424-
# Remove the ~/.config/safeeyes/safeeyes.json file
425-
delete(CONFIG_FILE_PATH)
426-
427-
if not os.path.isdir(CONFIG_DIRECTORY):
428-
mkdir(CONFIG_DIRECTORY)
429-
430-
# Copy the safeeyes.json
431-
shutil.copy2(SYSTEM_CONFIG_FILE_PATH, CONFIG_FILE_PATH)
432-
os.chmod(CONFIG_FILE_PATH, 0o600)
433-
434-
# initialize_safeeyes gets called when the configuration file is not present, which
435-
# happens just after installation or manual deletion of
436-
# .config/safeeyes/safeeyes.json file. In these cases, we want to force the creation
437-
# of a startup entry
438-
create_startup_entry(force=True)
439-
440-
441418
def cleanup_old_user_stylesheet():
442419
# Create the XDG_CONFIG_HOME(or ~/.config)/safeeyes/style directory
443420
if not os.path.isdir(STYLE_SHEET_DIRECTORY):
@@ -474,52 +451,6 @@ def cleanup_old_user_stylesheet():
474451
)
475452

476453

477-
def create_startup_entry(force=False):
478-
"""Create start up entry."""
479-
startup_dir_path = os.path.join(HOME_DIRECTORY, ".config/autostart")
480-
startup_entry = os.path.join(
481-
startup_dir_path, "io.github.slgobinath.SafeEyes.desktop"
482-
)
483-
# until Safe Eyes 2.1.5 the startup entry had another name
484-
# https://github.com/slgobinath/safeeyes/commit/684d16265a48794bb3fd670da67283fe4e2f591b#diff-0863348c2143a4928518a4d3661f150ba86d042bf5320b462ea2e960c36ed275L398
485-
obsolete_entry = os.path.join(startup_dir_path, "safeeyes.desktop")
486-
487-
create_link = False
488-
489-
if force:
490-
# if force is True, just create the link
491-
create_link = True
492-
else:
493-
# if force is False, we want to avoid creating the startup symlink if it was
494-
# manually deleted by the user, we want to create it only if a broken one is
495-
# found
496-
if os.path.islink(startup_entry):
497-
# if the link exists, check if it is broken
498-
try:
499-
os.stat(startup_entry)
500-
except FileNotFoundError:
501-
# a FileNotFoundError will get thrown if the startup symlink is broken
502-
create_link = True
503-
504-
if os.path.islink(obsolete_entry):
505-
# if a link with the old naming exists, delete it and create a new one
506-
create_link = True
507-
delete(obsolete_entry)
508-
509-
if create_link:
510-
# Create the folder if not exist
511-
mkdir(startup_dir_path)
512-
513-
# Remove existing files
514-
delete(startup_entry)
515-
516-
# Create the new startup entry
517-
try:
518-
os.symlink(SYSTEM_DESKTOP_FILE, startup_entry)
519-
except OSError:
520-
logging.error("Failed to create startup entry at %s" % startup_entry)
521-
522-
523454
def initialize_platform():
524455
"""Copy icons and generate desktop entries."""
525456
logging.debug("Initialize the platform")
@@ -589,19 +520,6 @@ def initialize_platform():
589520
logging.error("Failed to create icon link at %s" % local_icon)
590521

591522

592-
def reset_config():
593-
# Remove the ~/.config/safeeyes/safeeyes.json and safeeyes_style.css
594-
delete(CONFIG_FILE_PATH)
595-
596-
# Copy the safeeyes.json and safeeyes_style.css
597-
shutil.copy2(SYSTEM_CONFIG_FILE_PATH, CONFIG_FILE_PATH)
598-
599-
# Add write permission (e.g. if original file was stored in /nix/store)
600-
os.chmod(CONFIG_FILE_PATH, 0o600)
601-
602-
create_startup_entry()
603-
604-
605523
def initialize_logging(debug):
606524
"""Initialize the logging framework using the Safe Eyes specific
607525
configurations.

0 commit comments

Comments
 (0)