MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / main

Function main

tools/generate_exceptions.py:68–193  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

66
67
68def 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 # Removed in PostgreSQL 17, but still raised by older supported servers.
80 if not re.search(r'^72000\s', errcodes, re.MULTILINE):
81 errcodes = errcodes.replace(
82 'Section: Class F0',
83 'Section: Class 72 - Snapshot Failure\n\n'
84 '72000 E ERRCODE_SNAPSHOT_TOO_OLD snapshot_too_old\n\n'
85 'Section: Class F0',
86 )
87
88 section_re = re.compile(r'^Section: .*')
89
90 tpl = """\
91class {clsname}({base}):
92 {docstring}{sqlstate}"""
93
94 new_section = True
95 section_class = None
96
97 buf = '# GENERATED FROM postgresql/src/backend/utils/errcodes.txt\n' + \
98 '# DO NOT MODIFY, use tools/generate_exceptions.py to update\n\n' + \
99 'from ._base import * # NOQA\nfrom . import _base\n\n\n'
100
101 classes = []
102 clsnames = set()
103
104 def _add_class(clsname, base, sqlstate, docstring):
105 if sqlstate:
106 sqlstate = "sqlstate = '{}'".format(sqlstate)
107 else:
108 sqlstate = ''
109
110 txt = tpl.format(clsname=clsname, base=base, sqlstate=sqlstate,
111 docstring=docstring)
112
113 if not sqlstate and not docstring:
114 txt += 'pass'
115
116 if len(txt.splitlines()[0]) > 79:
117 txt = txt.replace('(', '(\n ', 1)
118
119 classes.append(txt)
120 clsnames.add(clsname)
121
122 for line in errcodes.splitlines():
123 if not line.strip() or line.startswith('#'):
124 continue
125

Callers 1

Calls 4

_get_error_nameFunction · 0.85
_add_classFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected