Skip to content

Commit

Permalink
Merge pull request #34 from ZLLentz/fix_encoding
Browse files Browse the repository at this point in the history
FIX: encoding issues with files on windows, better error reporting on twincat-lineids-remover
  • Loading branch information
ZLLentz authored Jan 9, 2025
2 parents f0d27cd + 745a5af commit 2a9e5ce
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.7.0
rev: v1.7.1
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
Expand Down
2 changes: 1 addition & 1 deletion forTwinCatRepos/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.7.0
rev: v1.7.1
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
Expand Down
10 changes: 5 additions & 5 deletions pre_commit_hooks/check_twincat_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def main(args=None):
versions = {}
pinned = {}
for filename in args.filenames:
with open(filename, "r") as file:
with open(filename, "r", encoding="utf-8") as file:
xml_content = file.read()
versions[filename] = get_tc_version(xml_content)
pinned[filename] = tc_version_pinned(xml_content)
Expand All @@ -86,10 +86,10 @@ def main(args=None):
reason_msg = f"\nReason: {args.reason}" if args.reason else ""
if args.fix:
for filename in mismatched_files:
with open(filename, "r") as file:
with open(filename, "r", encoding="utf-8") as file:
xml_content = file.read()
fixed_content = fix_tc_version(xml_content, args.target_version)
with open(filename, "w") as file:
with open(filename, "w", encoding="utf-8") as file:
file.write(fixed_content)

print(
Expand All @@ -116,10 +116,10 @@ def main(args=None):
if mismatched_files:
if args.fix:
for filename in mismatched_files:
with open(filename, "r") as file:
with open(filename, "r", encoding="utf-8") as file:
xml_content = file.read()
fixed_content = fix_pinned_version(xml_content, args.pinned)
with open(filename, "w") as file:
with open(filename, "w", encoding="utf-8") as file:
file.write(fixed_content)
print(
f"Fixed pinned state for:{itemize}{itemize.join(mismatched_files)}"
Expand Down
4 changes: 2 additions & 2 deletions pre_commit_hooks/leading_tabs_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def fix_file(filename, tab_width=TAB_WIDTH):
with open(filename, "r") as fd:
with open(filename, "r", encoding="utf-8") as fd:
original_lines = fd.readlines()
new_lines = []
changed = False
Expand All @@ -24,7 +24,7 @@ def fix_file(filename, tab_width=TAB_WIDTH):
new_lines.append(line)
if changed:
print(f"Fixing {filename}")
with open(filename, "w") as fd:
with open(filename, "w", encoding="utf-8") as fd:
fd.write("".join(new_lines))


Expand Down
7 changes: 5 additions & 2 deletions pre_commit_hooks/twincat_lineids_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def fix_file(filename):
with open(filename, "r") as fd:
with open(filename, "r", encoding="utf-8") as fd:
original_lines = fd.readlines()
new_lines = []
changed = False
Expand All @@ -17,7 +17,7 @@ def fix_file(filename):

if changed:
print(f"Fixing {filename}")
with open(filename, "w") as fd:
with open(filename, "w", encoding="utf-8") as fd:
fd.write("".join(new_lines))


Expand All @@ -26,11 +26,14 @@ def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*")
args = parser.parse_args()
filename = None
try:
for filename in args.filenames:
fix_file(filename)
return 0
except Exception as exc:
if filename is not None:
print(f"Error while processing {filename}")
print(exc)
return 1

Expand Down

0 comments on commit 2a9e5ce

Please sign in to comment.