Skip to content

Commit

Permalink
Remove _runtime hack
Browse files Browse the repository at this point in the history
The ModuleManager directory is now passed in as a separate parameter
  • Loading branch information
JakobGM committed May 2, 2018
1 parent 091f9a5 commit 797f452
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 343 deletions.
3 changes: 2 additions & 1 deletion astrality/astrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def exit_handler(signal=None, frame=None) -> None:
signal.signal(signal.SIGTERM, exit_handler)

try:
config, modules, global_context = user_configuration()
config, modules, global_context, directory = user_configuration()

# Delay further actions if configuration says so
time.sleep(config['config/astrality']['startup_delay'])
Expand All @@ -84,6 +84,7 @@ def exit_handler(signal=None, frame=None) -> None:
config=config,
modules=modules,
context=global_context,
directory=directory,
)
module_manager.finish_tasks()

Expand Down
37 changes: 2 additions & 35 deletions astrality/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,40 +144,15 @@ def dict_from_config_file(
}


def infer_runtime_variables_from_config(
config_directory: Path,
config_file: Path,
config: ApplicationConfig,
) -> Dict[str, Dict[str, Path]]:
"""Return infered runtime variables based on config file."""
temp_directory = Path(os.environ.get('TMPDIR', '/tmp')) / 'astrality'
if not temp_directory.is_dir():
os.mkdir(temp_directory)

return {
'_runtime': {
'config_directory': config_directory,
'config_file': config_file,
'temp_directory': temp_directory,
},
}


def user_configuration(
config_directory: Optional[Path] = None,
) -> Tuple[ApplicationConfig, Dict, Context]:
) -> Tuple[ApplicationConfig, Dict, Context, Path]:
"""
Return dictionary containing the users configuration.
Configuration is read from astrality.yml, modules.yml and context.yml,
and merged into a single dictionary, where the keys are prepended with
'config/', 'module/', and 'context/' respectively.
In addition, the section config['_runtime'] is inserted, which contains
several items specifying runtime specific values. Example keys are:
- config_directory
- config_file
- temp_directory
"""
config_directory, config_file = infer_config_location(config_directory)

Expand Down Expand Up @@ -207,21 +182,13 @@ def user_configuration(
prepend='module/',
)

# Some metadata used by other classes, stored in '_runtime' key
runtime = infer_runtime_variables_from_config(
config_directory,
config_file,
config,
)
config.update(runtime)

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

return config, modules_config, global_context
return config, modules_config, global_context, config_directory


def create_config_directory(path: Optional[Path] = None, empty=False) -> Path:
Expand Down
18 changes: 13 additions & 5 deletions astrality/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,19 +358,21 @@ class ModuleManager:
"""
A manager for operating on a set of modules.
:param config: Modules and global configuration options.
:param config: Global configuration options.
:param modules: Dictionary containing globally defined modules.
:param context: Global context.
:param directory: Directory containing global configuration.
"""

def __init__(
self,
config: ApplicationConfig,
config: ApplicationConfig = {},
modules: Dict[str, ModuleConfigDict] = {},
context: Context = Context(),
directory: Path = Path(__file__).parent / 'tests' / 'test_config',
) -> None:
"""Initialize a ModuleManager object from `astrality.yml` dict."""
self.config_directory = Path(config['_runtime']['config_directory'])
self.temp_directory = Path(config['_runtime']['temp_directory'])
self.config_directory = directory
self.application_config = config
self.application_context = context

Expand Down Expand Up @@ -645,7 +647,12 @@ def on_application_config_modified(self):
return

# Hot reloading is enabled, get the new configuration dict
new_application_config, new_modules, new_context = user_configuration(
(
new_application_config,
new_modules,
new_context,
directory,
) = user_configuration(
config_directory=self.config_directory,
)

Expand All @@ -655,6 +662,7 @@ def on_application_config_modified(self):
config=new_application_config,
modules=new_modules,
context=new_context,
directory=directory,
)

# Run all old exit actions, since the new config is valid
Expand Down
8 changes: 0 additions & 8 deletions astrality/tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ def test_environment_variable_interpolation(self, dummy_config):
== 'test_value, hello'


