()
| 66 | |
| 67 | |
| 68 | def main(): |
| 69 | parser = argparse.ArgumentParser( |
| 70 | description='generate _exceptions.py from postgres/errcodes.txt') |
| 71 | parser.add_argument('errcodesfile', type=str, |
| 72 | help='path to errcodes.txt in PostgreSQL source') |
| 73 | |
| 74 | args = parser.parse_args() |
| 75 | |
| 76 | with open(args.errcodesfile, 'r') as errcodes_f: |
| 77 | errcodes = errcodes_f.read() |
| 78 | |
| 79 | section_re = re.compile(r'^Section: .*') |
| 80 | |
| 81 | tpl = """\ |
| 82 | class {clsname}({base}): |
| 83 | {docstring}{sqlstate}""" |
| 84 | |
| 85 | new_section = True |
| 86 | section_class = None |
| 87 | |
| 88 | buf = '# GENERATED FROM postgresql/src/backend/utils/errcodes.txt\n' + \ |
| 89 | '# DO NOT MODIFY, use tools/generate_exceptions.py to update\n\n' + \ |
| 90 | 'from ._base import * # NOQA\nfrom . import _base\n\n\n' |
| 91 | |
| 92 | classes = [] |
| 93 | clsnames = set() |
| 94 | |
| 95 | def _add_class(clsname, base, sqlstate, docstring): |
| 96 | if sqlstate: |
| 97 | sqlstate = "sqlstate = '{}'".format(sqlstate) |
| 98 | else: |
| 99 | sqlstate = '' |
| 100 | |
| 101 | txt = tpl.format(clsname=clsname, base=base, sqlstate=sqlstate, |
| 102 | docstring=docstring) |
| 103 | |
| 104 | if not sqlstate and not docstring: |
| 105 | txt += 'pass' |
| 106 | |
| 107 | if len(txt.splitlines()[0]) > 79: |
| 108 | txt = txt.replace('(', '(\n ', 1) |
| 109 | |
| 110 | classes.append(txt) |
| 111 | clsnames.add(clsname) |
| 112 | |
| 113 | for line in errcodes.splitlines(): |
| 114 | if not line.strip() or line.startswith('#'): |
| 115 | continue |
| 116 | |
| 117 | if section_re.match(line): |
| 118 | new_section = True |
| 119 | continue |
| 120 | |
| 121 | parts = re.split(r'\s+', line) |
| 122 | |
| 123 | if len(parts) < 4: |
| 124 | continue |
| 125 |
no test coverage detected
searching dependent graphs…