Skip to content

Commit

Permalink
Revert "update sync_prototype.py"
Browse files Browse the repository at this point in the history
This reverts commit 7b4787c.
  • Loading branch information
jinyuagora committed Oct 10, 2023
1 parent 522c3f6 commit 5ee4543
Showing 1 changed file with 105 additions and 29 deletions.
134 changes: 105 additions & 29 deletions xml2json/sync_prototype.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,116 @@
#!/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
#
# <section id="prototype">
# <p props="rtc-ng" outputclass="codeblock">
# <codeblock props="android" outputclass="language-java">public abstract void onMetadataReceived(byte[] buffer, int uid, long timeStampMs);</codeblock>
# <codeblock props="ios mac" outputclass="language-objectivec">- (void)receiveMetadata:(NSData * _Nonnull)data
# fromUser:(NSInteger)uid atTimestamp:(NSTimeInterval)timestamp;</codeblock>
# </p>
# <p props="rtc" outputclass="codeblock">
# <codeblock props="android" outputclass="language-java"/>
# <codeblock props="ios mac" outputclass="language-objectivec"/>
# <codeblock props="windows unity" outputclass="language-cpp">virtual void onMetadataReceived(const Metadata &amp;metadata) = 0;
# };</codeblock>
# <codeblock props="electron" outputclass="language-typescript">on(evt: EngineEvents.RECEIVE_METADATA, cb: (
# metadata: Metadata
# ) => void): this;</codeblock>
# <codeblock props="rn" outputclass="language-typescript"/>
# <codeblock props="flutter" outputclass="language-dart"/>
# </p>
# </section>
#
# 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
import re
from os import path
import argparse

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")
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")

args = vars(parser.parse_args())
for section in en_dita_file_root.iter("section"):
if section.get("id") == "prototype":
refbody.remove(section)
refbody.insert(0, src_proto_section_obj)

src_dir = args['src_dir']
dest_dir = args['dest_dir']
dest_dita_file_tree.write(dest_dir + "//" + file_name, encoding='utf-8')

prefixes = ('api_', 'callback_', 'class_')
header = """<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd">\n"""

for file_name in os.listdir(src_dir):
with open(dest_dir + "//" + file_name, "r", encoding='utf-8') as f:
text = header + f.read()

if file_name.startswith(prefixes):
with open(dest_dir + "//" + file_name, "w", encoding='utf-8') as f:
f.write(text)

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'<section id="prototype".*?</section>'
proto = re.findall(pattern, cn_file_content, flags=re.DOTALL)
if not proto:
print(f"No proto in cn file: {cn_path}")
continue

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'<section id="prototype".*?</section>', 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
if __name__ == '__main__':
main()

0 comments on commit 5ee4543

Please sign in to comment.