Skip to content

Commit

Permalink
Remove unnesseccary TODO and refactor daemon_handler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
chase9 committed Apr 19, 2024
1 parent dbae8f2 commit 4757a61
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions yin_yang/daemon_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
import re
import shutil
import subprocess
from enum import Enum, auto
from pathlib import Path
import re

from yin_yang import helpers

from .config import ConfigWatcher, config
from .meta import ConfigEvent, Modes

Expand All @@ -15,7 +15,6 @@
SERVICE_PATH = SYSTEMD_PATH / 'yin_yang.service'



def create_files():
logger.debug('Creating systemd files')
if not SYSTEMD_PATH.is_dir():
Expand All @@ -24,13 +23,20 @@ def create_files():
shutil.copy('./resources/yin_yang.timer', TIMER_PATH)
if not SERVICE_PATH.is_file():
shutil.copy('./resources/yin_yang.service', SERVICE_PATH)
# TODO: Will this cause an issue switching back from flatpak?
if (helpers.is_flatpak()):
if helpers.is_flatpak():
with open(SERVICE_PATH, 'r') as service:
lines = service.readlines()
with open(SERVICE_PATH, 'w') as service:
for line in lines:
service.write(re.sub('ExecStart=\/usr\/bin\/yin-yang --systemd', 'ExecStart='+str(Path.home())+'/.local/share/flatpak/exports/bin/sh.oskar.yin-yang --systemd', line))
service.write(
re.sub(
'ExecStart=\/usr\/bin\/yin-yang --systemd',
'ExecStart='
+ str(Path.home())
+ '/.local/share/flatpak/exports/bin/sh.oskar.yin-yang --systemd',
line,
)
)


def run_command(command, **kwargs):
Expand Down Expand Up @@ -70,7 +76,9 @@ class _UpdateTimerStatus(Enum):
STOP = auto()

def __init__(self):
self._next_timer_update: SaveWatcher._UpdateTimerStatus = SaveWatcher._UpdateTimerStatus.NO_UPDATE
self._next_timer_update: SaveWatcher._UpdateTimerStatus = (
SaveWatcher._UpdateTimerStatus.NO_UPDATE
)

def _set_needed_updates(self, change_values):
assert change_values['old_value'] != change_values['new_value'], 'No change!'
Expand All @@ -83,15 +91,20 @@ def _set_needed_updates(self, change_values):
elif change_values['new_value'] == Modes.MANUAL.value:
self._next_timer_update = SaveWatcher._UpdateTimerStatus.STOP
else:
self._next_timer_update = SaveWatcher._UpdateTimerStatus.UPDATE_TIMES
self._next_timer_update = (
SaveWatcher._UpdateTimerStatus.UPDATE_TIMES
)
case 'times' | 'coordinates' | 'boot_offset':
self._next_timer_update = SaveWatcher._UpdateTimerStatus.UPDATE_TIMES

def _update_timer(self):
match self._next_timer_update:
case SaveWatcher._UpdateTimerStatus.STOP:
run_command('stop')
case SaveWatcher._UpdateTimerStatus.UPDATE_TIMES | SaveWatcher._UpdateTimerStatus.START:
case (
SaveWatcher._UpdateTimerStatus.UPDATE_TIMES
| SaveWatcher._UpdateTimerStatus.START
):
update_times()

self._next_timer_update = SaveWatcher._UpdateTimerStatus.NO_UPDATE
Expand Down

0 comments on commit 4757a61

Please sign in to comment.