Skip to content

Commit

Permalink
Merge pull request #136 from oskarsh/beta
Browse files Browse the repository at this point in the history
New plugins
  • Loading branch information
l0drex authored Nov 2, 2022
2 parents 25be68d + 69a30de commit f33fc6a
Show file tree
Hide file tree
Showing 25 changed files with 550 additions and 170 deletions.
2 changes: 1 addition & 1 deletion communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import datetime, time as dt_time
from pathlib import Path

from src.enums import PluginKey
from src.meta import PluginKey
from src.config import config

logging.basicConfig(filename=str(Path.home()) + '/.local/share/yin_yang.log', level=logging.DEBUG,
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from systemd import journal

from src import daemon_handler
from src.enums import ConfigEvent
from src.meta import ConfigEvent
from src import yin_yang
from src.config import config, Modes
from src.ui import main_window_connector
Expand Down
2 changes: 1 addition & 1 deletion resources_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
\x00\x00\x00\x10\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00.\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x83\x83\xfb0\x07\
\x00\x00\x01\x83\x83\xfd\x9cP\
\x00\x00\x00P\x00\x01\x00\x00\x00\x01\x00\x00\x07\xe6\
\x00\x00\x01\x83o\xd22\x9b\
"
Expand Down
57 changes: 37 additions & 20 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from suntime import Sun, SunTimeException
from src.plugins import get_plugins
from src.enums import Modes, Desktop, PluginKey, ConfigEvent
from src.meta import Modes, Desktop, PluginKey, ConfigEvent

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -150,6 +150,8 @@ def get_desktop() -> Desktop:
return Desktop.GNOME
case 'kde' | 'plasma' | 'plasma5':
return Desktop.KDE
case 'xfce':
return Desktop.XFCE
case _:
return Desktop.UNKNOWN

Expand Down Expand Up @@ -283,34 +285,49 @@ def get_plugin_key(self, plugin: str, key: PluginKey) -> Union[bool, str]:

return self['plugins'][plugin][key]

def update_plugin_key(self, plugin: str, key: PluginKey, value: Union[bool, str]) -> Union[bool, str]:
def update_plugin_key(self, plugin_name: str, key: PluginKey, value: Union[bool, str]) -> Union[bool, str]:
"""Update the value of a key in configuration
:param key: The setting to change
:param value: The value to set the setting to
:param plugin: Name of the plugin you may want to change
:param plugin_name: Name of the plugin you may want to change
:returns: new value
"""

plugin = plugin.casefold()
key_str = key.value.casefold()
plugin_name_lower = plugin_name.casefold()
key_value = key.value.casefold()

try:
old_value = self['plugins'][plugin][key_str]
if value != old_value:
self['plugins'][plugin][key_str] = value
self._changed = True
if ConfigEvent.CHANGE in self._listeners:
for listener in self._listeners[ConfigEvent.CHANGE]:
listener.notify(ConfigEvent.CHANGE, {
'key': key_str,
'old_value': old_value,
'new_value': value,
'plugin': plugin
})
return self.get_plugin_key(plugin, key)
old_value = self['plugins'][plugin_name_lower][key_value]
if value == old_value:
return value

self['plugins'][plugin_name_lower][key_value] = value
self._changed = True
if ConfigEvent.CHANGE in self._listeners:
for listener in self._listeners[ConfigEvent.CHANGE]:
listener.notify(ConfigEvent.CHANGE, {
'key': key_value,
'old_value': old_value,
'new_value': value,
'plugin': plugin_name_lower
})

# change value in the plugin
plugin = next(p for p in plugins if p.name.casefold() == plugin_name_lower)
match key:
case PluginKey.ENABLED:
plugin.enabled = value
case PluginKey.THEME_LIGHT:
plugin.theme_light = value
case PluginKey.THEME_DARK:
plugin.theme_dark = value
case _:
raise KeyError

return self.get_plugin_key(plugin_name_lower, key)
except KeyError as e:
logger.error(f'Error while updating {plugin}.{key_str}')
logger.error(f'Error while updating {plugin_name_lower}.{key_value}')
raise e

@property
Expand All @@ -319,7 +336,7 @@ def defaults(self) -> dict:

# NOTE: if you change or add new values here, make sure to update the version number and update_config() method
conf_default = {
'version': 3.1,
'version': 3.2,
'running': False,
'dark_mode': False,
'mode': Modes.MANUAL.value,
Expand Down
24 changes: 13 additions & 11 deletions src/daemon_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from enum import Enum, auto
from pathlib import Path

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

logger = logging.getLogger(__name__)
TIMER_PATH = str(Path.home()) + '/.local/share/systemd/user/yin_yang.timer'
Expand All @@ -14,8 +14,8 @@ def run_command(command, **kwargs):
return subprocess.run(['systemctl', '--user', command, 'yin_yang.timer'], **kwargs)


def update_times(saved_config):
if saved_config['mode'] == Modes.MANUAL.value:
def update_times():
if config.mode == Modes.MANUAL:
run_command('stop')
logger.debug('Stopping systemd timer')
return
Expand All @@ -25,7 +25,7 @@ def update_times(saved_config):
with open(TIMER_PATH, 'r') as file:
lines = file.readlines()

time_light, time_dark = saved_config['times']
time_light, time_dark = config.times
lines[4] = f'OnCalendar={time_light}\n'
lines[5] = f'OnCalendar={time_dark}\n'

Expand Down Expand Up @@ -56,24 +56,26 @@ def _set_needed_updates(self, change_values):
self._next_timer_update = SaveWatcher._UpdateTimerStatus.START
elif change_values['new_value'] == Modes.MANUAL.value:
self._next_timer_update = SaveWatcher._UpdateTimerStatus.STOP
else:
self._next_timer_update = SaveWatcher._UpdateTimerStatus.UPDATE_TIMES
case 'times' | 'coordinates':
self._next_timer_update = SaveWatcher._UpdateTimerStatus.UPDATE_TIMES

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

self._next_timer_update = SaveWatcher._UpdateTimerStatus.NO_UPDATE

def notify(self, event: ConfigEvent, values):
match event.value:
case ConfigEvent.CHANGE.value:
match event:
case ConfigEvent.CHANGE:
self._set_needed_updates(values)
case ConfigEvent.SAVE.value:
self._update_timer(values)
case ConfigEvent.SAVE:
self._update_timer()


watcher = SaveWatcher()
5 changes: 5 additions & 0 deletions src/enums.py → src/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Modes(Enum):
class Desktop(Enum):
KDE = 'kde'
GNOME = 'gnome'
XFCE = 'xfce'
UNKNOWN = 'unknown'


Expand All @@ -24,3 +25,7 @@ class PluginKey(Enum):
class ConfigEvent(Enum):
CHANGE = auto()
SAVE = auto()


class UnsupportedDesktopError(NotImplementedError):
pass
13 changes: 11 additions & 2 deletions src/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from src.enums import Desktop
from src.plugins import system, gtk, kvantum, wallpaper, firefox, vscode, atom, sound, notify
from src.meta import Desktop
from src.plugins import system, colors, gtk, kvantum, wallpaper, custom
from src.plugins import firefox, brave, gedit, only_office
from src.plugins import vscode, atom, konsole
from src.plugins import sound, notify

# NOTE initialize your plugin over here:
# The order in the list specifies the order in the config gui
Expand All @@ -9,12 +12,18 @@
def get_plugins(desktop: Desktop) -> [Plugin]:
return [
system.System(desktop),
colors.Colors(desktop),
gtk.Gtk(desktop),
kvantum.Kvantum(),
wallpaper.Wallpaper(desktop),
firefox.Firefox(),
brave.Brave(),
vscode.Vscode(),
atom.Atom(),
gedit.Gedit(),
only_office.OnlyOffice(),
konsole.Konsole(),
custom.Custom(),
sound.Sound(),
notify.Notification()
]
Expand Down
Loading

0 comments on commit f33fc6a

Please sign in to comment.