def test_config_directory_name(conf):
assert str(conf['_runtime']['config_directory'])[-7:] == '/config'


def test_name_of_config_file(conf):
assert '/astrality.yml' in str(conf['_runtime']['config_file'])


def test_generation_of_expanded_env_dict():
env_dict = generate_expanded_env_dict()
assert len(env_dict) == len(os.environ)
Expand Down
41 changes: 4 additions & 37 deletions astrality/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Application wide fixtures."""
import copy
import os
import shutil
from pathlib import Path

import pytest
Expand Down Expand Up @@ -44,6 +43,7 @@ def context():
conf_path = Path(this_test_file).parents[1] / 'config'
return user_configuration(conf_path)[2]


@pytest.fixture(scope='session', autouse=True)
def modules():
"""Return the modules object for the example configuration."""
Expand All @@ -58,37 +58,16 @@ def expanded_env_dict():
return generate_expanded_env_dict()


@pytest.fixture
def default_global_options():
"""Return dictionary containing all default global options."""
return copy.deepcopy(ASTRALITY_DEFAULT_GLOBAL_SETTINGS)


@pytest.fixture
def _runtime(temp_directory, test_config_directory):
return {'_runtime': {
'config_directory': test_config_directory,
'temp_directory': temp_directory,
}}


@pytest.fixture
def test_config_directory():
"""Return path to test config directory."""
return Path(__file__).parent / 'test_config'


@pytest.yield_fixture
def temp_directory():
def temp_directory(tmpdir):
"""Return path to temporary directory, and cleanup afterwards."""
temp_dir = Path('/tmp/astrality')
if not temp_dir.is_dir():
os.makedirs(temp_dir)

yield temp_dir

# Cleanup temp dir after test has been run
shutil.rmtree(temp_dir, ignore_errors=True)
return Path(tmpdir)


@pytest.fixture
Expand Down Expand Up @@ -140,17 +119,14 @@ def _module_factory(


@pytest.fixture
def module_manager_factory(default_global_options, _runtime):
def module_manager_factory():
"""Return ModuleManager factory for testing."""
default_global_options.update(_runtime)

def _module_manager_factory(
*modules,
context=Context(),
) -> ModuleManager:
"""Return ModuleManager object with given modules and context."""
module_manager = ModuleManager(
config=default_global_options,
context=context,
)
module_manager.modules = {
Expand All @@ -171,15 +147,6 @@ def _module_manager_factory(
return _module_manager_factory


@pytest.fixture
def module_manager(
default_global_options,
_runtime,
):
default_global_options.update(_runtime)
return ModuleManager(default_global_options)


@pytest.fixture
def create_temp_files(tmpdir):
"""Return temp file factory function."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from pathlib import Path

from astrality.module import ModuleManager


def test_copying_in_on_modified_block(
action_block_factory,
create_temp_files,
module_factory,
module_manager,
):
"""Module should copy properly."""
file1, file2, file3, file4 = create_temp_files(4)
Expand All @@ -21,6 +22,8 @@ def test_copying_in_on_modified_block(
],
)
module = module_factory(on_modified=action_block, path=Path('/a/b/c'))

module_manager = ModuleManager()
module_manager.modules = {'test': module}
module_manager.on_modified(Path('/a/b/c'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from pathlib import Path

from astrality.module import ModuleManager


def test_stowing(
action_block_factory,
create_temp_files,
module_factory,
module_manager,
):
"""ModuleManager should stow properly."""
template, target = create_temp_files(2)
Expand All @@ -26,6 +27,8 @@ def test_stowing(
module = module_factory(
on_exit=action_block,
)

module_manager = ModuleManager()
module_manager.modules = {'test': module}
module_manager.exit()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from pathlib import Path

from astrality.module import ModuleManager


def test_symlinking_in_on_startup_block(
action_block_factory,
module_factory,
module_manager,
create_temp_files,
):
"""ModuleManager should symlink properly."""
Expand All @@ -20,6 +21,8 @@ def test_symlinking_in_on_startup_block(
],
)
module = module_factory(on_startup=action_block)

module_manager = ModuleManager()
module_manager.modules = {'test': module}
module_manager.finish_tasks()

Expand Down

0 comments on commit 797f452

Please sign in to comment.