Move resolve_environment within __init__

resolve_environment is specific to ServiceLoader, the function
does not need to be on the global scope, it is a part of the
ServiceLoader object. The environment needs to be resolved
before we can make any service dicts, it belongs in the
constructor.

This is cleaning up the design a little and being clearer
about intent and scope of functions.

Signed-off-by: Mazz Mosley <mazz@houseofmnowster.com>
This commit is contained in:
Mazz Mosley 2015-08-24 12:23:45 +01:00
parent 8a6061bfb9
commit 02c52ae673
1 changed files with 22 additions and 23 deletions

View File

@ -169,17 +169,36 @@ class ServiceLoader(object):
self.service_dict = service_dict.copy()
self.service_dict['name'] = service_name
self.resolve_environment()
def detect_cycle(self, name):
if self.signature(name) in self.already_seen:
raise CircularReference(self.already_seen + [self.signature(name)])
def make_service_dict(self):
# service_dict = service_dict.copy()
# service_dict['name'] = name
self.service_dict = resolve_environment(self.service_dict, working_dir=self.working_dir)
self.service_dict = self.resolve_extends(self.service_dict)
return process_container_options(self.service_dict, working_dir=self.working_dir)
def resolve_environment(self):
"""
Unpack any environment variables from an env_file, if set.
Interpolate environment values if set.
"""
if 'environment' not in self.service_dict and 'env_file' not in self.service_dict:
return
env = {}
if 'env_file' in self.service_dict:
for f in get_env_files(self.service_dict, working_dir=self.working_dir):
env.update(env_vars_from_file(f))
del self.service_dict['env_file']
env.update(parse_environment(self.service_dict.get('environment')))
env = dict(resolve_env_var(k, v) for k, v in six.iteritems(env))
self.service_dict['environment'] = env
def resolve_extends(self, service_dict):
if 'extends' not in service_dict:
return service_dict
@ -334,26 +353,6 @@ def get_env_files(options, working_dir=None):
return [expand_path(working_dir, path) for path in env_files]
def resolve_environment(service_dict, working_dir=None):
service_dict = service_dict.copy()
if 'environment' not in service_dict and 'env_file' not in service_dict:
return service_dict
env = {}
if 'env_file' in service_dict:
for f in get_env_files(service_dict, working_dir=working_dir):
env.update(env_vars_from_file(f))
del service_dict['env_file']
env.update(parse_environment(service_dict.get('environment')))
env = dict(resolve_env_var(k, v) for k, v in six.iteritems(env))
service_dict['environment'] = env
return service_dict
def parse_environment(environment):
if not environment:
return {}