Skip to content

Instantly share code, notes, and snippets.

@ChristianKuehnel
Created February 1, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianKuehnel/a01cc4362b07c58281554ab46235a077 to your computer and use it in GitHub Desktop.
Save ChristianKuehnel/a01cc4362b07c58281554ab46235a077 to your computer and use it in GitHub Desktop.
Pyhton script to convert matcher Macros names to start with lowercase letter.
#!/usr/bin/env python3
import re
import os
# find matchers starting with upper case letter
pattern=re.compile(r"MATCHER(_P)?\((?P<name>[A-Z][^,\W]+)")
def process_file(file_path:str):
contents=open(file_path,"r").read()
for match in pattern.finditer(contents):
matcher_name = match.group("name")
fixed_name = matcher_name[0].lower() + matcher_name[1:]
# Ensure our replace position is not part of a substring, or starts with
# ".:>" is uncommon for a matcher
replacer=fr"([^a-zA-Z.:>])({matcher_name})([^a-zA-Z.:;])"
contents=re.sub(replacer, rf"\1{fixed_name}\3", contents)
open(file_path,"w").write(contents)
for root, dirs, files in os.walk(os.curdir):
for file in files:
process_file(os.path.join(root,file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment