Skip to content

Commit

Permalink
Allow context imports on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 7, 2018
1 parent d10f586 commit c7f9f24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
6 changes: 6 additions & 0 deletions astrality/config/astrality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module/wallpaper:
# periodic wallpapers.
# force_period: morning

on_startup:
import_context:
- from_file: wallpaper_themes/default/colors.yaml
from_section: '{period}'
to_section: colors

on_period_change:
run:
- feh --bg-fill wallpaper_themes/default/{period}.*
Expand Down
35 changes: 27 additions & 8 deletions astrality/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,22 @@ def exit_commands(self) -> Tuple[str, ...]:
in exit_commands
)

def context_section_imports(self) -> Tuple[ContextSectionImport, ...]:
"""Return what to import into the global application_context."""
def context_section_imports(
self,
trigger: str,
) -> Tuple[ContextSectionImport, ...]:
"""
Return what to import into the global application_context.
Trigger is one of 'on_startup', 'on_period_change', or 'on_exit'.
This determines which section of the module is used to get the context
import specification from.
"""
assert trigger in ('on_startup', 'on_period_change', 'on_startup',)

context_section_imports = []
import_config = self.module_config.get(
'on_period_change',
trigger,
{},
).get('import_context', [])

Expand Down Expand Up @@ -306,12 +317,12 @@ def finish_tasks(self) -> None:
module periods combination.
"""
if not self.startup_done:
self.import_context_sections()
self.import_context_sections('on_startup')
self.compile_templates()
self.startup()
self.period_change()
elif self.last_module_periods != self.module_periods():
self.import_context_sections()
self.import_context_sections('on_period_change')
self.compile_templates()
self.period_change()

Expand All @@ -330,10 +341,18 @@ def time_until_next_period(self) -> timedelta:
in self.modules.values()
)

def import_context_sections(self):
"""Import context sections defined by the managed modules."""
def import_context_sections(self, trigger: str) -> None:
"""
Import context sections defined by the managed modules.
Trigger is one of 'on_startup', 'on_period_change', or 'on_exit'.
This determines which section of the module is used to get the context
import specification from.
"""
assert trigger in ('on_startup', 'on_period_change', 'on_exit',)

for module in self.modules.values():
context_section_imports = module.context_section_imports()
context_section_imports = module.context_section_imports(trigger)

for csi in context_section_imports:
self.application_context = insert_into(
Expand Down
60 changes: 58 additions & 2 deletions astrality/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,20 +543,76 @@ def test_import_sections_on_period_change(config_with_modules, freezer):
'from_file': 'astrality/tests/templates/weekday.yaml',
'from_section': '{period}',
}]
config_with_modules.pop('module/solar_module')
module_manager = ModuleManager(config_with_modules)

assert 'env' in module_manager.application_context
assert module_manager.application_context['fonts'] == {1: 'FuraCode Nerd Font'}

sunday = datetime(year=2018, month=2, day=4)
freezer.move_to(sunday)
module_manager.finish_tasks()

# Make application_context comparisons easier
module_manager.application_context.pop('env')

# Startup does not count as a period change, so no context has been imported
assert module_manager.application_context == {
'fonts': Resolver({1: 'FuraCode Nerd Font'}),
}

monday = datetime(year=2018, month=2, day=5)
freezer.move_to(monday)
module_manager.finish_tasks()
module_manager.application_context.pop('env')

# The period has now changed, so context should be imported
assert module_manager.application_context == {
'fonts': Resolver({1: 'FuraCode Nerd Font'}),
'week': Resolver({'day': 'monday'}),
}

def test_import_sections_on_startup(config_with_modules, freezer):
# Insert day the module was started into 'start day'
config_with_modules['module/weekday_module']['on_startup']['import_context'] = [{
'to_section': 'start_day',
'from_file': 'astrality/tests/templates/weekday.yaml',
'from_section': '{period}',
}]

# Insert the current day into 'day_now'
config_with_modules['module/weekday_module']['on_period_change']['import_context'] = [{
'to_section': 'day_now',
'from_file': 'astrality/tests/templates/weekday.yaml',
'from_section': '{period}',
}]
config_with_modules.pop('module/solar_module')
module_manager = ModuleManager(config_with_modules)

# Remove 'env' context for easier comparisons
module_manager.application_context.pop('env')

# Before finishing tasks, no context sections are imported
assert module_manager.application_context['fonts'] == {1: 'FuraCode Nerd Font'}

# Start module on a monday
sunday = datetime(year=2018, month=2, day=4)
freezer.move_to(sunday)
module_manager.finish_tasks()
assert module_manager.application_context == {
'fonts': Resolver({1: 'FuraCode Nerd Font'}),
'start_day': Resolver({'day': 'sunday'}),
}

# 'now_day' should now be added, but 'start_day' should remain unchanged
monday = datetime(year=2018, month=2, day=5)
freezer.move_to(monday)
module_manager.finish_tasks()
assert module_manager.application_context == {
'fonts': Resolver({1: 'FuraCode Nerd Font'}),
'start_day': Resolver({'day': 'sunday'}),
'day_now': Resolver({'day': 'monday'}),
}



class TestModuleManager:
def test_invocation_of_module_manager_with_config(self, conf):
Expand Down

0 comments on commit c7f9f24

Please sign in to comment.