Skip to content

Commit

Permalink
Make compilations explicit on trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 8, 2018
1 parent c7f9f24 commit 662a1e5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
6 changes: 6 additions & 0 deletions astrality/config/astrality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module/wallpaper:
- from_file: wallpaper_themes/default/colors.yaml
from_section: '{period}'
to_section: colors
compile:
- conky.time
- conky.performance

on_period_change:
run:
Expand All @@ -39,6 +42,9 @@ module/wallpaper:
- from_file: wallpaper_themes/default/colors.yaml
from_section: '{period}'
to_section: colors
compile:
- conky.time
- conky.performance

on_exit:
run:
Expand Down
30 changes: 23 additions & 7 deletions astrality/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Iterable, List, Optional, Tuple
from typing import Any, Dict, List, Tuple

from astrality import compiler
from astrality.compiler import context
Expand Down Expand Up @@ -318,12 +318,12 @@ def finish_tasks(self) -> None:
"""
if not self.startup_done:
self.import_context_sections('on_startup')
self.compile_templates()
self.compile_templates('on_startup')
self.startup()
self.period_change()
elif self.last_module_periods != self.module_periods():
self.import_context_sections('on_period_change')
self.compile_templates()
self.compile_templates('on_period_change')
self.period_change()

def has_unfinished_tasks(self) -> bool:
Expand Down Expand Up @@ -362,15 +362,31 @@ def import_context_sections(self, trigger: str) -> None:
from_config_file=csi.from_config_file,
)

def compile_templates(self) -> None:
def compile_templates(self, trigger: str) -> None:
"""
Compile the module templates specified by the `templates` option.
Trigger is one of 'on_startup', 'on_period_change', or 'on_exit'.
This determines which section of the module is used to get the compile
specification from.
"""
assert trigger in ('on_startup', 'on_period_change', 'on_exit',)

for module in self.modules.values():
for files in module.templates.values():
for compilation in module.module_config.get(trigger, {}).get('compile', []):

# What to compile is given by [module.]template_name
*_module, template_name = compilation.split('.')
if len(_module) == 1:
# Explicit module has been specified
module_name = _module[0]
else:
# No module has been specified, use the module itself
module_name = module.name

compiler.compile_template(
template=files['source'],
target=files['target'],
template=self.modules[module_name].templates[template_name]['source'],
target=self.modules[module_name].templates[template_name]['target'],
context=self.application_context,
)

Expand Down
3 changes: 3 additions & 0 deletions astrality/tests/templates/no_context.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
one
two
three
45 changes: 43 additions & 2 deletions astrality/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def valid_module_section():
'target': '/tmp/compiled_result',
}
},
'on_startup': {'run': ['echo {period}']},
'on_startup': {
'run': ['echo {period}'],
'compile': ['template_name'],
},
'on_period_change': {'run': ['echo {template_name}']},
'on_exit': {'run': ['echo exit']},
}
Expand Down Expand Up @@ -444,7 +447,7 @@ def test_compilation_of_template(
simple_application_config['module/test_module']['timer']['type'] = 'solar'
compiled_template_content = 'some text\n' + os.environ['USER'] + '\nFuraMono Nerd Font'
module_manager = ModuleManager(simple_application_config)
module_manager.compile_templates()
module_manager.compile_templates('on_startup')

template_file = str(module.templates['template_name']['source'])
compiled_template = str(module.templates['template_name']['target'])
Expand Down Expand Up @@ -570,6 +573,44 @@ def test_import_sections_on_period_change(config_with_modules, freezer):
'week': Resolver({'day': 'monday'}),
}

def test_compiling_templates_on_cross_of_module_boundries():
module_A = {
'templates': {
'template_A': {
'source': '../tests/templates/no_context.template',
},
},
}
modules_config = {
'module/A': module_A,
'_runtime': {
'config_directory': Path(__file__).parent,
'temp_directory': Path('/tmp'),
},
}

module_manager = ModuleManager(modules_config)
module_manager.finish_tasks()

# Modules should not compile their templates unless they explicitly
# define a compile string in a on_* block.
with open(module_manager.modules['A'].templates['template_A']['target']) as compilation:
assert compilation.read() == ''

# We now insert another module, B, which compiles the template of the
# previous module, A
module_B = {
'on_startup': {
'compile': ['A.template_A'],
},
}
modules_config['module/B'] = module_B
module_manager = ModuleManager(modules_config)
module_manager.finish_tasks()
with open(module_manager.modules['A'].templates['template_A']['target']) as compilation:
assert compilation.read() == 'one\ntwo\nthree'


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'] = [{
Expand Down

0 comments on commit 662a1e5

Please sign in to comment.