|
25 | 25 | # CLI python script interface for ipwhois.IPWhois lookups.
|
26 | 26 |
|
27 | 27 | import argparse
|
| 28 | +from collections import namedtuple |
28 | 29 | import json
|
29 | 30 | from os import path
|
30 | 31 | from ipwhois import IPWhois
|
|
319 | 320 | CUR_DIR = path.dirname(__file__)
|
320 | 321 |
|
321 | 322 |
|
| 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 | + |
322 | 360 | def generate_output(line='0', short=None, name=None, value=None,
|
323 | 361 | is_parent=False, colorize=True):
|
324 | 362 | """
|
@@ -668,14 +706,16 @@ def generate_output_events(self, source, key, val, line='2', hr=True,
|
668 | 706 | str: The generated output.
|
669 | 707 | """
|
670 | 708 |
|
| 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 | + |
671 | 713 | output = generate_output(
|
672 | 714 | 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, |
679 | 719 | colorize=colorize
|
680 | 720 | )
|
681 | 721 |
|
@@ -764,14 +804,16 @@ def generate_output_list(self, source, key, val, line='2', hr=True,
|
764 | 804 | str: The generated output.
|
765 | 805 | """
|
766 | 806 |
|
| 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 | + |
767 | 811 | output = generate_output(
|
768 | 812 | 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, |
775 | 817 | colorize=colorize
|
776 | 818 | )
|
777 | 819 |
|
@@ -809,14 +851,16 @@ def generate_output_notices(self, source, key, val, line='1', hr=True,
|
809 | 851 | str: The generated output.
|
810 | 852 | """
|
811 | 853 |
|
| 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 | + |
812 | 858 | output = generate_output(
|
813 | 859 | 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, |
820 | 864 | colorize=colorize
|
821 | 865 | )
|
822 | 866 |
|
|
0 commit comments