Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Prompt user to reenter credentials when create napps upload fails and KeyboardInterrupt handled on create_napp() #284

Closed
wants to merge 8 commits into from
15 changes: 15 additions & 0 deletions kytos/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def authenticate(self):
username = self.config.get('auth', 'user')
password = getpass("Enter the password for {}: ".format(username))
response = requests.get(endpoint, auth=(username, password))
if response.status_code == 401:
invTokenStr = "{\"error\":\"Token not sent or expired: " \
"Signature has expired\"}\n"
# pylint: disable=superfluous-parens
if (invTokenStr == str(response.content, encoding="UTF-8")):
print("Seems the token was not set or is expired!"
"Please run \"kytos napps upload\" again.")
LOG.error(response.content)
LOG.error('ERROR: %s: %s', response.status_code,
response.reason)
print("Press Ctrl+C or CTRL+Z to stop the process.")
user = input("Enter the username: ")
self.config.set('auth', 'user', user)
self.authenticate()
# sys.exit(1)
if response.status_code != 201:
LOG.error(response.content)
LOG.error('ERROR: %s: %s', response.status_code, response.reason)
Expand Down
105 changes: 56 additions & 49 deletions kytos/utils/napps.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,56 +334,63 @@ def create_napp(cls, meta_package=False):
print(' - at least three characters')
print('--------------------------------------------------------------')
print('')
while not cls.valid_name(username):
username = input('Please, insert your NApps Server username: ')

while not cls.valid_name(napp_name):
napp_name = input('Please, insert your NApp name: ')

description = input('Please, insert a brief description for your'
'NApp [optional]: ')
if not description:
# pylint: disable=fixme
description = '# TODO: <<<< Insert your NApp description here >>>>'
# pylint: enable=fixme

context = {'username': username, 'napp': napp_name,
'description': description}

#: Creating the directory structure (username/napp_name)
os.makedirs(username, exist_ok=True)

#: Creating ``__init__.py`` files
with open(os.path.join(username, '__init__.py'), 'w') as init_file:
init_file.write(f'"""Napps for the user {username}.""""')

os.makedirs(os.path.join(username, napp_name))

#: Creating the other files based on the templates
templates = os.listdir(templates_path)
templates.remove('ui')
templates.remove('openapi.yml.template')

if meta_package:
templates.remove('main.py.template')
templates.remove('settings.py.template')

for tmp in templates:
fname = os.path.join(username, napp_name,
tmp.rsplit('.template')[0])
with open(fname, 'w') as file:
content = cls.render_template(templates_path, tmp, context)
file.write(content)

if not meta_package:
NAppsManager.create_ui_structure(username, napp_name,
ui_templates_path, context)
try:

print()
print(f'Congratulations! Your NApp has been bootstrapped!\nNow you '
'can go to the directory {username}/{napp_name} and begin to '
'code your NApp.')
print('Have fun!')
while not cls.valid_name(username):
username = input('Please, insert your NApps Server username: ')

while not cls.valid_name(napp_name):
napp_name = input('Please, insert your NApp name: ')

description = input('Please, insert a brief description for your'
'NApp [optional]: ')
if not description:
# pylint: disable=fixme, pointless-string-statement
description = '# TODO: <<<< Insert your NApp'
'description here >>>>'
# pylint: enable=fixme

context = {'username': username, 'napp': napp_name,
'description': description}

#: Creating the directory structure (username/napp_name)
os.makedirs(username, exist_ok=True)

#: Creating ``__init__.py`` files
with open(os.path.join(username, '__init__.py'), 'w') as init_file:
init_file.write(f'"""Napps for the user {username}.""""')

os.makedirs(os.path.join(username, napp_name))

#: Creating the other files based on the templates
templates = os.listdir(templates_path)
templates.remove('ui')
templates.remove('openapi.yml.template')

if meta_package:
templates.remove('main.py.template')
templates.remove('settings.py.template')

for tmp in templates:
fname = os.path.join(username, napp_name,
tmp.rsplit('.template')[0])
with open(fname, 'w') as file:
content = cls.render_template(templates_path, tmp, context)
file.write(content)

if not meta_package:
NAppsManager.create_ui_structure(username, napp_name,
ui_templates_path, context)

print()
print(f'Congratulations! Your NApp has been'
' bootstrapped!\nNow you can go to the'
' directory {username}/{napp_name} and'
' begin to code your NApp.')
print('Have fun!')
except KeyboardInterrupt:
print("User canceled napps creation.")
sys.exit(0)

@classmethod
def create_ui_structure(cls, username, napp_name, ui_templates_path,
Expand Down