Skip to content

Commit a8153e4

Browse files
committed
refactor: make it more clear when _create_startup_entry() is called
also merge duplicated code in initialize and reset
1 parent 06bbf3e commit a8153e4

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

safeeyes/configuration.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ def load(cls) -> "Config":
4747
force_upgrade_keys: list[str] = []
4848
# force_upgrade_keys = ['long_breaks', 'short_breaks']
4949

50-
# if _create_startup_entry finds a broken autostart symlink, it will repair
51-
# it
52-
cls._create_startup_entry(force=False)
5350
if user_config is None:
5451
cls._initialize_config()
5552
user_config = copy.deepcopy(system_config)
5653
cfg = cls(user_config, system_config)
5754
cfg.save()
55+
56+
# This gets called when the configuration file is not present, which
57+
# happens just after installation or manual deletion of
58+
# .config/safeeyes/safeeyes.json file. In this case, we want to force the
59+
# creation of a startup entry
60+
cls._create_startup_entry(force=True)
5861
return cfg
5962
else:
6063
system_config_version = system_config["meta"]["config_version"]
@@ -76,6 +79,11 @@ def load(cls) -> "Config":
7679

7780
cfg = cls(user_config, system_config)
7881
cfg.save()
82+
83+
# if _create_startup_entry finds a broken autostart symlink, it will repair
84+
# it
85+
cls._create_startup_entry(force=False)
86+
7987
return cfg
8088

8189
def __init__(
@@ -131,17 +139,11 @@ def __ne__(self, config):
131139
return self.__user_config != config.__user_config
132140

133141
@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)
142+
def reset_config(cls) -> "Config":
143+
cls._initialize_config()
140144

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+
# This calls _create_startup_entry()
146+
return Config.load()
145147

146148
@classmethod
147149
def _initialize_config(cls) -> None:
@@ -158,13 +160,9 @@ def _initialize_config(cls) -> None:
158160

159161
# Copy the safeeyes.json
160162
shutil.copy2(utility.SYSTEM_CONFIG_FILE_PATH, utility.CONFIG_FILE_PATH)
161-
os.chmod(utility.CONFIG_FILE_PATH, 0o600)
162163

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)
164+
# Add write permission (e.g. if original file was stored in /nix/store)
165+
os.chmod(utility.CONFIG_FILE_PATH, 0o600)
168166

169167
@classmethod
170168
def _create_startup_entry(cls, force: bool = False) -> None:

safeeyes/ui/settings_dialog.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +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-
Config.reset_config()
176-
self.config = Config.load()
175+
self.config = Config.reset_config()
177176
# Remove breaks from the container
178177
self.__clear_children(self.box_short_breaks)
179178
self.__clear_children(self.box_long_breaks)

0 commit comments

Comments
 (0)