Skip to content

Commit

Permalink
Merge pull request #22 from ZLLentz/fix_xml_retry
Browse files Browse the repository at this point in the history
FIX: make the xml formatter try several times in one run
  • Loading branch information
ZLLentz authored Jun 21, 2023
2 parents eb611da + 8ea5116 commit ea33201
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions pre_commit_hooks/xml_format.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
#!C:/miniconda/envs/plc-pre-commit/python.exe

import argparse

from lxml import etree

TAB_WIDTH = 2
RETRIES = 5


def fix_file(filename, tab_width=TAB_WIDTH):
def fix_file(
filename: str,
tab_width: int = TAB_WIDTH,
retries: int = RETRIES,
) -> None:
"""
Read a file, fix it, write the fix back to the file handle
"""
# lxml throws encoding errors unless we work in binary mode
with open(filename, 'rb') as fd:
with open(filename, "rb") as fd:
original_xml = fd.read()
iter_xml = original_xml

for _ in range(retries):
new_xml = xml_once(
original_xml=iter_xml,
tab_width=tab_width,
)
if new_xml == iter_xml:
break
iter_xml = new_xml

if new_xml != original_xml:
print(f"Fixing {filename}")
with open(filename, "wb") as fd:
fd.write(new_xml)


def xml_once(original_xml: str, tab_width: int) -> str:
"""
One iteration of the xml formatting.
This may need to run several times due to inconsistencies in lxml.
"""
# lxml is the easiest cross-platform way to do this using pre-commit
# xmllint is cross-platform but pre-commit does not help us set it up
xml_parser = etree.XMLParser(remove_blank_text=True)
Expand All @@ -25,18 +56,15 @@ def fix_file(filename, tab_width=TAB_WIDTH):
# lxml always outputs with unix line endings (LF)
if b'\r\n' in original_xml:
new_xml = new_xml.replace(b'\n', b'\r\n')

if new_xml != original_xml:
print(f'Fixing {filename}')
with open(filename, 'wb') as fd:
fd.write(new_xml)
return new_xml


def main(args=None):
if args is None:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument('--tab-width', type=int, default=TAB_WIDTH)
parser.add_argument("filenames", nargs="*")
parser.add_argument("--tab-width", type=int, default=TAB_WIDTH)
parser.add_argument("--retries", type=int, default=RETRIES)
args = parser.parse_args()
try:
for filename in args.filenames:
Expand Down

0 comments on commit ea33201

Please sign in to comment.