Skip to content

Commit 582f9ae

Browse files
committed
Update (configs, etc.) now handled by auto update module
1 parent 85d3c47 commit 582f9ae

3 files changed

Lines changed: 51 additions & 33 deletions

File tree

app_start.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AppStartManager:
1717
updater = inject.attr(AutoUpdater)
1818
state_controller = inject.attr(FilterStateController)
1919
interaction_manager = inject.attr(InteractionManager)
20+
config_file_manager = inject.attr(ConfigFileManager)
2021
config = inject.attr(ConfigObj)
2122
inversion_rules_file_manager = inject.attr(RulesFileManager)
2223
main_executor = inject.attr(MainExecutor)
@@ -25,7 +26,8 @@ class AppStartManager:
2526

2627
def setup(self):
2728
self.close_manager.setup()
28-
self.updater.on_update_applied = self.handle_update
29+
self.config_file_manager.setup()
30+
self.inversion_rules_file_manager.setup()
2931
self.interaction_manager.setup()
3032
self.tray.setup()
3133

@@ -55,40 +57,12 @@ async def run(self):
5557
pass
5658
print("Bye")
5759

58-
def handle_update(self,
59-
new_path: Path,
60-
current_path: Path,
61-
backup_filename: str):
62-
try:
63-
from shutil import copyfile
64-
from os import path
65-
66-
copy_list = [
67-
self.config.filename,
68-
self.inversion_rules_file_manager.filename
69-
]
70-
71-
for filename in copy_list:
72-
current_file_path = path.join(current_path, filename)
73-
new_file_path = path.join(new_path, filename)
74-
if path.exists(current_file_path):
75-
if not path.exists(new_file_path):
76-
copyfile(current_file_path, new_file_path)
77-
else:
78-
print(f"Skip {filename}: update contains same file")
79-
else:
80-
print(f"Skip {filename}: no such file")
81-
82-
except Exception as e:
83-
print("Failed to copy previous version data:", e)
84-
print("You may do this manually, from", backup_filename)
85-
print("Files to copy:", copy_list)
86-
8760

8861
def configure(binder: inject.Binder):
8962
# Couple of components
9063
# Handled at runtime
9164
config_manager = ConfigFileManager("config")
65+
binder.bind(ConfigFileManager, config_manager)
9266
binder.bind(ConfigObj, config_manager.config)
9367

9468
inversion_rules = InversionRulesController()

auto_update.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,60 @@ def __init__(self):
3838
from datetime import timedelta
3939
self.delay = timedelta(days=1).total_seconds()
4040
self.response = Queue()
41+
# carry-on baggage is list of filenames
42+
# moved to new location on update
43+
self.carryon: list[str] = []
4144
self.developer_mode = sys.argv[0].endswith(".py")
4245
if not self.developer_mode:
4346
self.thread = Thread(
4447
name="Update Checker",
45-
target=self.check_loop,
48+
target=self._run_check_loop,
4649
daemon=True
4750
)
4851
self.update_in_progress = False
4952

53+
def move_on_update(self, filename):
54+
self.carryon.append(filename)
55+
56+
def _move_carryon(self,
57+
current_path: Path,
58+
new_path: Path):
59+
if not self.carryon:
60+
return
61+
62+
from shutil import copyfile
63+
from os import path
64+
65+
for filename in self.carryon:
66+
current_file_path = path.join(current_path, filename)
67+
new_file_path = path.join(new_path, filename)
68+
if path.exists(current_file_path):
69+
if not path.exists(new_file_path):
70+
copyfile(current_file_path, new_file_path)
71+
else:
72+
print(f"Skip {filename}: update contains same file")
73+
else:
74+
print(f"Skip {filename}: no such file")
75+
5076
def on_update_applied(self,
5177
new_path: Path,
5278
current_path: Path,
5379
backup_filename: str):
54-
pass
80+
try:
81+
self._move_carryon(current_path, new_path)
82+
except Exception as e:
83+
print("Failed to copy previous version data:", e)
84+
print("You may do this manually, from", backup_filename)
85+
print("Files to copy:", self.carryon)
86+
input("Press enter to continue update")
5587

5688
def run_check_loop(self):
5789
if self.developer_mode:
5890
return
5991
self.thread.start()
6092
return self.thread
6193

62-
def check_loop(self):
94+
def _run_check_loop(self):
6395
while True:
6496
if self.config["update"]["check_for_updates"]:
6597
self.check_for_updates()

realtime_data_sync.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1+
import inject
12
import jsons
23
import yaml
34
from configobj import ConfigObj
45
from file_tracker import FileTracker
56
from inversion_rules import InversionRulesController, InversionRule, RULES
7+
from auto_update import AutoUpdater
68

79

810
class ConfigFileManager(FileTracker):
11+
updater = inject.attr(AutoUpdater)
12+
913
def __init__(self, name: str):
1014
self.config = ConfigObj(infile=name + ".ini",
1115
configspec=name + "_description.ini",
1216
create_empty=True,
1317
write_empty_values=True)
1418
super().__init__(self.config.filename)
1519

20+
def setup(self):
21+
self.updater.move_on_update(self.filename)
22+
1623
def load_file(self):
1724
self.validate_config()
1825

@@ -60,12 +67,17 @@ def on_partially_invalid(self, validation_response: dict):
6067

6168

6269
class RulesFileManager(FileTracker):
70+
updater = inject.attr(AutoUpdater)
71+
6372
def __init__(self, name: str, rules_controller: InversionRulesController):
6473
self.rules: RULES = None
6574
self.rules_controller = rules_controller
6675
self.rules_controller.on_modified = self.save_rules
6776
super().__init__(name + "_rules.yaml")
6877

78+
def setup(self):
79+
self.updater.move_on_update(self.filename)
80+
6981
def load_file(self):
7082
try:
7183
with self.observer.overlook():

0 commit comments

Comments
 (0)