Skip to content

Commit

Permalink
Import context in on_modified blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 20, 2018
1 parent a585098 commit cea0373
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
31 changes: 27 additions & 4 deletions astrality/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,27 @@ def modified_commands(self, specified_path: str) -> Tuple[str, ...]:
def context_section_imports(
self,
trigger: str,
modified: Optional[str] = None,
) -> Tuple[ContextSectionImport, ...]:
"""
Return what to import into the global application_context.
Trigger is one of 'on_startup', 'on_event', or 'on_exit'.
Trigger is one of 'on_startup', 'on_event', 'on_modified, or 'on_exit'.
This determines which section of the module is used to get the context
import specification from.
If trigger is 'on_modified', you also need to specify which file is
modified, in order to get the correct section.
"""
assert trigger in ('on_startup', 'on_event', 'on_exit',)
import_config: List[Dict[str, str]]
if modified:
assert trigger == 'on_modified'
import_config = self.module_config[trigger][modified]['import_context']
else:
assert trigger in ('on_startup', 'on_event', 'on_exit',)
import_config = self.module_config[trigger]['import_context']

context_section_imports = []
import_config = self.module_config[trigger]['import_context']

for context_import in import_config:
# Insert placeholders
from_path = self.interpolate_string(context_import['from_path'])
Expand Down Expand Up @@ -643,13 +651,28 @@ def on_modified(self, modified: Path) -> None:
module = watched_file.module
specified_path = watched_file.specified_path

# First import context sections in on_modified block
for csi in module.context_section_imports(
trigger='on_modified',
modified=specified_path,
):
self.application_context = insert_into(
context=self.application_context,
section=csi.into_section,
from_section=csi.from_section,
from_config_file=self.expand_path(csi.from_config_file),
)

# Now compile templates specified in on_modified block
for compilation in module.module_config['on_modified'][specified_path]['compile']:
template = self.templates[compilation['template']]
self.compile_template(
source=template.source,
target=template.target,
)


# Lastly, run commands specified in on_modified block
modified_commands = module.modified_commands(specified_path)
for command in modified_commands:
logger.info(f'[module/{module.name}] Running modified command.')
Expand Down
40 changes: 40 additions & 0 deletions astrality/tests/module/test_module_filewatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,43 @@ def test_recompile_templates_when_modified_overridden(
assert touch_target.is_file()

module_manager.exit()


def test_importing_context_on_modification(
three_watchable_files,
default_global_options,
_runtime,
test_config_directory,
):
"""Test that context values are imported in on_modified blocks."""
file1, *_ = three_watchable_files
mercedes_context = test_config_directory / 'context' / 'mercedes.yaml'

application_config = {
'module/module_name': {
'on_modified': {
str(file1): {
'import_context': {
'from_path': str(mercedes_context),
},
},
},
},
'context/car': {
'manufacturer': 'Tesla',
},
}
application_config.update(default_global_options)
application_config.update(_runtime)

module_manager = ModuleManager(application_config)
module_manager.finish_tasks()

# Sanity check before modifying file1
assert module_manager.application_context['car']['manufacturer'] == 'Tesla'

# After modifying file1, Mercedes should have been imported
file1.touch()
file1.write_text('new content, resulting in importing Mercedes')
time.sleep(0.7)
assert module_manager.application_context['car']['manufacturer'] == 'Mercedes'

0 comments on commit cea0373

Please sign in to comment.