From 912bbbb866053fc91792d71d9f07f294c59eba14 Mon Sep 17 00:00:00 2001 From: secynic Date: Wed, 2 Aug 2017 22:17:56 -0500 Subject: [PATCH] Fixed code duplication in ipwhois_cli.py -- partial (#181) --- CHANGES.rst | 5 + ipwhois/scripts/ipwhois_cli.py | 169 ++++++++++++++++++++------------- 2 files changed, 108 insertions(+), 66 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3c41f8e..e093784 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ Changelog ========= +1.1.0 (TBD) +----------- + +- Fixed code duplication in ipwhois_cli.py -- partial (#181) + 1.0.0 (2017-07-30) ------------------ diff --git a/ipwhois/scripts/ipwhois_cli.py b/ipwhois/scripts/ipwhois_cli.py index faf9a5b..fde0d72 100644 --- a/ipwhois/scripts/ipwhois_cli.py +++ b/ipwhois/scripts/ipwhois_cli.py @@ -359,6 +359,101 @@ def generate_output(line='0', short=None, name=None, value=None, return output +def is_multiline(value=None): + """ + The function for determining if a string has multiple lines. + + Args: + value (:obj:`str`): The value to check. + + Returns: + bool: True if line endings are detected, otherwise False. + """ + + return True if value and '\n' in value else False + + +def output_multiline(value=None, line='0', colorize=True): + """ + The function for providing output for strings with multiple lines. + + Args: + value (:obj:`str`): The value to output. + line (:obj:`str`): The line number (0-4). Determines indentation. + Defaults to '0'. + colorize (:obj:`bool`): Colorize the console output with ANSI colors. + Defaults to True. + + Returns: + str: The generated output. + """ + + output = '' + for v in value.split('\n'): + + output += generate_output( + line=line, + value=v, + colorize=colorize + ) + + return output + + +def output_whois_generic(value=None, line='0', hr=True, show_name=False, + colorize=True): + """ + The function for providing output for strings with multiple lines. + + Args: + value (:obj:`dict`): The event dictionary (required). + line (:obj:`str`): The line number (0-4). Determines indentation. + Defaults to '0'. + hr (:obj:`bool`): Enable human readable key translations. Defaults + to True. + show_name (:obj:`bool`): Show human readable name (default is to + only show short). Defaults to False. + colorize (:obj:`bool`): Colorize the console output with ANSI + colors. Defaults to True. + + Returns: + str: The generated output. + """ + + output = '' + for key, val in value.items(): + + if is_multiline(value=val): + + output += generate_output( + line=line, + short=HR_WHOIS['nets'][key]['_short'] if hr else key, + name=HR_WHOIS['nets'][key]['_name'] if ( + hr and show_name) else None, + is_parent=False if (val is None or + len(val) == 0) else True, + value='None' if (val is None or + len(val) == 0) else None, + colorize=colorize + ) + + output += output_multiline( + value=val, line=int(line)+1, colorize=colorize) + + else: + + output += generate_output( + line=line, + short=HR_WHOIS['nets'][key]['_short'] if hr else key, + name=HR_WHOIS['nets'][key]['_name'] if ( + hr and show_name) else None, + value=val, + colorize=colorize + ) + + return output + + class IPWhoisCLI: """ The CLI wrapper class for outputting formatted IPWhois results. @@ -1145,39 +1240,10 @@ def generate_output_whois_nets(self, json_data=None, hr=True, colorize=colorize ) - for key, val in net.items(): - - if val and '\n' in val: - - output += generate_output( - line='2', - short=HR_WHOIS['nets'][key]['_short'] if hr else key, - name=HR_WHOIS['nets'][key]['_name'] if ( - hr and show_name) else None, - is_parent=False if (val is None or - len(val) == 0) else True, - value='None' if (val is None or - len(val) == 0) else None, - colorize=colorize - ) - - for v in val.split('\n'): - output += generate_output( - line='3', - value=v, - colorize=colorize - ) - - else: - - output += generate_output( - line='2', - short=HR_WHOIS['nets'][key]['_short'] if hr else key, - name=HR_WHOIS['nets'][key]['_name'] if ( - hr and show_name) else None, - value=val, - colorize=colorize - ) + output += output_whois_generic( + value=net, line='2', hr=hr, show_name=show_name, + colorize=colorize + ) return output @@ -1213,39 +1279,10 @@ def generate_output_whois_referral(self, json_data=None, hr=True, if json_data['referral']: - for key, val in json_data['referral'].items(): - - if val and '\n' in val: - - output += generate_output( - line='1', - short=HR_WHOIS['nets'][key]['_short'] if hr else key, - name=HR_WHOIS['nets'][key]['_name'] if ( - hr and show_name) else None, - is_parent=False if (val is None or - len(val) == 0) else True, - value='None' if (val is None or - len(val) == 0) else None, - colorize=colorize - ) - - for v in val.split('\n'): - output += generate_output( - line='2', - value=v, - colorize=colorize - ) - - else: - - output += generate_output( - line='1', - short=HR_WHOIS['nets'][key]['_short'] if hr else key, - name=HR_WHOIS['nets'][key]['_name'] if ( - hr and show_name) else None, - value=val, - colorize=colorize - ) + output += output_whois_generic( + value=json_data['referral'], line='1', hr=hr, + show_name=show_name, colorize=colorize + ) return output