Skip to content

Commit 4efc7b0

Browse files
committed
Fixed RDAP output code duplication in ipwhois_cli.py (#181)
1 parent 9e338d1 commit 4efc7b0

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

ipwhois/scripts/ipwhois_cli.py

+62-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# CLI python script interface for ipwhois.IPWhois lookups.
2626

2727
import argparse
28+
from collections import namedtuple
2829
import json
2930
from os import path
3031
from ipwhois import IPWhois
@@ -319,6 +320,43 @@
319320
CUR_DIR = path.dirname(__file__)
320321

321322

323+
def output_rdap_generic(source, key, val, hr=True, show_name=False):
324+
"""
325+
The function processing and returning common RDAP output values.
326+
327+
Args:
328+
source (:obj:`dict`): The event source (required).
329+
key (:obj:`str`): The event data key (required).
330+
val (:obj:`dict`): The event data value (required).
331+
hr (:obj:`bool`): Enable human readable key translations. Defaults
332+
to True.
333+
show_name (:obj:`bool`): Show human readable name (default is to
334+
only show short). Defaults to False.
335+
336+
Returns:
337+
namedtuple:
338+
339+
:short (:obj:`str`): The abbreviated name for a field. See hr.py for
340+
values.
341+
:name (:obj:`str`): The name for a field. See hr.py for values.
342+
:is_parent (:obj:`bool`):
343+
:result (:obj:`str`): The result value.
344+
"""
345+
346+
short = HR_RDAP[source][key]['_short'] if hr else key,
347+
name = HR_RDAP[source][key]['_name'] if (hr and show_name) else None,
348+
is_parent = False if (val is None or
349+
len(val) == 0) else True,
350+
result = 'None' if (val is None or
351+
len(val) == 0) else val,
352+
353+
# Initialize the results named tuple
354+
output = namedtuple('generate_output_rdap_generic',
355+
'short, name, is_parent, result')
356+
357+
return output(short, name, is_parent, result)
358+
359+
322360
def generate_output(line='0', short=None, name=None, value=None,
323361
is_parent=False, colorize=True):
324362
"""
@@ -668,14 +706,16 @@ def generate_output_events(self, source, key, val, line='2', hr=True,
668706
str: The generated output.
669707
"""
670708

709+
short_eval, name_eval, is_parent, value_eval = output_rdap_generic(
710+
source=source, key=key, val=val, hr=hr, show_name=show_name
711+
)
712+
671713
output = generate_output(
672714
line=line,
673-
short=HR_RDAP[source][key]['_short'] if hr else key,
674-
name=HR_RDAP[source][key]['_name'] if (hr and show_name) else None,
675-
is_parent=False if (val is None or
676-
len(val) == 0) else True,
677-
value='None' if (val is None or
678-
len(val) == 0) else None,
715+
short=short_eval,
716+
name=name_eval,
717+
value=value_eval,
718+
is_parent=is_parent,
679719
colorize=colorize
680720
)
681721

@@ -764,14 +804,16 @@ def generate_output_list(self, source, key, val, line='2', hr=True,
764804
str: The generated output.
765805
"""
766806

807+
short_eval, name_eval, is_parent, value_eval = output_rdap_generic(
808+
source=source, key=key, val=val, hr=hr, show_name=show_name
809+
)
810+
767811
output = generate_output(
768812
line=line,
769-
short=HR_RDAP[source][key]['_short'] if hr else key,
770-
name=HR_RDAP[source][key]['_name'] if (hr and show_name) else None,
771-
is_parent=False if (val is None or
772-
len(val) == 0) else True,
773-
value='None' if (val is None or
774-
len(val) == 0) else None,
813+
short=short_eval,
814+
name=name_eval,
815+
value=value_eval,
816+
is_parent=is_parent,
775817
colorize=colorize
776818
)
777819

@@ -809,14 +851,16 @@ def generate_output_notices(self, source, key, val, line='1', hr=True,
809851
str: The generated output.
810852
"""
811853

854+
short_eval, name_eval, is_parent, value_eval = output_rdap_generic(
855+
source=source, key=key, val=val, hr=hr, show_name=show_name
856+
)
857+
812858
output = generate_output(
813859
line=line,
814-
short=HR_RDAP[source][key]['_short'] if hr else key,
815-
name=HR_RDAP[source][key]['_name'] if (hr and show_name) else None,
816-
is_parent=False if (val is None or
817-
len(val) == 0) else True,
818-
value='None' if (val is None or
819-
len(val) == 0) else None,
860+
short=short_eval,
861+
name=name_eval,
862+
value=value_eval,
863+
is_parent=is_parent,
820864
colorize=colorize
821865
)
822866

0 commit comments

Comments
 (0)