-
Notifications
You must be signed in to change notification settings - Fork 121
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
Self-references and += operator #81
Merged
darthbear
merged 8 commits into
chimpler:master
from
gilles-duboscq:topic/self-reference
May 6, 2016
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0afa2a
Implement self referential substitutions
bde3f44
Add unittest which tests self-references over ConfigTree boundaries
b2a639c
Update README.md and CHANGELOG.md
d390422
Fix formatting issues
4250bf1
Implement += operator
f867c28
Implement += for strings and make += operator work over parse boundaries
c692045
Merge remote-tracking branch 'origin/master' into topic/self-reference
gilles-duboscq fa7ad25
Fix python 2.6 compatibility issue
gilles-duboscq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,14 +230,17 @@ def include_config(token): | |
(Keyword('url') | Keyword('file')) - Literal('(').suppress() - quoted_string - Literal(')').suppress()))) \ | ||
.setParseAction(include_config) | ||
|
||
root_dict_expr = Forward() | ||
dict_expr = Forward() | ||
list_expr = Forward() | ||
multi_value_expr = ZeroOrMore((Literal( | ||
'\\') - eol).suppress() | comment_eol | include_expr | substitution_expr | dict_expr | list_expr | value_expr) | ||
# for a dictionary : or = is optional | ||
# last zeroOrMore is because we can have t = {a:4} {b: 6} {c: 7} which is dictionary concatenation | ||
inside_dict_expr = ConfigTreeParser(ZeroOrMore(comment_eol | include_expr | assign_expr | eol_comma)) | ||
inside_root_dict_expr = ConfigTreeParser(ZeroOrMore(comment_eol | include_expr | assign_expr | eol_comma), root=True) | ||
dict_expr << Suppress('{') - inside_dict_expr - Suppress('}') | ||
root_dict_expr << Suppress('{') - inside_root_dict_expr - Suppress('}') | ||
list_entry = ConcatenatedValueParser(multi_value_expr) | ||
list_expr << Suppress('[') - ListParser(list_entry - ZeroOrMore(eol_comma - list_entry)) - Suppress(']') | ||
|
||
|
@@ -250,7 +253,7 @@ def include_config(token): | |
) | ||
|
||
# the file can be { ... } where {} can be omitted or [] | ||
config_expr = ZeroOrMore(comment_eol | eol) + (list_expr | dict_expr | inside_dict_expr) + ZeroOrMore( | ||
config_expr = ZeroOrMore(comment_eol | eol) + (list_expr | root_dict_expr | inside_root_dict_expr) + ZeroOrMore( | ||
comment_eol | eol_comma) | ||
config = config_expr.parseString(content, parseAll=True)[0] | ||
if resolve: | ||
|
@@ -290,41 +293,105 @@ def _resolve_variable(config, substitution): | |
col=col(substitution.loc, substitution.instring))) | ||
return True, value | ||
|
||
@staticmethod | ||
def _fixup_self_references(config): | ||
if isinstance(config, ConfigTree) and config.root: | ||
for key in config: # Traverse history of element | ||
history = config.history[key] | ||
previous_item = history[0] | ||
for current_item in history[1:]: | ||
for substitution in ConfigParser._find_substitutions(current_item): | ||
prop_path = ConfigTree.parse_key(substitution.variable) | ||
if len(prop_path) > 1 and config.get(substitution.variable, None) is not None: | ||
continue # If value is present in latest version, don't do anything | ||
if prop_path[0] == key: | ||
if isinstance(previous_item, ConfigValues): # We hit a dead end, we cannot evaluate | ||
raise ConfigSubstitutionException("Property {} cannot be substituted. Check for cycles.".format(substitution.variable)) | ||
value = previous_item if len(prop_path) == 1 else previous_item.get(".".join(prop_path[1:])) | ||
(_,_,current_item) = ConfigParser._do_substitute(substitution, value) | ||
previous_item = current_item | ||
|
||
if len(history) == 1: # special case, when self optional referencing without existing | ||
for substitution in ConfigParser._find_substitutions(previous_item): | ||
prop_path = ConfigTree.parse_key(substitution.variable) | ||
if len(prop_path) > 1 and config.get(substitution.variable, None) is not None: | ||
continue # If value is present in latest version, don't do anything | ||
if prop_path[0] == key and substitution.optional: | ||
ConfigParser._do_substitute(substitution, None) | ||
|
||
# traverse config to find all the substitutions | ||
@staticmethod | ||
def _find_substitutions(item): | ||
"""Convert HOCON input into a JSON output | ||
|
||
:return: JSON string representation | ||
:type return: basestring | ||
""" | ||
if isinstance(item, ConfigValues): | ||
return item.get_substitutions() | ||
|
||
substitutions = [] | ||
if isinstance(item, ConfigTree): | ||
for key, child in item.items(): | ||
substitutions += ConfigParser._find_substitutions(child) | ||
elif isinstance(item, list): | ||
for child in item: | ||
substitutions += ConfigParser._find_substitutions(child) | ||
return substitutions | ||
|
||
@staticmethod | ||
def _do_substitute(substitution, resolved_value, is_optional_resolved=True): | ||
unresolved = False | ||
new_substitutions = [] | ||
if isinstance(resolved_value, ConfigValues): | ||
resolved_value = resolved_value.transform() | ||
if isinstance(resolved_value, ConfigValues): | ||
unresolved = True | ||
result = None | ||
else: | ||
# replace token by substitution | ||
config_values = substitution.parent | ||
# if it is a string, then add the extra ws that was present in the original string after the substitution | ||
formatted_resolved_value = resolved_value \ | ||
if resolved_value is None \ | ||
or isinstance(resolved_value, (dict, list)) \ | ||
or substitution.index == len(config_values.tokens) - 1 \ | ||
else (str(resolved_value) + substitution.ws) | ||
config_values.put(substitution.index, formatted_resolved_value) | ||
transformation = config_values.transform() | ||
result = None | ||
if transformation is None and not is_optional_resolved: | ||
result = config_values.overriden_value | ||
else: | ||
result = transformation[0] if isinstance(transformation, list) else transformation | ||
|
||
if result is None: | ||
del config_values.parent[config_values.key] | ||
else: | ||
config_values.parent[config_values.key] = result | ||
s = ConfigParser._find_substitutions(result) | ||
if s: | ||
new_substitutions = s | ||
unresolved = True | ||
|
||
return (unresolved, new_substitutions, result) | ||
|
||
@staticmethod | ||
def _final_fixup(item): | ||
if isinstance(item, ConfigValues): | ||
return item.transform() | ||
elif isinstance(item, list): | ||
return list([ConfigParser._final_fixup(child) for child in item]) | ||
elif isinstance(item, ConfigTree): | ||
items = list(item.items()) | ||
for key, child in items: | ||
item[key] = ConfigParser._final_fixup(child) | ||
return item | ||
|
||
@staticmethod | ||
def resolve_substitutions(config): | ||
# traverse config to find all the substitutions | ||
def find_substitutions(item): | ||
"""Convert HOCON input into a JSON output | ||
|
||
:return: JSON string representation | ||
:type return: basestring | ||
""" | ||
if isinstance(item, ConfigValues): | ||
return item.get_substitutions() | ||
|
||
substitutions = [] | ||
if isinstance(item, ConfigTree): | ||
for key, child in item.items(): | ||
substitutions += find_substitutions(child) | ||
elif isinstance(item, list): | ||
for child in item: | ||
substitutions += find_substitutions(child) | ||
|
||
return substitutions | ||
|
||
def final_fixup(item): | ||
if isinstance(item, ConfigValues): | ||
return item.transform() | ||
elif isinstance(item, list): | ||
return list([final_fixup(child) for child in item]) | ||
elif isinstance(item, ConfigTree): | ||
items = list(item.items()) | ||
for key, child in items: | ||
item[key] = final_fixup(child) | ||
|
||
return item | ||
|
||
substitutions = find_substitutions(config) | ||
ConfigParser._fixup_self_references(config) | ||
substitutions = ConfigParser._find_substitutions(config) | ||
|
||
if len(substitutions) > 0: | ||
unresolved = True | ||
|
@@ -340,43 +407,11 @@ def final_fixup(item): | |
if not is_optional_resolved and substitution.optional: | ||
resolved_value = None | ||
|
||
if isinstance(resolved_value, ConfigValues): | ||
resolved_value = resolved_value.transform() | ||
if isinstance(resolved_value, ConfigValues): | ||
unresolved = True | ||
else: | ||
# replace token by substitution | ||
config_values = substitution.parent | ||
# if it is a string, then add the extra ws that was present in the original string after the substitution | ||
formatted_resolved_value = resolved_value \ | ||
if resolved_value is None \ | ||
or isinstance(resolved_value, (dict, list)) \ | ||
or substitution.index == len(config_values.tokens) - 1 \ | ||
else (str(resolved_value) + substitution.ws) | ||
config_values.put(substitution.index, formatted_resolved_value) | ||
transformation = config_values.transform() | ||
if transformation is None and not is_optional_resolved: | ||
# if it does not override anything remove the key | ||
# otherwise put back old value that it was overriding | ||
if config_values.overriden_value is None: | ||
if config_values.key in config_values.parent: | ||
del config_values.parent[config_values.key] | ||
else: | ||
config_values.parent[config_values.key] = config_values.overriden_value | ||
s = find_substitutions(config_values.overriden_value) | ||
if s: | ||
substitutions.extend(s) | ||
unresolved = True | ||
else: | ||
result = transformation[0] if isinstance(transformation, list) else transformation | ||
config_values.parent[config_values.key] = result | ||
s = find_substitutions(result) | ||
if s: | ||
substitutions.extend(s) | ||
unresolved = True | ||
substitutions.remove(substitution) | ||
|
||
final_fixup(config) | ||
unresolved, new_subsitutions, _ = ConfigParser._do_substitute(substitution, resolved_value, is_optional_resolved) | ||
substitutions.extend(new_subsitutions) | ||
substitutions.remove(substitution) | ||
|
||
ConfigParser._final_fixup(config) | ||
if unresolved: | ||
raise ConfigSubstitutionException("Cannot resolve {variables}. Check for cycles.".format( | ||
variables=', '.join('${{{variable}}}: (line: {line}, col: {col})'.format( | ||
|
@@ -426,8 +461,9 @@ class ConfigTreeParser(TokenConverter): | |
Parse a config tree from tokens | ||
""" | ||
|
||
def __init__(self, expr=None): | ||
def __init__(self, expr=None, root=False): | ||
super(ConfigTreeParser, self).__init__(expr) | ||
self.root = root | ||
self.saveAsList = True | ||
|
||
def postParse(self, instring, loc, token_list): | ||
|
@@ -438,7 +474,7 @@ def postParse(self, instring, loc, token_list): | |
:param token_list: | ||
:return: | ||
""" | ||
config_tree = ConfigTree() | ||
config_tree = ConfigTree(root=self.root) | ||
for element in token_list: | ||
expanded_tokens = element.tokens if isinstance(element, ConfigInclude) else [element] | ||
|
||
|
@@ -455,13 +491,14 @@ def postParse(self, instring, loc, token_list): | |
if isinstance(value, list): | ||
config_tree.put(key, value, False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sanzinger Nice implementation of |
||
else: | ||
if isinstance(value, ConfigTree): | ||
existing_value = config_tree.get(key, None) | ||
if isinstance(value, ConfigTree) and not isinstance(existing_value, list): | ||
# Only Tree has to be merged with tree | ||
config_tree.put(key, value, True) | ||
elif isinstance(value, ConfigValues): | ||
conf_value = value | ||
value.parent = config_tree | ||
value.key = key | ||
existing_value = config_tree.get(key, None) | ||
if isinstance(existing_value, list) or isinstance(existing_value, ConfigTree): | ||
config_tree.put(key, conf_value, True) | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sanzinger Really nice implementation for supporting self references! 👍