1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#!/usr/bin/env python3
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
from importlib import import_module
import os
import sys
enum_names = [
'dns.dnssec.Algorithm',
'dns.edns.OptionType',
'dns.flags.Flag',
'dns.flags.EDNSFlag',
'dns.message.MessageSection',
'dns.opcode.Opcode',
'dns.rcode.Rcode',
'dns.rdataclass.RdataClass',
'dns.rdatatype.RdataType',
'dns.rdtypes.dnskeybase.Flag',
'dns.update.UpdateSection',
]
def generate():
for enum_name in enum_names:
dot = enum_name.rindex('.')
module_name = enum_name[:dot]
type_name = enum_name[dot + 1:]
lname = type_name.lower()
mod = import_module(module_name)
enum = getattr(mod, type_name)
filename = module_name.replace('.', '/') + '.py'
new_filename = filename + '.new'
with open(filename) as f:
with open(new_filename, 'w') as nf:
lines = f.readlines()
found = False
i = 0
length = len(lines)
while i < length:
l = lines[i].rstrip()
i += 1
if l.startswith(f'### BEGIN generated {type_name} ' +
'constants') or \
l.startswith(f'### BEGIN generated {lname} constants'):
found = True
break
else:
print(l, file=nf)
if found:
found = False
while i < length:
l = lines[i].rstrip()
i += 1
if l.startswith(f'### END generated {type_name} ' +
'constants') or \
l.startswith(f'### END generated {lname} constants'):
found = True
break
if not found:
print(f'Found begin marker for {type_name} but did ' +
'not find end marker!', file=sys.stderr)
sys.exit(1)
if not found:
print('', file=nf)
print(f'### BEGIN generated {type_name} constants', file=nf)
print('', file=nf)
# We have to use __members__.items() and not "in enum" because
# otherwise we miss values that have multiple names (e.g. NONE
# and TYPE0 for rdatatypes).
for name, value in enum.__members__.items():
print(f'{name} = {type_name}.{name}', file=nf)
print('', file=nf)
print(f'### END generated {type_name} constants', file=nf)
# Copy remaining lines (if any)
while i < length:
l = lines[i].rstrip()
i += 1
print(l, file=nf)
os.rename(new_filename, filename)
def main():
generate()
if __name__ == '__main__':
main()
|