-
-
Notifications
You must be signed in to change notification settings - Fork 145
add script to convert states #136
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
base: master
Are you sure you want to change the base?
Conversation
68eb7b6
to
886f3fb
Compare
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.
Thank you for the work. Please check my comments. Thanks.
""" | ||
Convert `states` to XML attributes (readonly, required, invisible). | ||
""" | ||
logger.info(f"Parsing states: {states}") # 添加日志,打印解析到的 states |
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.
Please translate the comment to English.
field_name = target.id | ||
states_value = None | ||
for keyword in getattr(node.value, "keywords", []): | ||
if keyword.arg == "states": |
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 think it is better to skip below logic if it is not states
.
if keyword.arg == "states": | |
if keyword.arg != "states": | |
continue |
if isinstance(keyword.value, ast.Dict): | ||
# Direct dictionary | ||
states_value = ast.literal_eval(keyword.value) | ||
elif isinstance(keyword.value, ast.Name): | ||
# Variable reference | ||
var_name = keyword.value.id | ||
if var_name in variable_definitions: | ||
states_value = ast.literal_eval( | ||
variable_definitions[var_name] | ||
) |
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.
if isinstance(keyword.value, ast.Dict): | |
# Direct dictionary | |
states_value = ast.literal_eval(keyword.value) | |
elif isinstance(keyword.value, ast.Name): | |
# Variable reference | |
var_name = keyword.value.id | |
if var_name in variable_definitions: | |
states_value = ast.literal_eval( | |
variable_definitions[var_name] | |
) | |
if isinstance(keyword.value, ast.Dict): | |
# Direct dictionary | |
states_value = ast.literal_eval(keyword.value) | |
elif isinstance(keyword.value, ast.Name): | |
# Variable reference | |
var_name = keyword.value.id | |
if var_name in variable_definitions: | |
states_value = ast.literal_eval( | |
variable_definitions[var_name] | |
) |
if not xml_attrs: | ||
logger.info( | ||
f"No valid attributes generated from states: {states}" | ||
) # 添加日志,打印未生成有效属性的情况 |
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.
Please translate the comment to English.
|
||
# Write the updated XML back to the file | ||
with open(xml_file, "wb") as f: | ||
tree.write(f, pretty_print=True, encoding="utf-8", xml_declaration=True) |
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.
This line causes test failure because lxml.etree is hard coded to write header with single quote. It is better to only write when there is change.
tree.write(f, pretty_print=True, encoding="utf-8", xml_declaration=True) | |
tree.write(f, pretty_print=True, encoding="utf-8", | |
doctype='<?xml version="1.0" encoding="utf-8"?>') |
if current_model: | ||
model_field_mapping[current_model] = {} |
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.
These two lines are not needed as model_field_mapping[current_model]
is not used anywhere and current_model
is part of the key used in model_field_mapping
.
# Create a copy of arch_root to modify independently | ||
arch_root_copy = et.Element(arch_root.tag, arch_root.attrib) | ||
for child in arch_root: | ||
arch_root_copy.append(child) |
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 could not see the benefit of arch_root_copy
. Below code prove that it moves, not copies, children from arch_root
to arch_root_copy
.
# Create a copy of arch_root to modify independently | |
arch_root_copy = et.Element(arch_root.tag, arch_root.attrib) | |
for child in arch_root: | |
arch_root_copy.append(child) | |
# Create a copy of arch_root to modify independently | |
arch_root_copy = et.Element(arch_root.tag, arch_root.attrib) | |
for child in arch_root: | |
arch_root_copy.append(child) | |
logger.info( | |
f"arch_root_copy, {arch_root_copy}, {len(arch_root_copy)}" | |
) | |
logger.info(f"arch_root, {arch_root}, {len(arch_root)}") |
No description provided.