-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
some refactorings to reduce cyclomatic complexity #25
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,10 @@ | |
|
||
from noipy import utils | ||
|
||
|
||
from noipy import dnsupdater | ||
from noipy import authinfo | ||
from noipy import __version__ | ||
|
||
|
||
EXECUTION_RESULT_OK = 0 | ||
EXECUTION_RESULT_NOK = 1 | ||
|
||
|
@@ -34,6 +32,67 @@ | |
r'(?:/?|[/?]\S+)$', re.IGNORECASE) | ||
|
||
|
||
def auth_credentials(user_token, password, provider_class): | ||
if provider_class.auth_type == 'T': | ||
user_arg = user_token or utils.read_input( | ||
"Paste your auth token: ") | ||
auth = authinfo.ApiAuth(usertoken=user_arg) | ||
else: | ||
user_arg = user_token or utils.read_input( | ||
"Type your username: ") | ||
pass_arg = password or getpass.getpass("Type your password: ") | ||
auth = authinfo.ApiAuth(user_arg, pass_arg) | ||
|
||
return auth | ||
|
||
|
||
def execute_ddns_update(update_ddns, ip, hostname, auth, | ||
updater_options, provider, provider_class, | ||
exec_result, process_message=None): | ||
response_code = None | ||
response_text = None | ||
if update_ddns: | ||
ip_address = ip if ip else utils.get_ip() | ||
if not ip_address: | ||
process_message = "Unable to get IP address. Check connection." | ||
exec_result = EXECUTION_RESULT_NOK | ||
elif ip_address == utils.get_dns_ip(hostname): | ||
process_message = "No update required." | ||
else: | ||
updater = provider_class(auth, hostname, updater_options) | ||
print("Updating hostname '%s' with IP address %s " | ||
"[provider: '%s']..." | ||
% (hostname, ip_address, provider)) | ||
response_code, response_text = updater.update_dns(ip_address) | ||
process_message = updater.status_message | ||
|
||
return { | ||
'exec_result': exec_result, | ||
'response_code': response_code, | ||
'response_text': response_text, | ||
'process_message': process_message, | ||
} | ||
|
||
|
||
def process_generic(url, updater_options, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issues of 77-79: Function `process_generic` has 5 arguments (exceeds 4 allowed). Consider refactoring. [structure] |
||
update_ddns, | ||
exec_result, process_message): | ||
if url: | ||
if not URL_RE.match(url): | ||
process_message = "Malformed URL." | ||
exec_result = EXECUTION_RESULT_NOK | ||
update_ddns = False | ||
else: | ||
updater_options['url'] = url | ||
else: | ||
process_message = "Must use --url if --provider is 'generic' " \ | ||
"(default)" | ||
exec_result = EXECUTION_RESULT_NOK | ||
update_ddns = False | ||
|
||
return update_ddns, exec_result, process_message, updater_options | ||
|
||
|
||
def execute_update(args): | ||
"""Execute the update based on command line args and returns a dictionary | ||
with 'execution result, ''response code', 'response info' and | ||
|
@@ -47,16 +106,7 @@ def execute_update(args): | |
auth = None | ||
|
||
if args.store: # --store argument | ||
if provider_class.auth_type == 'T': | ||
user_arg = args.usertoken or utils.read_input( | ||
"Paste your auth token: ") | ||
auth = authinfo.ApiAuth(usertoken=user_arg) | ||
else: | ||
user_arg = args.usertoken or utils.read_input( | ||
"Type your username: ") | ||
pass_arg = args.password or getpass.getpass("Type your password: ") | ||
auth = authinfo.ApiAuth(user_arg, pass_arg) | ||
|
||
auth = auth_credentials(args.usertoken, args.password, provider_class) | ||
authinfo.store(auth, args.provider, args.config) | ||
exec_result = EXECUTION_RESULT_OK | ||
if not args.hostname: | ||
|
@@ -67,10 +117,7 @@ def execute_update(args): | |
|
||
# informations arguments | ||
elif args.usertoken and args.hostname: | ||
if provider_class.auth_type == 'T': | ||
auth = authinfo.ApiAuth(args.usertoken) | ||
else: | ||
auth = authinfo.ApiAuth(args.usertoken, args.password) | ||
auth = auth_credentials(args.usertoken, args.password, provider_class) | ||
update_ddns = True | ||
exec_result = EXECUTION_RESULT_OK | ||
elif args.hostname: | ||
|
@@ -92,42 +139,19 @@ def execute_update(args): | |
"option.\nExecute noipy --help for more details." | ||
|
||
if update_ddns and args.provider == 'generic': | ||
if args.url: | ||
if not URL_RE.match(args.url): | ||
process_message = "Malformed URL." | ||
exec_result = EXECUTION_RESULT_NOK | ||
update_ddns = False | ||
else: | ||
updater_options['url'] = args.url | ||
else: | ||
process_message = "Must use --url if --provider is 'generic' " \ | ||
"(default)" | ||
exec_result = EXECUTION_RESULT_NOK | ||
update_ddns = False | ||
|
||
response_code = None | ||
response_text = None | ||
if update_ddns: | ||
ip_address = args.ip if args.ip else utils.get_ip() | ||
if not ip_address: | ||
process_message = "Unable to get IP address. Check connection." | ||
exec_result = EXECUTION_RESULT_NOK | ||
elif ip_address == utils.get_dns_ip(args.hostname): | ||
process_message = "No update required." | ||
else: | ||
updater = provider_class(auth, args.hostname, updater_options) | ||
print("Updating hostname '%s' with IP address %s " | ||
"[provider: '%s']..." | ||
% (args.hostname, ip_address, args.provider)) | ||
response_code, response_text = updater.update_dns(ip_address) | ||
process_message = updater.status_message | ||
|
||
proc_result = { | ||
'exec_result': exec_result, | ||
'response_code': response_code, | ||
'response_text': response_text, | ||
'process_message': process_message, | ||
} | ||
result = process_generic( | ||
args.url, updater_options, update_ddns, | ||
exec_result, process_message) | ||
|
||
update_ddns = result[0] | ||
exec_result = result[1] | ||
process_message = result[2] | ||
updater_options = result[3] | ||
|
||
proc_result = execute_ddns_update(update_ddns, args.ip, args.hostname, | ||
auth, updater_options, args.provider, | ||
provider_class, exec_result, | ||
process_message) | ||
|
||
return proc_result | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This functions has too many parameters and still has some Cognitive Complexity.
Maybe try a different approach here.