-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
add py support for ParamsDependency #4456
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import ast | ||
from contextlib import contextmanager | ||
|
||
from funcy import reraise | ||
|
||
from ._common import ParseError, _dump_data, _load_data, _modify_data | ||
|
||
_PARAMS_KEY = "__params_old_key_for_update__" | ||
_PARAMS_TEXT_KEY = "__params_text_key_for_update__" | ||
|
||
|
||
class PythonFileCorruptedError(ParseError): | ||
def __init__(self, path, message="Python file structure is corrupted"): | ||
super().__init__(path, message) | ||
|
||
|
||
def load_py(path, tree=None): | ||
return _load_data(path, parser=parse_py, tree=tree) | ||
|
||
|
||
def parse_py(text, path): | ||
"""Parses text from .py file into Python structure.""" | ||
with reraise(SyntaxError, PythonFileCorruptedError(path)): | ||
tree = ast.parse(text, filename=path) | ||
|
||
result = _ast_tree_to_dict(tree) | ||
return result | ||
|
||
|
||
def parse_py_for_update(text, path): | ||
"""Parses text into dict for update params.""" | ||
with reraise(SyntaxError, PythonFileCorruptedError(path)): | ||
tree = ast.parse(text, filename=path) | ||
|
||
result = _ast_tree_to_dict(tree) | ||
result.update({_PARAMS_KEY: _ast_tree_to_dict(tree, lineno=True)}) | ||
result.update({_PARAMS_TEXT_KEY: text}) | ||
return result | ||
|
||
|
||
def _dump(data, stream): | ||
|
||
old_params = data[_PARAMS_KEY] | ||
new_params = { | ||
key: value | ||
for key, value in data.items() | ||
if key not in [_PARAMS_KEY, _PARAMS_TEXT_KEY] | ||
} | ||
old_lines = data[_PARAMS_TEXT_KEY].splitlines(True) | ||
|
||
def _update_lines(lines, old_dct, new_dct): | ||
for key, value in new_dct.items(): | ||
if isinstance(value, dict): | ||
lines = _update_lines(lines, old_dct[key], value) | ||
elif value != old_dct[key]["value"]: | ||
lineno = old_dct[key]["lineno"] | ||
lines[lineno] = lines[lineno].replace( | ||
f" = {old_dct[key]['value']}", f" = {value}" | ||
) | ||
else: | ||
continue | ||
return lines | ||
|
||
new_lines = _update_lines(old_lines, old_params, new_params) | ||
new_text = "".join(new_lines) | ||
|
||
try: | ||
ast.parse(new_text) | ||
except SyntaxError: | ||
raise PythonFileCorruptedError( | ||
stream.name, | ||
"Python file structure is corrupted after update params", | ||
) | ||
|
||
stream.write(new_text) | ||
stream.close() | ||
|
||
|
||
def dump_py(path, data, tree=None): | ||
return _dump_data(path, data, dumper=_dump, tree=tree) | ||
|
||
|
||
@contextmanager | ||
def modify_py(path, tree=None): | ||
with _modify_data(path, parse_py_for_update, dump_py, tree=tree) as d: | ||
yield d | ||
|
||
|
||
def _ast_tree_to_dict(tree, only_self_params=False, lineno=False): | ||
"""Parses ast trees to dict. | ||
|
||
:param tree: ast.Tree | ||
:param only_self_params: get only self params from class __init__ function | ||
:param lineno: add params line number (needed for update) | ||
:return: | ||
""" | ||
result = {} | ||
for _body in tree.body: | ||
try: | ||
if isinstance(_body, (ast.Assign, ast.AnnAssign)): | ||
result.update( | ||
_ast_assign_to_dict(_body, only_self_params, lineno) | ||
) | ||
elif isinstance(_body, ast.ClassDef): | ||
result.update( | ||
{_body.name: _ast_tree_to_dict(_body, lineno=lineno)} | ||
) | ||
elif ( | ||
isinstance(_body, ast.FunctionDef) and _body.name == "__init__" | ||
): | ||
result.update( | ||
_ast_tree_to_dict( | ||
_body, only_self_params=True, lineno=lineno | ||
) | ||
) | ||
except ValueError: | ||
continue | ||
except AttributeError: | ||
continue | ||
return result | ||
|
||
|
||
def _ast_assign_to_dict(assign, only_self_params=False, lineno=False): | ||
result = {} | ||
|
||
if isinstance(assign, ast.AnnAssign): | ||
name = _get_ast_name(assign.target, only_self_params) | ||
elif len(assign.targets) == 1: | ||
name = _get_ast_name(assign.targets[0], only_self_params) | ||
else: | ||
raise AttributeError | ||
|
||
if isinstance(assign.value, ast.Dict): | ||
value = {} | ||
for key, val in zip(assign.value.keys, assign.value.values): | ||
if lineno: | ||
value[_get_ast_value(key)] = { | ||
"lineno": assign.lineno - 1, | ||
"value": _get_ast_value(val), | ||
} | ||
else: | ||
value[_get_ast_value(key)] = _get_ast_value(val) | ||
elif isinstance(assign.value, ast.List): | ||
value = [_get_ast_value(val) for val in assign.value.elts] | ||
elif isinstance(assign.value, ast.Set): | ||
values = [_get_ast_value(val) for val in assign.value.elts] | ||
value = set(values) | ||
elif isinstance(assign.value, ast.Tuple): | ||
values = [_get_ast_value(val) for val in assign.value.elts] | ||
value = tuple(values) | ||
else: | ||
value = _get_ast_value(assign.value) | ||
|
||
if lineno and not isinstance(assign.value, ast.Dict): | ||
result[name] = {"lineno": assign.lineno - 1, "value": value} | ||
else: | ||
result[name] = value | ||
|
||
return result | ||
|
||
|
||
def _get_ast_name(target, only_self_params=False): | ||
if hasattr(target, "id") and not only_self_params: | ||
result = target.id | ||
elif hasattr(target, "attr") and target.value.id == "self": | ||
result = target.attr | ||
else: | ||
raise AttributeError | ||
return result | ||
|
||
|
||
def _get_ast_value(value): | ||
if isinstance(value, ast.Num): | ||
result = value.n | ||
elif isinstance(value, ast.Str): | ||
result = value.s | ||
elif isinstance(value, ast.NameConstant): | ||
result = value.value | ||
else: | ||
raise ValueError | ||
Comment on lines
+173
to
+180
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. Note that 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. Fine as-is for now though. |
||
return result |
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
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
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
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.
I believe this is not cross-python-version compatible, right? Not a blocker though.