2018-04-14 14:56:39 +03:00
|
|
|
def generate_errors(errors, f):
|
|
|
|
# Exact/regex match to create {CODE: ErrorClassName}
|
|
|
|
exact_match = []
|
|
|
|
regex_match = []
|
|
|
|
|
|
|
|
# Find out what subclasses to import and which to create
|
|
|
|
import_base, create_base = set(), {}
|
|
|
|
for error in errors:
|
|
|
|
if error.subclass_exists:
|
|
|
|
import_base.add(error.subclass)
|
|
|
|
else:
|
|
|
|
create_base[error.subclass] = error.int_code
|
|
|
|
|
|
|
|
if error.has_captures:
|
|
|
|
regex_match.append(error)
|
|
|
|
else:
|
|
|
|
exact_match.append(error)
|
|
|
|
|
|
|
|
# Imports and new subclass creation
|
2018-06-18 22:02:42 +03:00
|
|
|
f.write('from .rpcbaseerrors import RPCError, {}\n'
|
2018-04-14 14:56:39 +03:00
|
|
|
.format(", ".join(sorted(import_base))))
|
|
|
|
|
|
|
|
for cls, int_code in sorted(create_base.items(), key=lambda t: t[1]):
|
|
|
|
f.write('\n\nclass {}(RPCError):\n code = {}\n'
|
|
|
|
.format(cls, int_code))
|
|
|
|
|
|
|
|
# Error classes generation
|
|
|
|
for error in errors:
|
2020-02-14 20:22:17 +03:00
|
|
|
f.write('\n\nclass {}({}):\n '.format(error.name, error.subclass))
|
2018-04-14 14:56:39 +03:00
|
|
|
|
|
|
|
if error.has_captures:
|
2020-02-14 20:22:17 +03:00
|
|
|
f.write('def __init__(self, request, capture=0):\n '
|
|
|
|
' self.request = request\n ')
|
|
|
|
f.write(' self.{} = int(capture)\n '
|
2018-04-14 14:56:39 +03:00
|
|
|
.format(error.capture_name))
|
2020-02-14 20:22:17 +03:00
|
|
|
else:
|
|
|
|
f.write('def __init__(self, request):\n '
|
|
|
|
' self.request = request\n ')
|
2018-04-14 14:56:39 +03:00
|
|
|
|
2018-10-15 19:35:51 +03:00
|
|
|
f.write('super(Exception, self).__init__('
|
2018-10-21 16:22:46 +03:00
|
|
|
'{}'.format(repr(error.description)))
|
2018-04-14 14:56:39 +03:00
|
|
|
|
|
|
|
if error.has_captures:
|
2018-12-03 13:08:20 +03:00
|
|
|
f.write('.format({0}=self.{0})'.format(error.capture_name))
|
2018-04-14 14:56:39 +03:00
|
|
|
|
2020-02-14 20:22:17 +03:00
|
|
|
f.write(' + self._fmt_request(self.request))\n\n')
|
|
|
|
f.write(' def __reduce__(self):\n ')
|
|
|
|
if error.has_captures:
|
|
|
|
f.write('return type(self), (self.request, self.{})\n'.format(error.capture_name))
|
|
|
|
else:
|
|
|
|
f.write('return type(self), (self.request,)\n')
|
2018-04-14 14:56:39 +03:00
|
|
|
|
|
|
|
# Create the actual {CODE: ErrorClassName} dict once classes are defined
|
2018-06-18 20:09:45 +03:00
|
|
|
f.write('\n\nrpc_errors_dict = {\n')
|
|
|
|
for error in exact_match:
|
2018-04-14 14:56:39 +03:00
|
|
|
f.write(' {}: {},\n'.format(repr(error.pattern), error.name))
|
2018-06-18 20:09:45 +03:00
|
|
|
f.write('}\n\nrpc_errors_re = (\n')
|
|
|
|
for error in regex_match:
|
|
|
|
f.write(' ({}, {}),\n'.format(repr(error.pattern), error.name))
|
|
|
|
f.write(')\n')
|