Skip to content

Commit b507d2b

Browse files
committed
Fix typo; add comments; make xmlpp_process_imports() local
1 parent ad1bb98 commit b507d2b

2 files changed

Lines changed: 33 additions & 33 deletions

File tree

clockwork.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
name: xml-preprocessor
2-
version: 1.0.0
2+
version: 1.2.0
33
description: Allows Python expressions and reduces duplication in Google-Samsung Watch Face Format XML.

preprocess.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def xmlpp_parse_args(): # parse command-line arguments
3030
else: xmlpp_source_file = xmlpp_arg
3131

3232
if xmlpp_source_file is None or xmlpp_dest_file is None or xmlpp_USAGE_ERROR:
33-
print("XML Preprocessor 1.1.0")
33+
print("XML Preprocessor 1.2.0")
3434
print("Usage: preprocess.py sourceFile destinationFile [-d] [-y]")
3535
print(" -d prints debugging info")
3636
print(" -y overwrites destinationFile")
@@ -40,41 +40,41 @@ def xmlpp_parse_args(): # parse command-line arguments
4040
raise xmlpp_error("can't find "+xmlpp_source_file)
4141

4242
if os.path.exists(xmlpp_dest_file) and not xmlpp_OVERWRITE:
43-
xmlpp_overwrite_input = input("Desination file already exists; overwrite it (y/n)? ")
43+
xmlpp_overwrite_input = input("Destination file already exists; overwrite it (y/n)? ")
4444
if (xmlpp_overwrite_input != "y"): exit(2)
4545

46-
def xmlpp_process_imports(xmlpp_element, xmlpp_source):
47-
"""Process all Import elements under the given element."""
48-
xmlpp_index = 0
49-
while xmlpp_index < len(xmlpp_element):
50-
xmlpp_child_el = xmlpp_element[xmlpp_index]
51-
if xmlpp_child_el.tag == "Import":
52-
xmlpp_href = xmlpp_child_el.get('href')
53-
if xmlpp_DEBUG: print(f" Found <Import href=\"{xmlpp_href}\" />")
54-
xmlpp_include_path = os.path.join(os.path.dirname(xmlpp_source), xmlpp_href)
55-
if not os.path.exists(xmlpp_include_path):
56-
raise xmlpp_error(f"Can't find \"{xmlpp_href}\" imported by \"{xmlpp_source}\".")
57-
xmlpp_element.remove(xmlpp_child_el) # remove <Import>
58-
if xmlpp_href.lower().endswith('.py'):
59-
xmlpp_el = xmlpp_ET.Element('Define')
60-
with open(xmlpp_include_path, 'r') as xmlpp_file:
61-
xmlpp_el.text = f"\n{xmlpp_file.read()}\n"
62-
xmlpp_element.insert(xmlpp_index, xmlpp_el)
63-
else: # assume .xml
64-
xmlpp_child_tree = xmlpp_load_tree(xmlpp_include_path, True)
65-
xmlpp_child_root = xmlpp_child_tree.getroot()
66-
for xmlpp_el in xmlpp_child_root:
67-
xmlpp_element.insert(xmlpp_index, xmlpp_el)
68-
xmlpp_index += 1
69-
else:
70-
# Recursively process imports in child elements
71-
xmlpp_process_imports(xmlpp_child_el, xmlpp_source)
72-
xmlpp_index += 1
73-
7446
def xmlpp_load_tree(xmlpp_source, xmlpp_require_widget=False):
7547
# Read source, and recursively splice in <Import> files.
7648
# Returns tree.
7749

50+
def xmlpp_process_imports(xmlpp_element, xmlpp_source):
51+
"""Process all Import elements under the given element."""
52+
xmlpp_index = 0
53+
while xmlpp_index < len(xmlpp_element):
54+
xmlpp_child_el = xmlpp_element[xmlpp_index]
55+
if xmlpp_child_el.tag == "Import":
56+
xmlpp_href = xmlpp_child_el.get('href')
57+
if xmlpp_DEBUG: print(f" Found <Import href=\"{xmlpp_href}\" />")
58+
xmlpp_include_path = os.path.join(os.path.dirname(xmlpp_source), xmlpp_href) # assume href is relative to source
59+
if not os.path.exists(xmlpp_include_path):
60+
raise xmlpp_error(f"Can't find \"{xmlpp_href}\" imported by \"{xmlpp_source}\".")
61+
xmlpp_element.remove(xmlpp_child_el) # remove <Import>
62+
if xmlpp_href.lower().endswith('.py'):
63+
xmlpp_el = xmlpp_ET.Element('Define')
64+
with open(xmlpp_include_path, 'r') as xmlpp_file:
65+
xmlpp_el.text = f"\n{xmlpp_file.read()}\n" # \n so as not to interfere with python indentation
66+
xmlpp_element.insert(xmlpp_index, xmlpp_el)
67+
else: # assume .xml
68+
xmlpp_child_tree = xmlpp_load_tree(xmlpp_include_path, True)
69+
xmlpp_child_root = xmlpp_child_tree.getroot()
70+
for xmlpp_el in xmlpp_child_root:
71+
xmlpp_element.insert(xmlpp_index, xmlpp_el)
72+
xmlpp_index += 1 # skip over inserted elements coz they've already been recursed in case of nested <Import>s
73+
else:
74+
# Recursively process imports in child elements
75+
xmlpp_process_imports(xmlpp_child_el, xmlpp_source)
76+
xmlpp_index += 1
77+
7878
if xmlpp_DEBUG and not xmlpp_require_widget: print(f"Loading source file(s)...")
7979
if xmlpp_DEBUG: print(f" Loading \"{xmlpp_source}\"")
8080
try:
@@ -85,10 +85,10 @@ def xmlpp_load_tree(xmlpp_source, xmlpp_require_widget=False):
8585
xmlpp_root = xmlpp_tree.getroot()
8686
if xmlpp_require_widget and xmlpp_root.tag != "Widget":
8787
raise xmlpp_error(f"Root element of imported file \"{xmlpp_source}\" isn't <Widget>.")
88-
88+
8989
# Process imports throughout the entire tree
9090
xmlpp_process_imports(xmlpp_root, xmlpp_source)
91-
91+
9292
return xmlpp_tree
9393

9494
def xmlpp_exec_all_definitions(): # execute all <Define> elements and delete them

0 commit comments

Comments
 (0)