Skip to content

Instantly share code, notes, and snippets.

@LukasWoodtli
Created September 5, 2022 18:58
Show Gist options
  • Save LukasWoodtli/88fb6bca3d39fad6fe33c67e83c51674 to your computer and use it in GitHub Desktop.
Save LukasWoodtli/88fb6bca3d39fad6fe33c67e83c51674 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: ISO8859-15 -*-
# This might need some adjustments:
# Save file with the correct encoding and
# make sure that the chars to be replaced
# are encoded correctly in this file.
import os
import sys
import subprocess
import re
from shutil import move
# inspired by
# https://stackoverflow.com/a/241506/1272072
# make sure the chars that need to be replaced
# are encoded correctly in this file!
def comment_remover(text):
def replacer(match):
s = match.group(0)
r = s.replace("Ä", "Ae")
r = r.replace("Ö", "Oe")
r = r.replace("Ü", "Ue")
r = r.replace("ä", "ae")
r = r.replace("ö", "oe")
r = r.replace("ü", "ue")
return r
pattern = re.compile(r"\/\*(\*(?!\/)|[^*])*\*\/", re.DOTALL | re.MULTILINE)
text = re.sub(pattern, replacer, text)
pattern = re.compile(r'//.*?$', re.DOTALL | re.MULTILINE)
return re.sub(pattern, replacer, text)
def process_files(f):
bakfile = f + ".umlaut"
move(f, bakfile)
with open(bakfile, "r", encoding="ISO8859-15") as infile:
with open(f, "w", encoding="ISO8859-15") as outfile:
outfile.write(comment_remover(infile.read()))
RELEVANT_FILE_ENDINGS = [".c", ".cpp", ".h", ".hpp"]
def is_relevant_file(file):
for ending in RELEVANT_FILE_ENDINGS:
if not file.startswith(".") and file.endswith(ending):
return True
return False
def proces_directory(dir):
for root, dirs, files in os.walk(dir):
for file in files:
if is_relevant_file(file):
file_path = os.path.join(root, file)
ret = subprocess.run(["file", file_path], stdout=subprocess.PIPE)
if b"ISO-8859" in ret.stdout:
print(f"Processing {file_path}")
process_files(file_path)
if __name__ == "__main__":
input_path = sys.argv[1]
if os.path.isfile(input_path):
process_files(input_path)
else:
proces_directory(input_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment