From: Bob Halley Date: Mon, 3 Aug 2020 13:35:22 +0000 (-0700) Subject: add constants X-Git-Tag: v2.1.0rc1~112^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82bc428366df9694ce0b4cfb7d4503e9d0c38248;p=thirdparty%2Fdnspython.git add constants --- diff --git a/dns/dnssec.py b/dns/dnssec.py index 89199966..79752aae 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -584,3 +584,25 @@ else: validate = _validate # type: ignore validate_rrsig = _validate_rrsig # type: ignore _have_pyca = True + +### BEGIN generated algorithm constants + +RSAMD5 = Algorithm.RSAMD5 +DH = Algorithm.DH +DSA = Algorithm.DSA +ECC = Algorithm.ECC +RSASHA1 = Algorithm.RSASHA1 +DSANSEC3SHA1 = Algorithm.DSANSEC3SHA1 +RSASHA1NSEC3SHA1 = Algorithm.RSASHA1NSEC3SHA1 +RSASHA256 = Algorithm.RSASHA256 +RSASHA512 = Algorithm.RSASHA512 +ECCGOST = Algorithm.ECCGOST +ECDSAP256SHA256 = Algorithm.ECDSAP256SHA256 +ECDSAP384SHA384 = Algorithm.ECDSAP384SHA384 +ED25519 = Algorithm.ED25519 +ED448 = Algorithm.ED448 +INDIRECT = Algorithm.INDIRECT +PRIVATEDNS = Algorithm.PRIVATEDNS +PRIVATEOID = Algorithm.PRIVATEOID + +### END generated algorithm constants diff --git a/dns/edns.py b/dns/edns.py index 087592b5..75ecdf18 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -352,3 +352,18 @@ def register_type(implementation, otype): """ _type_to_class[otype] = implementation + +### BEGIN generated optiontype constants + +NSID = OptionType.NSID +DAU = OptionType.DAU +DHU = OptionType.DHU +N3U = OptionType.N3U +ECS = OptionType.ECS +EXPIRE = OptionType.EXPIRE +COOKIE = OptionType.COOKIE +KEEPALIVE = OptionType.KEEPALIVE +PADDING = OptionType.PADDING +CHAIN = OptionType.CHAIN + +### END generated optiontype constants diff --git a/dns/flags.py b/dns/flags.py index 4eb6d90c..a53197c7 100644 --- a/dns/flags.py +++ b/dns/flags.py @@ -104,3 +104,21 @@ def edns_to_text(flags): """ return _to_text(flags, EDNSFlag) + +### BEGIN generated flag constants + +QR = Flag.QR +AA = Flag.AA +TC = Flag.TC +RD = Flag.RD +RA = Flag.RA +AD = Flag.AD +CD = Flag.CD + +### END generated flag constants + +### BEGIN generated ednsflag constants + +DO = EDNSFlag.DO + +### END generated ednsflag constants diff --git a/dns/message.py b/dns/message.py index 1dfb0280..1a5787f7 100644 --- a/dns/message.py +++ b/dns/message.py @@ -1465,3 +1465,12 @@ def make_response(query, recursion_available=False, our_payload=8192, tsig_error, b'', query.keyalgorithm) response.request_mac = query.mac return response + +### BEGIN generated messagesection constants + +QUESTION = MessageSection.QUESTION +ANSWER = MessageSection.ANSWER +AUTHORITY = MessageSection.AUTHORITY +ADDITIONAL = MessageSection.ADDITIONAL + +### END generated messagesection constants diff --git a/dns/opcode.py b/dns/opcode.py index 5a76326a..32f209fa 100644 --- a/dns/opcode.py +++ b/dns/opcode.py @@ -105,3 +105,13 @@ def is_update(flags): """ return from_flags(flags) == Opcode.UPDATE + +### BEGIN generated opcode constants + +QUERY = Opcode.QUERY +IQUERY = Opcode.IQUERY +STATUS = Opcode.STATUS +NOTIFY = Opcode.NOTIFY +UPDATE = Opcode.UPDATE + +### END generated opcode constants diff --git a/dns/rcode.py b/dns/rcode.py index 846bf6dc..6e33045b 100644 --- a/dns/rcode.py +++ b/dns/rcode.py @@ -137,3 +137,29 @@ def to_text(value, tsig=False): if tsig and value == Rcode.BADVERS: return 'BADSIG' return Rcode.to_text(value) + +### BEGIN generated rcode constants + +NOERROR = Rcode.NOERROR +FORMERR = Rcode.FORMERR +SERVFAIL = Rcode.SERVFAIL +NXDOMAIN = Rcode.NXDOMAIN +NOTIMP = Rcode.NOTIMP +REFUSED = Rcode.REFUSED +YXDOMAIN = Rcode.YXDOMAIN +YXRRSET = Rcode.YXRRSET +NXRRSET = Rcode.NXRRSET +NOTAUTH = Rcode.NOTAUTH +NOTZONE = Rcode.NOTZONE +DSOTYPENI = Rcode.DSOTYPENI +BADVERS = Rcode.BADVERS +BADSIG = Rcode.BADSIG +BADKEY = Rcode.BADKEY +BADTIME = Rcode.BADTIME +BADMODE = Rcode.BADMODE +BADNAME = Rcode.BADNAME +BADALG = Rcode.BADALG +BADTRUNC = Rcode.BADTRUNC +BADCOOKIE = Rcode.BADCOOKIE + +### END generated rcode constants diff --git a/dns/rdataclass.py b/dns/rdataclass.py index 7943a95a..af0bd997 100644 --- a/dns/rdataclass.py +++ b/dns/rdataclass.py @@ -100,3 +100,17 @@ def is_metaclass(rdclass): if rdclass in _metaclasses: return True return False + +### BEGIN generated rdataclass constants + +RESERVED0 = RdataClass.RESERVED0 +IN = RdataClass.IN +INTERNET = RdataClass.INTERNET +CH = RdataClass.CH +CHAOS = RdataClass.CHAOS +HS = RdataClass.HS +HESIOD = RdataClass.HESIOD +NONE = RdataClass.NONE +ANY = RdataClass.ANY + +### END generated rdataclass constants diff --git a/dns/rdatatype.py b/dns/rdatatype.py index c793d5a0..1abc59c9 100644 --- a/dns/rdatatype.py +++ b/dns/rdatatype.py @@ -219,3 +219,81 @@ def register_type(rdtype, rdtype_text, is_singleton=False): _registered_by_value[rdtype] = rdtype_text if is_singleton: _singletons.add(rdtype) + +### BEGIN generated rdatatype constants + +TYPE0 = RdataType.TYPE0 +NONE = RdataType.NONE +A = RdataType.A +NS = RdataType.NS +MD = RdataType.MD +MF = RdataType.MF +CNAME = RdataType.CNAME +SOA = RdataType.SOA +MB = RdataType.MB +MG = RdataType.MG +MR = RdataType.MR +NULL = RdataType.NULL +WKS = RdataType.WKS +PTR = RdataType.PTR +HINFO = RdataType.HINFO +MINFO = RdataType.MINFO +MX = RdataType.MX +TXT = RdataType.TXT +RP = RdataType.RP +AFSDB = RdataType.AFSDB +X25 = RdataType.X25 +ISDN = RdataType.ISDN +RT = RdataType.RT +NSAP = RdataType.NSAP +NSAP_PTR = RdataType.NSAP_PTR +SIG = RdataType.SIG +KEY = RdataType.KEY +PX = RdataType.PX +GPOS = RdataType.GPOS +AAAA = RdataType.AAAA +LOC = RdataType.LOC +NXT = RdataType.NXT +SRV = RdataType.SRV +NAPTR = RdataType.NAPTR +KX = RdataType.KX +CERT = RdataType.CERT +A6 = RdataType.A6 +DNAME = RdataType.DNAME +OPT = RdataType.OPT +APL = RdataType.APL +DS = RdataType.DS +SSHFP = RdataType.SSHFP +IPSECKEY = RdataType.IPSECKEY +RRSIG = RdataType.RRSIG +NSEC = RdataType.NSEC +DNSKEY = RdataType.DNSKEY +DHCID = RdataType.DHCID +NSEC3 = RdataType.NSEC3 +NSEC3PARAM = RdataType.NSEC3PARAM +TLSA = RdataType.TLSA +HIP = RdataType.HIP +NINFO = RdataType.NINFO +CDS = RdataType.CDS +CDNSKEY = RdataType.CDNSKEY +OPENPGPKEY = RdataType.OPENPGPKEY +CSYNC = RdataType.CSYNC +SPF = RdataType.SPF +UNSPEC = RdataType.UNSPEC +EUI48 = RdataType.EUI48 +EUI64 = RdataType.EUI64 +TKEY = RdataType.TKEY +TSIG = RdataType.TSIG +IXFR = RdataType.IXFR +AXFR = RdataType.AXFR +MAILB = RdataType.MAILB +MAILA = RdataType.MAILA +ANY = RdataType.ANY +URI = RdataType.URI +CAA = RdataType.CAA +AVC = RdataType.AVC +AMTRELAY = RdataType.AMTRELAY +TA = RdataType.TA +DLV = RdataType.DLV + +### END generated rdatatype constants diff --git a/dns/rdtypes/dnskeybase.py b/dns/rdtypes/dnskeybase.py index 0243d6f3..1fac4bdc 100644 --- a/dns/rdtypes/dnskeybase.py +++ b/dns/rdtypes/dnskeybase.py @@ -72,3 +72,11 @@ class DNSKEYBase(dns.rdata.Rdata): key = parser.get_remaining() return cls(rdclass, rdtype, header[0], header[1], header[2], key) + +### BEGIN generated flag constants + +SEP = Flag.SEP +REVOKE = Flag.REVOKE +ZONE = Flag.ZONE + +### END generated flag constants diff --git a/dns/update.py b/dns/update.py index 8e796504..b4cb755a 100644 --- a/dns/update.py +++ b/dns/update.py @@ -310,3 +310,12 @@ class UpdateMessage(dns.message.Message): # backwards compatibility Update = UpdateMessage + +### BEGIN generated updatesection constants + +ZONE = UpdateSection.ZONE +PREREQ = UpdateSection.PREREQ +UPDATE = UpdateSection.UPDATE +ADDITIONAL = UpdateSection.ADDITIONAL + +### END generated updatesection constants diff --git a/util/constants-tool b/util/constants-tool new file mode 100755 index 00000000..baf54b65 --- /dev/null +++ b/util/constants-tool @@ -0,0 +1,112 @@ +#!/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:] + 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'): + 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'): + 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 check(): + ok = True + for enum_name in enum_names: + dot = enum_name.rindex('.') + module_name = enum_name[:dot] + type_name = enum_name[dot + 1:] + mod = import_module(module_name) + enum = getattr(mod, type_name) + for name, value in enum.__members__.items(): + try: + if value != getattr(mod, name): + ok = False + print(f'{name} != {value}', file=sys.stderr) + except Exception: + ok = False + print('exception checking', name, file=sys.stderr) + return ok + +def usage(): + print('usage: constants-tool [generate|check]', file=sys.stderr) + sys.exit(1) + +def main(): + if len(sys.argv) < 2: + usage() + if sys.argv[1] == 'generate': + generate() + elif sys.argv[1] == 'check': + if not check(): + sys.exit(2) + else: + usage() + +if __name__ == '__main__': + main()