Skip to content

Commit

Permalink
Add support for externally defined modules
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 23, 2018
1 parent a9abdb3 commit b450705
Show file tree
Hide file tree
Showing 25 changed files with 504 additions and 188 deletions.
2 changes: 1 addition & 1 deletion astrality/astrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def exit_handler(signal=None, frame=None) -> None:
config = user_configuration()

# Delay further actions if configuration says so
time.sleep(config['settings/astrality']['startup_delay'])
time.sleep(config['config/astrality']['startup_delay'])

module_manager = ModuleManager(config)
module_manager.finish_tasks()
Expand Down
2 changes: 1 addition & 1 deletion astrality/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def context(config: ApplicationConfig) -> Context:
for section_name, section in config.items():
if not isinstance(section_name, str) or \
not len(section_name) > 8 or \
not section_name[:8] == 'context/':
not section_name[:8].lower() == 'context/':
continue
else:
category = section_name[8:]
Expand Down
33 changes: 21 additions & 12 deletions astrality/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@

ApplicationConfig = Dict[str, Dict[str, Any]]

ASTRALITY_DEFAULT_GLOBAL_SETTINGS = {'settings/astrality': {
ASTRALITY_DEFAULT_GLOBAL_SETTINGS = {'config/astrality': {
'hot_reload_config': False,
'recompile_modified_templates': False,
'startup_delay': 0,
'run_timeout': 0,
'requires_timeout': 1,
Expand Down Expand Up @@ -183,9 +182,9 @@ def user_configuration(config_directory: Optional[Path] = None) -> ApplicationCo
))

# Insert default global settings that are not specified
user_settings = config.get('settings/astrality', {})
config['settings/astrality'] = ASTRALITY_DEFAULT_GLOBAL_SETTINGS['settings/astrality'].copy()
config['settings/astrality'].update(user_settings)
user_settings = config.get('config/astrality', {})
config['config/astrality'] = ASTRALITY_DEFAULT_GLOBAL_SETTINGS['config/astrality'].copy()
config['config/astrality'].update(user_settings)

return config

Expand Down Expand Up @@ -419,14 +418,14 @@ def module_config_dict(self) -> ModuleConfig:
with_env=False,
)

# We rename each module to module/module_name[path_to_module_directory]
# We rename each module to module/{self.name}.module_name
# in order to prevent naming conflicts when using modules provided
# from a third party with the same name as another managed module.
# This way you can use a module named "conky" from two third parties,
# in addition to providing your own.
for section in tuple(modules_dict.keys()):
if len(section) > 7 and section[:7].lower() == 'module/':
non_conflicting_module_name = str(section) + f'[{self.directory}]'
non_conflicting_module_name = 'module/' + '.'.join((self.name, section[7:],))
module_section = modules_dict.pop(section)
modules_dict[non_conflicting_module_name] = module_section

Expand All @@ -450,13 +449,18 @@ def __eq__(self, other) -> bool:

class GlobalModulesConfigDict(TypedDict, total=False):
"""Dictionary defining configuration options for Modules."""
modules_directory_path: str
enabled: List[ExternalModuleDict]
recompile_modified_templates: bool
modules_directory: str
enabled_modules: List[ExternalModuleDict]


class GlobalModulesConfig:
"""User modules configuration."""

# If modified templates should be automatically recompiled when the user
# edits the template
recompile_modified_templates: bool

# The path to the modules directory, by default:
# $ASTRALITY_CONFIG_HOME/modules
modules_directory_path: Path
Expand All @@ -474,11 +478,16 @@ def __init__(
) -> None:
"""Initialize a GlobalModulesConfig object from a dictionary."""

self.recompile_modified_templates = config.get(
'recompile_modified_templates',
False,
)

# Determine the directory which contains external modules
if 'modules_directory_path' in config:
if 'modules_directory' in config:
# Custom modules folder
modules_path = expand_path(
path=Path(config['modules_directory_path']),
path=Path(config['modules_directory']),
config_directory=config_directory,
)
self.modules_directory_path = modules_path
Expand All @@ -488,7 +497,7 @@ def __init__(

# Initialize all the external module sources
self._external_module_sources: Dict[Path, ExternalModuleSource] = {}
for external_module_dict in config.get('enabled', []):
for external_module_dict in config.get('enabled_modules', []):
external_module_source = ExternalModuleSource(
config=external_module_dict,
modules_directory_path=self.modules_directory_path,
Expand Down
102 changes: 19 additions & 83 deletions astrality/config/astrality.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
settings/astrality:
config/astrality:
# If hot_reload_config is enabled, modifications to this file automatically
# runs:
# 1) exit actions from the old configuration
# 2) startup actions from the new configuration
# Requires restart if enabled
hot_reload_config: true

# Modified templates can be automatically recompiled
recompile_modified_templates: true

# You can delay astrality on startup. The delay is given in seconds.
startup_delay: 0

Expand All @@ -22,6 +19,22 @@ settings/astrality:
# in seconds.
requires_timeout: 1

config/modules:
# Modified templates can be automatically recompiled
recompile_modified_templates: true

# There are two possible ways to define modules. Either in this file, as
# shown further below, or in separate external module directories within the
# following specified directory, relatively interpreted as:
# $ASTRALITY_CONFIG_HOME/modules
modules_directory: modules

# You enable external modules by specifying the <name> of each external
# module. The result is that the configurations contained in the following
# files are merged into this file: <modules_directory>/<name>/modules.yaml
enabled:
- name: solar_desktop


context/host:
# Here we define some context values which often change between host
Expand All @@ -44,7 +57,7 @@ context/host:
interfaces:
wlan:
# You can also use command substitution in order to insert the
# standard output of a shell command into a configuration option.
# standard output of a shell command into a configuration option.
#
# This is also used by the polybar template to point the
# wireless-internet polybar module to the correct interface.
Expand Down Expand Up @@ -119,7 +132,7 @@ module/terminals:
# Kitty: https://github.com/kovidgoyal/kitty

enabled: false
requires:
requires:
- command -v alacritty
- command -v kitty

Expand Down Expand Up @@ -148,80 +161,3 @@ module/polybar:
modules/polybar/config.template:
compile:
- template: modules/polybar/config.template


module/solar_desktop:
# A module for changing your wallpaper based on the suns position in the
# sky, while changing the font color of two conky modules to fit the
# wallpaper's colors. This module demonstrates more advanced features.
#
# I reccomend you to read the docs at https://astrality.readthedocs.io
# if you want to understand something that seems unclear.
enabled: true

# A module can require shell commands to return successfully in order to
# enable the module. Here, we check if `feh` and `conky` are installed.
requires:
- command -v feh
- command -v conky

event_listener:
# The solar event listener provides the following events:
# sunrise, morning, afternoon, sunset, and night.
type: solar

# Since this is dependent on your exact location, you have to specify
# your GPS coordinates. They are can be easily obtained from:
# https://www.latlong.net/
latitude: 63.446827
longitude: 10.421906
elevation: 0

# You can force a event listener to always return the same event for
# debugging. For example, if you want to check if all your periodic
# color schemes fit your the respective periodic wallpapers, without
# waiting for sunset, etc..
# force_event: sunrise

on_startup:
# Execute all actions specified in `on_event` on Astrality startup,
# since the conky templates are compiled in that block, and we need
# those compiled templates in the run actions below.
trigger: on_event

# Start conky modules on startup.
# We do not have to restart conky when we recompile the templates,
# since conky automatically reloads when the -c source changes
# change.
#
# We surround the template paths with {} since we want to refer to the
# *compiled* templates, and not the template sources.
run:
- conky -c {modules/solar_desktop/time.template}
- conky -c {modules/solar_desktop/performance.template}

on_event:
# Import the context section named the same as the current event,
# e.g. `night`, into the global context section named `wallpaper_colors`,
# from the file: modules/solar_desktop/solar_colors.yaml.
import_context:
from_path: modules/solar_desktop/solar_colors.yaml
from_section: '{event}'
to_section: wallpaper_colors

# Compile the templates specified in the conky module, as their
# context values have changed due to the import_context action above.
compile:
- template: modules/solar_desktop/time.template
- template: modules/solar_desktop/performance.template

# Run shell command to change the desktop wallpaper named the same
# as the current event.
run: feh --bg-fill modules/solar_desktop/{event}.*

on_exit:
# Kill the conky processes on Astrality shutdown.
run: pkill -f conky


# vim:filetype=yaml
10 changes: 10 additions & 0 deletions astrality/config/modules/polybar/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

# Terminate already running bar instances
killall -q polybar

# Wait until the processes have been shut down
while pgrep -x polybar >/dev/null; do sleep 1; done

# Launch polybar
polybar burma &
72 changes: 72 additions & 0 deletions astrality/config/modules/solar_desktop/modules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module/solar_desktop:
# A module for changing your wallpaper based on the suns position in the
# sky, while changing the font color of two conky modules to fit the
# wallpaper's colors. This module demonstrates more advanced features.
#
# I reccomend you to read the docs at https://astrality.readthedocs.io
# if you want to understand something that seems unclear.
enabled: true

# A module can require shell commands to return successfully in order to
# enable the module. Here, we check if `feh` and `conky` are installed.
requires:
- command -v feh
- command -v conky

event_listener:
# The solar event listener provides the following events:
# sunrise, morning, afternoon, sunset, and night.
type: solar

# Since this is dependent on your exact location, you have to specify
# your GPS coordinates. They are can be easily obtained from:
# https://www.latlong.net/
latitude: 63.446827
longitude: 10.421906
elevation: 0

# You can force a event listener to always return the same event for
# debugging. For example, if you want to check if all your periodic
# color schemes fit your the respective periodic wallpapers, without
# waiting for sunset, etc..
# force_event: sunrise

on_startup:
# Execute all actions specified in `on_event` on Astrality startup,
# since the conky templates are compiled in that block, and we need
# those compiled templates in the run actions below.
trigger: on_event

# Start conky modules on startup.
# We do not have to restart conky when we recompile the templates,
# since conky automatically reloads when the -c source changes
# change.
#
# We surround the template paths with {} since we want to refer to the
# *compiled* templates, and not the template sources.
run:
- conky -c {time.template}
- conky -c {performance.template}

on_event:
# Import the context section named the same as the current event,
# e.g. `night`, into the global context section named `wallpaper_colors`,
# from the file: modules/solar_desktop/solar_colors.yaml.
import_context:
from_path: solar_colors.yaml
from_section: '{event}'
to_section: wallpaper_colors

# Compile the templates specified in the conky module, as their
# context values have changed due to the import_context action above.
compile:
- template: time.template
- template: performance.template

# Run shell command to change the desktop wallpaper named the same
# as the current event.
run: feh --bg-fill {event}.*

on_exit:
# Kill the conky processes on Astrality shutdown.
run: pkill -f conky

0 comments on commit b450705

Please sign in to comment.