Last active
August 12, 2016 08:10
-
-
Save oesteban/9f557ec6af62014a74142137048e1c7b to your computer and use it in GitHub Desktop.
Fixes docstrings for unicode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import re | |
| import sys | |
| from builtins import open | |
| squote_exp = re.compile(r"(?P<ms>u?'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*')", | |
| re.UNICODE) | |
| def add_upref(m): | |
| ms = m.group('ms') | |
| if ms[0] == 'u': | |
| return ms | |
| return 'u' + ms | |
| def add_flag(matchobj): | |
| firstline = matchobj.group('firstline') | |
| secondline = matchobj.group('secondline') | |
| flag = '\n' | |
| if '"""' in secondline or firstline.endswith('\\'): | |
| return firstline + flag + secondline + '\n' | |
| if '+IGNORE_UNICODE' not in firstline and '+SKIP' not in firstline: | |
| flag = ' +IGNORE_UNICODE' + flag | |
| if '# doctest:' not in firstline: | |
| flag = ' # doctest:' + flag | |
| secondline = squote_exp.sub(add_upref, secondline) | |
| return firstline + flag + secondline + '\n' | |
| def main(): | |
| from argparse import ArgumentParser, RawTextHelpFormatter | |
| parser = ArgumentParser(description='Regex doctests fixer', | |
| formatter_class=RawTextHelpFormatter) | |
| parser.add_argument('input_file', action='store', help='the file to be fixed') | |
| args = parser.parse_args() | |
| with open(args.input_file, 'r') as myfile: | |
| data=''.join(myfile.read()) | |
| flag_expr = re.compile(r'(?P<firstline>(?:\ *\>{3}\ .*))(?:\n)(?P<secondline>(?:\ )*(?<!\>{3}\ )([\w\d\[\]\{\}]*(?<!u)[\'"]{1}.*[\'"]{1}.*\n?)+)\n', re.UNICODE) | |
| with open(args.input_file, 'w') as myfile: | |
| myfile.write(flag_expr.sub(add_flag, data)) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment