Skip to content

make makeqstrdata.py more pythonic #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions py/makeqstrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import re
import sys
from collections import OrderedDict

# codepoint2name is different in Python 2 to Python 3
import platform
Expand All @@ -24,10 +25,14 @@ def compute_hash(qstr):
hash = (hash * 33) ^ ord(char)
return hash & 0xffff

# given a list of (name,regex) pairs, find the first one that matches the given line
def re_match(regexs, line):
for name, regex in regexs:
match = re.match(regex, line)
cpp_regexes = OrderedDict()
cpp_regexes['qstr'] = r'Q\((.+)\)$'
cpp_regexes['cdecl'] = r'(typedef|extern) [A-Za-z0-9_* ]+;$'

# given a line, find the first cpp regex it matches
def get_line_match(line):
for name in cpp_regexes:
match = re.match(cpp_regexes[name], line)
if match:
return name, match
return None, None
Expand All @@ -47,12 +52,12 @@ def do_work(infiles):
continue

# work out what kind of line it is
match_kind, match = re_match([('qstr', r'Q\((.+)\)$'), ('cdecl', r'(typedef|extern) [A-Za-z0-9_* ]+;$')], line)
if match_kind is None:
line_type, match = get_line_match(line)
if line_type is None:
# unknown line format
print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr)
return False
elif match_kind != 'qstr':
elif line_type != 'qstr':
# not a line with a qstr
continue

Expand Down