From 7b4787c5aae3513feb7c5e7ce2d727adb01bd5e9 Mon Sep 17 00:00:00 2001 From: jinyu Date: Tue, 10 Oct 2023 10:49:23 +0800 Subject: [PATCH] update sync_prototype.py --- xml2json/sync_prototype.py | 134 ++++++++----------------------------- 1 file changed, 29 insertions(+), 105 deletions(-) diff --git a/xml2json/sync_prototype.py b/xml2json/sync_prototype.py index 8fc2d658844..f3ae29dfd20 100644 --- a/xml2json/sync_prototype.py +++ b/xml2json/sync_prototype.py @@ -1,116 +1,40 @@ -#!/usr/local/bin/python3 -# -*- coding: utf-8 -*- -# -# Developed by Lutkin Wang -# -# Story: Replace the prototype section so that manual copy & paste is no longer needed -# -#
-#

-# public abstract void onMetadataReceived(byte[] buffer, int uid, long timeStampMs); -# - (void)receiveMetadata:(NSData * _Nonnull)data -# fromUser:(NSInteger)uid atTimestamp:(NSTimeInterval)timestamp; -#

-#

-# -# -# virtual void onMetadataReceived(const Metadata &metadata) = 0; -# }; -# on(evt: EngineEvents.RECEIVE_METADATA, cb: ( -# metadata: Metadata -# ) => void): this; -# -# -#

-#
-# -# from: C:\Users\WL\Documents\GitHub\doc_source\dita\RTC\API\xxx.dita -# -# with: C:\Users\WL\Documents\GitHub\doc_source\en-US\dita\RTC\API\xxx.dita -# - -import xml.etree.ElementTree as ET import os -from os import path +import re import argparse +parser = argparse.ArgumentParser(description="Prototype syncer") -def main(): - - parser = argparse.ArgumentParser(description="Prototype syncer") - - parser.add_argument("--src_dir", - help="src dir", - action="store") - parser.add_argument("--dest_dir", help="dest dir", action="store") - - args = vars(parser.parse_args()) - - src_dir = args['src_dir'] - dest_dir = args['dest_dir'] - - # src_dir = "C:\\Users\\WL\\Documents\\GitHub\\doc_source\\dita\\RTC\\API" - # src_dir = "D:\\github_lucas\\doc_source\\dita\\RTC\\API" - # src_dir = "C:\\Users\\WL\\Documents\\GitHub\\\doc_source\\en-US\\dita\\RTC\\API" - - # dest_dir = "C:\\Users\\WL\\Documents\\GitHub\\doc_source\\en-US\\dita\\RTC\\API" - # dest_dir = "D:\\github_lucas\\doc_source\\en-US\\dita\\RTC\\API" - # dest_dir = "C:\\Users\\WL\\Documents\\GitHub\\doc_source\\dita\\RTC\\API" - - src_proto_section_obj = None - dest_proto_section_obj = None - dest_dita_file_tree = None - - # Copy cn protos to en - for file_name in os.listdir(src_dir): - - file_ready_for_copy = True - - if file_name.startswith("api_") or file_name.startswith("class_"): - - try: - cn_path = path.join(src_dir, file_name) - cn_dita_file_tree = ET.parse(cn_path) - cn_dita_file_root = cn_dita_file_tree.getroot() - except ET.ParseError as e: - print("[ERROR] Parse error for: " + file_name + " Code: " + str(e.code) + " Position: " + str(e.position)) - - en_path = path.join(dest_dir, file_name) - - try: - dest_dita_file_tree = ET.parse(en_path) - en_dita_file_root = dest_dita_file_tree.getroot() - except FileNotFoundError as e: - print("[ERROR] File not found in en: " + file_name) - file_ready_for_copy = False - - except ET.ParseError as e: - print("[ERROR] Parse error for: " + file_name + " Code: " + str(e.code) + " Position: " + str(e.position)) - file_ready_for_copy = False - - if file_ready_for_copy: - - for section in cn_dita_file_root.iter("section"): - if section.get("id") == "prototype": - src_proto_section_obj = section - - refbody = en_dita_file_root.find("./refbody") +parser.add_argument("--src_dir",help="src dir",action="store") +parser.add_argument("--dest_dir", help="dest dir", action="store") - for section in en_dita_file_root.iter("section"): - if section.get("id") == "prototype": - refbody.remove(section) - refbody.insert(0, src_proto_section_obj) +args = vars(parser.parse_args()) - dest_dita_file_tree.write(dest_dir + "//" + file_name, encoding='utf-8') +src_dir = args['src_dir'] +dest_dir = args['dest_dir'] - header = """\n\n""" +prefixes = ('api_', 'callback_', 'class_') - with open(dest_dir + "//" + file_name, "r", encoding='utf-8') as f: - text = header + f.read() +for file_name in os.listdir(src_dir): - with open(dest_dir + "//" + file_name, "w", encoding='utf-8') as f: - f.write(text) + if file_name.startswith(prefixes): + cn_path = os.path.join(src_dir, file_name) + with open(cn_path, 'r') as cn_file: + cn_file_content = cn_file.read() + pattern = r'
' + proto = re.findall(pattern, cn_file_content, flags=re.DOTALL) + if not proto: + print(f"No proto in cn file: {cn_path}") + continue -if __name__ == '__main__': - main() + en_path = os.path.join(dest_dir, file_name) + try: + with open(en_path, 'r+') as en_file: + en_file_content = en_file.read() + en_file.seek(0) + updated_content = re.sub(r'
', proto[0], en_file_content, flags=re.DOTALL) + en_file.write(updated_content) + en_file.truncate() + except FileNotFoundError: + print(f"No corresponding en file: {en_path}") + continue \ No newline at end of file