mirror of
https://github.com/GAM-team/GAM.git
synced 2025-07-10 14:43:34 +00:00
utils.py for simple util functions
This commit is contained in:
200
src/gam.py
200
src/gam.py
@ -23,6 +23,7 @@ For more information, see http://git.io/gam
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from var import *
|
from var import *
|
||||||
|
import utils
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
@ -34,7 +35,6 @@ import collections
|
|||||||
import csv
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
from htmlentitydefs import name2codepoint
|
from htmlentitydefs import name2codepoint
|
||||||
from HTMLParser import HTMLParser
|
|
||||||
import json
|
import json
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import platform
|
import platform
|
||||||
@ -78,74 +78,6 @@ Go to the following link in your browser:
|
|||||||
{address}
|
{address}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def convertUTF8(data):
|
|
||||||
if isinstance(data, str):
|
|
||||||
return data
|
|
||||||
if isinstance(data, unicode):
|
|
||||||
if GM_Globals[GM_WINDOWS]:
|
|
||||||
return data
|
|
||||||
return data.encode(GM_Globals[GM_SYS_ENCODING])
|
|
||||||
if isinstance(data, collections.Mapping):
|
|
||||||
return dict(map(convertUTF8, data.iteritems()))
|
|
||||||
if isinstance(data, collections.Iterable):
|
|
||||||
return type(data)(map(convertUTF8, data))
|
|
||||||
return data
|
|
||||||
|
|
||||||
class _DeHTMLParser(HTMLParser):
|
|
||||||
def __init__(self):
|
|
||||||
HTMLParser.__init__(self)
|
|
||||||
self.__text = []
|
|
||||||
|
|
||||||
def handle_data(self, data):
|
|
||||||
self.__text.append(data)
|
|
||||||
|
|
||||||
def handle_charref(self, name):
|
|
||||||
self.__text.append(unichr(int(name[1:], 16)) if name.startswith('x') else unichr(int(name)))
|
|
||||||
|
|
||||||
def handle_entityref(self, name):
|
|
||||||
cp = name2codepoint.get(name)
|
|
||||||
if cp:
|
|
||||||
self.__text.append(unichr(cp))
|
|
||||||
else:
|
|
||||||
self.__text.append(u'&'+name)
|
|
||||||
|
|
||||||
def handle_starttag(self, tag, attrs):
|
|
||||||
if tag == 'p':
|
|
||||||
self.__text.append('\n\n')
|
|
||||||
elif tag == 'br':
|
|
||||||
self.__text.append('\n')
|
|
||||||
elif tag == 'a':
|
|
||||||
for attr in attrs:
|
|
||||||
if attr[0] == 'href':
|
|
||||||
self.__text.append('({0}) '.format(attr[1]))
|
|
||||||
break
|
|
||||||
elif tag == 'div':
|
|
||||||
if not attrs:
|
|
||||||
self.__text.append('\n')
|
|
||||||
elif tag in ['http:', 'https']:
|
|
||||||
self.__text.append(' ({0}//{1}) '.format(tag, attrs[0][0]))
|
|
||||||
|
|
||||||
def handle_startendtag(self, tag, attrs):
|
|
||||||
if tag == 'br':
|
|
||||||
self.__text.append('\n\n')
|
|
||||||
|
|
||||||
def text(self):
|
|
||||||
return re.sub(r'\n{2}\n+', '\n\n', re.sub(r'\n +', '\n', ''.join(self.__text))).strip()
|
|
||||||
|
|
||||||
def dehtml(text):
|
|
||||||
try:
|
|
||||||
parser = _DeHTMLParser()
|
|
||||||
parser.feed(text.encode(u'utf-8'))
|
|
||||||
parser.close()
|
|
||||||
return parser.text()
|
|
||||||
except:
|
|
||||||
from traceback import print_exc
|
|
||||||
print_exc(file=sys.stderr)
|
|
||||||
return text
|
|
||||||
|
|
||||||
def indentMultiLineText(message, n=0):
|
|
||||||
return message.replace(u'\n', u'\n{0}'.format(u' '*n)).rstrip()
|
|
||||||
|
|
||||||
def showUsage():
|
def showUsage():
|
||||||
doGAMVersion(checkForArgs=False)
|
doGAMVersion(checkForArgs=False)
|
||||||
print u'''
|
print u'''
|
||||||
@ -163,26 +95,14 @@ gam.exe update group announcements add member jsmith
|
|||||||
...
|
...
|
||||||
|
|
||||||
'''
|
'''
|
||||||
def formatMaxMessageBytes(maxMessageBytes):
|
|
||||||
if maxMessageBytes < ONE_KILO_BYTES:
|
|
||||||
return maxMessageBytes
|
|
||||||
if maxMessageBytes < ONE_MEGA_BYTES:
|
|
||||||
return u'{0}K'.format(maxMessageBytes / ONE_KILO_BYTES)
|
|
||||||
return u'{0}M'.format(maxMessageBytes / ONE_MEGA_BYTES)
|
|
||||||
|
|
||||||
def formatMilliSeconds(millis):
|
|
||||||
seconds, millis = divmod(millis, 1000)
|
|
||||||
minutes, seconds = divmod(seconds, 60)
|
|
||||||
hours, minutes = divmod(minutes, 60)
|
|
||||||
return u'%02d:%02d:%02d' % (hours, minutes, seconds)
|
|
||||||
#
|
#
|
||||||
# Error handling
|
# Error handling
|
||||||
#
|
#
|
||||||
def stderrErrorMsg(message):
|
def stderrErrorMsg(message):
|
||||||
sys.stderr.write(convertUTF8(u'\n{0}{1}\n'.format(ERROR_PREFIX, message)))
|
sys.stderr.write(utils.convertUTF8(u'\n{0}{1}\n'.format(ERROR_PREFIX, message)))
|
||||||
|
|
||||||
def stderrWarningMsg(message):
|
def stderrWarningMsg(message):
|
||||||
sys.stderr.write(convertUTF8(u'\n{0}{1}\n'.format(WARNING_PREFIX, message)))
|
sys.stderr.write(utils.convertUTF8(u'\n{0}{1}\n'.format(WARNING_PREFIX, message)))
|
||||||
|
|
||||||
def systemErrorExit(sysRC, message):
|
def systemErrorExit(sysRC, message):
|
||||||
if message:
|
if message:
|
||||||
@ -1190,7 +1110,7 @@ def printShowDelegates(users, csvFormat):
|
|||||||
if csvStyle:
|
if csvStyle:
|
||||||
print u'%s,%s,%s' % (user, delegateAddress, status)
|
print u'%s,%s,%s' % (user, delegateAddress, status)
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u"Delegator: %s\n Delegate: %s\n Status: %s\n Delegate Email: %s\n Delegate ID: %s\n" % (user, delegateName, status, delegateAddress, delegationId))
|
print utils.convertUTF8(u"Delegator: %s\n Delegate: %s\n Status: %s\n Delegate Email: %s\n Delegate ID: %s\n" % (user, delegateName, status, delegateAddress, delegationId))
|
||||||
if csvFormat:
|
if csvFormat:
|
||||||
writeCSVfile(csvRows, titles, u'Delegates', todrive)
|
writeCSVfile(csvRows, titles, u'Delegates', todrive)
|
||||||
|
|
||||||
@ -2022,15 +1942,15 @@ def doGetCourseInfo():
|
|||||||
print u' Teachers:'
|
print u' Teachers:'
|
||||||
for teacher in teachers:
|
for teacher in teachers:
|
||||||
try:
|
try:
|
||||||
print convertUTF8(u' %s - %s' % (teacher[u'profile'][u'name'][u'fullName'], teacher[u'profile'][u'emailAddress']))
|
print utils.convertUTF8(u' %s - %s' % (teacher[u'profile'][u'name'][u'fullName'], teacher[u'profile'][u'emailAddress']))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print convertUTF8(u' %s' % teacher[u'profile'][u'name'][u'fullName'])
|
print utils.convertUTF8(u' %s' % teacher[u'profile'][u'name'][u'fullName'])
|
||||||
print u' Students:'
|
print u' Students:'
|
||||||
for student in students:
|
for student in students:
|
||||||
try:
|
try:
|
||||||
print convertUTF8(u' %s - %s' % (student[u'profile'][u'name'][u'fullName'], student[u'profile'][u'emailAddress']))
|
print utils.convertUTF8(u' %s - %s' % (student[u'profile'][u'name'][u'fullName'], student[u'profile'][u'emailAddress']))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print convertUTF8(u' %s' % student[u'profile'][u'name'][u'fullName'])
|
print utils.convertUTF8(u' %s' % student[u'profile'][u'name'][u'fullName'])
|
||||||
|
|
||||||
def doPrintCourses():
|
def doPrintCourses():
|
||||||
croom = buildGAPIObject(u'classroom')
|
croom = buildGAPIObject(u'classroom')
|
||||||
@ -2342,7 +2262,7 @@ def changeCalendarAttendees(users):
|
|||||||
#print u' skipping cancelled event'
|
#print u' skipping cancelled event'
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
event_summary = convertUTF8(event[u'summary'])
|
event_summary = utils.convertUTF8(event[u'summary'])
|
||||||
except (KeyError, UnicodeEncodeError, UnicodeDecodeError):
|
except (KeyError, UnicodeEncodeError, UnicodeDecodeError):
|
||||||
event_summary = event[u'id']
|
event_summary = event[u'id']
|
||||||
try:
|
try:
|
||||||
@ -3249,11 +3169,11 @@ def deletePhoto(users):
|
|||||||
|
|
||||||
def _showCalendar(userCalendar, j, jcount):
|
def _showCalendar(userCalendar, j, jcount):
|
||||||
print u' Calendar: {0} ({1}/{2})'.format(userCalendar[u'id'], j, jcount)
|
print u' Calendar: {0} ({1}/{2})'.format(userCalendar[u'id'], j, jcount)
|
||||||
print convertUTF8(u' Summary: {0}'.format(userCalendar.get(u'summaryOverride', userCalendar[u'summary'])))
|
print utils.convertUTF8(u' Summary: {0}'.format(userCalendar.get(u'summaryOverride', userCalendar[u'summary'])))
|
||||||
print convertUTF8(u' Description: {0}'.format(userCalendar.get(u'description', u'')))
|
print utils.convertUTF8(u' Description: {0}'.format(userCalendar.get(u'description', u'')))
|
||||||
print u' Access Level: {0}'.format(userCalendar[u'accessRole'])
|
print u' Access Level: {0}'.format(userCalendar[u'accessRole'])
|
||||||
print u' Timezone: {0}'.format(userCalendar[u'timeZone'])
|
print u' Timezone: {0}'.format(userCalendar[u'timeZone'])
|
||||||
print convertUTF8(u' Location: {0}'.format(userCalendar.get(u'location', u'')))
|
print utils.convertUTF8(u' Location: {0}'.format(userCalendar.get(u'location', u'')))
|
||||||
print u' Hidden: {0}'.format(userCalendar.get(u'hidden', u'False'))
|
print u' Hidden: {0}'.format(userCalendar.get(u'hidden', u'False'))
|
||||||
print u' Selected: {0}'.format(userCalendar.get(u'selected', u'False'))
|
print u' Selected: {0}'.format(userCalendar.get(u'selected', u'False'))
|
||||||
print u' Color ID: {0}, Background Color: {1}, Foreground Color: {2}'.format(userCalendar[u'colorId'], userCalendar[u'backgroundColor'], userCalendar[u'foregroundColor'])
|
print u' Color ID: {0}, Background Color: {1}, Foreground Color: {2}'.format(userCalendar[u'colorId'], userCalendar[u'backgroundColor'], userCalendar[u'foregroundColor'])
|
||||||
@ -3421,7 +3341,7 @@ def printDriveActivity(users):
|
|||||||
|
|
||||||
def printPermission(permission):
|
def printPermission(permission):
|
||||||
if u'name' in permission:
|
if u'name' in permission:
|
||||||
print convertUTF8(permission[u'name'])
|
print utils.convertUTF8(permission[u'name'])
|
||||||
elif u'id' in permission:
|
elif u'id' in permission:
|
||||||
if permission[u'id'] == u'anyone':
|
if permission[u'id'] == u'anyone':
|
||||||
print u'Anyone'
|
print u'Anyone'
|
||||||
@ -3759,7 +3679,7 @@ def printDriveFolderContents(feed, folderId, indent):
|
|||||||
for f_file in feed:
|
for f_file in feed:
|
||||||
for parent in f_file[u'parents']:
|
for parent in f_file[u'parents']:
|
||||||
if folderId == parent[u'id']:
|
if folderId == parent[u'id']:
|
||||||
print u' ' * indent, convertUTF8(f_file[u'title'])
|
print u' ' * indent, utils.convertUTF8(f_file[u'title'])
|
||||||
if f_file[u'mimeType'] == u'application/vnd.google-apps.folder':
|
if f_file[u'mimeType'] == u'application/vnd.google-apps.folder':
|
||||||
printDriveFolderContents(feed, f_file[u'id'], indent+1)
|
printDriveFolderContents(feed, f_file[u'id'], indent+1)
|
||||||
break
|
break
|
||||||
@ -3829,11 +3749,11 @@ def deleteEmptyDriveFolders(users):
|
|||||||
children = callGAPI(drive.children(), u'list',
|
children = callGAPI(drive.children(), u'list',
|
||||||
folderId=folder[u'id'], fields=u'items(id)', maxResults=1)
|
folderId=folder[u'id'], fields=u'items(id)', maxResults=1)
|
||||||
if not u'items' in children or len(children[u'items']) == 0:
|
if not u'items' in children or len(children[u'items']) == 0:
|
||||||
print convertUTF8(u' deleting empty folder %s...' % folder[u'title'])
|
print utils.convertUTF8(u' deleting empty folder %s...' % folder[u'title'])
|
||||||
callGAPI(drive.files(), u'delete', fileId=folder[u'id'])
|
callGAPI(drive.files(), u'delete', fileId=folder[u'id'])
|
||||||
deleted_empty = True
|
deleted_empty = True
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' not deleting folder %s because it contains at least 1 item (%s)' % (folder[u'title'], children[u'items'][0][u'id']))
|
print utils.convertUTF8(u' not deleting folder %s because it contains at least 1 item (%s)' % (folder[u'title'], children[u'items'][0][u'id']))
|
||||||
|
|
||||||
def doEmptyDriveTrash(users):
|
def doEmptyDriveTrash(users):
|
||||||
for user in users:
|
for user in users:
|
||||||
@ -4064,7 +3984,7 @@ def downloadDriveFile(users):
|
|||||||
extension = None
|
extension = None
|
||||||
result = callGAPI(drive.files(), u'get', fileId=fileId, fields=u'fileSize,title,mimeType,downloadUrl,exportLinks')
|
result = callGAPI(drive.files(), u'get', fileId=fileId, fields=u'fileSize,title,mimeType,downloadUrl,exportLinks')
|
||||||
if result[u'mimeType'] == MIMETYPE_GA_FOLDER:
|
if result[u'mimeType'] == MIMETYPE_GA_FOLDER:
|
||||||
print convertUTF8(u'Skipping download of folder %s' % result[u'title'])
|
print utils.convertUTF8(u'Skipping download of folder %s' % result[u'title'])
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
result[u'fileSize'] = int(result[u'fileSize'])
|
result[u'fileSize'] = int(result[u'fileSize'])
|
||||||
@ -4088,10 +4008,10 @@ def downloadDriveFile(users):
|
|||||||
extension = exportFormat[u'ext']
|
extension = exportFormat[u'ext']
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u'Skipping download of file {0}, Format {1} not available'.format(result[u'title'], u','.join(exportFormatChoices)))
|
print utils.convertUTF8(u'Skipping download of file {0}, Format {1} not available'.format(result[u'title'], u','.join(exportFormatChoices)))
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u'Skipping download of file {0}, Format not downloadable')
|
print utils.convertUTF8(u'Skipping download of file {0}, Format not downloadable')
|
||||||
continue
|
continue
|
||||||
file_title = result[u'title']
|
file_title = result[u'title']
|
||||||
safe_file_title = u''.join(c for c in file_title if c in safe_filename_chars)
|
safe_file_title = u''.join(c for c in file_title if c in safe_filename_chars)
|
||||||
@ -4104,7 +4024,7 @@ def downloadDriveFile(users):
|
|||||||
break
|
break
|
||||||
y += 1
|
y += 1
|
||||||
filename = os.path.join(targetFolder, u'({0})-{1}'.format(y, safe_file_title))
|
filename = os.path.join(targetFolder, u'({0})-{1}'.format(y, safe_file_title))
|
||||||
print convertUTF8(my_line % filename)
|
print utils.convertUTF8(my_line % filename)
|
||||||
if revisionId:
|
if revisionId:
|
||||||
download_url = u'{0}&revision={1}'.format(download_url, revisionId)
|
download_url = u'{0}&revision={1}'.format(download_url, revisionId)
|
||||||
_, content = drive._http.request(download_url)
|
_, content = drive._http.request(download_url)
|
||||||
@ -4434,9 +4354,9 @@ def getPop(users):
|
|||||||
|
|
||||||
def _showSendAs(result, j, jcount, formatSig):
|
def _showSendAs(result, j, jcount, formatSig):
|
||||||
if result[u'displayName']:
|
if result[u'displayName']:
|
||||||
print convertUTF8(u'SendAs Address: {0} <{1}>{2}'.format(result[u'displayName'], result[u'sendAsEmail'], currentCount(j, jcount)))
|
print utils.convertUTF8(u'SendAs Address: {0} <{1}>{2}'.format(result[u'displayName'], result[u'sendAsEmail'], currentCount(j, jcount)))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u'SendAs Address: <{0}>{1}'.format(result[u'sendAsEmail'], currentCount(j, jcount)))
|
print utils.convertUTF8(u'SendAs Address: <{0}>{1}'.format(result[u'sendAsEmail'], currentCount(j, jcount)))
|
||||||
if result.get(u'replyToAddress'):
|
if result.get(u'replyToAddress'):
|
||||||
print u' ReplyTo: {0}'.format(result[u'replyToAddress'])
|
print u' ReplyTo: {0}'.format(result[u'replyToAddress'])
|
||||||
print u' IsPrimary: {0}'.format(result.get(u'isPrimary', False))
|
print u' IsPrimary: {0}'.format(result.get(u'isPrimary', False))
|
||||||
@ -4449,9 +4369,9 @@ def _showSendAs(result, j, jcount, formatSig):
|
|||||||
if not signature:
|
if not signature:
|
||||||
signature = u'None'
|
signature = u'None'
|
||||||
if formatSig:
|
if formatSig:
|
||||||
print convertUTF8(indentMultiLineText(dehtml(signature), n=4))
|
print utils.convertUTF8(utils.indentMultiLineText(utils.dehtml(signature), n=4))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(indentMultiLineText(signature, n=4))
|
print utils.convertUTF8(utils.indentMultiLineText(signature, n=4))
|
||||||
|
|
||||||
def _processTags(tagReplacements, message):
|
def _processTags(tagReplacements, message):
|
||||||
while True:
|
while True:
|
||||||
@ -4880,7 +4800,7 @@ def showLabels(users):
|
|||||||
for label in labels[u'labels']:
|
for label in labels[u'labels']:
|
||||||
if onlyUser and (label[u'type'] == u'system'):
|
if onlyUser and (label[u'type'] == u'system'):
|
||||||
continue
|
continue
|
||||||
print convertUTF8(label[u'name'])
|
print utils.convertUTF8(label[u'name'])
|
||||||
for a_key in label:
|
for a_key in label:
|
||||||
if a_key == u'name':
|
if a_key == u'name':
|
||||||
continue
|
continue
|
||||||
@ -5125,7 +5045,7 @@ def _showFilter(userFilter, j, jcount, labels):
|
|||||||
elif item == u'sizeComparison':
|
elif item == u'sizeComparison':
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' {0} "{1}"'.format(item, userFilter[u'criteria'][item]))
|
print utils.convertUTF8(u' {0} "{1}"'.format(item, userFilter[u'criteria'][item]))
|
||||||
else:
|
else:
|
||||||
print u' ERROR: No Filter criteria'
|
print u' ERROR: No Filter criteria'
|
||||||
print u' Actions:'
|
print u' Actions:'
|
||||||
@ -5134,7 +5054,7 @@ def _showFilter(userFilter, j, jcount, labels):
|
|||||||
if labelId in FILTER_ADD_LABEL_TO_ARGUMENT_MAP:
|
if labelId in FILTER_ADD_LABEL_TO_ARGUMENT_MAP:
|
||||||
print u' {0}'.format(FILTER_ADD_LABEL_TO_ARGUMENT_MAP[labelId])
|
print u' {0}'.format(FILTER_ADD_LABEL_TO_ARGUMENT_MAP[labelId])
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' label "{0}"'.format(_getLabelName(labels, labelId)))
|
print utils.convertUTF8(u' label "{0}"'.format(_getLabelName(labels, labelId)))
|
||||||
for labelId in userFilter[u'action'].get(u'removeLabelIds', []):
|
for labelId in userFilter[u'action'].get(u'removeLabelIds', []):
|
||||||
if labelId in FILTER_REMOVE_LABEL_TO_ARGUMENT_MAP:
|
if labelId in FILTER_REMOVE_LABEL_TO_ARGUMENT_MAP:
|
||||||
print u' {0}'.format(FILTER_REMOVE_LABEL_TO_ARGUMENT_MAP[labelId])
|
print u' {0}'.format(FILTER_REMOVE_LABEL_TO_ARGUMENT_MAP[labelId])
|
||||||
@ -5688,15 +5608,15 @@ def getVacation(users):
|
|||||||
print u' End Date: {0}'.format(datetime.datetime.fromtimestamp(int(result[u'endTime'])/1000).strftime('%Y-%m-%d'))
|
print u' End Date: {0}'.format(datetime.datetime.fromtimestamp(int(result[u'endTime'])/1000).strftime('%Y-%m-%d'))
|
||||||
else:
|
else:
|
||||||
print u' End Date: Not specified'
|
print u' End Date: Not specified'
|
||||||
print convertUTF8(u' Subject: {0}'.format(result.get(u'responseSubject', u'None')))
|
print utils.convertUTF8(u' Subject: {0}'.format(result.get(u'responseSubject', u'None')))
|
||||||
sys.stdout.write(u' Message:\n ')
|
sys.stdout.write(u' Message:\n ')
|
||||||
if result.get(u'responseBodyPlainText'):
|
if result.get(u'responseBodyPlainText'):
|
||||||
print convertUTF8(indentMultiLineText(result[u'responseBodyPlainText'], n=4))
|
print utils.convertUTF8(utils.indentMultiLineText(result[u'responseBodyPlainText'], n=4))
|
||||||
elif result.get(u'responseBodyHtml'):
|
elif result.get(u'responseBodyHtml'):
|
||||||
if formatReply:
|
if formatReply:
|
||||||
print convertUTF8(indentMultiLineText(dehtml(result[u'responseBodyHtml']), n=4))
|
print utils.convertUTF8(utils.indentMultiLineText(utils.dehtml(result[u'responseBodyHtml']), n=4))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(indentMultiLineText(result[u'responseBodyHtml'], n=4))
|
print utils.convertUTF8(utils.indentMultiLineText(result[u'responseBodyHtml'], n=4))
|
||||||
else:
|
else:
|
||||||
print u'None'
|
print u'None'
|
||||||
|
|
||||||
@ -7127,9 +7047,9 @@ def doGetUserInfo(user_email=None):
|
|||||||
user = callGAPI(cd.users(), u'get', userKey=user_email, projection=projection, customFieldMask=customFieldMask, viewType=viewType)
|
user = callGAPI(cd.users(), u'get', userKey=user_email, projection=projection, customFieldMask=customFieldMask, viewType=viewType)
|
||||||
print u'User: %s' % user[u'primaryEmail']
|
print u'User: %s' % user[u'primaryEmail']
|
||||||
if u'name' in user and u'givenName' in user[u'name']:
|
if u'name' in user and u'givenName' in user[u'name']:
|
||||||
print convertUTF8(u'First Name: %s' % user[u'name'][u'givenName'])
|
print utils.convertUTF8(u'First Name: %s' % user[u'name'][u'givenName'])
|
||||||
if u'name' in user and u'familyName' in user[u'name']:
|
if u'name' in user and u'familyName' in user[u'name']:
|
||||||
print convertUTF8(u'Last Name: %s' % user[u'name'][u'familyName'])
|
print utils.convertUTF8(u'Last Name: %s' % user[u'name'][u'familyName'])
|
||||||
if u'isAdmin' in user:
|
if u'isAdmin' in user:
|
||||||
print u'Is a Super Admin: %s' % user[u'isAdmin']
|
print u'Is a Super Admin: %s' % user[u'isAdmin']
|
||||||
if u'isDelegatedAdmin' in user:
|
if u'isDelegatedAdmin' in user:
|
||||||
@ -7170,26 +7090,26 @@ def doGetUserInfo(user_email=None):
|
|||||||
contentType = notes.get(u'contentType', u'text_plain')
|
contentType = notes.get(u'contentType', u'text_plain')
|
||||||
print u' %s: %s' % (u'contentType', contentType)
|
print u' %s: %s' % (u'contentType', contentType)
|
||||||
if contentType == u'text_html':
|
if contentType == u'text_html':
|
||||||
print convertUTF8(indentMultiLineText(u' value: {0}'.format(dehtml(notes[u'value'])), n=2))
|
print utils.convertUTF8(utils.indentMultiLineText(u' value: {0}'.format(utils.dehtml(notes[u'value'])), n=2))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(indentMultiLineText(u' value: {0}'.format(notes[u'value']), n=2))
|
print utils.convertUTF8(utils.indentMultiLineText(u' value: {0}'.format(notes[u'value']), n=2))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(indentMultiLineText(u' value: {0}'.format(notes), n=2))
|
print utils.convertUTF8(utils.indentMultiLineText(u' value: {0}'.format(notes), n=2))
|
||||||
print u''
|
print u''
|
||||||
if u'ims' in user:
|
if u'ims' in user:
|
||||||
print u'IMs:'
|
print u'IMs:'
|
||||||
for im in user[u'ims']:
|
for im in user[u'ims']:
|
||||||
for key in im:
|
for key in im:
|
||||||
print convertUTF8(u' %s: %s' % (key, im[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, im[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'addresses' in user:
|
if u'addresses' in user:
|
||||||
print u'Addresses:'
|
print u'Addresses:'
|
||||||
for address in user[u'addresses']:
|
for address in user[u'addresses']:
|
||||||
for key in address:
|
for key in address:
|
||||||
if key != u'formatted':
|
if key != u'formatted':
|
||||||
print convertUTF8(u' %s: %s' % (key, address[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, address[key]))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, address[key].replace(u'\n', u'\\n')))
|
print utils.convertUTF8(u' %s: %s' % (key, address[key].replace(u'\n', u'\\n')))
|
||||||
print u''
|
print u''
|
||||||
if u'organizations' in user:
|
if u'organizations' in user:
|
||||||
print u'Organizations:'
|
print u'Organizations:'
|
||||||
@ -7197,13 +7117,13 @@ def doGetUserInfo(user_email=None):
|
|||||||
for key in org:
|
for key in org:
|
||||||
if key == u'customType' and not org[key]:
|
if key == u'customType' and not org[key]:
|
||||||
continue
|
continue
|
||||||
print convertUTF8(u' %s: %s' % (key, org[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, org[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'phones' in user:
|
if u'phones' in user:
|
||||||
print u'Phones:'
|
print u'Phones:'
|
||||||
for phone in user[u'phones']:
|
for phone in user[u'phones']:
|
||||||
for key in phone:
|
for key in phone:
|
||||||
print convertUTF8(u' %s: %s' % (key, phone[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, phone[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'emails' in user:
|
if u'emails' in user:
|
||||||
if len(user[u'emails']) > 1:
|
if len(user[u'emails']) > 1:
|
||||||
@ -7215,9 +7135,9 @@ def doGetUserInfo(user_email=None):
|
|||||||
if key == u'type' and an_email[key] == u'custom':
|
if key == u'type' and an_email[key] == u'custom':
|
||||||
continue
|
continue
|
||||||
if key == u'customType':
|
if key == u'customType':
|
||||||
print convertUTF8(u' type: %s' % an_email[key])
|
print utils.convertUTF8(u' type: %s' % an_email[key])
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, an_email[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, an_email[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'relations' in user:
|
if u'relations' in user:
|
||||||
print u'Relations:'
|
print u'Relations:'
|
||||||
@ -7226,9 +7146,9 @@ def doGetUserInfo(user_email=None):
|
|||||||
if key == u'type' and relation[key] == u'custom':
|
if key == u'type' and relation[key] == u'custom':
|
||||||
continue
|
continue
|
||||||
elif key == u'customType':
|
elif key == u'customType':
|
||||||
print convertUTF8(u' %s: %s' % (u'type', relation[key]))
|
print utils.convertUTF8(u' %s: %s' % (u'type', relation[key]))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, relation[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, relation[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'externalIds' in user:
|
if u'externalIds' in user:
|
||||||
print u'External IDs:'
|
print u'External IDs:'
|
||||||
@ -7237,9 +7157,9 @@ def doGetUserInfo(user_email=None):
|
|||||||
if key == u'type' and externalId[key] == u'custom':
|
if key == u'type' and externalId[key] == u'custom':
|
||||||
continue
|
continue
|
||||||
elif key == u'customType':
|
elif key == u'customType':
|
||||||
print convertUTF8(u' %s: %s' % (u'type', externalId[key]))
|
print utils.convertUTF8(u' %s: %s' % (u'type', externalId[key]))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, externalId[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, externalId[key]))
|
||||||
print u''
|
print u''
|
||||||
if u'websites' in user:
|
if u'websites' in user:
|
||||||
print u'Websites:'
|
print u'Websites:'
|
||||||
@ -7248,9 +7168,9 @@ def doGetUserInfo(user_email=None):
|
|||||||
if key == u'type' and website[key] == u'custom':
|
if key == u'type' and website[key] == u'custom':
|
||||||
continue
|
continue
|
||||||
elif key == u'customType':
|
elif key == u'customType':
|
||||||
print convertUTF8(u' %s: %s' % (u'type', website[key]))
|
print utils.convertUTF8(u' %s: %s' % (u'type', website[key]))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, website[key]))
|
print utils.convertUTF8(u' %s: %s' % (key, website[key]))
|
||||||
print u''
|
print u''
|
||||||
if getSchemas:
|
if getSchemas:
|
||||||
if u'customSchemas' in user:
|
if u'customSchemas' in user:
|
||||||
@ -7261,12 +7181,12 @@ def doGetUserInfo(user_email=None):
|
|||||||
if isinstance(user[u'customSchemas'][schema][field], list):
|
if isinstance(user[u'customSchemas'][schema][field], list):
|
||||||
print u' %s:' % field
|
print u' %s:' % field
|
||||||
for an_item in user[u'customSchemas'][schema][field]:
|
for an_item in user[u'customSchemas'][schema][field]:
|
||||||
print convertUTF8(u' type: %s' % (an_item[u'type']))
|
print utils.convertUTF8(u' type: %s' % (an_item[u'type']))
|
||||||
if an_item[u'type'] == u'custom':
|
if an_item[u'type'] == u'custom':
|
||||||
print convertUTF8(u' customType: %s' % (an_item[u'customType']))
|
print utils.convertUTF8(u' customType: %s' % (an_item[u'customType']))
|
||||||
print convertUTF8(u' value: %s' % (an_item[u'value']))
|
print utils.convertUTF8(u' value: %s' % (an_item[u'value']))
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (field, user[u'customSchemas'][schema][field]))
|
print utils.convertUTF8(u' %s: %s' % (field, user[u'customSchemas'][schema][field]))
|
||||||
print
|
print
|
||||||
if getAliases:
|
if getAliases:
|
||||||
if u'aliases' in user:
|
if u'aliases' in user:
|
||||||
@ -7344,7 +7264,7 @@ def doGetGroupInfo(group_name=None):
|
|||||||
for val in value:
|
for val in value:
|
||||||
print u' %s' % val
|
print u' %s' % val
|
||||||
else:
|
else:
|
||||||
print convertUTF8(u' %s: %s' % (key, value))
|
print utils.convertUTF8(u' %s: %s' % (key, value))
|
||||||
try:
|
try:
|
||||||
for key, value in settings.items():
|
for key, value in settings.items():
|
||||||
if key in [u'kind', u'etag', u'description', u'email', u'name']:
|
if key in [u'kind', u'etag', u'description', u'email', u'name']:
|
||||||
@ -7488,7 +7408,7 @@ def doGetCrosInfo():
|
|||||||
for i in xrange(min(listLimit, lenATR) if listLimit else lenATR):
|
for i in xrange(min(listLimit, lenATR) if listLimit else lenATR):
|
||||||
print u' date: {0}'.format(activeTimeRanges[i][u'date'])
|
print u' date: {0}'.format(activeTimeRanges[i][u'date'])
|
||||||
print u' activeTime: {0}'.format(str(activeTimeRanges[i][u'activeTime']))
|
print u' activeTime: {0}'.format(str(activeTimeRanges[i][u'activeTime']))
|
||||||
print u' duration: {0}'.format(formatMilliSeconds(activeTimeRanges[i][u'activeTime']))
|
print u' duration: {0}'.format(utils.formatMilliSeconds(activeTimeRanges[i][u'activeTime']))
|
||||||
recentUsers = cros.get(u'recentUsers', [])
|
recentUsers = cros.get(u'recentUsers', [])
|
||||||
lenRU = len(recentUsers)
|
lenRU = len(recentUsers)
|
||||||
if lenRU:
|
if lenRU:
|
||||||
@ -7511,13 +7431,13 @@ def print_json(object_name, object_value, spacing=u''):
|
|||||||
sys.stdout.write(u'%s%s: ' % (spacing, object_name))
|
sys.stdout.write(u'%s%s: ' % (spacing, object_name))
|
||||||
if isinstance(object_value, list):
|
if isinstance(object_value, list):
|
||||||
if len(object_value) == 1 and isinstance(object_value[0], (str, unicode, int, bool)):
|
if len(object_value) == 1 and isinstance(object_value[0], (str, unicode, int, bool)):
|
||||||
sys.stdout.write(convertUTF8(u'%s\n' % object_value[0]))
|
sys.stdout.write(utils.convertUTF8(u'%s\n' % object_value[0]))
|
||||||
return
|
return
|
||||||
if object_name is not None:
|
if object_name is not None:
|
||||||
sys.stdout.write(u'\n')
|
sys.stdout.write(u'\n')
|
||||||
for a_value in object_value:
|
for a_value in object_value:
|
||||||
if isinstance(a_value, (str, unicode, int, bool)):
|
if isinstance(a_value, (str, unicode, int, bool)):
|
||||||
sys.stdout.write(convertUTF8(u' %s%s\n' % (spacing, a_value)))
|
sys.stdout.write(utils.convertUTF8(u' %s%s\n' % (spacing, a_value)))
|
||||||
else:
|
else:
|
||||||
print_json(None, a_value, u' %s' % spacing)
|
print_json(None, a_value, u' %s' % spacing)
|
||||||
elif isinstance(object_value, dict):
|
elif isinstance(object_value, dict):
|
||||||
@ -7527,7 +7447,7 @@ def print_json(object_name, object_value, spacing=u''):
|
|||||||
for another_object in object_value:
|
for another_object in object_value:
|
||||||
print_json(another_object, object_value[another_object], u' %s' % spacing)
|
print_json(another_object, object_value[another_object], u' %s' % spacing)
|
||||||
else:
|
else:
|
||||||
sys.stdout.write(convertUTF8(u'%s\n' % (object_value)))
|
sys.stdout.write(utils.convertUTF8(u'%s\n' % (object_value)))
|
||||||
|
|
||||||
def doUpdateNotification():
|
def doUpdateNotification():
|
||||||
cd = buildGAPIObject(u'directory')
|
cd = buildGAPIObject(u'directory')
|
||||||
@ -7722,7 +7642,7 @@ def doGetNotifications():
|
|||||||
print u'ID: %s' % notification[u'notificationId']
|
print u'ID: %s' % notification[u'notificationId']
|
||||||
print u'Read Status: %s' % ([u'READ', u'UNREAD'][notification[u'isUnread']])
|
print u'Read Status: %s' % ([u'READ', u'UNREAD'][notification[u'isUnread']])
|
||||||
print u''
|
print u''
|
||||||
print convertUTF8(dehtml(notification[u'body']))
|
print utils.convertUTF8(utils.dehtml(notification[u'body']))
|
||||||
print u''
|
print u''
|
||||||
print u'--------------'
|
print u'--------------'
|
||||||
print u''
|
print u''
|
||||||
@ -7848,7 +7768,7 @@ def printShowTokens(i, entityType, users, csvFormat):
|
|||||||
print u' Client ID: %s' % token[u'clientId']
|
print u' Client ID: %s' % token[u'clientId']
|
||||||
for item in token:
|
for item in token:
|
||||||
if item not in [u'clientId', u'scopes']:
|
if item not in [u'clientId', u'scopes']:
|
||||||
print convertUTF8(u' %s: %s' % (item, token.get(item, u'')))
|
print utils.convertUTF8(u' %s: %s' % (item, token.get(item, u'')))
|
||||||
item = u'scopes'
|
item = u'scopes'
|
||||||
print u' %s:' % item
|
print u' %s:' % item
|
||||||
for it in token.get(item, []):
|
for it in token.get(item, []):
|
||||||
|
75
src/utils.py
Normal file
75
src/utils.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
from HTMLParser import HTMLParser
|
||||||
|
|
||||||
|
def convertUTF8(data):
|
||||||
|
if isinstance(data, str):
|
||||||
|
return data
|
||||||
|
if isinstance(data, unicode):
|
||||||
|
if GM_Globals[GM_WINDOWS]:
|
||||||
|
return data
|
||||||
|
return data.encode(GM_Globals[GM_SYS_ENCODING])
|
||||||
|
if isinstance(data, collections.Mapping):
|
||||||
|
return dict(map(convertUTF8, data.iteritems()))
|
||||||
|
if isinstance(data, collections.Iterable):
|
||||||
|
return type(data)(map(convertUTF8, data))
|
||||||
|
return data
|
||||||
|
|
||||||
|
class _DeHTMLParser(HTMLParser):
|
||||||
|
def __init__(self):
|
||||||
|
HTMLParser.__init__(self)
|
||||||
|
self.__text = []
|
||||||
|
|
||||||
|
def handle_data(self, data):
|
||||||
|
self.__text.append(data)
|
||||||
|
|
||||||
|
def handle_charref(self, name):
|
||||||
|
self.__text.append(unichr(int(name[1:], 16)) if name.startswith('x') else unichr(int(name)))
|
||||||
|
|
||||||
|
def handle_entityref(self, name):
|
||||||
|
cp = name2codepoint.get(name)
|
||||||
|
if cp:
|
||||||
|
self.__text.append(unichr(cp))
|
||||||
|
else:
|
||||||
|
self.__text.append(u'&'+name)
|
||||||
|
|
||||||
|
def handle_starttag(self, tag, attrs):
|
||||||
|
if tag == 'p':
|
||||||
|
self.__text.append('\n\n')
|
||||||
|
elif tag == 'br':
|
||||||
|
self.__text.append('\n')
|
||||||
|
elif tag == 'a':
|
||||||
|
for attr in attrs:
|
||||||
|
if attr[0] == 'href':
|
||||||
|
self.__text.append('({0}) '.format(attr[1]))
|
||||||
|
break
|
||||||
|
elif tag == 'div':
|
||||||
|
if not attrs:
|
||||||
|
self.__text.append('\n')
|
||||||
|
elif tag in ['http:', 'https']:
|
||||||
|
self.__text.append(' ({0}//{1}) '.format(tag, attrs[0][0]))
|
||||||
|
|
||||||
|
def handle_startendtag(self, tag, attrs):
|
||||||
|
if tag == 'br':
|
||||||
|
self.__text.append('\n\n')
|
||||||
|
|
||||||
|
def text(self):
|
||||||
|
return re.sub(r'\n{2}\n+', '\n\n', re.sub(r'\n +', '\n', ''.join(self.__text))).strip()
|
||||||
|
|
||||||
|
def dehtml(text):
|
||||||
|
try:
|
||||||
|
parser = _DeHTMLParser()
|
||||||
|
parser.feed(text.encode(u'utf-8'))
|
||||||
|
parser.close()
|
||||||
|
return parser.text()
|
||||||
|
except:
|
||||||
|
from traceback import print_exc
|
||||||
|
print_exc(file=sys.stderr)
|
||||||
|
return text
|
||||||
|
|
||||||
|
def indentMultiLineText(message, n=0):
|
||||||
|
return message.replace(u'\n', u'\n{0}'.format(u' '*n)).rstrip()
|
||||||
|
|
||||||
|
def formatMilliSeconds(millis):
|
||||||
|
seconds, millis = divmod(millis, 1000)
|
||||||
|
minutes, seconds = divmod(seconds, 60)
|
||||||
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
return u'%02d:%02d:%02d' % (hours, minutes, seconds)
|
Reference in New Issue
Block a user