Standardize getting login_hint for create project and oauth request (#325)

* Use RE to validate login_hint email address

http://stackoverflow.com/questions/201323/using-a-regular-expression-to-
validate-an-email-address

Per the W3C HTML5 spec:

* Use simpler RE pattern for email validation
This commit is contained in:
Ross Scroggs
2016-11-11 08:26:17 -08:00
committed by Jay Lee
parent a12ec0ffdc
commit a6a94060c6

View File

@@ -6883,17 +6883,22 @@ def doDelProjects():
except googleapiclient.errors.HttpError: except googleapiclient.errors.HttpError:
pass pass
def doCreateProject(): VALIDEMAIL_PATTERN = re.compile(r'^[^@]+@[^@]+\.[^@]+$')
try:
login_hint = sys.argv[3] def getValidateLoginHint(login_hint):
except IndexError: if login_hint:
login_hint = login_hint.strip()
if VALIDEMAIL_PATTERN.match(login_hint):
return login_hint
while True: while True:
login_hint = raw_input(u'\nWhat is your G Suite admin email address? ') login_hint = raw_input(u'\nWhat is your G Suite admin email address? ').strip()
if login_hint.find(u'@') == -1: if VALIDEMAIL_PATTERN.match(login_hint):
return login_hint
print u'Error: that is not a valid email address' print u'Error: that is not a valid email address'
else:
break def doCreateProject(login_hint=None):
from oauth2client.contrib.dictionary_storage import DictionaryStorage from oauth2client.contrib.dictionary_storage import DictionaryStorage
login_hint = getValidateLoginHint(login_hint)
project_id = u'gam-project' project_id = u'gam-project'
for i in range(3): for i in range(3):
project_id += u'-%s' % ''.join(random.choice(string.digits + string.ascii_lowercase) for i in range(3)) project_id += u'-%s' % ''.join(random.choice(string.digits + string.ascii_lowercase) for i in range(3))
@@ -10094,13 +10099,7 @@ See this site for instructions:
print u'ERROR: the format of your client secrets file:\n\n%s\n\n is incorrect. Please recreate the file.' print u'ERROR: the format of your client secrets file:\n\n%s\n\n is incorrect. Please recreate the file.'
sys.exit(3) sys.exit(3)
if not login_hint or login_hint.find(u'@') == -1: login_hint = getValidateLoginHint(login_hint)
while True:
login_hint = raw_input(u'\nWhat is your G Suite admin email address? ')
if login_hint.find(u'@') == -1:
print u'Error: that is not a valid email address'
else:
break
num_scopes = len(OAUTH2_SCOPES) num_scopes = len(OAUTH2_SCOPES)
menu = OAUTH2_MENU % tuple(range(num_scopes)) menu = OAUTH2_MENU % tuple(range(num_scopes))
selected_scopes = [] selected_scopes = []
@@ -10355,7 +10354,11 @@ def ProcessGAMCommand(args):
elif argument in [u'guardianinvite', u'inviteguardian', u'guardian']: elif argument in [u'guardianinvite', u'inviteguardian', u'guardian']:
doInviteGuardian() doInviteGuardian()
elif argument in [u'project', u'apiproject']: elif argument in [u'project', u'apiproject']:
doCreateProject() try:
login_hint = sys.argv[3]
except IndexError:
login_hint = None
doCreateProject(login_hint)
else: else:
print u'ERROR: %s is not a valid argument for "gam create"' % argument print u'ERROR: %s is not a valid argument for "gam create"' % argument
sys.exit(2) sys.exit(2)