src/pyparsing/tools/cvt_pyparsing_pep8_names.py
1import warnings
2from functools import lru_cache
3import pyparsing as pp
4
5
6@lru_cache(maxsize=None)
7def camel_to_snake(s: str) -> str:
8 """
9 Convert CamelCase to snake_case.
10 """
11 return "".join(f"_{c.lower()}" if c.isupper() else c for c in s).lstrip("_")
12
13
14pre_pep8_method_names = """
15addCondition addParseAction anyCloseTag anyOpenTag asDict asList cStyleComment canParseNext conditionAsParseAction
16convertToDate convertToDatetime convertToFloat convertToInteger countedArray cppStyleComment dblQuotedString
17dblSlashComment defaultName dictOf disableMemoization downcaseTokens enableLeftRecursion enablePackrat getName
18htmlComment ignoreWhitespace infixNotation inlineLiteralsUsing javaStyleComment leaveWhitespace
19lineEnd lineStart matchOnlyAtCol matchPreviousExpr matchPreviousLiteral nestedExpr nullDebugAction oneOf
20originalTextFor parseFile parseString parseWithTabs pythonStyleComment quotedString removeQuotes replaceWith
21resetCache restOfLine runTests scanString searchString setBreak setDebug setDebugActions setDefaultWhitespaceChars
22setFailAction setName setParseAction setResultsName setWhitespaceChars sglQuotedString stringEnd stringStart tokenMap
23traceParseAction transformString tryParse unicodeString upcaseTokens withAttribute withClass
24""".split()
25
26special_changes = {
27 "opAssoc": "OpAssoc",
28 "delimitedList": "DelimitedList",
29 "delimited_list": "DelimitedList",
30 "replaceHTMLEntity": "replace_html_entity",
31 "makeHTMLTags": "make_html_tags",
32 "makeXMLTags": "make_xml_tags",
33 "commonHTMLEntity": "common_html_entity",
34 "stripHTMLTags": "strip_html_tags",
35 "indentedBlock": "IndentedBlock",
36 "locatedExpr": "Located",
37}
38
39pre_pep8_arg_names = """parseAll maxMatches listAllMatches callDuringTry includeSeparators fullDump printResults
40failureTests postParse matchString identChars maxMismatches initChars bodyChars asKeyword excludeChars asGroupList
41asMatch quoteChar escChar escQuote unquoteResults endQuoteChar convertWhitespaceEscapes notChars wordChars stopOn
42failOn joinString markerString intExpr useRegex asString ignoreExpr""".split()
43
44special_changes_arg_names = {
45 "asList": "aslist",
46}
47
48pre_pep8_method_name = pp.one_of(pre_pep8_method_names, as_keyword=True)
49pre_pep8_method_name.set_parse_action(lambda t: camel_to_snake(t[0]))
50special_pre_pep8_name = pp.one_of(special_changes, as_keyword=True)
51def update_special_changes(s, l, t):
52 if t[0] == "indentedBlock":
53 warnings.warn(
54 "Conversion of 'indentedBlock' to new 'IndentedBlock'"
55 " requires added code changes to remove 'indentStack' argument\n"
56 f" {pp.lineno(l, s)}: {pp.line(l, s)}",
57 stacklevel=2,
58 )
59 elif t[0] == "locatedExpr":
60 warnings.warn(
61 "Conversion of 'locatedExpr' to new 'Located'"
62 " may require added code changes - Located does not automatically"
63 " group parsed elements\n"
64 f" {pp.lineno(l, s)}: {pp.line(l, s)}",
65 stacklevel=2,
66 )
67 return special_changes[t[0]]
68special_pre_pep8_name.set_parse_action(update_special_changes)
69# only replace arg names if part of an arg list
70pre_pep8_arg_name = pp.Regex(
71 rf"{pp.util.make_compressed_re(pre_pep8_arg_names)}"
72) + pp.FollowedBy("=")
73pre_pep8_arg_name.set_parse_action(lambda t: camel_to_snake(t[0]))
74special_pre_pep8_arg_name = pp.one_of(special_changes_arg_names, as_keyword=True) + pp.FollowedBy("=")
75special_pre_pep8_arg_name.set_parse_action(lambda t: special_changes_arg_names[t[0]])
76
77pep8_converter = special_pre_pep8_arg_name | pre_pep8_method_name | special_pre_pep8_name | pre_pep8_arg_name
78
79if __name__ == "__main__":
80 import argparse
81 from pathlib import Path
82 import sys
83
84 argparser = argparse.ArgumentParser(
85 description = (
86 "Utility to convert Python pyparsing scripts using legacy"
87 " camelCase names to use PEP8 snake_case names."
88 "\nBy default, this script will only show whether this script would make any changes."
89 )
90 )
91 argparser.add_argument("--verbose", "-v", action="store_true", help="Show unified diff for each source file")
92 argparser.add_argument("-vv", action="store_true", dest="verbose2", help="Show unified diff for each source file, plus names of scanned files with no changes")
93 argparser.add_argument("--update", "-u", action="store_true", help="Update source files in-place")
94 argparser.add_argument("--encoding", type=str, default="utf-8", help="Encoding of source files (default: utf-8)")
95 argparser.add_argument("--exit-zero-even-if-changed", "-exit0", action="store_true", help="Exit with status code 0 even if changes were made")
96 argparser.add_argument("source_filename", nargs="+", help="Source filenames or filename patterns of Python files to be converted")
97 args = argparser.parse_args()
98
99
100 def show_diffs(original, modified):
101 import difflib
102
103 diff = difflib.unified_diff(
104 original.splitlines(), modified.splitlines(), lineterm=""
105 )
106 sys.stdout.writelines(f"{diff_line}\n" for diff_line in diff)
107
108 exit_status = 0
109
110 for filename_pattern in args.source_filename:
111
112 for filename in Path().glob(filename_pattern):
113 if not Path(filename).is_file():
114 continue
115
116 try:
117 original_contents = Path(filename).read_text(encoding=args.encoding)
118 modified_contents = pep8_converter.transform_string(
119 original_contents
120 )
121
122 if modified_contents != original_contents:
123 if args.update:
124 Path(filename).write_text(modified_contents, encoding=args.encoding)
125 print(f"Converted {filename}")
126 else:
127 print(f"Found required changes in {filename}")
128
129 if args.verbose:
130 show_diffs(original_contents, modified_contents)
131 print()
132
133 exit_status = 1
134
135 else:
136 if args.verbose2:
137 print(f"No required changes in {filename}")
138
139 except Exception as e:
140 print(f"Failed to convert {filename}: {type(e).__name__}: {e}")
141
142 sys.exit(exit_status if not args.exit_zero_even_if_changed else 0)