Skip to content

Commit 9e338d1

Browse files
committed
Fixed code duplication in ipwhois_utils_cli.py -- partial (#181)
1 parent 912bbbb commit 9e338d1

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

CHANGES.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Changelog
44
1.1.0 (TBD)
55
-----------
66

7-
- Fixed code duplication in ipwhois_cli.py -- partial (#181)
7+
- Fixed code duplication in ipwhois_cli.py, ipwhois_utils_cli.py -- partial
8+
(#181)
89

910
1.0.0 (2017-07-30)
1011
------------------

ipwhois/scripts/ipwhois_utils_cli.py

+39-42
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@
114114
# Get the args
115115
script_args = parser.parse_args()
116116

117+
118+
def generate_output_is_defined(result_list=''):
119+
"""
120+
The function for formatting CLI output results.
121+
122+
Args:
123+
result_list (:obj:`list`): The ipv*_is_defined result to output.
124+
125+
Returns:
126+
str: The generated output.
127+
"""
128+
129+
try:
130+
131+
if result:
132+
133+
output = ('{0}{1} is defined{2}:\n{3}'.format(
134+
ANSI['green'] if script_args.colorize else '',
135+
result_list,
136+
ANSI['end'] if script_args.colorize else '',
137+
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
138+
))
139+
140+
else:
141+
142+
output = ('{0}{1} is not defined{2}'.format(
143+
ANSI['yellow'] if script_args.colorize else '',
144+
result_list,
145+
ANSI['end'] if script_args.colorize else ''
146+
))
147+
148+
except Exception as e:
149+
150+
output = ('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))
151+
152+
return output
153+
117154
if script_args.ipv4_lstrip_zeros:
118155

119156
print(ipv4_lstrip_zeros(address=script_args.ipv4_lstrip_zeros[0]))
@@ -178,51 +215,11 @@
178215

179216
elif script_args.ipv4_is_defined:
180217

181-
try:
182-
183-
result = ipv4_is_defined(address=script_args.ipv4_is_defined[0])
184-
185-
if result[0]:
186-
print('{0}{1} is defined{2}:\n{3}'.format(
187-
ANSI['green'] if script_args.colorize else '',
188-
script_args.ipv4_is_defined[0],
189-
ANSI['end'] if script_args.colorize else '',
190-
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
191-
))
192-
else:
193-
print('{0}{1} is not defined{2}'.format(
194-
ANSI['yellow'] if script_args.colorize else '',
195-
script_args.ipv4_is_defined[0],
196-
ANSI['end'] if script_args.colorize else ''
197-
))
198-
199-
except Exception as e:
200-
201-
print('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))
218+
generate_output_is_defined(result_list=script_args.ipv4_is_defined[0])
202219

203220
elif script_args.ipv6_is_defined:
204221

205-
try:
206-
207-
result = ipv6_is_defined(address=script_args.ipv6_is_defined[0])
208-
209-
if result[0]:
210-
print('{0}{1} is defined{2}:\n{3}'.format(
211-
ANSI['green'] if script_args.colorize else '',
212-
script_args.ipv6_is_defined[0],
213-
ANSI['end'] if script_args.colorize else '',
214-
'Name: {0}\nRFC: {1}'.format(result[1], result[2])
215-
))
216-
else:
217-
print('{0}{1} is not defined{2}'.format(
218-
ANSI['yellow'] if script_args.colorize else '',
219-
script_args.ipv6_is_defined[0],
220-
ANSI['end'] if script_args.colorize else ''
221-
))
222-
223-
except Exception as e:
224-
225-
print('{0}Error{1}: {2}'.format(ANSI['red'], ANSI['end'], str(e)))
222+
generate_output_is_defined(result_list=script_args.ipv6_is_defined[0])
226223

227224
elif script_args.unique_everseen:
228225

0 commit comments

Comments
 (0)