def _processSendAs(user, i, count, entityType, emailAddress, j, jcount, gmail, function, formatSig, **kwargs):
userDefined = True
try:
result = callGAPI(gmail.users().settings().sendAs(), function,
throw_reasons=GAPI.GMAIL_THROW_REASONS+[GAPI.NOT_FOUND, GAPI.ALREADY_EXISTS, GAPI.DUPLICATE, GAPI.CANNOT_DELETE_PRIMARY_SENDAS, GAPI.INVALID_ARGUMENT],
userId=u'me', **kwargs)
if function == u'get':
_showSendAs(result, j, jcount, formatSig)
else:
entityActionPerformed([Ent.USER, user, entityType, emailAddress], j, jcount)
except (GAPI.notFound, GAPI.alreadyExists, GAPI.duplicate, GAPI.cannotDeletePrimarySendAs, GAPI.invalidArgument) as e:
entityActionFailedWarning([Ent.USER, user, entityType, emailAddress], str(e), j, jcount)
except (GAPI.serviceNotAvailable, GAPI.badRequest):
entityServiceNotApplicableWarning(Ent.USER, user, i, count)
userDefined = False
return userDefined
python类get()的实例源码
def adjustRedirectedSTDFilesIfNotMultiprocessing():
def adjustRedirectedSTDFile(stdtype):
rdFd = GM.Globals[stdtype].get(GM.REDIRECT_FD)
rdMultiFd = GM.Globals[stdtype].get(GM.REDIRECT_MULTI_FD)
if rdFd and rdMultiFd and rdFd != rdMultiFd:
try:
rdFd.write(rdMultiFd.getvalue())
rdMultiFd.close()
GM.Globals[stdtype][GM.REDIRECT_MULTI_FD] = rdFd
if (stdtype == GM.STDOUT) and (GM.Globals.get(GM.SAVED_STDOUT) is not None):
sys.stdout = rdFd
except IOError as e:
systemErrorExit(FILE_ERROR_RC, e)
adjustRedirectedSTDFile(GM.STDOUT)
if GM.Globals[GM.STDERR].get(GM.REDIRECT_NAME) != u'stdout':
adjustRedirectedSTDFile(GM.STDERR)
else:
GM.Globals[GM.STDERR][GM.REDIRECT_MULTI_FD] = GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD]
def decodeHtmlentities(string):
string = entitiesfix(string)
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
def substitute_entity(match):
from htmlentitydefs import name2codepoint as n2cp
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent)).encode('utf-8')
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp).encode('utf-8')
else:
return match.group()
return entity_re.subn(substitute_entity, string)[0]
def decodeHtmlentities(string):
string = entitiesfix(string)
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
def substitute_entity(match):
from htmlentitydefs import name2codepoint as n2cp
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent)).encode('utf-8')
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp).encode('utf-8')
else:
return match.group()
return entity_re.subn(substitute_entity, string)[0]
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
Added extra KeyboardInterrupt handling
"""
try:
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
except KeyboardInterrupt:
process.terminate()
raise
scrapertools.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def decodeHtmlentities(string):
string = entitiesfix(string)
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
def substitute_entity(match):
from htmlentitydefs import name2codepoint as n2cp
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent)).encode('utf-8')
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp).encode('utf-8')
else:
return match.group()
return entity_re.subn(substitute_entity, string)[0]
def getChoice(choices, **opts):
if Cmd.ArgumentsRemaining():
choice = Cmd.Current().strip().lower()
if choice:
if choice in opts.get(CHOICE_ALIASES, []):
choice = opts[CHOICE_ALIASES][choice]
if choice not in choices:
choice = choice.replace(u'_', u'').replace(u'-', u'')
if choice in opts.get(CHOICE_ALIASES, []):
choice = opts[CHOICE_ALIASES][choice]
if choice in choices:
Cmd.Advance()
return choice if not opts.get(MAP_CHOICE, False) else choices[choice]
if opts.get(DEFAULT_CHOICE, NO_DEFAULT) != NO_DEFAULT:
return opts[DEFAULT_CHOICE]
invalidChoiceExit(choices, False)
elif opts.get(DEFAULT_CHOICE, NO_DEFAULT) != NO_DEFAULT:
return opts[DEFAULT_CHOICE]
missingChoiceExit(choices)
def buildGAPIObject(api):
GM.Globals[GM.CURRENT_API_USER] = None
_, httpObj, service, cred_family = getAPIversionHttpService(api)
credentials = getClientCredentials(cred_family)
try:
API_Scopes = set(list(service._rootDesc[u'auth'][u'oauth2'][u'scopes']))
except KeyError:
API_Scopes = set(API.VAULT_SCOPES) if api == API.VAULT else set()
GM.Globals[GM.CURRENT_API_SCOPES] = list(API_Scopes.intersection(credentials.scopes))
if not GM.Globals[GM.CURRENT_API_SCOPES]:
systemErrorExit(NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(service._rootDesc[u'title']))
try:
service._http = credentials.authorize(httpObj)
except httplib2.ServerNotFoundError as e:
systemErrorExit(NETWORK_ERROR_RC, str(e))
except oauth2client.client.AccessTokenRefreshError as e:
return handleOAuthTokenError(str(e), False)
if not GC.Values[GC.DOMAIN]:
GC.Values[GC.DOMAIN] = credentials.id_token.get(u'hd', u'UNKNOWN').lower()
if not GC.Values[GC.CUSTOMER_ID]:
GC.Values[GC.CUSTOMER_ID] = GC.MY_CUSTOMER
GM.Globals[GM.ADMIN] = credentials.id_token.get(u'email', u'UNKNOWN').lower()
GM.Globals[GM.OAUTH2_CLIENT_ID] = credentials.client_id
return service
def initGDataObject(gdataObj, api):
if hasattr(sys, u'_MEIPASS') and not GM.Globals[GM.CACERTS_TXT]:
GM.Globals[GM.CACERTS_TXT] = os.path.join(sys._MEIPASS, u'httplib2', u'cacerts.txt')
os.environ['REQUESTS_CA_BUNDLE'] = GM.Globals[GM.CACERTS_TXT]
os.environ['DEFAULT_CA_BUNDLE_PATH'] = GM.Globals[GM.CACERTS_TXT]
_, _, api_version, cred_family = API.getVersion(api)
disc_file, discovery = readDiscoveryFile(api_version)
GM.Globals[GM.CURRENT_API_USER] = None
credentials = getClientCredentials(cred_family)
try:
GM.Globals[GM.CURRENT_API_SCOPES] = list(set(list(discovery[u'auth'][u'oauth2'][u'scopes'])).intersection(credentials.scopes))
except KeyError:
invalidDiscoveryJsonExit(disc_file)
if not GM.Globals[GM.CURRENT_API_SCOPES]:
systemErrorExit(NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(discovery.get(u'title', api_version)))
getGDataOAuthToken(gdataObj, credentials)
if GC.Values[GC.DEBUG_LEVEL] > 0:
gdataObj.debug = True
return gdataObj
def splitEmailAddressOrUID(emailAddressOrUID):
normalizedEmailAddressOrUID = normalizeEmailAddressOrUID(emailAddressOrUID)
atLoc = normalizedEmailAddressOrUID.find(u'@')
if atLoc > 0:
return (normalizedEmailAddressOrUID, normalizedEmailAddressOrUID[:atLoc], normalizedEmailAddressOrUID[atLoc+1:])
try:
cd = buildGAPIObject(API.DIRECTORY)
result = callGAPI(cd.users(), u'get',
throw_reasons=GAPI.USER_GET_THROW_REASONS,
userKey=normalizedEmailAddressOrUID, fields=u'primaryEmail')
if u'primaryEmail' in result:
normalizedEmailAddressOrUID = result[u'primaryEmail'].lower()
atLoc = normalizedEmailAddressOrUID.find(u'@')
return (normalizedEmailAddressOrUID, normalizedEmailAddressOrUID[:atLoc], normalizedEmailAddressOrUID[atLoc+1:])
except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.backendError, GAPI.systemError):
pass
return (normalizedEmailAddressOrUID, normalizedEmailAddressOrUID, GC.Values[GC.DOMAIN])
# Convert Org Unit Id to Org Unit Path
def convertOrgUnitIDtoPath(orgUnitId, cd):
if GM.Globals[GM.MAP_ORGUNIT_ID_TO_NAME] is None:
GM.Globals[GM.MAP_ORGUNIT_ID_TO_NAME] = {}
orgUnitPath = None
else:
orgUnitPath = GM.Globals[GM.MAP_ORGUNIT_ID_TO_NAME].get(orgUnitId)
if not orgUnitPath:
if cd is None:
cd = buildGAPIObject(API.DIRECTORY)
try:
orgUnitPath = callGAPI(cd.orgunits(), u'get',
throw_reasons=[GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.BACKEND_ERROR, GAPI.BAD_REQUEST, GAPI.INVALID_CUSTOMER_ID, GAPI.LOGIN_REQUIRED],
customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=orgUnitId, fields=u'orgUnitPath')[u'orgUnitPath']
except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError, GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired):
orgUnitPath = orgUnitId
GM.Globals[GM.MAP_ORGUNIT_ID_TO_NAME][orgUnitId] = orgUnitPath
return orgUnitPath
def doInfoResoldCustomer():
res = buildGAPIObject(API.RESELLER)
customerId = getString(Cmd.OB_CUSTOMER_ID)
checkForExtraneousArguments()
try:
customerInfo = callGAPI(res.customers(), u'get',
throw_reasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN],
customerId=customerId)
printKeyValueList([u'Customer ID', customerInfo[u'customerId']])
printKeyValueList([u'Customer Domain', customerInfo[u'customerDomain']])
printKeyValueList([u'Customer Domain Verified', customerInfo[u'customerDomainVerified']])
_showCustomerAddressPhoneNumber(customerInfo)
printKeyValueList([u'Customer Alternate Email', customerInfo[u'alternateEmail']])
printKeyValueList([u'Customer Admin Console URL', customerInfo[u'resourceUiUrl']])
except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden) as e:
entityActionFailedWarning([Ent.CUSTOMER_ID, customerId], str(e))
def doInfoDomainAlias():
cd = buildGAPIObject(API.DIRECTORY)
domainAliasName = getString(Cmd.OB_DOMAIN_ALIAS)
checkForExtraneousArguments()
try:
result = callGAPI(cd.domainAliases(), u'get',
throw_reasons=[GAPI.DOMAIN_ALIAS_NOT_FOUND, GAPI.BAD_REQUEST, GAPI.NOT_FOUND, GAPI.FORBIDDEN],
customer=GC.Values[GC.CUSTOMER_ID], domainAliasName=domainAliasName)
aliasSkipObjects = [u'domainAliasName',]
_showDomainAlias(result, aliasSkipObjects)
except GAPI.domainAliasNotFound:
entityActionFailedWarning([Ent.DOMAIN_ALIAS, domainAliasName], Msg.DOES_NOT_EXIST)
except (GAPI.badRequest, GAPI.notFound, GAPI.forbidden):
accessErrorExit(cd)
# gam print domainaliases [todrive [<ToDriveAttributes>]]
def getOrgUnitId(cd=None):
if cd is None:
cd = buildGAPIObject(API.DIRECTORY)
orgUnit = getOrgUnitItem()
if orgUnit[:3] == u'id:':
return (orgUnit, orgUnit)
try:
result = callGAPI(cd.orgunits(), u'get',
throw_reasons=[GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.BACKEND_ERROR, GAPI.BAD_REQUEST, GAPI.INVALID_CUSTOMER_ID, GAPI.LOGIN_REQUIRED],
customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=makeOrgUnitPathRelative(orgUnit),
fields=u'orgUnitId')
return (orgUnit, result[u'orgUnitId'])
except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError):
entityDoesNotExistExit(Ent.ORGANIZATIONAL_UNIT, orgUnit)
except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired):
accessErrorExit(cd)
def doInfoCustomer():
cd = buildGAPIObject(API.DIRECTORY)
checkForExtraneousArguments()
try:
customerInfo = callGAPI(cd.customers(), u'get',
throw_reasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN],
customerKey=GC.Values[GC.CUSTOMER_ID])
printKeyValueList([u'Customer ID', customerInfo[u'id']])
printKeyValueList([u'Primary Domain', customerInfo[u'customerDomain']])
printKeyValueList([u'Customer Creation Time', formatLocalTime(customerInfo[u'customerCreationTime'])])
verified = callGAPI(cd.domains(), u'get',
customer=customerInfo[u'id'], domainName=customerInfo[u'customerDomain'], fields=u'verified')[u'verified']
printKeyValueList([u'Primary Domain Verified', verified])
printKeyValueList([u'Default Language', customerInfo[u'language']])
_showCustomerAddressPhoneNumber(customerInfo)
printKeyValueList([u'Admin Secondary Email', customerInfo[u'alternateEmail']])
_showCustomerLicenseInfo(customerInfo[u'id'])
except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden):
accessErrorExit(cd)
# gam update customer [primary <DomainName>] [adminsecondaryemail|alternateemail <EmailAddress>] [language <LanguageCode] [phone|phonenumber <String>]
# [contact|contactname <String>] [name|organizationname <String>]
# [address1|addressline1 <String>] [address2|addressline2 <String>] [address3|addressline3 <String>]
# [locality <String>] [region <String>] [postalcode <String>] [country|countrycode <String>]
def checkOrgUnitPathExists(cd, orgUnitPath, i=0, count=0, showError=False):
if orgUnitPath == u'/':
return (True, orgUnitPath)
try:
return (True, callGAPI(cd.orgunits(), u'get',
throw_reasons=[GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.BACKEND_ERROR, GAPI.BAD_REQUEST, GAPI.INVALID_CUSTOMER_ID, GAPI.LOGIN_REQUIRED],
customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=makeOrgUnitPathRelative(orgUnitPath),
fields=u'orgUnitPath')[u'orgUnitPath'])
except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError):
pass
except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired):
errMsg = accessErrorMessage(cd)
if errMsg:
systemErrorExit(INVALID_DOMAIN_RC, errMsg)
if showError:
entityActionFailedWarning([Ent.ORGANIZATIONAL_UNIT, orgUnitPath], Msg.DOES_NOT_EXIST, i, count)
return (False, orgUnitPath)
def writeStdout(data):
try:
GM.Globals[GM.STDOUT].get(GM.REDIRECT_MULTI_FD, sys.stdout).write(data)
except IOError as e:
systemErrorExit(FILE_ERROR_RC, e)
def flushStdout():
try:
GM.Globals[GM.STDOUT].get(GM.REDIRECT_MULTI_FD, sys.stdout).flush()
except IOError as e:
systemErrorExit(FILE_ERROR_RC, e)