Skip to content

Commit

Permalink
Fixed code duplication in ipwhois_utils_cli.py -- partial (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
secynic committed Aug 4, 2017
1 parent 912bbbb commit 9e338d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
1.1.0 (TBD)
-----------

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

1.0.0 (2017-07-30)
------------------
Expand Down
81 changes: 39 additions & 42 deletions ipwhois/scripts/ipwhois_utils_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@
# Get the args
script_args = parser.parse_args()


def generate_output_is_defined(result_list=''):
"""
The function for formatting CLI output results.
Args:
result_list (:obj:`list`): The ipv*_is_defined result to output.
Returns:
str: The generated output.
"""

try:

if result:

output = ('{0}{1} is defined{2}:\n{3}'.format(
ANSI['green'] if script_args.colorize else '',
result_list,
ANSI['end'] if script_args.colorize else '',
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
))

else:

output = ('{0}{1} is not defined{2}'.format(
ANSI['yellow'] if script_args.colorize else '',
result_list,
ANSI['end'] if script_args.colorize else ''
))

except Exception as e:

output = ('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))

return output

if script_args.ipv4_lstrip_zeros:

print(ipv4_lstrip_zeros(address=script_args.ipv4_lstrip_zeros[0]))
Expand Down Expand Up @@ -178,51 +215,11 @@

elif script_args.ipv4_is_defined:

try:

result = ipv4_is_defined(address=script_args.ipv4_is_defined[0])

if result[0]:
print('{0}{1} is defined{2}:\n{3}'.format(
ANSI['green'] if script_args.colorize else '',
script_args.ipv4_is_defined[0],
ANSI['end'] if script_args.colorize else '',
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
))
else:
print('{0}{1} is not defined{2}'.format(
ANSI['yellow'] if script_args.colorize else '',
script_args.ipv4_is_defined[0],
ANSI['end'] if script_args.colorize else ''
))

except Exception as e:

print('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))
generate_output_is_defined(result_list=script_args.ipv4_is_defined[0])

elif script_args.ipv6_is_defined:

try:

result = ipv6_is_defined(address=script_args.ipv6_is_defined[0])

if result[0]:
print('{0}{1} is defined{2}:\n{3}'.format(
ANSI['green'] if script_args.colorize else '',
script_args.ipv6_is_defined[0],
ANSI['end'] if script_args.colorize else '',
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
))
else:
print('{0}{1} is not defined{2}'.format(
ANSI['yellow'] if script_args.colorize else '',
script_args.ipv6_is_defined[0],
ANSI['end'] if script_args.colorize else ''
))

except Exception as e:

print('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))
generate_output_is_defined(result_list=script_args.ipv6_is_defined[0])

elif script_args.unique_everseen:

Expand Down

0 comments on commit 9e338d1

Please sign in to comment.