import re
import sys
EXTRA_EXPORTS = [
"SmartGestureAction",
"OperateIntention",
"BaseGestureHandlingProposal",
"TargetedGestureProposal",
"ClickActionProposal",
"SelectActionProposal",
"NoneActionProposal",
"BackPressActionProposal",
"PageSwitchActionProposal",
"ScrollActionProposal",
"GestureHandlingResolution",
]
def append_export_name(export_state, export_name):
if export_state["count"] != 0 and export_state["count"] % 5 == 0:
export_state["content"] += ",\n\t"
export_state["is_first"] = True
if export_state["is_first"]:
export_state["is_first"] = False
else:
export_state["content"] += ", "
export_state["content"] += export_name
export_state["count"] += 1
def main(input_path, output_path):
with open(output_path, "w", encoding="utf-8") as ohos:
export_state = {
"content": "export default { ",
"count": 0,
"is_first": True,
}
declared_names = set()
with open(input_path, "r", encoding="utf-8") as f:
line = f.readline()
while (line):
ohos.write(line)
if (line.startswith("class ")):
class_name = re.match(r"class\s+(\w+)", line).group(1)
declared_names.add(class_name)
append_export_name(export_state, class_name)
const_match = re.match(r"const\s+(\w+)\s*=", line)
if const_match:
declared_names.add(const_match.group(1))
line = f.readline()
for export_name in EXTRA_EXPORTS:
if export_name in declared_names:
append_export_name(export_state, export_name)
ohos.write("\n")
export_content = export_state["content"] + " };\n"
export_content += "globalThis.__getUIContext__ = __getUIContext__;\n"
export_content += "globalThis.__getFrameNodeByNodeId__ = __getFrameNodeByNodeId__;\n"
export_content += "globalThis.__addAvailableInstanceId__ = __addAvailableInstanceId__;\n"
export_content += "globalThis.__removeAvailableInstanceId__ = __removeAvailableInstanceId__;\n"
export_content += "globalThis.__checkRegexValid__ = __checkRegexValid__;\n"
export_content += "globalThis.__getResourceId__ = __getResourceId__;"
ohos.write("\n" + export_content)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])