Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ale Path Expansion Fixes #551

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions ale/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ def find_latest_metakernel(path, year):


def dict_merge(dct, merge_dct):
new_dct = dct.copy()
for k, v in merge_dct.items():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], Mapping)):
dict_merge(dct[k], merge_dct[k])
new_dct[k] = dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
new_dct[k] = merge_dct[k]

return dct
return new_dct


def get_isis_preferences(isis_preferences=None):
Expand Down Expand Up @@ -178,15 +179,23 @@ def dict_to_lower(d):


def expandvars(path, env_dict=os.environ, default=None, case_sensitive=True):
user_dict = env_dict if case_sensitive else dict_to_lower(env_dict)
if env_dict != os.environ:
env_dict = dict_merge(env_dict, os.environ)

def replace_var(m):
group0 = m.group(0) if case_sensitive else m.group(0).lower()
group1 = m.group(1) if case_sensitive else m.group(1).lower()
while "$" in path:
user_dict = env_dict if case_sensitive else dict_to_lower(env_dict)

return user_dict.get(m.group(2) or group1, group0 if default is None else default)
reVar = r'\$(\w+|\{([^}]*)\})'
return re.sub(reVar, replace_var, path)
def replace_var(m):
group1 = m.group(1) if case_sensitive else m.group(1).lower()
val = user_dict.get(m.group(2) or group1 if default is None else default)
if not val:
raise KeyError(f"Failed to evaluate {m.group(0)} from env_dict. " +
f"Should {m.group(0)} be an environment variable?")

return val
reVar = r'\$(\w+|\{([^}]*)\})'
path = re.sub(reVar, replace_var, path)
return path


def generate_kernels_from_cube(cube, expand=False, format_as='list'):
Expand Down Expand Up @@ -281,7 +290,7 @@ def load_table_data(key):
if not "DataDirectory" in isisprefs:
warnings.warn("No IsisPreferences file found, is your ISISROOT env var set?")

kernels = [expandvars(expandvars(k, isisprefs['DataDirectory'], case_sensitive=False)) for k in kernels]
kernels = [expandvars(k, isisprefs['DataDirectory'], case_sensitive=False) for k in kernels]
# Ensure that the ISIS Addendum kernel is last in case it overrides
# some values from the default Instrument kernel
# Sorts planetary constants kernel first so it can be overridden by more specific kernels
Expand All @@ -295,7 +304,7 @@ def load_table_data(key):
for kern_list in mk_paths:
for index, kern in enumerate(mk_paths[kern_list]):
if kern is not None:
mk_paths[kern_list][index] = expandvars(expandvars(kern, isisprefs['DataDirectory'], case_sensitive=False))
mk_paths[kern_list][index] = expandvars(kern, isisprefs['DataDirectory'], case_sensitive=False)
return mk_paths
else:
raise Exception(f'{format_as} is not a valid return format')
Expand Down
Loading