mirror of
https://gitlab.nic.cz/knot/knot-dns.git
synced 2026-02-03 18:49:28 -05:00
samples/probe_dump.py: use python idiomatic formatting
This commit is contained in:
parent
1729a53709
commit
863f32d8f0
1 changed files with 25 additions and 20 deletions
|
|
@ -12,6 +12,7 @@ import libknot
|
|||
import libknot.probe
|
||||
import sys
|
||||
|
||||
|
||||
def extract_dns_header(probe, fieldname):
|
||||
val = getattr(probe, fieldname)
|
||||
return {
|
||||
|
|
@ -32,25 +33,28 @@ def extract_dns_header(probe, fieldname):
|
|||
"rcode": val.rcode,
|
||||
}
|
||||
|
||||
|
||||
def extract_addr(probe, fieldname):
|
||||
val = getattr(probe, fieldname)
|
||||
return probe.addr_str(val)
|
||||
|
||||
|
||||
def extract_safe(probe, fieldname):
|
||||
convert_func = getattr
|
||||
try:
|
||||
convert_func = {
|
||||
'local_addr': extract_addr,
|
||||
'remote_addr': extract_addr,
|
||||
'query_hdr': extract_dns_header,
|
||||
'reply_hdr': extract_dns_header,
|
||||
'query_name': lambda probe, _: probe.qname_str(),
|
||||
"local_addr": extract_addr,
|
||||
"remote_addr": extract_addr,
|
||||
"query_hdr": extract_dns_header,
|
||||
"reply_hdr": extract_dns_header,
|
||||
"query_name": lambda probe, _: probe.qname_str(),
|
||||
}[fieldname]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
return convert_func(probe, fieldname)
|
||||
|
||||
|
||||
def convert_json(probe):
|
||||
probe_as_dict = {}
|
||||
for field in probe._fields_:
|
||||
|
|
@ -61,6 +65,7 @@ def convert_json(probe):
|
|||
probe_as_dict[fieldname] = extract_safe(probe, fieldname)
|
||||
return probe_as_dict
|
||||
|
||||
|
||||
def probe_loop(args):
|
||||
try:
|
||||
libknot.Knot(args.libknot_path)
|
||||
|
|
@ -72,7 +77,7 @@ def probe_loop(args):
|
|||
data = libknot.probe.KnotProbeDataArray(8)
|
||||
|
||||
try:
|
||||
while (True):
|
||||
while True:
|
||||
if probe.consume(data, 1000) > 0:
|
||||
for item in data:
|
||||
if args.json:
|
||||
|
|
|
|||
Loading…
Reference in a new issue