Page MenuHomec4science

utils.py
No OneTemporary

File Metadata

Created
Tue, Jun 4, 06:51

utils.py

# -*- coding: utf-8 -*-
import logging
import getpass
from . import colored
from phabricator import Phabricator
__author__ = "Nicolas Richart"
__copyright__ = "Copyright (C) 2016, EPFL (Ecole Polytechnique Fédérale " \
"de Lausanne) - SCITAS (Scientific IT and Application " \
"Support)"
__credits__ = ["Nicolas Richart"]
__license__ = "BSD"
__version__ = "0.1"
__maintainer__ = "Nicolas Richart"
__email__ = "nicolas.richart@epfl.ch"
_logger = logging.getLogger(__name__)
def ask_question(question, possible_answer=None, default='y'):
if possible_answer is None:
possible_answer = {'y': True, 'n': False}
answers = '/'.join([k if not k == default else k.upper()
for k in possible_answer.keys()])
answer = None
while answer not in possible_answer.keys():
answer = input('{0} ({1})? '.format(question, answers))
answer = answer.lower()
if answer == '':
return possible_answer[default]
return possible_answer[answer]
def get_password(service, username, keyring=None, **kwargs):
service = 'getmystuph/' + service
if keyring:
_print_service = colored('{0}@{1}'.format(username, service), 'blue')
_logger.debug('Try to retrieve password from keyring \'{0}\''.format(_print_service)) # noqa: E501
keyring_passwd = keyring.get_password(service, username)
if keyring_passwd is None:
_logger.debug('Password for \'{0}\' not in keyring'.format(_print_service)) # noqa: E501
keyring_passwd = getpass.getpass(
"Password for {0}@{1}: ".format(username, service))
store = ask_question("Do you want to store your password " +
"in the system keyring ?")
if store:
_logger.debug('Adding password for \'{0}\' in keyring'.format(_print_service)) # noqa: E501
keyring.set_password(service, username, keyring_passwd)
else:
_logger.warning('To avoid this message to reappear, ' +
'remove the \'use_keyring\' from the ' +
'configuration file.')
else:
_logger.debug('Password for \'{0}\' found in keyring'.format(_print_service)) # noqa: E501
return keyring_passwd
else:
getpass_passwd = getpass.getpass(
"Password for {0}@{1}: ".format(username, service))
return getpass_passwd
def get_phabricator_instance(host=None, username=None, token=None):
_phab = None
try:
_phab = Phabricator(host=host,
username=username,
token=token)
_phab.update_interfaces()
# this request is just to make an actual connection
_phab.user.whoami()
except Exception as e:
_logger.error(
'Could not connect to phabricator, either give the' +
' connection with the default configuration of arc' +
' or in the backend configuration of the configuration' +
' file:\n' +
' in/out:\n' +
' username: mylogin\n' +
' host: https://c4science.ch/\n' +
' token: cli-g3amff25kdpnnv2tqvigmr4omnn7\n')
raise e
return _phab

Event Timeline