Skip to content

Commit

Permalink
Merge pull request #15 from rruiter87/master
Browse files Browse the repository at this point in the history
add option to change tab width
  • Loading branch information
ZLLentz authored Mar 30, 2022
2 parents 9211045 + 5e79512 commit 0b41c4f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pre_commit_hooks/xml_format.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!C:/miniconda/envs/plc-pre-commit/python.exe

import argparse

from lxml import etree

TAB_WIDTH = 2


def fix_file(filename):
def fix_file(filename, tab_width=TAB_WIDTH):
# lxml throws encoding errors unless we work in binary mode
with open(filename, 'rb') as fd:
original_xml = fd.read()
Expand All @@ -14,6 +15,7 @@ def fix_file(filename):
# xmllint is cross-platform but pre-commit does not help us set it up
xml_parser = etree.XMLParser(remove_blank_text=True)
parse_tree = etree.XML(original_xml, parser=xml_parser).getroottree()
etree.indent(parse_tree, space=" " * tab_width)
new_xml = etree.tostring(parse_tree,
pretty_print=True,
xml_declaration=True,
Expand All @@ -34,10 +36,11 @@ 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)
args = parser.parse_args()
try:
for filename in args.filenames:
fix_file(filename)
fix_file(filename, tab_width=args.tab_width)
return 0
except Exception as exc:
print(exc)
Expand Down

0 comments on commit 0b41c4f

Please sign in to comment.