Skip to content

Commit

Permalink
Fixed code duplication in ipwhois_cli.py -- partial (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
secynic committed Aug 3, 2017
1 parent 34c7245 commit 912bbbb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 66 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.1.0 (TBD)
-----------

- Fixed code duplication in ipwhois_cli.py -- partial (#181)

1.0.0 (2017-07-30)
------------------

Expand Down
169 changes: 103 additions & 66 deletions ipwhois/scripts/ipwhois_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 912bbbb

Please sign in to comment.