diff --git a/src/gam/__init__.py b/src/gam/__init__.py index ce9aa2f2..e09ab4d5 100755 --- a/src/gam/__init__.py +++ b/src/gam/__init__.py @@ -3207,3 +3207,62 @@ def ProcessGAMCommand(args, processGamCfg=True, inLoop=False, closeSTD=True): # Process GAM command def CallGAMCommand(args, processGamCfg=True, inLoop=False, closeSTD=False): return ProcessGAMCommand(args, processGamCfg=processGamCfg, inLoop=inLoop, closeSTD=closeSTD) + +# Re-export util names for _gam().X access by util/ modules. +# This block runs AFTER all modules are fully loaded (no circular import risk). +# Only names actually referenced via _gam() are included. +from gamlib import glskus as SKU # noqa: E402,F811 +from tempfile import TemporaryFile # noqa: E402,F401 +from util.access import ( # noqa: E402,F401 + APIAccessDeniedExit, ClientAPIAccessDeniedExit, SvcAcctAPIAccessDeniedExit, accessErrorExit, + accessErrorExitNonDirectory, checkEntityAFDNEorAccessErrorExit, + checkEntityDNEorAccessErrorExit, entityUnknownWarning, +) +from util.api import ( # noqa: E402,F401,F811 + _getAdminEmail, _getSvcAcctData, buildGAPIObject, buildGAPIServiceObject, + callGAPI, callGAPIitems, callGAPIpages, chooseSaAPI, yieldGAPIpages, +) +from util.args import ( # noqa: E402,F401,F811 + ARCHIVED_ARGUMENTS, ISOformatTimeStamp, LOCALE_CODES_MAP, NAME_EMAIL_ADDRESS_PATTERN, + SUSPENDED_ARGUMENTS, YYYYMMDDTHHMMSSZ_FORMAT, YYYYMMDD_FORMAT, YYYYMMDD_PATTERN, + _getIsArchived, _getIsSuspended, checkDataField, checkForExtraneousArguments, + checkMatchSkipFields, checkSubkeyField, encodeOrgUnitPath, escapeCRsNLs, + formatLocalTime, formatLocalTimestamp, getBoolean, getCharSet, getCharacter, + getDelimiter, getEmailAddress, getEmailAddressDomain, getLanguageCode, + getMatchSkipFields, getPhraseDNEorSNA, getREPattern, getString, + getSheetEntity, getSheetIdFromSheetEntity, makeOrgUnitPathAbsolute, + makeOrgUnitPathRelative, orgUnitPathQuery, protectedSheetId, removeCourseIdScope, + shlexSplitList, splitEmailAddress, todaysTime, validateEmailAddressOrUID, +) +from util.csv_pf import CheckInputRowFilterHeaders # noqa: E402,F401 +from util.display import ( # noqa: E402,F401 + FIRST_ITEM_MARKER, LAST_ITEM_MARKER, TOTAL_ITEMS_MARKER, actionPerformedNumItems, + entityActionFailedWarning, entityActionNotPerformedWarning, entityActionPerformed, + entityActionPerformedMessage, entityDoesNotExistWarning, entityPerformActionNumItems, + entityServiceNotApplicableWarning, getPageMessage, getPageMessageForWhom, printBlankLine, + printGettingAllAccountEntities, printGettingAllEntityItemsForWhom, + printGotEntityItemsForWhom, printJSONKey, printJSONValue, printKeyValueList, + setGettingAllEntityItemsForWhom, userDriveServiceNotEnabledWarning, + userServiceNotEnabledWarning, +) +from util.email import send_email # noqa: E402,F401,F811 +from util.entity import checkUserExists, convertUIDtoEmailAddress # noqa: E402,F401,F811 +from util.errors import ( # noqa: E402,F401,F811 + csvDataAlreadySavedErrorExit, csvFieldErrorExit, entityActionFailedExit, + entityDoesNotExistExit, expiredRevokedOauth2TxtExit, formatChoiceList, + invalidArgumentExit, invalidChoiceExit, invalidDiscoveryJsonExit, + invalidOauth2TxtExit, invalidOauth2serviceJsonExit, missingArgumentExit, +) +from util.fileio import ( # noqa: E402,F401,F811 + StringIOobject, checkAPICallsRate, closeFile, fdErrorMessage, fileErrorMessage, + getGDocSheetDataFailedExit, getGDocSheetDataRetryWarning, incrAPICallsRetryData, + openFile, readFile, writeFile, +) +from util.gdoc import ( # noqa: E402,F401 + getGDocData, getStorageFileData, openCSVFileReader, +) +from util.output import ( # noqa: E402,F401,F811 + currentCountNL, flushStderr, flushStdout, + printWarningMessage, readStdin, stderrWarningMsg, writeStdout, +) + diff --git a/src/gam/util/access.py b/src/gam/util/access.py index a9fe22ed..fe69a30b 100644 --- a/src/gam/util/access.py +++ b/src/gam/util/access.py @@ -16,8 +16,7 @@ from gamlib import glindent from gamlib import glmsgs as Msg -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] Act = glaction.GamAction() @@ -27,139 +26,125 @@ Ind = glindent.GamIndent() # Something's wrong with CustomerID?? def accessErrorMessage(cd, errMsg=None): - _m = _getMain() if cd is None: - cd = _m.buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) try: - _m.callGAPI(cd.customers(), 'get', + _gam().callGAPI(cd.customers(), 'get', throwReasons=[GAPI.BAD_REQUEST, GAPI.INVALID_INPUT, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], customerKey=GC.Values[GC.CUSTOMER_ID], fields='id') except (GAPI.badRequest, GAPI.invalidInput): - return _m.formatKeyValueList('', + return _gam().formatKeyValueList('', [Ent.Singular(Ent.CUSTOMER_ID), GC.Values[GC.CUSTOMER_ID], Msg.INVALID], '') except GAPI.resourceNotFound: - return _m.formatKeyValueList('', + return _gam().formatKeyValueList('', [Ent.Singular(Ent.CUSTOMER_ID), GC.Values[GC.CUSTOMER_ID], Msg.DOES_NOT_EXIST], '') except (GAPI.forbidden, GAPI.permissionDenied): - return _m.formatKeyValueList('', + return _gam().formatKeyValueList('', Ent.FormatEntityValueList([Ent.CUSTOMER_ID, GC.Values[GC.CUSTOMER_ID], Ent.DOMAIN, GC.Values[GC.DOMAIN], Ent.USER, GM.Globals[GM.ADMIN]])+[Msg.ACCESS_FORBIDDEN], '') if errMsg: - return _m.formatKeyValueList('', + return _gam().formatKeyValueList('', [Ent.Singular(Ent.CUSTOMER_ID), GC.Values[GC.CUSTOMER_ID], errMsg], '') return None def accessErrorExit(cd, errMsg=None): - _m = _getMain() - _m.systemErrorExit(_m.INVALID_DOMAIN_RC, accessErrorMessage(cd or _m.buildGAPIObject(API.DIRECTORY), errMsg)) + _gam().systemErrorExit(_gam().INVALID_DOMAIN_RC, accessErrorMessage(cd or _gam().buildGAPIObject(API.DIRECTORY), errMsg)) def accessErrorExitNonDirectory(api, errMsg): - _m = _getMain() - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, - _m.formatKeyValueList('', + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, + _gam().formatKeyValueList('', Ent.FormatEntityValueList([Ent.CUSTOMER_ID, GC.Values[GC.CUSTOMER_ID], Ent.DOMAIN, GC.Values[GC.DOMAIN], Ent.API, api])+[errMsg], '')) def ClientAPIAccessDeniedExit(errMsg=None): - _m = _getMain() if errMsg is None: - _m.stderrErrorMsg(Msg.API_ACCESS_DENIED) + _gam().stderrErrorMsg(Msg.API_ACCESS_DENIED) missingScopes = API.getClientScopesSet(GM.Globals[GM.CURRENT_CLIENT_API])-GM.Globals[GM.CURRENT_CLIENT_API_SCOPES] if missingScopes: - _m.writeStderr(Msg.API_CHECK_CLIENT_AUTHORIZATION.format(GM.Globals[GM.OAUTH2_CLIENT_ID], + _gam().writeStderr(Msg.API_CHECK_CLIENT_AUTHORIZATION.format(GM.Globals[GM.OAUTH2_CLIENT_ID], ','.join(sorted(missingScopes)))) - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, None) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, None) else: - _m.stderrErrorMsg(errMsg) - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, Msg.REAUTHENTICATION_IS_NEEDED) + _gam().stderrErrorMsg(errMsg) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, Msg.REAUTHENTICATION_IS_NEEDED) def SvcAcctAPIAccessDenied(): - _m = _getMain() - _m._getSvcAcctData() + _gam()._getSvcAcctData() if (GM.Globals[GM.CURRENT_SVCACCT_API] == API.GMAIL and GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES] and GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES][0] == API.GMAIL_SEND_SCOPE): - _m.systemErrorExit(_m.OAUTH2SERVICE_JSON_REQUIRED_RC, Msg.NO_SVCACCT_ACCESS_ALLOWED) - _m.stderrErrorMsg(Msg.API_ACCESS_DENIED) + _gam().systemErrorExit(_gam().OAUTH2SERVICE_JSON_REQUIRED_RC, Msg.NO_SVCACCT_ACCESS_ALLOWED) + _gam().stderrErrorMsg(Msg.API_ACCESS_DENIED) apiOrScopes = API.getAPIName(GM.Globals[GM.CURRENT_SVCACCT_API]) if GM.Globals[GM.CURRENT_SVCACCT_API] else ','.join(sorted(GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES])) - _m.writeStderr(Msg.API_CHECK_SVCACCT_AUTHORIZATION.format(GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]['client_id'], + _gam().writeStderr(Msg.API_CHECK_SVCACCT_AUTHORIZATION.format(GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]['client_id'], apiOrScopes, - GM.Globals[GM.CURRENT_SVCACCT_USER] or _m._getAdminEmail())) + GM.Globals[GM.CURRENT_SVCACCT_USER] or _gam()._getAdminEmail())) def SvcAcctAPIAccessDeniedExit(): - _m = _getMain() SvcAcctAPIAccessDenied() - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, None) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, None) def SvcAcctAPIDisabledExit(): - _m = _getMain() if not GM.Globals[GM.CURRENT_SVCACCT_USER] and GM.Globals[GM.CURRENT_CLIENT_API]: ClientAPIAccessDeniedExit() if GM.Globals[GM.CURRENT_SVCACCT_API]: - _m.stderrErrorMsg(Msg.SERVICE_ACCOUNT_API_DISABLED.format(API.getAPIName(GM.Globals[GM.CURRENT_SVCACCT_API]))) - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, None) - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, Msg.API_ACCESS_DENIED) + _gam().stderrErrorMsg(Msg.SERVICE_ACCOUNT_API_DISABLED.format(API.getAPIName(GM.Globals[GM.CURRENT_SVCACCT_API]))) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, None) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, Msg.API_ACCESS_DENIED) def APIAccessDeniedExit(): - _m = _getMain() if not GM.Globals[GM.CURRENT_SVCACCT_USER] and GM.Globals[GM.CURRENT_CLIENT_API]: ClientAPIAccessDeniedExit() if GM.Globals[GM.CURRENT_SVCACCT_API]: SvcAcctAPIAccessDeniedExit() - _m.systemErrorExit(_m.API_ACCESS_DENIED_RC, Msg.API_ACCESS_DENIED) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, Msg.API_ACCESS_DENIED) def checkEntityDNEorAccessErrorExit(cd, entityType, entityName, i=0, count=0): - _m = _getMain() message = accessErrorMessage(cd) if message: - _m.systemErrorExit(_m.INVALID_DOMAIN_RC, message) - _m.entityDoesNotExistWarning(entityType, entityName, i, count) + _gam().systemErrorExit(_gam().INVALID_DOMAIN_RC, message) + _gam().entityDoesNotExistWarning(entityType, entityName, i, count) def checkEntityAFDNEorAccessErrorExit(cd, entityType, entityName, i=0, count=0): - _m = _getMain() message = accessErrorMessage(cd) if message: - _m.systemErrorExit(_m.INVALID_DOMAIN_RC, message) - _m.entityActionFailedWarning([entityType, entityName], Msg.DOES_NOT_EXIST, i, count) + _gam().systemErrorExit(_gam().INVALID_DOMAIN_RC, message) + _gam().entityActionFailedWarning([entityType, entityName], Msg.DOES_NOT_EXIST, i, count) def checkEntityItemValueAFDNEorAccessErrorExit(cd, entityType, entityName, itemType, itemValue, i=0, count=0): - _m = _getMain() message = accessErrorMessage(cd) if message: - _m.systemErrorExit(_m.INVALID_DOMAIN_RC, message) - _m.entityActionFailedWarning([entityType, entityName, itemType, itemValue], Msg.DOES_NOT_EXIST, i, count) + _gam().systemErrorExit(_gam().INVALID_DOMAIN_RC, message) + _gam().entityActionFailedWarning([entityType, entityName, itemType, itemValue], Msg.DOES_NOT_EXIST, i, count) def entityUnknownWarning(entityType, entityName, i=0, count=0): - _m = _getMain() - domain = _m.getEmailAddressDomain(entityName) + domain = _gam().getEmailAddressDomain(entityName) if (domain.endswith(GC.Values[GC.DOMAIN])) or (domain.endswith('google.com')): - _m.entityDoesNotExistWarning(entityType, entityName, i, count) + _gam().entityDoesNotExistWarning(entityType, entityName, i, count) else: - _m.entityServiceNotApplicableWarning(entityType, entityName, i, count) + _gam().entityServiceNotApplicableWarning(entityType, entityName, i, count) def entityOrEntityUnknownWarning(entity1Type, entity1Name, entity2Type, entity2Name, i=0, count=0): - _m = _getMain() - _m.setSysExitRC(_m.ENTITY_DOES_NOT_EXIST_RC) - _m.writeStderr(_m.formatKeyValueList(Ind.Spaces(), - [f'{Msg.EITHER} {Ent.Singular(entity1Type)}', entity1Name, _m.getPhraseDNEorSNA(entity1Name), None, - f'{Msg.OR} {Ent.Singular(entity2Type)}', entity2Name, _m.getPhraseDNEorSNA(entity2Name)], - _m.currentCountNL(i, count))) + _gam().setSysExitRC(_gam().ENTITY_DOES_NOT_EXIST_RC) + _gam().writeStderr(_gam().formatKeyValueList(Ind.Spaces(), + [f'{Msg.EITHER} {Ent.Singular(entity1Type)}', entity1Name, _gam().getPhraseDNEorSNA(entity1Name), None, + f'{Msg.OR} {Ent.Singular(entity2Type)}', entity2Name, _gam().getPhraseDNEorSNA(entity2Name)], + _gam().currentCountNL(i, count))) def duplicateAliasGroupUserWarning(cd, entityValueList, i=0, count=0): - _m = _getMain() email = entityValueList[1] try: - result = _m.callGAPI(cd.users(), 'get', + result = _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=email, fields='id,primaryEmail') if (result['primaryEmail'].lower() == email) or (result['id'] == email): @@ -169,7 +154,7 @@ def duplicateAliasGroupUserWarning(cd, entityValueList, i=0, count=0): except (GAPI.userNotFound, GAPI.badRequest, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.backendError, GAPI.systemError): try: - result = _m.callGAPI(cd.groups(), 'get', + result = _gam().callGAPI(cd.groups(), 'get', throwReasons=GAPI.GROUP_GET_THROW_REASONS, groupKey=email, fields='id,email') if (result['email'].lower() == email) or (result['id'] == email): @@ -179,10 +164,10 @@ def duplicateAliasGroupUserWarning(cd, entityValueList, i=0, count=0): except (GAPI.groupNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest): kvList = [Ent.EMAIL, email] - _m.writeStderr(_m.formatKeyValueList(Ind.Spaces(), + _gam().writeStderr(_gam().formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+ [Act.Failed(), Msg.DUPLICATE]+ Ent.FormatEntityValueList(kvList), - _m.currentCountNL(i, count))) - _m.setSysExitRC(_m.ENTITY_DUPLICATE_RC) + _gam().currentCountNL(i, count))) + _gam().setSysExitRC(_gam().ENTITY_DUPLICATE_RC) return kvList[0] diff --git a/src/gam/util/api.py b/src/gam/util/api.py index d5cbd1d4..eea760b5 100644 --- a/src/gam/util/api.py +++ b/src/gam/util/api.py @@ -56,8 +56,7 @@ DEVELOPER_PREVIEW_DISCOVERY_URI = "https://{api}.googleapis.com/$discovery/rest? _DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] def _getEnt(): return sys.modules['gam'].Ent @@ -67,13 +66,12 @@ def _getInd(): def handleServerError(e): - m = _getMain() errMsg = str(e) if 'setting tls' not in errMsg: - m.systemErrorExit(m.NETWORK_ERROR_RC, errMsg) - m.stderrErrorMsg(errMsg) - m.writeStderr(Msg.DISABLE_TLS_MIN_MAX) - m.systemErrorExit(m.NETWORK_ERROR_RC, None) + _gam().systemErrorExit(_gam().NETWORK_ERROR_RC, errMsg) + _gam().stderrErrorMsg(errMsg) + _gam().writeStderr(Msg.DISABLE_TLS_MIN_MAX) + _gam().systemErrorExit(_gam().NETWORK_ERROR_RC, None) def getHttpObj(cache=None, timeout=None, override_min_tls=None, override_max_tls=None): tls_minimum_version = override_min_tls if override_min_tls else GC.Values[GC.TLS_MIN_VERSION] if GC.Values[GC.TLS_MIN_VERSION] else None @@ -113,7 +111,7 @@ def _force_user_agent(user_agent): def _lazy_force_user_agent(request_method): """Wraps a request method to lazily insert GAM_USER_AGENT at call time.""" def wrapped_request_method(*args, **kwargs): - user_agent = _getMain().GAM_USER_AGENT + user_agent = _gam().GAM_USER_AGENT if kwargs.get('headers') is not None: if kwargs['headers'].get('user-agent'): if user_agent not in kwargs['headers']['user-agent']: @@ -157,14 +155,13 @@ def transportCreateRequest(httpObj=None): return transportAgentRequest(httpObj) def doGAMCheckForUpdates(forceCheck): - m = _getMain() Ind = _getInd() def _gamLatestVersionNotAvailable(): if forceCheck: - m.systemErrorExit(m.NETWORK_ERROR_RC, Msg.GAM_LATEST_VERSION_NOT_AVAILABLE) + _gam().systemErrorExit(_gam().NETWORK_ERROR_RC, Msg.GAM_LATEST_VERSION_NOT_AVAILABLE) try: - _, c = getHttpObj(timeout=10).request(m.GAM_LATEST_RELEASE, 'GET', headers={'Accept': 'application/vnd.github.v3.text+json'}) + _, c = getHttpObj(timeout=10).request(_gam().GAM_LATEST_RELEASE, 'GET', headers={'Accept': 'application/vnd.github.v3.text+json'}) try: release_data = json.loads(c) except (IndexError, KeyError, SyntaxError, TypeError, ValueError): @@ -173,17 +170,17 @@ def doGAMCheckForUpdates(forceCheck): if not isinstance(release_data, dict) or 'tag_name' not in release_data: _gamLatestVersionNotAvailable() return - current_version = m.__version__ + current_version = _gam().__version__ latest_version = release_data['tag_name'] if latest_version[0].lower() == 'v': latest_version = latest_version[1:] - m.printKeyValueList(['Version Check', None]) + _gam().printKeyValueList(['Version Check', None]) Ind.Increment() - m.printKeyValueList(['Current', current_version]) - m.printKeyValueList([' Latest', latest_version]) + _gam().printKeyValueList(['Current', current_version]) + _gam().printKeyValueList([' Latest', latest_version]) Ind.Decrement() if forceCheck < 0: - m.setSysExitRC(1 if latest_version > current_version else 0) + _gam().setSysExitRC(1 if latest_version > current_version else 0) return except (httplib2.HttpLib2Error, httplib2.ServerNotFoundError, google.auth.exceptions.TransportError, @@ -250,13 +247,12 @@ class signjwtSignJwt(google.auth.crypt.Signer): def sign(self, message): ''' Call IAM Credentials SignJWT API to get our signed JWT ''' - m = _getMain() request = get_adc_request() try: credentials, _ = google.auth.default(scopes=[API.IAM_SCOPE], request=request) except (google.auth.exceptions.DefaultCredentialsError, google.auth.exceptions.RefreshError) as e: - m.systemErrorExit(m.API_ACCESS_DENIED_RC, str(e)) + _gam().systemErrorExit(_gam().API_ACCESS_DENIED_RC, str(e)) httpObj = transportAuthorizedHttp(credentials, http=getHttpObj()) # refresh here so we can use the proper request from above httpObj.credentials.refresh(request) @@ -267,42 +263,40 @@ class signjwtSignJwt(google.auth.crypt.Signer): return signed_jwt def handleOAuthTokenError(e, softErrors, displayError=False, i=0, count=0): - m = _getMain() Ent = _getEnt() errMsg = str(e).replace('.', '') if ((errMsg in API.OAUTH2_TOKEN_ERRORS) or errMsg.startswith('Invalid response') or errMsg.startswith('invalid_request: Invalid impersonation "sub" field')): if not GM.Globals[GM.CURRENT_SVCACCT_USER]: - m.ClientAPIAccessDeniedExit() + _gam().ClientAPIAccessDeniedExit() # 403 Forbidden, API disabled, user not enabled # 400 Bad Request, user not defined if softErrors: - m.entityActionFailedWarning([Ent.USER, GM.Globals[GM.CURRENT_SVCACCT_USER], Ent.USER, None], errMsg, i, count) + _gam().entityActionFailedWarning([Ent.USER, GM.Globals[GM.CURRENT_SVCACCT_USER], Ent.USER, None], errMsg, i, count) return None - m.systemErrorExit(m.SERVICE_NOT_APPLICABLE_RC, Msg.SERVICE_NOT_APPLICABLE_THIS_ADDRESS.format(GM.Globals[GM.CURRENT_SVCACCT_USER])) + _gam().systemErrorExit(_gam().SERVICE_NOT_APPLICABLE_RC, Msg.SERVICE_NOT_APPLICABLE_THIS_ADDRESS.format(GM.Globals[GM.CURRENT_SVCACCT_USER])) if errMsg in API.OAUTH2_UNAUTHORIZED_ERRORS: if not GM.Globals[GM.CURRENT_SVCACCT_USER]: - m.ClientAPIAccessDeniedExit() + _gam().ClientAPIAccessDeniedExit() # 401 Unauthorized, API disabled, user enabled if softErrors: if displayError: apiOrScopes = API.getAPIName(GM.Globals[GM.CURRENT_SVCACCT_API]) if GM.Globals[GM.CURRENT_SVCACCT_API] else ','.join(sorted(GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES])) - m.userServiceNotEnabledWarning(GM.Globals[GM.CURRENT_SVCACCT_USER], apiOrScopes, i, count) + _gam().userServiceNotEnabledWarning(GM.Globals[GM.CURRENT_SVCACCT_USER], apiOrScopes, i, count) return None - m.SvcAcctAPIAccessDeniedExit() + _gam().SvcAcctAPIAccessDeniedExit() if errMsg in API.REFRESH_PERM_ERRORS: if softErrors: return None if not GM.Globals[GM.CURRENT_SVCACCT_USER]: - m.expiredRevokedOauth2TxtExit() - m.stderrErrorMsg(f'Authentication Token Error - {errMsg}') - m.APIAccessDeniedExit() + _gam().expiredRevokedOauth2TxtExit() + _gam().stderrErrorMsg(f'Authentication Token Error - {errMsg}') + _gam().APIAccessDeniedExit() def getOauth2TxtCredentials(exitOnError=True, api=None, noDASA=False, refreshOnly=False, noScopes=False): - m = _getMain() if not noDASA and GC.Values[GC.ENABLE_DASA]: - jsonData = m.readFile(GC.Values[GC.OAUTH2SERVICE_JSON], continueOnError=True, displayError=False) + jsonData = _gam().readFile(GC.Values[GC.OAUTH2SERVICE_JSON], continueOnError=True, displayError=False) if jsonData: try: if api in API.APIS_NEEDING_ACCESS_TOKEN: @@ -320,9 +314,9 @@ def getOauth2TxtCredentials(exitOnError=True, api=None, noDASA=False, refreshOnl sjsigner = signjwtSignJwt(jsonDict) return (True, signjwtJWTCredentials._from_signer_and_info(sjsigner, jsonDict, audience=audience)) except (IndexError, KeyError, SyntaxError, TypeError, ValueError) as e: - m.invalidOauth2serviceJsonExit(str(e)) - m.invalidOauth2serviceJsonExit(Msg.NO_DATA) - jsonData = m.readFile(GC.Values[GC.OAUTH2_TXT], continueOnError=True, displayError=False) + _gam().invalidOauth2serviceJsonExit(str(e)) + _gam().invalidOauth2serviceJsonExit(Msg.NO_DATA) + jsonData = _gam().readFile(GC.Values[GC.OAUTH2_TXT], continueOnError=True, displayError=False) if jsonData: try: jsonDict = json.loads(jsonData) @@ -332,11 +326,11 @@ def getOauth2TxtCredentials(exitOnError=True, api=None, noDASA=False, refreshOnl if not refreshOnly: if set(jsonDict.get('scopes', API.REQUIRED_SCOPES)) == API.REQUIRED_SCOPES_SET: if exitOnError: - m.systemErrorExit(m.OAUTH2_TXT_REQUIRED_RC, Msg.NO_CLIENT_ACCESS_ALLOWED) + _gam().systemErrorExit(_gam().OAUTH2_TXT_REQUIRED_RC, Msg.NO_CLIENT_ACCESS_ALLOWED) return (False, None) else: GM.Globals[GM.CREDENTIALS_SCOPES] = set(jsonDict.pop('scopes', API.REQUIRED_SCOPES)) - token_expiry = jsonDict.get('token_expiry', m.REFRESH_EXPIRY) + token_expiry = jsonDict.get('token_expiry', _gam().REFRESH_EXPIRY) if GC.Values[GC.TRUNCATE_CLIENT_ID]: # chop off .apps.googleusercontent.com suffix as it's not needed and we need to keep things short for the Auth URL. jsonDict['client_id'] = re.sub(r'\.apps\.googleusercontent\.com$', '', jsonDict['client_id']) @@ -349,19 +343,18 @@ def getOauth2TxtCredentials(exitOnError=True, api=None, noDASA=False, refreshOnl creds.token = jsonDict['access_token'] creds._id_token = jsonDict['id_token_jwt'] GM.Globals[GM.DECODED_ID_TOKEN] = jsonDict['id_token'] - creds.expiry = arrow.Arrow.strptime(token_expiry, m.YYYYMMDDTHHMMSSZ_FORMAT, tzinfo='UTC').naive + creds.expiry = arrow.Arrow.strptime(token_expiry, _gam().YYYYMMDDTHHMMSSZ_FORMAT, tzinfo='UTC').naive return (not noScopes, creds) if jsonDict and exitOnError: - m.invalidOauth2TxtExit(Msg.INVALID) + _gam().invalidOauth2TxtExit(Msg.INVALID) except (IndexError, KeyError, SyntaxError, TypeError, ValueError) as e: if exitOnError: - m.invalidOauth2TxtExit(str(e)) + _gam().invalidOauth2TxtExit(str(e)) if exitOnError: - m.systemErrorExit(m.OAUTH2_TXT_REQUIRED_RC, Msg.NO_CLIENT_ACCESS_ALLOWED) + _gam().systemErrorExit(_gam().OAUTH2_TXT_REQUIRED_RC, Msg.NO_CLIENT_ACCESS_ALLOWED) return (False, None) def _getValueFromOAuth(field, credentials=None): - m = _getMain() if not GM.Globals[GM.DECODED_ID_TOKEN]: request = transportCreateRequest() if credentials is None: @@ -373,9 +366,9 @@ def _getValueFromOAuth(field, credentials=None): clock_skew_in_seconds=GC.Values[GC.CLOCK_SKEW_IN_SECONDS]) except ValueError as e: if 'Token used too early' in str(e): - m.stderrErrorMsg(Msg.PLEASE_CORRECT_YOUR_SYSTEM_TIME) - m.systemErrorExit(m.SYSTEM_ERROR_RC, str(e)) - return GM.Globals[GM.DECODED_ID_TOKEN].get(field, m.UNKNOWN) + _gam().stderrErrorMsg(Msg.PLEASE_CORRECT_YOUR_SYSTEM_TIME) + _gam().systemErrorExit(_gam().SYSTEM_ERROR_RC, str(e)) + return GM.Globals[GM.DECODED_ID_TOKEN].get(field, _gam().UNKNOWN) def _getAdminEmail(): if GC.Values[GC.ADMIN_EMAIL]: @@ -383,7 +376,6 @@ def _getAdminEmail(): return _getValueFromOAuth('email') def writeClientCredentials(creds, filename): - m = _getMain() creds_data = { 'client_id': creds.client_id, 'client_secret': creds.client_secret, @@ -391,28 +383,27 @@ def writeClientCredentials(creds, filename): 'refresh_token': creds.refresh_token, 'scopes': sorted(creds.scopes or GM.Globals[GM.CREDENTIALS_SCOPES]), 'token': creds.token, - 'token_expiry': creds.expiry.strftime(m.YYYYMMDDTHHMMSSZ_FORMAT), + 'token_expiry': creds.expiry.strftime(_gam().YYYYMMDDTHHMMSSZ_FORMAT), 'token_uri': creds.token_uri, } expected_iss = ['https://accounts.google.com', 'accounts.google.com'] if _getValueFromOAuth('iss', creds) not in expected_iss: - m.systemErrorExit(m.OAUTH2_TXT_REQUIRED_RC, f'Wrong OAuth 2.0 credentials issuer. Got {_getValueFromOAuth("iss", creds)} expected one of {", ".join(expected_iss)}') + _gam().systemErrorExit(_gam().OAUTH2_TXT_REQUIRED_RC, f'Wrong OAuth 2.0 credentials issuer. Got {_getValueFromOAuth("iss", creds)} expected one of {", ".join(expected_iss)}') request = transportCreateRequest() try: creds_data['decoded_id_token'] = google.oauth2.id_token.verify_oauth2_token(creds.id_token, request, clock_skew_in_seconds=GC.Values[GC.CLOCK_SKEW_IN_SECONDS]) except ValueError as e: if 'Token used too early' in str(e): - m.stderrErrorMsg(Msg.PLEASE_CORRECT_YOUR_SYSTEM_TIME) - m.systemErrorExit(m.SYSTEM_ERROR_RC, str(e)) + _gam().stderrErrorMsg(Msg.PLEASE_CORRECT_YOUR_SYSTEM_TIME) + _gam().systemErrorExit(_gam().SYSTEM_ERROR_RC, str(e)) GM.Globals[GM.DECODED_ID_TOKEN] = creds_data['decoded_id_token'] if filename != '-': - m.writeFile(filename, json.dumps(creds_data, indent=2, sort_keys=True)+'\n') + _gam().writeFile(filename, json.dumps(creds_data, indent=2, sort_keys=True)+'\n') else: - m.writeStdout(json.dumps(creds_data, ensure_ascii=False, indent=2, sort_keys=True)+'\n') + _gam().writeStdout(json.dumps(creds_data, ensure_ascii=False, indent=2, sort_keys=True)+'\n') def shortenURL(long_url): - m = _getMain() if GC.Values[GC.NO_SHORT_URLS]: return long_url httpObj = getHttpObj(timeout=10) @@ -421,7 +412,7 @@ def shortenURL(long_url): resp, content = httpObj.request(URL_SHORTENER_ENDPOINT, 'POST', payload, headers={'Content-Type': 'application/json', - 'User-Agent': m.GAM_USER_AGENT}) + 'User-Agent': _gam().GAM_USER_AGENT}) except: return long_url if resp.status != 200: @@ -440,13 +431,12 @@ def runSqliteQuery(db_file, query): return curr.fetchone()[0] def refreshCredentialsWithReauth(credentials): - m = _getMain() def gcloudError(): - m.writeStderr(f'Failed to run gcloud as {admin_email}. Please make sure it\'s setup') + _gam().writeStderr(f'Failed to run gcloud as {admin_email}. Please make sure it\'s setup') e = Msg.REAUTHENTICATION_IS_NEEDED handleOAuthTokenError(e, False) - m.writeStderr(Msg.CALLING_GCLOUD_FOR_REAUTH) + _gam().writeStderr(Msg.CALLING_GCLOUD_FOR_REAUTH) if 'termios' in sys.modules: import termios old_settings = termios.tcgetattr(sys.stdin) @@ -454,7 +444,7 @@ def refreshCredentialsWithReauth(credentials): # First makes sure gcloud has a valid access token and thus # should also have a valid RAPT token try: - devnull = open(os.devnull, 'w', encoding=m.UTF8) + devnull = open(os.devnull, 'w', encoding=_gam().UTF8) subprocess.run(['gcloud', 'auth', 'print-identity-token', @@ -472,7 +462,7 @@ def refreshCredentialsWithReauth(credentials): if 'termios' in sys.modules: import termios termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) - m.printBlankLine() + _gam().printBlankLine() raise KeyboardInterrupt from e token_path = gcloud_path_result.stdout.decode().strip() if not token_path: @@ -484,19 +474,18 @@ def refreshCredentialsWithReauth(credentials): except TypeError: gcloudError() if not credentials._rapt_token: - m.systemErrorExit(m.SYSTEM_ERROR_RC, + _gam().systemErrorExit(_gam().SYSTEM_ERROR_RC, 'Failed to retrieve reauth token from gcloud. You may need to wait until gcloud is also prompted for reauth.') def getClientCredentials(forceRefresh=False, forceWrite=False, filename=None, api=None, noDASA=False, refreshOnly=False, noScopes=False): """Gets OAuth2 credentials which are guaranteed to be fresh and valid. Locks during read and possible write so that only one process will attempt refresh/write when running in parallel. """ - m = _getMain() lock = FileLock(GM.Globals[GM.OAUTH2_TXT_LOCK], mode=GC.Values[GC.OAUTH2_TXT_LOCK_MODE]) with lock: writeCreds, credentials = getOauth2TxtCredentials(api=api, noDASA=noDASA, refreshOnly=refreshOnly, noScopes=noScopes) if not credentials: - m.invalidOauth2TxtExit('') + _gam().invalidOauth2TxtExit('') if credentials.expired or forceRefresh: triesLimit = 3 for n in range(1, triesLimit+1): @@ -507,7 +496,7 @@ def getClientCredentials(forceRefresh=False, forceWrite=False, filename=None, ap break except (httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if n != triesLimit: - waitOnFailure(n, triesLimit, m.NETWORK_ERROR_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().NETWORK_ERROR_RC, str(e)) continue handleServerError(e) except google.auth.exceptions.RefreshError as e: @@ -522,14 +511,13 @@ def getClientCredentials(forceRefresh=False, forceWrite=False, filename=None, ap return credentials def waitOnFailure(n, triesLimit, error_code, error_message): - m = _getMain() delta = min(2 ** n, 60)+float(random.randint(1, 1000))/1000 if n > 3: - m.writeStderr(f'Temporary error: {error_code} - {error_message}, Backing off: {int(delta)} seconds, Retry: {n}/{triesLimit}\n') - m.flushStderr() + _gam().writeStderr(f'Temporary error: {error_code} - {error_message}, Backing off: {int(delta)} seconds, Retry: {n}/{triesLimit}\n') + _gam().flushStderr() time.sleep(delta) if GC.Values[GC.SHOW_API_CALLS_RETRY_DATA]: - m.incrAPICallsRetryData(error_message, delta) + _gam().incrAPICallsRetryData(error_message, delta) def clearServiceCache(service): if hasattr(service._http, 'http') and hasattr(service._http.http, 'cache'): @@ -551,7 +539,6 @@ def getAPIService(api, httpObj): discoveryServiceUrl=DISCOVERY_URIS[v2discovery], static_discovery=False) def getService(api, httpObj): - m = _getMain() hasLocalJSON = API.hasLocalJSON(api) api, version, v2discovery = API.getVersion(api) if api in GM.Globals[GM.CURRENT_API_SERVICES] and version in GM.Globals[GM.CURRENT_API_SERVICES][api]: @@ -577,22 +564,22 @@ def getService(api, httpObj): clearServiceCache(service) return service except googleapiclient.errors.UnknownApiNameOrVersion as e: - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, Msg.UNKNOWN_API_OR_VERSION.format(str(e), m.__author__)) + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, Msg.UNKNOWN_API_OR_VERSION.format(str(e), _gam().__author__)) except (googleapiclient.errors.InvalidJsonError, KeyError, ValueError) as e: if n != triesLimit: - waitOnFailure(n, triesLimit, m.INVALID_JSON_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().INVALID_JSON_RC, str(e)) continue - m.systemErrorExit(m.INVALID_JSON_RC, str(e)) + _gam().systemErrorExit(_gam().INVALID_JSON_RC, str(e)) except (http.client.ResponseNotReady, OSError, googleapiclient.errors.HttpError) as e: errMsg = f'Connection error: {str(e) or repr(e)}' if n != triesLimit: - waitOnFailure(n, triesLimit, m.SOCKET_ERROR_RC, errMsg) + waitOnFailure(n, triesLimit, _gam().SOCKET_ERROR_RC, errMsg) continue - m.systemErrorExit(m.SOCKET_ERROR_RC, errMsg) + _gam().systemErrorExit(_gam().SOCKET_ERROR_RC, errMsg) except (httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if n != triesLimit: httpObj.connections = {} - waitOnFailure(n, triesLimit, m.NETWORK_ERROR_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().NETWORK_ERROR_RC, str(e)) continue handleServerError(e) disc_file, discovery = readDiscoveryFile(f'{api}-{version}') @@ -604,9 +591,9 @@ def getService(api, httpObj): clearServiceCache(service) return service except (googleapiclient.errors.InvalidJsonError, KeyError, ValueError) as e: - m.invalidDiscoveryJsonExit(disc_file, str(e)) + _gam().invalidDiscoveryJsonExit(disc_file, str(e)) except IOError as e: - m.systemErrorExit(m.FILE_ERROR_RC, str(e)) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, str(e)) def defaultSvcAcctScopes(): scopesList = API.getSvcAcctScopesList(GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY], False) @@ -622,17 +609,16 @@ def defaultSvcAcctScopes(): return saScopes def _getSvcAcctData(): - m = _getMain() if not GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]: - jsonData = m.readFile(GC.Values[GC.OAUTH2SERVICE_JSON], continueOnError=True, displayError=True) + jsonData = _gam().readFile(GC.Values[GC.OAUTH2SERVICE_JSON], continueOnError=True, displayError=True) if not jsonData: - m.invalidOauth2serviceJsonExit(Msg.NO_DATA) + _gam().invalidOauth2serviceJsonExit(Msg.NO_DATA) try: GM.Globals[GM.OAUTH2SERVICE_JSON_DATA] = json.loads(jsonData) except (IndexError, KeyError, SyntaxError, TypeError, ValueError) as e: - m.invalidOauth2serviceJsonExit(str(e)) + _gam().invalidOauth2serviceJsonExit(str(e)) if not GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]: - m.systemErrorExit(m.OAUTH2SERVICE_JSON_REQUIRED_RC, Msg.NO_SVCACCT_ACCESS_ALLOWED) + _gam().systemErrorExit(_gam().OAUTH2SERVICE_JSON_REQUIRED_RC, Msg.NO_SVCACCT_ACCESS_ALLOWED) requiredFields = ['client_email', 'client_id', 'project_id', 'token_uri'] key_type = GM.Globals[GM.OAUTH2SERVICE_JSON_DATA].get('key_type', 'default') if key_type == 'default': @@ -642,7 +628,7 @@ def _getSvcAcctData(): if field not in GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]: missingFields.append(field) if missingFields: - m.invalidOauth2serviceJsonExit(Msg.MISSING_FIELDS.format(','.join(missingFields))) + _gam().invalidOauth2serviceJsonExit(Msg.MISSING_FIELDS.format(','.join(missingFields))) # Some old oauth2service.json files have: 'https://accounts.google.com/o/oauth2/auth' which no longer works if GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]['token_uri'] == 'https://accounts.google.com/o/oauth2/auth': GM.Globals[GM.OAUTH2SERVICE_JSON_DATA]['token_uri'] = API.GOOGLE_OAUTH2_TOKEN_ENDPOINT @@ -654,7 +640,6 @@ def _getSvcAcctData(): GM.Globals[GM.SVCACCT_SCOPES] = GM.Globals[GM.OAUTH2SERVICE_JSON_DATA].pop(API.OAUTH2SA_SCOPES) def getSvcAcctCredentials(scopesOrAPI, userEmail, softErrors=False, forceOauth=False): - m = _getMain() _getSvcAcctData() if isinstance(scopesOrAPI, str): GM.Globals[GM.CURRENT_SVCACCT_API] = scopesOrAPI @@ -665,7 +650,7 @@ def getSvcAcctCredentials(scopesOrAPI, userEmail, softErrors=False, forceOauth=F if scopesOrAPI != API.CHAT_EVENTS and not GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES]: if softErrors: return None - m.SvcAcctAPIAccessDeniedExit() + _gam().SvcAcctAPIAccessDeniedExit() if scopesOrAPI in {API.PEOPLE, API.PEOPLE_DIRECTORY, API.PEOPLE_OTHERCONTACTS}: GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES].append(API.USERINFO_PROFILE_SCOPE) if scopesOrAPI in {API.PEOPLE_OTHERCONTACTS}: @@ -692,7 +677,7 @@ def getSvcAcctCredentials(scopesOrAPI, userEmail, softErrors=False, forceOauth=F except (ValueError, IndexError, KeyError) as e: if softErrors: return None - m.invalidOauth2serviceJsonExit(str(e)) + _gam().invalidOauth2serviceJsonExit(str(e)) credentials = credentials.with_scopes(GM.Globals[GM.CURRENT_SVCACCT_API_SCOPES]) else: audience = f'https://{scopesOrAPI}.googleapis.com/' @@ -714,7 +699,7 @@ def getSvcAcctCredentials(scopesOrAPI, userEmail, softErrors=False, forceOauth=F except (ValueError, IndexError, KeyError) as e: if softErrors: return None - m.invalidOauth2serviceJsonExit(str(e)) + _gam().invalidOauth2serviceJsonExit(str(e)) GM.Globals[GM.CURRENT_SVCACCT_USER] = userEmail if userEmail: credentials = credentials.with_subject(userEmail) @@ -723,7 +708,6 @@ def getSvcAcctCredentials(scopesOrAPI, userEmail, softErrors=False, forceOauth=F return credentials def getGDataOAuthToken(gdataObj, credentials=None): - m = _getMain() if not credentials: credentials = getClientCredentials(refreshOnly=True) try: @@ -742,14 +726,13 @@ def getGDataOAuthToken(gdataObj, credentials=None): GM.Globals[GM.ADMIN] = GM.Globals[GM.DECODED_ID_TOKEN].get('email', 'UNKNOWN').lower() GM.Globals[GM.OAUTH2_CLIENT_ID] = credentials.client_id gdataObj.domain = GC.Values[GC.DOMAIN] - gdataObj.source = m.GAM_USER_AGENT + gdataObj.source = _gam().GAM_USER_AGENT return True def checkGDataError(e, service): - m = _getMain() error = e.args reason = error[0].get('reason', '') - body = error[0].get('body', '').decode(m.UTF8) + body = error[0].get('body', '').decode(_gam().UTF8) # First check for errors that need special handling if reason in ['Token invalid - Invalid token: Stateless token expired', 'Token invalid - Invalid token: Token not found', 'gone']: keep_domain = service.domain @@ -758,7 +741,7 @@ def checkGDataError(e, service): return (GDATA.TOKEN_EXPIRED, reason) error_code = getattr(e, 'error_code', 600) if GC.Values[GC.DEBUG_LEVEL] > 0: - m.writeStdout(f'{m.ERROR_PREFIX} {error_code}: {reason}, {body}\n') + _gam().writeStdout(f'{_gam().ERROR_PREFIX} {error_code}: {reason}, {body}\n') if error_code == 600: if (body.startswith('Quota exceeded for the current request') or body.startswith('Quota exceeded for quota metric') or @@ -859,7 +842,6 @@ def callGData(service, function, bailOnInternalServerError=False, softErrors=False, throwErrors=None, retryErrors=None, triesLimit=0, **kwargs): - m = _getMain() if throwErrors is None: throwErrors = [] if retryErrors is None: @@ -869,7 +851,7 @@ def callGData(service, function, allRetryErrors = GDATA.NON_TERMINATING_ERRORS+retryErrors method = getattr(service, function) if GC.Values[GC.API_CALLS_RATE_CHECK]: - m.checkAPICallsRate() + _gam().checkAPICallsRate() for n in range(1, triesLimit+1): try: return method(**kwargs) @@ -886,14 +868,14 @@ def callGData(service, function, raise GDATA.ERROR_CODE_EXCEPTION_MAP[error_code](error_message) raise if softErrors: - m.stderrErrorMsg(f'{error_code} - {error_message}{["", ": Giving up."][n > 1]}') + _gam().stderrErrorMsg(f'{error_code} - {error_message}{["", ": Giving up."][n > 1]}') return None if error_code == GDATA.INSUFFICIENT_PERMISSIONS: - m.APIAccessDeniedExit() - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, f'{error_code} - {error_message}') + _gam().APIAccessDeniedExit() + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, f'{error_code} - {error_message}') except (httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if n != triesLimit: - waitOnFailure(n, triesLimit, m.NETWORK_ERROR_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().NETWORK_ERROR_RC, str(e)) continue handleServerError(e) except google.auth.exceptions.RefreshError as e: @@ -904,33 +886,31 @@ def callGData(service, function, except (http.client.ResponseNotReady, OSError) as e: errMsg = f'Connection error: {str(e) or repr(e)}' if n != triesLimit: - waitOnFailure(n, triesLimit, m.SOCKET_ERROR_RC, errMsg) + waitOnFailure(n, triesLimit, _gam().SOCKET_ERROR_RC, errMsg) continue if softErrors: - m.writeStderr(f'\n{m.ERROR_PREFIX}{errMsg} - Giving up.\n') + _gam().writeStderr(f'\n{_gam().ERROR_PREFIX}{errMsg} - Giving up.\n') return None - m.systemErrorExit(m.SOCKET_ERROR_RC, errMsg) + _gam().systemErrorExit(_gam().SOCKET_ERROR_RC, errMsg) def writeGotMessage(msg): - m = _getMain() if GC.Values[GC.SHOW_GETTINGS_GOT_NL]: - m.writeStderr(msg) + _gam().writeStderr(msg) else: - m.writeStderr('\r') + _gam().writeStderr('\r') msgLen = len(msg) if msgLen < GM.Globals[GM.LAST_GOT_MSG_LEN]: - m.writeStderr(msg+' '*(GM.Globals[GM.LAST_GOT_MSG_LEN]-msgLen)) + _gam().writeStderr(msg+' '*(GM.Globals[GM.LAST_GOT_MSG_LEN]-msgLen)) else: - m.writeStderr(msg) + _gam().writeStderr(msg) GM.Globals[GM.LAST_GOT_MSG_LEN] = msgLen - m.flushStderr() + _gam().flushStderr() def callGDataPages(service, function, pageMessage=None, softErrors=False, throwErrors=None, retryErrors=None, uri=None, **kwargs): - m = _getMain() Ent = _getEnt() if throwErrors is None: throwErrors = [] @@ -955,32 +935,30 @@ def callGDataPages(service, function, nextLink = None pageItems = 0 if pageMessage: - show_message = pageMessage.replace(m.TOTAL_ITEMS_MARKER, str(totalItems)) + show_message = pageMessage.replace(_gam().TOTAL_ITEMS_MARKER, str(totalItems)) writeGotMessage(show_message.format(Ent.ChooseGetting(totalItems))) if nextLink is None: if pageMessage and (pageMessage[-1] != '\n'): - m.writeStderr('\r\n') - m.flushStderr() + _gam().writeStderr('\r\n') + _gam().flushStderr() return allResults uri = nextLink.href if 'url_params' in kwargs: kwargs['url_params'].pop('start-index', None) def checkGAPIError(e, softErrors=False, retryOnHttpError=False, mapNotFound=True): - m = _getMain() - def makeErrorDict(code, reason, message): return {'error': {'code': code, 'errors': [{'reason': reason, 'message': message}]}} try: - error = json.loads(e.content.decode(m.UTF8)) + error = json.loads(e.content.decode(_gam().UTF8)) if GC.Values[GC.DEBUG_LEVEL] > 0: - m.writeStdout(f'{m.ERROR_PREFIX} JSON: {str(error)}\n') + _gam().writeStdout(f'{_gam().ERROR_PREFIX} JSON: {str(error)}\n') except (IndexError, KeyError, SyntaxError, TypeError, ValueError): - eContent = e.content.decode(m.UTF8) if isinstance(e.content, bytes) else e.content + eContent = e.content.decode(_gam().UTF8) if isinstance(e.content, bytes) else e.content lContent = eContent.lower() if GC.Values[GC.DEBUG_LEVEL] > 0: - m.writeStdout(f'{m.ERROR_PREFIX} HTTP: {str(eContent)}\n') + _gam().writeStdout(f'{_gam().ERROR_PREFIX} HTTP: {str(eContent)}\n') if eContent[0:15] != '': if (e.resp['status'] == '403') and (lContent.startswith('request rate higher than configured')): return (e.resp['status'], GAPI.QUOTA_EXCEEDED, eContent) @@ -993,7 +971,7 @@ def checkGAPIError(e, softErrors=False, retryOnHttpError=False, mapNotFound=True if (e.resp['status'] == '504') and ('gateway timeout' in lContent): return (e.resp['status'], GAPI.GATEWAY_TIMEOUT, eContent) else: - tg = m.HTML_TITLE_PATTERN.match(lContent) + tg = _gam().HTML_TITLE_PATTERN.match(lContent) lContent = tg.group(1) if tg else 'bad request' if (e.resp['status'] == '403') and ('invalid domain.' in lContent): error = makeErrorDict(403, GAPI.NOT_FOUND, 'Domain not found') @@ -1020,10 +998,10 @@ def checkGAPIError(e, softErrors=False, retryOnHttpError=False, mapNotFound=True elif retryOnHttpError: return (-1, None, eContent) elif softErrors: - m.stderrErrorMsg(eContent) + _gam().stderrErrorMsg(eContent) return (0, None, None) else: - m.systemErrorExit(m.HTTP_ERROR_RC, eContent) + _gam().systemErrorExit(_gam().HTTP_ERROR_RC, eContent) requiredScopes = '' wwwAuthenticate = e.resp.get('www-authenticate', '') if 'insufficient_scope' in wwwAuthenticate: @@ -1129,9 +1107,9 @@ def checkGAPIError(e, softErrors=False, retryOnHttpError=False, mapNotFound=True http_status = 400 error = makeErrorDict(http_status, GAPI.INVALID, message) else: - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, str(error)) + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, str(error)) else: - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, str(error)) + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, str(error)) try: reason = error['error']['errors'][0]['reason'] for messageItem in GAPI.REASON_MESSAGE_MAP.get(reason, []): @@ -1157,7 +1135,6 @@ def callGAPI(service, function, softErrors=False, mapNotFound=True, throwReasons=None, retryReasons=None, triesLimit=0, **kwargs): - m = _getMain() if throwReasons is None: throwReasons = [] if retryReasons is None: @@ -1168,7 +1145,7 @@ def callGAPI(service, function, method = getattr(service, function) svcparms = dict(list(kwargs.items())+GM.Globals[GM.EXTRA_ARGS_LIST]) if GC.Values[GC.API_CALLS_RATE_CHECK]: - m.checkAPICallsRate() + _gam().checkAPICallsRate() for n in range(1, triesLimit+1): try: return method(**svcparms).execute() @@ -1181,7 +1158,7 @@ def callGAPI(service, function, # service._http.credentials.refresh(getHttpObj()) service._http.credentials.refresh(transportCreateRequest()) except TypeError: - m.systemErrorExit(m.HTTP_ERROR_RC, message) + _gam().systemErrorExit(_gam().HTTP_ERROR_RC, message) continue if http_status == 0: return None @@ -1202,17 +1179,17 @@ def callGAPI(service, function, raise GAPI.REASON_EXCEPTION_MAP[reason](message) raise e if softErrors: - m.stderrErrorMsg(f'{http_status}: {reason} - {message}{["", ": Giving up."][n > 1]}') + _gam().stderrErrorMsg(f'{http_status}: {reason} - {message}{["", ": Giving up."][n > 1]}') return None if reason == GAPI.INSUFFICIENT_PERMISSIONS: - m.APIAccessDeniedExit() - m.systemErrorExit(m.HTTP_ERROR_RC, m.formatHTTPError(http_status, reason, message)) + _gam().APIAccessDeniedExit() + _gam().systemErrorExit(_gam().HTTP_ERROR_RC, _gam().formatHTTPError(http_status, reason, message)) except googleapiclient.errors.MediaUploadSizeError as e: raise e except (httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if n != triesLimit: service._http.connections = {} - waitOnFailure(n, triesLimit, m.NETWORK_ERROR_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().NETWORK_ERROR_RC, str(e)) continue handleServerError(e) except google.auth.exceptions.RefreshError as e: @@ -1223,23 +1200,22 @@ def callGAPI(service, function, except (http.client.ResponseNotReady, OSError) as e: errMsg = f'Connection error: {str(e) or repr(e)}' if n != triesLimit: - waitOnFailure(n, triesLimit, m.SOCKET_ERROR_RC, errMsg) + waitOnFailure(n, triesLimit, _gam().SOCKET_ERROR_RC, errMsg) continue if softErrors: - m.writeStderr(f'\n{m.ERROR_PREFIX}{errMsg} - Giving up.\n') + _gam().writeStderr(f'\n{_gam().ERROR_PREFIX}{errMsg} - Giving up.\n') return None - m.systemErrorExit(m.SOCKET_ERROR_RC, errMsg) + _gam().systemErrorExit(_gam().SOCKET_ERROR_RC, errMsg) except ValueError as e: if clearServiceCache(service): continue - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, str(e)) + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, str(e)) except TypeError as e: - m.systemErrorExit(m.GOOGLE_API_ERROR_RC, str(e)) + _gam().systemErrorExit(_gam().GOOGLE_API_ERROR_RC, str(e)) def _showGAPIpagesResult(results, pageItems, totalItems, pageMessage, messageAttribute, entityType): - m = _getMain() Ent = _getEnt() - showMessage = pageMessage.replace(m.TOTAL_ITEMS_MARKER, str(totalItems)) + showMessage = pageMessage.replace(_gam().TOTAL_ITEMS_MARKER, str(totalItems)) if pageItems: if messageAttribute: firstItem = results[0] if pageItems > 0 else {} @@ -1253,11 +1229,11 @@ def _showGAPIpagesResult(results, pageItems, totalItems, pageMessage, messageAtt lastItem = lastItem.get(attr, {}) firstItem = str(firstItem) lastItem = str(lastItem) - showMessage = showMessage.replace(m.FIRST_ITEM_MARKER, firstItem) - showMessage = showMessage.replace(m.LAST_ITEM_MARKER, lastItem) + showMessage = showMessage.replace(_gam().FIRST_ITEM_MARKER, firstItem) + showMessage = showMessage.replace(_gam().LAST_ITEM_MARKER, lastItem) else: - showMessage = showMessage.replace(m.FIRST_ITEM_MARKER, '') - showMessage = showMessage.replace(m.LAST_ITEM_MARKER, '') + showMessage = showMessage.replace(_gam().FIRST_ITEM_MARKER, '') + showMessage = showMessage.replace(_gam().LAST_ITEM_MARKER, '') writeGotMessage(showMessage.replace('{0}', str(Ent.Choose(entityType, totalItems)))) def _processGAPIpagesResult(results, items, allResults, totalItems, pageMessage, messageAttribute, entityType): @@ -1280,10 +1256,9 @@ def _processGAPIpagesResult(results, items, allResults, totalItems, pageMessage, return (pageToken, totalItems) def _finalizeGAPIpagesResult(pageMessage): - m = _getMain() if pageMessage and (pageMessage[-1] != '\n'): - m.writeStderr('\r\n') - m.flushStderr() + _gam().writeStderr('\r\n') + _gam().flushStderr() def _setMaxArgResults(maxItems, pageArgsInBody, kwargs): if pageArgsInBody: @@ -1403,25 +1378,23 @@ def callGAPIitems(service, function, items, return [] def readDiscoveryFile(api_version): - m = _getMain() disc_filename = f'{api_version}.json' disc_file = os.path.join(GM.Globals[GM.GAM_PATH], disc_filename) if hasattr(sys, '_MEIPASS'): - json_string = m.readFile(os.path.join(sys._MEIPASS, disc_filename), continueOnError=True, displayError=True) #pylint: disable=no-member + json_string = _gam().readFile(os.path.join(sys._MEIPASS, disc_filename), continueOnError=True, displayError=True) #pylint: disable=no-member elif os.path.isfile(disc_file): - json_string = m.readFile(disc_file, continueOnError=True, displayError=True) + json_string = _gam().readFile(disc_file, continueOnError=True, displayError=True) else: json_string = None if not json_string: - m.invalidDiscoveryJsonExit(disc_file, Msg.NO_DATA) + _gam().invalidDiscoveryJsonExit(disc_file, Msg.NO_DATA) try: discovery = json.loads(json_string) return (disc_file, discovery) except (IndexError, KeyError, SyntaxError, TypeError, ValueError) as e: - m.invalidDiscoveryJsonExit(disc_file, str(e)) + _gam().invalidDiscoveryJsonExit(disc_file, str(e)) def buildGAPIObject(api, credentials=None): - m = _getMain() if credentials is None: credentials = getClientCredentials(api=api, refreshOnly=True) httpObj = transportAuthorizedHttp(credentials, http=getHttpObj(cache=GM.Globals[GM.CACHE_DIR])) @@ -1433,7 +1406,7 @@ def buildGAPIObject(api, credentials=None): GM.Globals[GM.CURRENT_CLIENT_API] = api GM.Globals[GM.CURRENT_CLIENT_API_SCOPES] = API_Scopes.intersection(GM.Globals[GM.CREDENTIALS_SCOPES]) if api not in API.SCOPELESS_APIS and not GM.Globals[GM.CURRENT_CLIENT_API_SCOPES]: - m.systemErrorExit(m.NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(API.getAPIName(api))) + _gam().systemErrorExit(_gam().NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(API.getAPIName(api))) if not GC.Values[GC.DOMAIN]: GC.Values[GC.DOMAIN] = GM.Globals[GM.DECODED_ID_TOKEN].get('hd', 'UNKNOWN').lower() if not GC.Values[GC.CUSTOMER_ID]: @@ -1443,10 +1416,9 @@ def buildGAPIObject(api, credentials=None): return service def getSaUser(user): - m = _getMain() currentClientAPI = GM.Globals[GM.CURRENT_CLIENT_API] currentClientAPIScopes = GM.Globals[GM.CURRENT_CLIENT_API_SCOPES] - userEmail = m.convertUIDtoEmailAddress(user) if user else None + userEmail = _gam().convertUIDtoEmailAddress(user) if user else None GM.Globals[GM.CURRENT_CLIENT_API] = currentClientAPI GM.Globals[GM.CURRENT_CLIENT_API_SCOPES] = currentClientAPIScopes return userEmail @@ -1458,7 +1430,6 @@ def chooseSaAPI(api1, api2): return api2 def buildGAPIServiceObject(api, user, i=0, count=0, displayError=True): - m = _getMain() userEmail = getSaUser(user) if GM.Globals[GM.HTTP_OBJECT] is None: GM.Globals[GM.HTTP_OBJECT] = getHttpObj(cache=GM.Globals[GM.CACHE_DIR]) @@ -1475,7 +1446,7 @@ def buildGAPIServiceObject(api, user, i=0, count=0, displayError=True): except (httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if n != triesLimit: httpObj.connections = {} - waitOnFailure(n, triesLimit, m.NETWORK_ERROR_RC, str(e)) + waitOnFailure(n, triesLimit, _gam().NETWORK_ERROR_RC, str(e)) continue handleServerError(e) except google.auth.exceptions.RefreshError as e: @@ -1485,12 +1456,12 @@ def buildGAPIServiceObject(api, user, i=0, count=0, displayError=True): if isinstance(e, str): eContent = e else: - eContent = e.content.decode(m.UTF8) if isinstance(e.content, bytes) else e.content + eContent = e.content.decode(_gam().UTF8) if isinstance(e.content, bytes) else e.content if eContent[0:15] == '': if GC.Values[GC.DEBUG_LEVEL] > 0: - m.writeStdout(f'{m.ERROR_PREFIX} HTTP: {str(eContent)}\n') + _gam().writeStdout(f'{_gam().ERROR_PREFIX} HTTP: {str(eContent)}\n') lContent = eContent.lower() - tg = m.HTML_TITLE_PATTERN.match(lContent) + tg = _gam().HTML_TITLE_PATTERN.match(lContent) lContent = tg.group(1) if tg else '' if lContent.startswith('Error 502 (Server Error)'): time.sleep(30) @@ -1504,12 +1475,11 @@ def buildGAPIObjectNoAuthentication(api): return service def initGDataObject(gdataObj, api): - m = _getMain() GM.Globals[GM.CURRENT_CLIENT_API] = api credentials = getClientCredentials(noDASA=True, refreshOnly=True) GM.Globals[GM.CURRENT_CLIENT_API_SCOPES] = API.getClientScopesSet(api).intersection(GM.Globals[GM.CREDENTIALS_SCOPES]) if not GM.Globals[GM.CURRENT_CLIENT_API_SCOPES]: - m.systemErrorExit(m.NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(API.getAPIName(api))) + _gam().systemErrorExit(_gam().NO_SCOPES_FOR_API_RC, Msg.NO_SCOPES_FOR_API.format(API.getAPIName(api))) getGDataOAuthToken(gdataObj, credentials) if GC.Values[GC.DEBUG_LEVEL] > 0: gdataObj.debug = True diff --git a/src/gam/util/args.py b/src/gam/util/args.py index be6a98da..d2b962f0 100644 --- a/src/gam/util/args.py +++ b/src/gam/util/args.py @@ -115,8 +115,7 @@ def _getEnt(): return sys.modules['gam'].Ent # Lazy accessor for main module -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] # --- Constants duplicated from __init__.py --- # These are simple literals that never change, duplicated to avoid diff --git a/src/gam/util/batch.py b/src/gam/util/batch.py index 2a87979c..4c0f15a3 100644 --- a/src/gam/util/batch.py +++ b/src/gam/util/batch.py @@ -27,8 +27,7 @@ from gamlib import glmsgs as Msg from util.csv_pf import CSVPrintFile -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] Cmd = glclargs.GamCLArgs() @@ -72,18 +71,18 @@ def CSVFileQueueHandler(mpQueue, mpQueueStdout, mpQueueStderr, csvPF, datetimeNo def reopenSTDFile(stdtype): if GM.Globals[stdtype][GM.REDIRECT_NAME] == 'null': - GM.Globals[stdtype][GM.REDIRECT_FD] = open(os.devnull, GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=_getMain().UTF8) + GM.Globals[stdtype][GM.REDIRECT_FD] = open(os.devnull, GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=_gam().UTF8) elif GM.Globals[stdtype][GM.REDIRECT_NAME] == '-': GM.Globals[stdtype][GM.REDIRECT_FD] = os.fdopen(os.dup([sys.stderr.fileno(), sys.stdout.fileno()][stdtype == GM.STDOUT]), GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=GM.Globals[GM.SYS_ENCODING]) elif stdtype == GM.STDERR and GM.Globals[stdtype][GM.REDIRECT_NAME] == 'stdout': GM.Globals[stdtype][GM.REDIRECT_FD] = GM.Globals[GM.STDOUT][GM.REDIRECT_FD] else: - GM.Globals[stdtype][GM.REDIRECT_FD] = _getMain().openFile(GM.Globals[stdtype][GM.REDIRECT_NAME], GM.Globals[stdtype][GM.REDIRECT_MODE]) + GM.Globals[stdtype][GM.REDIRECT_FD] = _gam().openFile(GM.Globals[stdtype][GM.REDIRECT_NAME], GM.Globals[stdtype][GM.REDIRECT_MODE]) if stdtype == GM.STDERR and GM.Globals[stdtype][GM.REDIRECT_NAME] == 'stdout': GM.Globals[stdtype][GM.REDIRECT_MULTI_FD] = GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD] else: - GM.Globals[stdtype][GM.REDIRECT_MULTI_FD] = GM.Globals[stdtype][GM.REDIRECT_FD] if not GM.Globals[stdtype][GM.REDIRECT_MULTIPROCESS] else _getMain().StringIOobject() + GM.Globals[stdtype][GM.REDIRECT_MULTI_FD] = GM.Globals[stdtype][GM.REDIRECT_FD] if not GM.Globals[stdtype][GM.REDIRECT_MULTIPROCESS] else _gam().StringIOobject() GM.Globals[GM.DATETIME_NOW] = datetimeNow GC.Values[GC.TIMEZONE] = tzinfo @@ -158,11 +157,11 @@ def CSVFileQueueHandler(mpQueue, mpQueueStdout, mpQueueStderr, csvPF, datetimeNo if mpQueueStdout: mpQueueStdout.put((0, GM.REDIRECT_QUEUE_DATA, GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD].getvalue())) else: - _getMain().flushStdout() + _gam().flushStdout() if mpQueueStderr and mpQueueStderr is not mpQueueStdout: mpQueueStderr.put((0, GM.REDIRECT_QUEUE_DATA, GM.Globals[GM.STDERR][GM.REDIRECT_MULTI_FD].getvalue())) else: - _getMain().flushStderr() + _gam().flushStderr() def initializeCSVFileQueueHandler(mpManager, mpQueueStdout, mpQueueStderr): mpQueue = mpManager.Queue() @@ -178,7 +177,7 @@ def terminateCSVFileQueueHandler(mpQueue, mpQueueHandler): GM.Globals[GM.PARSER] = None GM.Globals[GM.CSVFILE][GM.REDIRECT_QUEUE] = None if multiprocessing.get_start_method() != 'fork': - mpQueue.put((GM.REDIRECT_QUEUE_ARGS, _getMain().Cmd.AllArguments())) + mpQueue.put((GM.REDIRECT_QUEUE_ARGS, _gam().Cmd.AllArguments())) savedValues = saveNonPickleableValues() mpQueue.put((GM.REDIRECT_QUEUE_GLOBALS, GM.Globals)) restoreNonPickleableValues(savedValues) @@ -200,27 +199,27 @@ def StdQueueHandler(mpQueue, stdtype, gmGlobals, gcValues): if data[1] is not None: _writeData(data[1]) if GC.Values[GC.SHOW_MULTIPROCESS_INFO]: - _writeData(PROCESS_MSG.format(pidData[pid]['queue'], pid, 'End', _getMain().currentISOformatTimeStamp(), data[0], pidData[pid]['cmd'])) + _writeData(PROCESS_MSG.format(pidData[pid]['queue'], pid, 'End', _gam().currentISOformatTimeStamp(), data[0], pidData[pid]['cmd'])) fd.flush() except IOError as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, _getMain().fdErrorMessage(fd, GM.Globals[stdtype][GM.REDIRECT_NAME], e)) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, _gam().fdErrorMessage(fd, GM.Globals[stdtype][GM.REDIRECT_NAME], e)) if multiprocessing.get_start_method() != 'fork': signal.signal(signal.SIGINT, signal.SIG_IGN) GM.Globals = gmGlobals.copy() GC.Values = gcValues.copy() - pid0DataItem = [_getMain().KEYBOARD_INTERRUPT_RC, None] + pid0DataItem = [_gam().KEYBOARD_INTERRUPT_RC, None] pidData = {} if multiprocessing.get_start_method() != 'fork': if GM.Globals[stdtype][GM.REDIRECT_NAME] == 'null': - fd = open(os.devnull, GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=_getMain().UTF8) + fd = open(os.devnull, GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=_gam().UTF8) elif GM.Globals[stdtype][GM.REDIRECT_NAME] == '-': fd = os.fdopen(os.dup([sys.stderr.fileno(), sys.stdout.fileno()][GM.Globals[stdtype][GM.REDIRECT_QUEUE] == 'stdout']), GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=GM.Globals[GM.SYS_ENCODING]) elif GM.Globals[stdtype][GM.REDIRECT_NAME] == 'stdout' and GM.Globals[stdtype][GM.REDIRECT_QUEUE] == 'stderr': fd = os.fdopen(os.dup(sys.stdout.fileno()), GM.Globals[stdtype][GM.REDIRECT_MODE], encoding=GM.Globals[GM.SYS_ENCODING]) else: - fd = _getMain().openFile(GM.Globals[stdtype][GM.REDIRECT_NAME], GM.Globals[stdtype][GM.REDIRECT_MODE]) + fd = _gam().openFile(GM.Globals[stdtype][GM.REDIRECT_NAME], GM.Globals[stdtype][GM.REDIRECT_MODE]) else: fd = GM.Globals[stdtype][GM.REDIRECT_FD] while True: @@ -230,7 +229,7 @@ def StdQueueHandler(mpQueue, stdtype, gmGlobals, gcValues): break if dataType == GM.REDIRECT_QUEUE_START: pidData[pid] = {'queue': GM.Globals[stdtype][GM.REDIRECT_QUEUE], - 'start': _getMain().currentISOformatTimeStamp(), + 'start': _gam().currentISOformatTimeStamp(), 'cmd': Cmd.QuotedArgumentList(dataItem)} if pid == 0 and GC.Values[GC.SHOW_MULTIPROCESS_INFO]: fd.write(PROCESS_MSG.format(pidData[pid]['queue'], pid, 'Start', pidData[pid]['start'], 0, pidData[pid]['cmd'])) @@ -246,7 +245,7 @@ def StdQueueHandler(mpQueue, stdtype, gmGlobals, gcValues): break for pid in pidData: if pid != 0: - _writePidData(pid, [_getMain().KEYBOARD_INTERRUPT_RC, None]) + _writePidData(pid, [_gam().KEYBOARD_INTERRUPT_RC, None]) _writePidData(0, pid0DataItem) if fd not in [sys.stdout, sys.stderr]: try: @@ -267,7 +266,7 @@ def batchWriteStderr(data): sys.stderr.write(data) sys.stderr.flush() except IOError as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, _getMain().fileErrorMessage('stderr', e)) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, _gam().fileErrorMessage('stderr', e)) def writeStdQueueHandler(mpQueue, item): while True: @@ -276,7 +275,7 @@ def writeStdQueueHandler(mpQueue, item): return except Exception as e: time.sleep(1) - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{item[0]}/{GM.Globals[GM.NUM_BATCH_ITEMS]},Error,{str(e)}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{item[0]}/{GM.Globals[GM.NUM_BATCH_ITEMS]},Error,{str(e)}\n') def terminateStdQueueHandler(mpQueue, mpQueueHandler): mpQueue.put((0, GM.REDIRECT_QUEUE_EOF, None)) @@ -339,7 +338,7 @@ def ProcessGAMCommandMulti(pid, numItems, logCmd, mpQueueCSVFile, mpQueueStdout, if mpQueueCSVFile: GM.Globals[GM.CSVFILE][GM.REDIRECT_QUEUE] = mpQueueCSVFile if mpQueueStdout: - GM.Globals[GM.STDOUT] = {GM.REDIRECT_NAME: '', GM.REDIRECT_FD: None, GM.REDIRECT_MULTI_FD: _getMain().StringIOobject()} + GM.Globals[GM.STDOUT] = {GM.REDIRECT_NAME: '', GM.REDIRECT_FD: None, GM.REDIRECT_MULTI_FD: _gam().StringIOobject()} if debugLevel: sys.stdout = GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD] # mpQueueStdout.put((pid, GM.REDIRECT_QUEUE_START, args)) @@ -348,14 +347,14 @@ def ProcessGAMCommandMulti(pid, numItems, logCmd, mpQueueCSVFile, mpQueueStdout, GM.Globals[GM.STDOUT] = {} if mpQueueStderr: if mpQueueStderr is not mpQueueStdout: - GM.Globals[GM.STDERR] = {GM.REDIRECT_NAME: '', GM.REDIRECT_FD: None, GM.REDIRECT_MULTI_FD: _getMain().StringIOobject()} + GM.Globals[GM.STDERR] = {GM.REDIRECT_NAME: '', GM.REDIRECT_FD: None, GM.REDIRECT_MULTI_FD: _gam().StringIOobject()} # mpQueueStderr.put((pid, GM.REDIRECT_QUEUE_START, args)) writeStdQueueHandler(mpQueueStderr, (pid, GM.REDIRECT_QUEUE_START, args)) else: GM.Globals[GM.STDERR][GM.REDIRECT_MULTI_FD] = GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD] else: GM.Globals[GM.STDERR] = {} - sysRC = _getMain().ProcessGAMCommand(args) + sysRC = _gam().ProcessGAMCommand(args) with mplock: if mpQueueStdout: # mpQueueStdout.put((pid, GM.REDIRECT_QUEUE_END, [sysRC, GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD].getvalue()])) @@ -405,9 +404,9 @@ def MultiprocessGAMCommands(items, showCmds): def poolCallback(result): poolProcessResults[0] -= 1 if showCmds: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{result[0]}/{numItems},End,{result[1]},{result[2]}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{result[0]}/{numItems},End,{result[1]},{result[2]}\n') if GM.Globals[GM.CMDLOG_LOGGER]: - GM.Globals[GM.CMDLOG_LOGGER].info(f'{_getMain().currentISOformatTimeStamp()},{result[1]},{result[2]}') + GM.Globals[GM.CMDLOG_LOGGER].info(f'{_gam().currentISOformatTimeStamp()},{result[1]},{result[2]}') if GM.Globals[GM.MULTIPROCESS_EXIT_CONDITION] is not None and checkChildProcessRC(result[1]): GM.Globals[GM.MULTIPROCESS_EXIT_PROCESSING] = True @@ -418,8 +417,8 @@ def MultiprocessGAMCommands(items, showCmds): def handleControlC(source): nonlocal controlC batchWriteStderr(f'Control-C (Multiprocess-{source})\n') - _getMain().setSysExitRC(_getMain().KEYBOARD_INTERRUPT_RC) - batchWriteStderr(Msg.BATCH_CSV_TERMINATE_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + _gam().setSysExitRC(_gam().KEYBOARD_INTERRUPT_RC) + batchWriteStderr(Msg.BATCH_CSV_TERMINATE_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, poolProcessResults[0], PROCESS_PLURAL_SINGULAR[poolProcessResults[0] == 1])) pool.terminate() @@ -445,21 +444,21 @@ def MultiprocessGAMCommands(items, showCmds): else: pool = multiprocessing.Pool(processes=numPoolProcesses, initializer=initGamWorker, initargs=(l,), maxtasksperchild=200) except IOError as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, e) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, e) except AssertionError as e: - _getMain().Cmd.SetLocation(0) - _getMain().usageErrorExit(str(e)) + _gam().Cmd.SetLocation(0) + _gam().usageErrorExit(str(e)) if multiprocessing.get_start_method() != 'fork': savedValues = saveNonPickleableValues() if GM.Globals[GM.STDOUT][GM.REDIRECT_MULTIPROCESS]: mpQueueStdout, mpQueueHandlerStdout = initializeStdQueueHandler(mpManager, GM.STDOUT, GM.Globals, GC.Values) - mpQueueStdout.put((0, GM.REDIRECT_QUEUE_START, _getMain().Cmd.AllArguments())) + mpQueueStdout.put((0, GM.REDIRECT_QUEUE_START, _gam().Cmd.AllArguments())) else: mpQueueStdout = None if GM.Globals[GM.STDERR][GM.REDIRECT_MULTIPROCESS]: if GM.Globals[GM.STDERR][GM.REDIRECT_NAME] != 'stdout': mpQueueStderr, mpQueueHandlerStderr = initializeStdQueueHandler(mpManager, GM.STDERR, GM.Globals, GC.Values) - mpQueueStderr.put((0, GM.REDIRECT_QUEUE_START, _getMain().Cmd.AllArguments())) + mpQueueStderr.put((0, GM.REDIRECT_QUEUE_START, _gam().Cmd.AllArguments())) else: mpQueueStderr = mpQueueStdout else: @@ -479,7 +478,7 @@ def MultiprocessGAMCommands(items, showCmds): # signal.signal(signal.SIGINT, origSigintHandler) controlC = False signal.signal(signal.SIGINT, signal_handler) - batchWriteStderr(Msg.USING_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.USING_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, numPoolProcesses, PROCESS_PLURAL_SINGULAR[numPoolProcesses == 1])) try: @@ -491,7 +490,7 @@ def MultiprocessGAMCommands(items, showCmds): if controlC: break if item[0] == Cmd.COMMIT_BATCH_CMD: - batchWriteStderr(Msg.COMMIT_BATCH_WAIT_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.COMMIT_BATCH_WAIT_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, poolProcessResults[0], PROCESS_PLURAL_SINGULAR[poolProcessResults[0] == 1])) while poolProcessResults[0] > 0: @@ -503,24 +502,24 @@ def MultiprocessGAMCommands(items, showCmds): completedProcesses.append(p) for p in completedProcesses: del poolProcessResults[p] - batchWriteStderr(Msg.COMMIT_BATCH_COMPLETE.format(_getMain().currentISOformatTimeStamp(), numItems, Msg.PROCESSES)) + batchWriteStderr(Msg.COMMIT_BATCH_COMPLETE.format(_gam().currentISOformatTimeStamp(), numItems, Msg.PROCESSES)) if len(item) > 1: - _getMain().readStdin(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}') + _gam().readStdin(f'{_gam().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}') continue if item[0] == Cmd.PRINT_CMD: batchWriteStderr(Cmd.QuotedArgumentList(item[1:])+'\n') continue if item[0] == Cmd.SLEEP_CMD: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},Sleepiing {item[1]} seconds\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},0/{numItems},Sleepiing {item[1]} seconds\n') time.sleep(int(item[1])) continue pid += 1 if not showCmds and ((pid % 100 == 0) or (pid == numItems)): - batchWriteStderr(Msg.PROCESSING_ITEM_N_OF_M.format(_getMain().currentISOformatTimeStamp(), pid, numItems)) + batchWriteStderr(Msg.PROCESSING_ITEM_N_OF_M.format(_gam().currentISOformatTimeStamp(), pid, numItems)) if showCmds or GM.Globals[GM.CMDLOG_LOGGER]: logCmd = Cmd.QuotedArgumentList(item) if showCmds: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},Start,0,{logCmd}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},Start,0,{logCmd}\n') else: logCmd = '' poolProcessResults[pid] = pool.apply_async(ProcessGAMCommandMulti, @@ -566,7 +565,7 @@ def MultiprocessGAMCommands(items, showCmds): else: waitRemaining = 'unlimited' while poolProcessResults[0] > 0: - batchWriteStderr(Msg.BATCH_CSV_WAIT_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.BATCH_CSV_WAIT_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, poolProcessResults[0], PROCESS_PLURAL_SINGULAR[poolProcessResults[0] == 1], Msg.BATCH_CSV_WAIT_LIMIT.format(waitRemaining))) @@ -585,7 +584,7 @@ def MultiprocessGAMCommands(items, showCmds): if GC.Values[GC.PROCESS_WAIT_LIMIT] > 0: delta = int(time.time()-processWaitStart) if delta >= GC.Values[GC.PROCESS_WAIT_LIMIT]: - batchWriteStderr(Msg.BATCH_CSV_TERMINATE_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.BATCH_CSV_TERMINATE_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, poolProcessResults[0], PROCESS_PLURAL_SINGULAR[poolProcessResults[0] == 1])) pool.terminate() @@ -597,7 +596,7 @@ def MultiprocessGAMCommands(items, showCmds): except KeyboardInterrupt: handleControlC('KBI') pool.join() - batchWriteStderr(Msg.BATCH_CSV_PROCESSING_COMPLETE.format(_getMain().currentISOformatTimeStamp(), numItems)) + batchWriteStderr(Msg.BATCH_CSV_PROCESSING_COMPLETE.format(_gam().currentISOformatTimeStamp(), numItems)) if mpQueueCSVFile: terminateCSVFileQueueHandler(mpQueueCSVFile, mpQueueHandlerCSVFile) if mpQueueStdout: @@ -618,11 +617,11 @@ def threadBatchWorker(showCmds=False, numItems=0): sysRC = subprocess.call(item, stdout=GM.Globals[GM.STDOUT].get(GM.REDIRECT_MULTI_FD, sys.stdout), stderr=GM.Globals[GM.STDERR].get(GM.REDIRECT_MULTI_FD, sys.stderr)) if showCmds: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},End,{sysRC},{logCmd}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},End,{sysRC},{logCmd}\n') if GM.Globals[GM.MULTIPROCESS_EXIT_CONDITION] is not None and checkChildProcessRC(sysRC): GM.Globals[GM.MULTIPROCESS_EXIT_PROCESSING] = True except Exception as e: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},Error,{str(e)},{logCmd}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},Error,{str(e)},{logCmd}\n') GM.Globals[GM.TBATCH_QUEUE].task_done() BATCH_COMMANDS = [Cmd.GAM_CMD, Cmd.COMMIT_BATCH_CMD, Cmd.PRINT_CMD, Cmd.SLEEP_CMD, Cmd.DATETIME_CMD, Cmd.SET_CMD, Cmd.CLEAR_CMD] @@ -633,12 +632,12 @@ def ThreadBatchGAMCommands(items, showCmds): return pythonCmd = [sys.executable] if not getattr(sys, 'frozen', False): # we're not frozen - pythonCmd.append(os.path.realpath(_getMain().Cmd.Argument(0))) + pythonCmd.append(os.path.realpath(_gam().Cmd.Argument(0))) GM.Globals[GM.NUM_BATCH_ITEMS] = numItems = len(items) numWorkerThreads = min(numItems, GC.Values[GC.NUM_TBATCH_THREADS]) # GM.Globals[GM.TBATCH_QUEUE].put() gets blocked when trying to create more items than there are workers GM.Globals[GM.TBATCH_QUEUE] = queue.Queue(maxsize=numWorkerThreads) - batchWriteStderr(Msg.USING_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.USING_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, numWorkerThreads, THREAD_PLURAL_SINGULAR[numWorkerThreads == 1])) for _ in range(numWorkerThreads): @@ -651,28 +650,28 @@ def ThreadBatchGAMCommands(items, showCmds): if GM.Globals[GM.MULTIPROCESS_EXIT_PROCESSING]: break if item[0] == Cmd.COMMIT_BATCH_CMD: - batchWriteStderr(Msg.COMMIT_BATCH_WAIT_N_PROCESSES.format(_getMain().currentISOformatTimeStamp(), + batchWriteStderr(Msg.COMMIT_BATCH_WAIT_N_PROCESSES.format(_gam().currentISOformatTimeStamp(), numItems, numThreadsInUse, THREAD_PLURAL_SINGULAR[numThreadsInUse == 1])) GM.Globals[GM.TBATCH_QUEUE].join() - batchWriteStderr(Msg.COMMIT_BATCH_COMPLETE.format(_getMain().currentISOformatTimeStamp(), numItems, Msg.THREADS)) + batchWriteStderr(Msg.COMMIT_BATCH_COMPLETE.format(_gam().currentISOformatTimeStamp(), numItems, Msg.THREADS)) numThreadsInUse = 0 if len(item) > 1: - _getMain().readStdin(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}') + _gam().readStdin(f'{_gam().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}') continue if item[0] == Cmd.PRINT_CMD: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},0/{numItems},{Cmd.QuotedArgumentList(item[1:])}\n') continue if item[0] == Cmd.SLEEP_CMD: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},Sleeping {item[1]} seconds\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},0/{numItems},Sleeping {item[1]} seconds\n') time.sleep(int(item[1])) continue pid += 1 if not showCmds and ((pid % 100 == 0) or (pid == numItems)): - batchWriteStderr(Msg.PROCESSING_ITEM_N_OF_M.format(_getMain().currentISOformatTimeStamp(), pid, numItems)) + batchWriteStderr(Msg.PROCESSING_ITEM_N_OF_M.format(_gam().currentISOformatTimeStamp(), pid, numItems)) if showCmds: logCmd = Cmd.QuotedArgumentList(item) - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},Start,{Cmd.QuotedArgumentList(item)}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},Start,{Cmd.QuotedArgumentList(item)}\n') else: logCmd = '' if item[0] == Cmd.GAM_CMD: @@ -682,43 +681,43 @@ def ThreadBatchGAMCommands(items, showCmds): numThreadsInUse += 1 GM.Globals[GM.TBATCH_QUEUE].join() if showCmds: - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},0/{numItems},Complete\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},0/{numItems},Complete\n') def _getShowCommands(): - if _getMain().checkArgumentPresent('showcmds'): - return _getMain().getBoolean() + if _gam().checkArgumentPresent('showcmds'): + return _gam().getBoolean() return GC.Values[GC.SHOW_COMMANDS] def _getSkipRows(): - if _getMain().checkArgumentPresent('skiprows'): - return _getMain().getInteger(minVal=0) + if _gam().checkArgumentPresent('skiprows'): + return _gam().getInteger(minVal=0) # return GC.Values[GC.CSV_INPUT_ROW_SKIP] return 0 def _getMaxRows(): - if _getMain().checkArgumentPresent('maxrows'): - return _getMain().getInteger(minVal=0) + if _gam().checkArgumentPresent('maxrows'): + return _gam().getInteger(minVal=0) return GC.Values[GC.CSV_INPUT_ROW_LIMIT] # gam batch [showcmds []] def doBatch(threadBatch=False): - filename = _getMain().getString(Cmd.OB_FILE_NAME) + filename = _gam().getString(Cmd.OB_FILE_NAME) if (filename == '-') and (GC.Values[GC.DEBUG_LEVEL] > 0): - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.BATCH_CMD)) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.BATCH_CMD)) filenameLower = filename.lower() if filenameLower not in {'gdoc', 'gcsdoc'}: - encoding = _getMain().getCharSet() - filename = _getMain().setFilePath(filename, GC.INPUT_DIR) - f = _getMain().openFile(filename, encoding=encoding, stripUTFBOM=True) + encoding = _gam().getCharSet() + filename = _gam().setFilePath(filename, GC.INPUT_DIR) + f = _gam().openFile(filename, encoding=encoding, stripUTFBOM=True) elif filenameLower == 'gdoc': - f = _getMain().getGDocData(filenameLower) - _getMain().getCharSet() + f = _gam().getGDocData(filenameLower) + _gam().getCharSet() else: #filenameLower == 'gcsdoc': - f = _getMain().getStorageFileData(filenameLower) - _getMain().getCharSet() + f = _gam().getStorageFileData(filenameLower) + _gam().getCharSet() showCmds = _getShowCommands() - _getMain().checkForExtraneousArguments() + _gam().checkForExtraneousArguments() validCommands = BATCH_COMMANDS if not threadBatch else TBATCH_COMMANDS kwValues = {} items = [] @@ -733,40 +732,40 @@ def doBatch(threadBatch=False): try: argv = shlex.split(line) except ValueError as e: - _getMain().writeStderr(f'Command: >>>{line.strip()}<<<\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{str(e)}\n') + _gam().writeStderr(f'Command: >>>{line.strip()}<<<\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{str(e)}\n') errors += 1 continue if argv: cmd = argv[0].strip().lower() if cmd == Cmd.DATETIME_CMD: if len(argv) == 2: - kwValues['datetime'] = _getMain().todaysTime().strftime(argv[1]) + kwValues['datetime'] = _gam().todaysTime().strftime(argv[1]) else: - _getMain().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.DATETIME_CMD} DateTimeFormat>)>\n') + _gam().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.DATETIME_CMD} DateTimeFormat>)>\n') errors += 1 continue if cmd == Cmd.SET_CMD: if len(argv) == 3: kwValues[argv[1]] = argv[2] else: - _getMain().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.SET_CMD} keyword value>)>\n') + _gam().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.SET_CMD} keyword value>)>\n') errors += 1 continue if cmd == Cmd.CLEAR_CMD: if len(argv) == 2: kwValues.pop(argv[1], None) else: - _getMain().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.CLEAR_CMD} keyword>)>\n') + _gam().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.CLEAR_CMD} keyword>)>\n') errors += 1 continue if cmd == Cmd.SLEEP_CMD: if len(argv) != 2 or not argv[1].isdigit(): - _getMain().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.SLEEP_CMD} integer>)>\n') + _gam().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{Cmd.SLEEP_CMD} integer>)>\n') errors += 1 continue if (not cmd) or ((len(argv) == 1) and (cmd not in [Cmd.COMMIT_BATCH_CMD, Cmd.PRINT_CMD])): @@ -774,28 +773,28 @@ def doBatch(threadBatch=False): if cmd in validCommands: items.append(argv) else: - _getMain().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') - _getMain().writeStderr(f'{_getMain().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{_getMain().formatChoiceList(validCommands)}>\n') + _gam().writeStderr(f'Command: >>>{Cmd.QuotedArgumentList([argv[0]])}<<< {Cmd.QuotedArgumentList(argv[1:])}\n') + _gam().writeStderr(f'{_gam().ERROR_PREFIX}{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_INVALID][1]}: {Msg.EXPECTED} <{_gam().formatChoiceList(validCommands)}>\n') errors += 1 except IOError as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, _getMain().fileErrorMessage(filename, e)) - _getMain().closeFile(f) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, _gam().fileErrorMessage(filename, e)) + _gam().closeFile(f) if errors == 0: if not threadBatch: MultiprocessGAMCommands(items, showCmds) else: ThreadBatchGAMCommands(items, showCmds) else: - _getMain().writeStderr(Msg.BATCH_NOT_PROCESSED_ERRORS.format(_getMain().ERROR_PREFIX, filename, errors, ERROR_PLURAL_SINGULAR[errors == 1])) - _getMain().setSysExitRC(_getMain().USAGE_ERROR_RC) + _gam().writeStderr(Msg.BATCH_NOT_PROCESSED_ERRORS.format(_gam().ERROR_PREFIX, filename, errors, ERROR_PLURAL_SINGULAR[errors == 1])) + _gam().setSysExitRC(_gam().USAGE_ERROR_RC) # gam tbatch [showcmds []] def doThreadBatch(): - _getMain().adjustRedirectedSTDFilesIfNotMultiprocessing() + _gam().adjustRedirectedSTDFilesIfNotMultiprocessing() doBatch(True) def doAutoBatch(entityType, entityList, CL_command): - remaining = _getMain().Cmd.Remaining() + remaining = _gam().Cmd.Remaining() items = [] initial_argv = [Cmd.GAM_CMD] if GM.Globals[GM.SECTION] and not GM.Globals[GM.GAM_CFG_SECTION]: @@ -824,8 +823,8 @@ def getSubFields(initial_argv, fieldNames): subFields = {} GAM_argv = initial_argv[:] GAM_argvI = len(GAM_argv) - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().Cmd.Current() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().Cmd.Current() if not myarg: GAM_argv.append(myarg) elif SUB_PATTERN.search(myarg): @@ -839,17 +838,17 @@ def getSubFields(initial_argv, fieldNames): if not rematch: fieldName = submatch.group(1) if fieldName not in fieldNames: - _getMain().csvFieldErrorExit(fieldName, fieldNames) + _gam().csvFieldErrorExit(fieldName, fieldNames) subFields[GAM_argvI].append((SUB_TYPE, fieldName, submatch.start(), submatch.end())) else: fieldName = rematch.group(1) if fieldName not in fieldNames: - _getMain().csvFieldErrorExit(fieldName, fieldNames) + _gam().csvFieldErrorExit(fieldName, fieldNames) try: re.compile(rematch.group(2)) subFields[GAM_argvI].append((RE_TYPE, fieldName, submatch.start(), submatch.end(), rematch.group(2), rematch.group(3))) except re.error as e: - _getMain().usageErrorExit(f'{Cmd.OB_RE_PATTERN} {Msg.ERROR}: {e}') + _gam().usageErrorExit(f'{Cmd.OB_RE_PATTERN} {Msg.ERROR}: {e}') pos = submatch.end() GAM_argv.append(myarg) elif myarg[0] == '~': @@ -858,11 +857,11 @@ def getSubFields(initial_argv, fieldNames): subFields[GAM_argvI] = [(SUB_TYPE, fieldName, 0, len(myarg))] GAM_argv.append(myarg) else: - _getMain().csvFieldErrorExit(fieldName, fieldNames) + _gam().csvFieldErrorExit(fieldName, fieldNames) else: GAM_argv.append(myarg) GAM_argvI += 1 - _getMain().Cmd.Advance() + _gam().Cmd.Advance() return(GAM_argv, subFields) def processSubFields(GAM_argv, row, subFields): @@ -889,28 +888,28 @@ def processSubFields(GAM_argv, row, subFields): # [skiprows ] [maxrows ] # gam def doCSV(testMode=False): - filename = _getMain().getString(Cmd.OB_FILE_NAME) + filename = _gam().getString(Cmd.OB_FILE_NAME) if (filename == '-') and (GC.Values[GC.DEBUG_LEVEL] > 0): - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.CSV_CMD)) - f, csvFile, fieldnames = _getMain().openCSVFileReader(filename) - matchFields, skipFields = _getMain().getMatchSkipFields(fieldnames) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.CSV_CMD)) + f, csvFile, fieldnames = _gam().openCSVFileReader(filename) + matchFields, skipFields = _gam().getMatchSkipFields(fieldnames) showCmds = _getShowCommands() skipRows = _getSkipRows() maxRows = _getMaxRows() - _getMain().checkArgumentPresent(Cmd.GAM_CMD, required=True) - if not _getMain().Cmd.ArgumentsRemaining(): - _getMain().missingArgumentExit(Cmd.OB_GAM_ARGUMENT_LIST) + _gam().checkArgumentPresent(Cmd.GAM_CMD, required=True) + if not _gam().Cmd.ArgumentsRemaining(): + _gam().missingArgumentExit(Cmd.OB_GAM_ARGUMENT_LIST) initial_argv = [Cmd.GAM_CMD] - if GM.Globals[GM.SECTION] and not GM.Globals[GM.GAM_CFG_SECTION] and not _getMain().Cmd.PeekArgumentPresent(Cmd.SELECT_CMD): + if GM.Globals[GM.SECTION] and not GM.Globals[GM.GAM_CFG_SECTION] and not _gam().Cmd.PeekArgumentPresent(Cmd.SELECT_CMD): initial_argv.extend([Cmd.SELECT_CMD, GM.Globals[GM.SECTION]]) GAM_argv, subFields = getSubFields(initial_argv, fieldnames) if GC.Values[GC.CSV_INPUT_ROW_FILTER] or GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]: - _getMain().CheckInputRowFilterHeaders(fieldnames, GC.Values[GC.CSV_INPUT_ROW_FILTER], GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]) + _gam().CheckInputRowFilterHeaders(fieldnames, GC.Values[GC.CSV_INPUT_ROW_FILTER], GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]) items = [] i = 0 for row in csvFile: - if _getMain().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): + if _gam().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): i += 1 if skipRows: if i <= skipRows: @@ -920,21 +919,21 @@ def doCSV(testMode=False): items.append(processSubFields(GAM_argv, row, subFields)) if maxRows and i >= maxRows: break - _getMain().closeFile(f) + _gam().closeFile(f) if not testMode: MultiprocessGAMCommands(items, showCmds) else: numItems = min(len(items), 10) - _getMain().writeStdout(Msg.CSV_FILE_HEADERS.format(filename)) - _getMain().Ind.Increment() + _gam().writeStdout(Msg.CSV_FILE_HEADERS.format(filename)) + _gam().Ind.Increment() for field in fieldnames: - _getMain().writeStdout(f'{_getMain().Ind.Spaces()}{field}\n') - _getMain().Ind.Decrement() - _getMain().writeStdout(Msg.CSV_SAMPLE_COMMANDS.format(numItems, _getMain().GAM)) - _getMain().Ind.Increment() + _gam().writeStdout(f'{_gam().Ind.Spaces()}{field}\n') + _gam().Ind.Decrement() + _gam().writeStdout(Msg.CSV_SAMPLE_COMMANDS.format(numItems, _gam().GAM)) + _gam().Ind.Increment() for i in range(numItems): - _getMain().writeStdout(f'{_getMain().Ind.Spaces()}{Cmd.QuotedArgumentList(items[i])}\n') - _getMain().Ind.Decrement() + _gam().writeStdout(f'{_gam().Ind.Spaces()}{Cmd.QuotedArgumentList(items[i])}\n') + _gam().Ind.Decrement() def doCSVTest(): doCSV(testMode=True) @@ -945,23 +944,23 @@ def doCSVTest(): # [skiprows ] [maxrows ] # gam def doLoop(loopCmd): - filename = _getMain().getString(Cmd.OB_FILE_NAME) + filename = _gam().getString(Cmd.OB_FILE_NAME) if (filename == '-') and (GC.Values[GC.DEBUG_LEVEL] > 0): - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.LOOP_CMD)) - f, csvFile, fieldnames = _getMain().openCSVFileReader(filename) - matchFields, skipFields = _getMain().getMatchSkipFields(fieldnames) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.BATCH_CSV_LOOP_DASH_DEBUG_INCOMPATIBLE.format(Cmd.LOOP_CMD)) + f, csvFile, fieldnames = _gam().openCSVFileReader(filename) + matchFields, skipFields = _gam().getMatchSkipFields(fieldnames) showCmds = _getShowCommands() skipRows = _getSkipRows() maxRows = _getMaxRows() - _getMain().checkArgumentPresent(Cmd.GAM_CMD, required=True) - if not _getMain().Cmd.ArgumentsRemaining(): - _getMain().missingArgumentExit(Cmd.OB_GAM_ARGUMENT_LIST) + _gam().checkArgumentPresent(Cmd.GAM_CMD, required=True) + if not _gam().Cmd.ArgumentsRemaining(): + _gam().missingArgumentExit(Cmd.OB_GAM_ARGUMENT_LIST) if GC.Values[GC.CSV_INPUT_ROW_FILTER] or GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]: - _getMain().CheckInputRowFilterHeaders(fieldnames, GC.Values[GC.CSV_INPUT_ROW_FILTER], GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]) - choice = _getMain().Cmd.Current().strip().lower() + _gam().CheckInputRowFilterHeaders(fieldnames, GC.Values[GC.CSV_INPUT_ROW_FILTER], GC.Values[GC.CSV_INPUT_ROW_DROP_FILTER]) + choice = _gam().Cmd.Current().strip().lower() if choice == Cmd.LOOP_CMD: - _getMain().usageErrorExit(Msg.NESTED_LOOP_CMD_NOT_ALLOWED) + _gam().usageErrorExit(Msg.NESTED_LOOP_CMD_NOT_ALLOWED) # gam loop ... gam redirect|select|config ... process gam.cfg on each iteration # gam redirect|select|config ... loop ... gam redirect|select|config ... process gam.cfg on each iteration # gam loop ... gam !redirect|select|config ... no further processing of gam.cfg @@ -981,13 +980,13 @@ def doLoop(loopCmd): else: LoopGlobals = {GM.CMDLOG_LOGGER: None, GM.CMDLOG_HANDLER: None} if (GM.Globals[GM.PID] > 0) and GC.Values[GC.CMDLOG]: - _getMain().openGAMCommandLog(LoopGlobals, 'looplog') + _gam().openGAMCommandLog(LoopGlobals, 'looplog') if LoopGlobals[GM.CMDLOG_LOGGER]: - _getMain().writeGAMCommandLog(LoopGlobals, loopCmd, '*') + _gam().writeGAMCommandLog(LoopGlobals, loopCmd, '*') if not showCmds: i = 0 for row in csvFile: - if _getMain().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): + if _gam().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): i += 1 if skipRows: if i <= skipRows: @@ -997,20 +996,20 @@ def doLoop(loopCmd): item = processSubFields(GAM_argv, row, subFields) logCmd = Cmd.QuotedArgumentList(item) if i % 100 == 0: - batchWriteStderr(Msg.PROCESSING_ITEM_N.format(_getMain().currentISOformatTimeStamp(), i)) - sysRC = _getMain().ProcessGAMCommand(item, processGamCfg=processGamCfg, inLoop=True) + batchWriteStderr(Msg.PROCESSING_ITEM_N.format(_gam().currentISOformatTimeStamp(), i)) + sysRC = _gam().ProcessGAMCommand(item, processGamCfg=processGamCfg, inLoop=True) if (GM.Globals[GM.PID] > 0) and LoopGlobals[GM.CMDLOG_LOGGER]: - _getMain().writeGAMCommandLog(LoopGlobals, logCmd, sysRC) - if (sysRC > 0) and (GM.Globals[GM.SYSEXITRC] <= _getMain().HARD_ERROR_RC): + _gam().writeGAMCommandLog(LoopGlobals, logCmd, sysRC) + if (sysRC > 0) and (GM.Globals[GM.SYSEXITRC] <= _gam().HARD_ERROR_RC): break if maxRows and i >= maxRows: break - _getMain().closeFile(f) + _gam().closeFile(f) else: items = [] i = 0 for row in csvFile: - if _getMain().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): + if _gam().checkMatchSkipFields(row, fieldnames, matchFields, skipFields): i += 1 if skipRows: if i <= skipRows: @@ -1020,26 +1019,26 @@ def doLoop(loopCmd): items.append(processSubFields(GAM_argv, row, subFields)) if maxRows and i >= maxRows: break - _getMain().closeFile(f) + _gam().closeFile(f) numItems = len(items) pid = 0 for item in items: pid += 1 logCmd = Cmd.QuotedArgumentList(item) - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},Start,0,{logCmd}\n') - sysRC = _getMain().ProcessGAMCommand(item, processGamCfg=processGamCfg, inLoop=True) - batchWriteStderr(f'{_getMain().currentISOformatTimeStamp()},{pid}/{numItems},End,{sysRC},{logCmd}\n') + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},Start,0,{logCmd}\n') + sysRC = _gam().ProcessGAMCommand(item, processGamCfg=processGamCfg, inLoop=True) + batchWriteStderr(f'{_gam().currentISOformatTimeStamp()},{pid}/{numItems},End,{sysRC},{logCmd}\n') if (GM.Globals[GM.PID] > 0) and LoopGlobals[GM.CMDLOG_LOGGER]: - _getMain().writeGAMCommandLog(LoopGlobals, logCmd, sysRC) - if (sysRC > 0) and (GM.Globals[GM.SYSEXITRC] <= _getMain().HARD_ERROR_RC): + _gam().writeGAMCommandLog(LoopGlobals, logCmd, sysRC) + if (sysRC > 0) and (GM.Globals[GM.SYSEXITRC] <= _gam().HARD_ERROR_RC): break if (GM.Globals[GM.PID] > 0) and LoopGlobals[GM.CMDLOG_LOGGER]: - _getMain().closeGAMCommandLog(LoopGlobals) + _gam().closeGAMCommandLog(LoopGlobals) if multi: terminateCSVFileQueueHandler(mpQueue, mpQueueHandler) def _doList(entityList, entityType): - _getMain().buildGAPIObject(API.DIRECTORY) + _gam().buildGAPIObject(API.DIRECTORY) if GM.Globals[GM.CSV_DATA_DICT]: keyField = GM.Globals[GM.CSV_KEY_FIELD] dataField = GM.Globals[GM.CSV_DATA_FIELD] @@ -1047,26 +1046,26 @@ def _doList(entityList, entityType): keyField = 'Entity' dataField = 'Data' csvPF = CSVPrintFile(keyField) - if _getMain().checkArgumentPresent('todrive'): + if _gam().checkArgumentPresent('todrive'): csvPF.GetTodriveParameters() if entityList is None: - entityList = _getMain().getEntityList(Cmd.OB_ENTITY) - showData = _getMain().checkArgumentPresent('data') + entityList = _gam().getEntityList(Cmd.OB_ENTITY) + showData = _gam().checkArgumentPresent('data') if showData: if not entityType: - itemType, itemList = _getMain().getEntityToModify(crosAllowed=True) + itemType, itemList = _gam().getEntityToModify(crosAllowed=True) else: itemType = None - itemList = _getMain().getEntityList(Cmd.OB_ENTITY) + itemList = _gam().getEntityList(Cmd.OB_ENTITY) entityItemLists = itemList if isinstance(itemList, dict) else None csvPF.AddTitle(dataField) else: entityItemLists = None - dataDelimiter = _getMain().getDelimiter() - _getMain().checkForExtraneousArguments() - _, _, entityList = _getMain().getEntityArgument(entityList) + dataDelimiter = _gam().getDelimiter() + _gam().checkForExtraneousArguments() + _, _, entityList = _gam().getEntityArgument(entityList) for entity in entityList: - entityEmail = _getMain().normalizeEmailAddressOrUID(entity) + entityEmail = _gam().normalizeEmailAddressOrUID(entity) if showData: if entityItemLists: if entity not in entityItemLists: @@ -1075,7 +1074,7 @@ def _doList(entityList, entityType): itemList = entityItemLists[entity] if itemType == Cmd.ENTITY_USERS: for i, item in enumerate(itemList): - itemList[i] = _getMain().normalizeEmailAddressOrUID(item) + itemList[i] = _gam().normalizeEmailAddressOrUID(item) if dataDelimiter: csvPF.WriteRow({keyField: entityEmail, dataField: dataDelimiter.join(itemList)}) else: @@ -1098,10 +1097,10 @@ def doListUser(entityList): _doList(entityList, Cmd.ENTITY_USERS) def _showCount(entityList, entityType): - _getMain().buildGAPIObject(API.DIRECTORY) - _getMain().checkForExtraneousArguments() - _, count, entityList = _getMain().getEntityArgument(entityList) - _getMain().actionPerformedNumItems(count, entityType) + _gam().buildGAPIObject(API.DIRECTORY) + _gam().checkForExtraneousArguments() + _, count, entityList = _gam().getEntityArgument(entityList) + _gam().actionPerformedNumItems(count, entityType) # gam show count def showCountCrOS(entityList): diff --git a/src/gam/util/csv_pf.py b/src/gam/util/csv_pf.py index faeaf0cb..92859e8f 100644 --- a/src/gam/util/csv_pf.py +++ b/src/gam/util/csv_pf.py @@ -21,8 +21,7 @@ from gamlib import glglobals as GM from gamlib import glmsgs as Msg -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] def addFieldToFieldsList(fieldName, fieldsChoiceMap, fieldsList): fields = fieldsChoiceMap[fieldName.lower()] @@ -32,10 +31,10 @@ def addFieldToFieldsList(fieldName, fieldsChoiceMap, fieldsList): fieldsList.append(fields) def _getFieldsList(): - return _getMain().getString(_getMain().Cmd.OB_FIELD_NAME_LIST).lower().replace('_', '').replace(',', ' ').split() + return _gam().getString(_gam().Cmd.OB_FIELD_NAME_LIST).lower().replace('_', '').replace(',', ' ').split() def _getRawFields(requiredField=None): - rawFields = _getMain().getString(_getMain().Cmd.OB_FIELDS) + rawFields = _gam().getString(_gam().Cmd.OB_FIELDS) if requiredField is None or requiredField in rawFields: return rawFields return f'{requiredField},{rawFields}' @@ -45,15 +44,15 @@ def CheckInputRowFilterHeaders(titlesList, rowFilter, rowDropFilter): for filterVal in rowFilter: columns = [t for t in titlesList if filterVal[0].match(t)] if not columns: - _getMain().stderrErrorMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_INPUT_COLUMNS.format(GC.CSV_INPUT_ROW_FILTER, filterVal[0].pattern)) + _gam().stderrErrorMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_INPUT_COLUMNS.format(GC.CSV_INPUT_ROW_FILTER, filterVal[0].pattern)) status = False for filterVal in rowDropFilter: columns = [t for t in titlesList if filterVal[0].match(t)] if not columns: - _getMain().stderrErrorMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_INPUT_COLUMNS.format(GC.CSV_INPUT_ROW_DROP_FILTER, filterVal[0].pattern)) + _gam().stderrErrorMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_INPUT_COLUMNS.format(GC.CSV_INPUT_ROW_DROP_FILTER, filterVal[0].pattern)) status = False if not status: - sys.exit(_getMain().USAGE_ERROR_RC) + sys.exit(_gam().USAGE_ERROR_RC) def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, rowDropFilterModeAll): def rowRegexFilterMatch(filterPattern): @@ -80,9 +79,9 @@ def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, return False def stripTimeFromDateTime(rowDate): - if _getMain().YYYYMMDD_PATTERN.match(rowDate): + if _gam().YYYYMMDD_PATTERN.match(rowDate): try: - rowTime = arrow.Arrow.strptime(rowDate, _getMain().YYYYMMDD_FORMAT) + rowTime = arrow.Arrow.strptime(rowDate, _gam().YYYYMMDD_FORMAT) except ValueError: return None else: @@ -90,14 +89,14 @@ def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, rowTime = arrow.get(rowDate) except (arrow.parser.ParserError, OverflowError): return None - return _getMain().ISOformatTimeStamp(arrow.Arrow(rowTime.year, rowTime.month, rowTime.day, tzinfo='UTC')) + return _gam().ISOformatTimeStamp(arrow.Arrow(rowTime.year, rowTime.month, rowTime.day, tzinfo='UTC')) def rowDateTimeFilterMatch(dateMode, op, filterDate): def checkMatch(rowDate): if not rowDate or not isinstance(rowDate, str): return False if rowDate == GC.Values[GC.NEVER_TIME]: - rowDate = _getMain().NEVER_TIME + rowDate = _gam().NEVER_TIME if dateMode: rowDate = stripTimeFromDateTime(rowDate) if not rowDate: @@ -129,7 +128,7 @@ def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, if not rowDate or not isinstance(rowDate, str): return False if rowDate == GC.Values[GC.NEVER_TIME]: - rowDate = _getMain().NEVER_TIME + rowDate = _gam().NEVER_TIME if dateMode: rowDate = stripTimeFromDateTime(rowDate) if not rowDate: @@ -149,7 +148,7 @@ def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, return True def getHourMinuteFromDateTime(rowDate): - if _getMain().YYYYMMDD_PATTERN.match(rowDate): + if _gam().YYYYMMDD_PATTERN.match(rowDate): return None try: rowTime = arrow.get(rowDate) @@ -284,7 +283,7 @@ def RowFilterMatch(row, titlesList, rowFilter, rowFilterModeAll, rowDropFilter, if isinstance(rowBoolean, bool): return rowBoolean == filterBoolean if isinstance(rowBoolean, str): - if rowBoolean.lower() in _getMain().TRUE_FALSE: + if rowBoolean.lower() in _gam().TRUE_FALSE: return rowBoolean.capitalize() == str(filterBoolean) ##### Blank = False if not rowBoolean: @@ -477,7 +476,7 @@ def getFieldsList(myarg, fieldsChoiceMap, fieldsList, initialField=None, fieldsA if field in fieldsChoiceMap: addMappedFields(fieldsChoiceMap[field]) else: - _getMain().invalidChoiceExit(field, fieldsChoiceMap, True) + _gam().invalidChoiceExit(field, fieldsChoiceMap, True) else: return False return True @@ -669,7 +668,7 @@ class CSVPrintFile(): if field in fieldsChoiceMap: self.AddField(field, fieldsChoiceMap, fieldsList) else: - _getMain().invalidChoiceExit(field, fieldsChoiceMap, True) + _gam().invalidChoiceExit(field, fieldsChoiceMap, True) else: return False return True @@ -688,52 +687,52 @@ class CSVPrintFile(): def GetTodriveParameters(self): def invalidTodriveFileIdExit(entityValueList, message, location): - _getMain().Cmd.SetLocation(location-1) - _getMain().usageErrorExit(_getMain().formatKeyValueList('', _getMain().Ent.FormatEntityValueList([_getMain().Ent.DRIVE_FILE_ID, self.todrive['fileId']]+entityValueList)+[message], '')) + _gam().Cmd.SetLocation(location-1) + _gam().usageErrorExit(_gam().formatKeyValueList('', _gam().Ent.FormatEntityValueList([_gam().Ent.DRIVE_FILE_ID, self.todrive['fileId']]+entityValueList)+[message], '')) def invalidTodriveParentExit(entityType, message): - _getMain().Cmd.SetLocation(tdparentLocation-1) + _gam().Cmd.SetLocation(tdparentLocation-1) if not localParent: - _getMain().usageErrorExit(Msg.INVALID_ENTITY.format(_getMain().Ent.Singular(entityType), - _getMain().formatKeyValueList('', - [_getMain().Ent.Singular(_getMain().Ent.CONFIG_FILE), GM.Globals[GM.GAM_CFG_FILE], - _getMain().Ent.Singular(_getMain().Ent.ITEM), GC.TODRIVE_PARENT, - _getMain().Ent.Singular(_getMain().Ent.VALUE), self.todrive['parent'], + _gam().usageErrorExit(Msg.INVALID_ENTITY.format(_gam().Ent.Singular(entityType), + _gam().formatKeyValueList('', + [_gam().Ent.Singular(_gam().Ent.CONFIG_FILE), GM.Globals[GM.GAM_CFG_FILE], + _gam().Ent.Singular(_gam().Ent.ITEM), GC.TODRIVE_PARENT, + _gam().Ent.Singular(_gam().Ent.VALUE), self.todrive['parent'], message], ''))) else: - _getMain().usageErrorExit(Msg.INVALID_ENTITY.format(_getMain().Ent.Singular(entityType), message)) + _gam().usageErrorExit(Msg.INVALID_ENTITY.format(_gam().Ent.Singular(entityType), message)) def invalidTodriveUserExit(entityType, message): - _getMain().Cmd.SetLocation(tduserLocation-1) + _gam().Cmd.SetLocation(tduserLocation-1) if not localUser: - _getMain().usageErrorExit(Msg.INVALID_ENTITY.format(_getMain().Ent.Singular(entityType), - _getMain().formatKeyValueList('', - [_getMain().Ent.Singular(_getMain().Ent.CONFIG_FILE), GM.Globals[GM.GAM_CFG_FILE], - _getMain().Ent.Singular(_getMain().Ent.ITEM), GC.TODRIVE_USER, - _getMain().Ent.Singular(_getMain().Ent.VALUE), self.todrive['user'], + _gam().usageErrorExit(Msg.INVALID_ENTITY.format(_gam().Ent.Singular(entityType), + _gam().formatKeyValueList('', + [_gam().Ent.Singular(_gam().Ent.CONFIG_FILE), GM.Globals[GM.GAM_CFG_FILE], + _gam().Ent.Singular(_gam().Ent.ITEM), GC.TODRIVE_USER, + _gam().Ent.Singular(_gam().Ent.VALUE), self.todrive['user'], message], ''))) else: - _getMain().usageErrorExit(Msg.INVALID_ENTITY.format(_getMain().Ent.Singular(entityType), message)) + _gam().usageErrorExit(Msg.INVALID_ENTITY.format(_gam().Ent.Singular(entityType), message)) def getDriveObject(): if not GC.Values[GC.TODRIVE_CLIENTACCESS]: - _, drive = _getMain().buildGAPIServiceObject(_getMain().chooseSaAPI(API.DRIVETD, API.DRIVE3), self.todrive['user']) + _, drive = _gam().buildGAPIServiceObject(_gam().chooseSaAPI(API.DRIVETD, API.DRIVE3), self.todrive['user']) if not drive: - invalidTodriveUserExit(_getMain().Ent.USER, Msg.NOT_FOUND) + invalidTodriveUserExit(_gam().Ent.USER, Msg.NOT_FOUND) else: - drive = _getMain().buildGAPIObject(API.DRIVE3) + drive = _gam().buildGAPIObject(API.DRIVE3) return drive CELL_WRAP_MAP = {'clip': 'CLIP', 'overflow': 'OVERFLOW_CELL', 'overflowcell': 'OVERFLOW_CELL', 'wrap': 'WRAP'} CELL_NUMBER_FORMAT_MAP = {'text': 'TEXT', 'number': 'NUMBER'} localUser = localParent = False - tdfileidLocation = tdparentLocation = tdaddsheetLocation = tdupdatesheetLocation = tduserLocation = _getMain().Cmd.Location() + tdfileidLocation = tdparentLocation = tdaddsheetLocation = tdupdatesheetLocation = tduserLocation = _gam().Cmd.Location() tdsheetLocation = {} for sheetEntity in self.TDSHEET_ENTITY_MAP.values(): - tdsheetLocation[sheetEntity] = _getMain().Cmd.Location() + tdsheetLocation[sheetEntity] = _gam().Cmd.Location() self.todrive = {'user': GC.Values[GC.TODRIVE_USER], 'title': None, 'description': None, 'sheetEntity': None, 'addsheet': False, 'updatesheet': False, 'sheettitle': None, 'cellwrap': None, 'cellnumberformat': None, 'clearfilter': GC.Values[GC.TODRIVE_CLEARFILTER], @@ -748,150 +747,150 @@ class CSVPrintFile(): 'localcopy': GC.Values[GC.TODRIVE_LOCALCOPY], 'uploadnodata': GC.Values[GC.TODRIVE_UPLOAD_NODATA], 'nobrowser': GC.Values[GC.TODRIVE_NOBROWSER], 'noemail': GC.Values[GC.TODRIVE_NOEMAIL], 'returnidonly': False, 'alert': [], 'share': [], 'notify': False, 'subject': None, 'from': None} - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if myarg == 'tduser': - self.todrive['user'] = _getMain().getString(_getMain().Cmd.OB_EMAIL_ADDRESS) - tduserLocation = _getMain().Cmd.Location() + self.todrive['user'] = _gam().getString(_gam().Cmd.OB_EMAIL_ADDRESS) + tduserLocation = _gam().Cmd.Location() localUser = True elif myarg == 'tdtitle': - self.todrive['title'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['title'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tddescription': - self.todrive['description'] = _getMain().getString(_getMain().Cmd.OB_STRING) + self.todrive['description'] = _gam().getString(_gam().Cmd.OB_STRING) elif myarg in self.TDSHEET_ENTITY_MAP: sheetEntity = self.TDSHEET_ENTITY_MAP[myarg] - tdsheetLocation[sheetEntity] = _getMain().Cmd.Location() - self.todrive[sheetEntity] = _getMain().getSheetEntity(True) + tdsheetLocation[sheetEntity] = _gam().Cmd.Location() + self.todrive[sheetEntity] = _gam().getSheetEntity(True) elif myarg == 'tdaddsheet': - tdaddsheetLocation = _getMain().Cmd.Location() - self.todrive['addsheet'] = _getMain().getBoolean() + tdaddsheetLocation = _gam().Cmd.Location() + self.todrive['addsheet'] = _gam().getBoolean() if self.todrive['addsheet']: self.todrive['updatesheet'] = False elif myarg == 'tdupdatesheet': - tdupdatesheetLocation = _getMain().Cmd.Location() - self.todrive['updatesheet'] = _getMain().getBoolean() + tdupdatesheetLocation = _gam().Cmd.Location() + self.todrive['updatesheet'] = _gam().getBoolean() if self.todrive['updatesheet']: self.todrive['addsheet'] = False elif myarg == 'tdcellwrap': - self.todrive['cellwrap'] = _getMain().getChoice(CELL_WRAP_MAP, mapChoice=True) + self.todrive['cellwrap'] = _gam().getChoice(CELL_WRAP_MAP, mapChoice=True) elif myarg == 'tdcellnumberformat': - self.todrive['cellnumberformat'] = _getMain().getChoice(CELL_NUMBER_FORMAT_MAP, mapChoice=True) + self.todrive['cellnumberformat'] = _gam().getChoice(CELL_NUMBER_FORMAT_MAP, mapChoice=True) elif myarg == 'tdclearfilter': - self.todrive['clearfilter'] = _getMain().getBoolean() + self.todrive['clearfilter'] = _gam().getBoolean() elif myarg == 'tdlocale': - self.todrive['locale'] = _getMain().getLanguageCode(_getMain().LOCALE_CODES_MAP) + self.todrive['locale'] = _gam().getLanguageCode(_gam().LOCALE_CODES_MAP) elif myarg == 'tdtimezone': - self.todrive['timeZone'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['timeZone'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tdtimestamp': - self.todrive['timestamp'] = _getMain().getBoolean() + self.todrive['timestamp'] = _gam().getBoolean() elif myarg == 'tdtimeformat': - self.todrive['timeformat'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['timeformat'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tdsheettitle': - self.todrive['sheettitle'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['sheettitle'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tdsheettimestamp': - self.todrive['sheettimestamp'] = _getMain().getBoolean() + self.todrive['sheettimestamp'] = _gam().getBoolean() elif myarg == 'tdsheettimeformat': - self.todrive['sheettimeformat'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['sheettimeformat'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tddaysoffset': - self.todrive['daysoffset'] = _getMain().getInteger(minVal=0) + self.todrive['daysoffset'] = _gam().getInteger(minVal=0) elif myarg == 'tdhoursoffset': - self.todrive['hoursoffset'] = _getMain().getInteger(minVal=0) + self.todrive['hoursoffset'] = _gam().getInteger(minVal=0) elif myarg == 'tdsheetdaysoffset': - self.todrive['sheetdaysoffset'] = _getMain().getInteger(minVal=0) + self.todrive['sheetdaysoffset'] = _gam().getInteger(minVal=0) elif myarg == 'tdsheethoursoffset': - self.todrive['sheethoursoffset'] = _getMain().getInteger(minVal=0) + self.todrive['sheethoursoffset'] = _gam().getInteger(minVal=0) elif myarg == 'tdfileid': - self.todrive['fileId'] = _getMain().getString(_getMain().Cmd.OB_DRIVE_FILE_ID) - tdfileidLocation = _getMain().Cmd.Location() + self.todrive['fileId'] = _gam().getString(_gam().Cmd.OB_DRIVE_FILE_ID) + tdfileidLocation = _gam().Cmd.Location() elif myarg == 'tdretaintitle': - self.todrive['retaintitle'] = _getMain().getBoolean() + self.todrive['retaintitle'] = _gam().getBoolean() elif myarg == 'tdparent': - self.todrive['parent'] = _getMain().escapeDriveFileName(_getMain().getString(_getMain().Cmd.OB_DRIVE_FOLDER_NAME, minLen=0)) - tdparentLocation = _getMain().Cmd.Location() + self.todrive['parent'] = _gam().escapeDriveFileName(_gam().getString(_gam().Cmd.OB_DRIVE_FOLDER_NAME, minLen=0)) + tdparentLocation = _gam().Cmd.Location() localParent = True elif myarg == 'tdlocalcopy': - self.todrive['localcopy'] = _getMain().getBoolean() + self.todrive['localcopy'] = _gam().getBoolean() elif myarg == 'tduploadnodata': - self.todrive['uploadnodata'] = _getMain().getBoolean() + self.todrive['uploadnodata'] = _gam().getBoolean() elif myarg == 'tdnobrowser': - self.todrive['nobrowser'] = _getMain().getBoolean() + self.todrive['nobrowser'] = _gam().getBoolean() elif myarg == 'tdnoemail': - self.todrive['noemail'] = _getMain().getBoolean() + self.todrive['noemail'] = _gam().getBoolean() elif myarg == 'tdreturnidonly': - self.todrive['returnidonly'] = _getMain().getBoolean() + self.todrive['returnidonly'] = _gam().getBoolean() elif myarg == 'tdnoescapechar': - self.todrive['noescapechar'] = _getMain().getBoolean() + self.todrive['noescapechar'] = _gam().getBoolean() elif myarg == 'tdalert': - self.todrive['alert'].append({'emailAddress': _getMain().normalizeEmailAddressOrUID(_getMain().getString(_getMain().Cmd.OB_EMAIL_ADDRESS))}) + self.todrive['alert'].append({'emailAddress': _gam().normalizeEmailAddressOrUID(_gam().getString(_gam().Cmd.OB_EMAIL_ADDRESS))}) elif myarg == 'tdshare': - self.todrive['share'].append({'emailAddress': _getMain().normalizeEmailAddressOrUID(_getMain().getString(_getMain().Cmd.OB_EMAIL_ADDRESS)), + self.todrive['share'].append({'emailAddress': _gam().normalizeEmailAddressOrUID(_gam().getString(_gam().Cmd.OB_EMAIL_ADDRESS)), 'type': 'user', - 'role': _getMain().getChoice(self.TDSHARE_ACL_ROLES_MAP, mapChoice=True)}) + 'role': _gam().getChoice(self.TDSHARE_ACL_ROLES_MAP, mapChoice=True)}) elif myarg == 'tdnotify': - self.todrive['notify'] = _getMain().getBoolean() + self.todrive['notify'] = _gam().getBoolean() elif myarg == 'tdsubject': - self.todrive['subject'] = _getMain().getString(_getMain().Cmd.OB_STRING, minLen=0) + self.todrive['subject'] = _gam().getString(_gam().Cmd.OB_STRING, minLen=0) elif myarg == 'tdfrom': - self.todrive['from'] = _getMain().getString(_getMain().Cmd.OB_EMAIL_ADDRESS) + self.todrive['from'] = _gam().getString(_gam().Cmd.OB_EMAIL_ADDRESS) else: - _getMain().Cmd.Backup() + _gam().Cmd.Backup() break if self.todrive['addsheet']: if not self.todrive['fileId']: - _getMain().Cmd.SetLocation(tdaddsheetLocation-1) - _getMain().missingArgumentExit('tdfileid') + _gam().Cmd.SetLocation(tdaddsheetLocation-1) + _gam().missingArgumentExit('tdfileid') if self.todrive['sheetEntity'] and self.todrive['sheetEntity']['sheetId']: - _getMain().Cmd.SetLocation(tdsheetLocation[sheetEntity]-1) - _getMain().invalidArgumentExit('tdsheet ') + _gam().Cmd.SetLocation(tdsheetLocation[sheetEntity]-1) + _gam().invalidArgumentExit('tdsheet ') if self.todrive['updatesheet'] and (not self.todrive['fileId'] or not self.todrive['sheetEntity']): - _getMain().Cmd.SetLocation(tdupdatesheetLocation-1) - _getMain().missingArgumentExit('tdfileid and tdsheet') + _gam().Cmd.SetLocation(tdupdatesheetLocation-1) + _gam().missingArgumentExit('tdfileid and tdsheet') if self.todrive['sheetEntity'] and self.todrive['sheetEntity']['sheetId'] and (not self.todrive['fileId'] or not self.todrive['updatesheet']): - _getMain().Cmd.SetLocation(tdsheetLocation['sheetEntity']-1) - _getMain().missingArgumentExit('tdfileid and tdupdatesheet') + _gam().Cmd.SetLocation(tdsheetLocation['sheetEntity']-1) + _gam().missingArgumentExit('tdfileid and tdupdatesheet') if not self.todrive['user'] or GC.Values[GC.TODRIVE_CLIENTACCESS]: - self.todrive['user'] = _getMain()._getAdminEmail() + self.todrive['user'] = _gam()._getAdminEmail() if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] and not GC.Values[GC.TODRIVE_CLIENTACCESS]: - user = _getMain().checkUserExists(_getMain().buildGAPIObject(API.DIRECTORY), self.todrive['user']) + user = _gam().checkUserExists(_gam().buildGAPIObject(API.DIRECTORY), self.todrive['user']) if not user: - invalidTodriveUserExit(_getMain().Ent.USER, Msg.NOT_FOUND) + invalidTodriveUserExit(_gam().Ent.USER, Msg.NOT_FOUND) self.todrive['user'] = user else: - self.todrive['user'] = _getMain().normalizeEmailAddressOrUID(self.todrive['user']) + self.todrive['user'] = _gam().normalizeEmailAddressOrUID(self.todrive['user']) if self.todrive['fileId']: drive = getDriveObject() try: - result = _getMain().callGAPI(drive.files(), 'get', + result = _gam().callGAPI(drive.files(), 'get', throwReasons=GAPI.DRIVE_GET_THROW_REASONS, fileId=self.todrive['fileId'], fields='id,mimeType,capabilities(canEdit)', supportsAllDrives=True) - if result['mimeType'] == _getMain().MIMETYPE_GA_FOLDER: - invalidTodriveFileIdExit([], Msg.NOT_AN_ENTITY.format(_getMain().Ent.Singular(_getMain().Ent.DRIVE_FILE)), tdfileidLocation) + if result['mimeType'] == _gam().MIMETYPE_GA_FOLDER: + invalidTodriveFileIdExit([], Msg.NOT_AN_ENTITY.format(_gam().Ent.Singular(_gam().Ent.DRIVE_FILE)), tdfileidLocation) if not result['capabilities']['canEdit']: invalidTodriveFileIdExit([], Msg.NOT_WRITABLE, tdfileidLocation) if self.todrive['sheetEntity']: - if result['mimeType'] != _getMain().MIMETYPE_GA_SPREADSHEET: - invalidTodriveFileIdExit([], f'{Msg.NOT_A} {_getMain().Ent.Singular(_getMain().Ent.SPREADSHEET)}', tdfileidLocation) + if result['mimeType'] != _gam().MIMETYPE_GA_SPREADSHEET: + invalidTodriveFileIdExit([], f'{Msg.NOT_A} {_gam().Ent.Singular(_gam().Ent.SPREADSHEET)}', tdfileidLocation) if not GC.Values[GC.TODRIVE_CLIENTACCESS]: - _, sheet = _getMain().buildGAPIServiceObject(_getMain().chooseSaAPI(API.SHEETSTD, API.SHEETS), self.todrive['user']) + _, sheet = _gam().buildGAPIServiceObject(_gam().chooseSaAPI(API.SHEETSTD, API.SHEETS), self.todrive['user']) if sheet is None: - invalidTodriveUserExit(_getMain().Ent.USER, Msg.NOT_FOUND) + invalidTodriveUserExit(_gam().Ent.USER, Msg.NOT_FOUND) else: - sheet = _getMain().buildGAPIObject(API.SHEETS) + sheet = _gam().buildGAPIObject(API.SHEETS) try: - spreadsheet = _getMain().callGAPI(sheet.spreadsheets(), 'get', + spreadsheet = _gam().callGAPI(sheet.spreadsheets(), 'get', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=self.todrive['fileId'], fields='spreadsheetUrl,sheets(properties(sheetId,title),protectedRanges(range(sheetId),requestingUserCanEdit))') for sheetEntity in self.TDSHEET_ENTITY_MAP.values(): if self.todrive[sheetEntity]: - sheetId = _getMain().getSheetIdFromSheetEntity(spreadsheet, self.todrive[sheetEntity]) + sheetId = _gam().getSheetIdFromSheetEntity(spreadsheet, self.todrive[sheetEntity]) if sheetId is None: - if not self.todrive['addsheet'] and ((sheetEntity != 'sheetEntity') or (self.todrive[sheetEntity]['sheetType'] == _getMain().Ent.SHEET_ID)): + if not self.todrive['addsheet'] and ((sheetEntity != 'sheetEntity') or (self.todrive[sheetEntity]['sheetType'] == _gam().Ent.SHEET_ID)): invalidTodriveFileIdExit([self.todrive[sheetEntity]['sheetType'], self.todrive[sheetEntity]['sheetValue']], Msg.NOT_FOUND, tdsheetLocation[sheetEntity]) else: if self.todrive['addsheet']: invalidTodriveFileIdExit([self.todrive[sheetEntity]['sheetType'], self.todrive[sheetEntity]['sheetValue']], Msg.ALREADY_EXISTS, tdsheetLocation[sheetEntity]) - if _getMain().protectedSheetId(spreadsheet, sheetId): + if _gam().protectedSheetId(spreadsheet, sheetId): invalidTodriveFileIdExit([self.todrive[sheetEntity]['sheetType'], self.todrive[sheetEntity]['sheetValue']], Msg.NOT_WRITABLE, tdsheetLocation[sheetEntity]) self.todrive[sheetEntity]['sheetId'] = sheetId except (GAPI.notFound, GAPI.forbidden, GAPI.permissionDenied, @@ -901,45 +900,45 @@ class CSVPrintFile(): except GAPI.fileNotFound: invalidTodriveFileIdExit([], Msg.NOT_FOUND, tdfileidLocation) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - invalidTodriveUserExit(_getMain().Ent.USER, str(e)) - elif not self.todrive['parent'] or self.todrive['parent'] == _getMain().ROOT: - self.todrive['parentId'] = _getMain().ROOT + invalidTodriveUserExit(_gam().Ent.USER, str(e)) + elif not self.todrive['parent'] or self.todrive['parent'] == _gam().ROOT: + self.todrive['parentId'] = _gam().ROOT else: drive = getDriveObject() if self.todrive['parent'].startswith('id:'): try: - result = _getMain().callGAPI(drive.files(), 'get', + result = _gam().callGAPI(drive.files(), 'get', throwReasons=GAPI.DRIVE_USER_THROW_REASONS+[GAPI.FILE_NOT_FOUND, GAPI.INVALID], fileId=self.todrive['parent'][3:], fields='id,mimeType,capabilities(canEdit)', supportsAllDrives=True) except GAPI.fileNotFound: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_ID, Msg.NOT_FOUND) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_ID, Msg.NOT_FOUND) except GAPI.invalid as e: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_ID, str(e)) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_ID, str(e)) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - invalidTodriveUserExit(_getMain().Ent.USER, str(e)) - if result['mimeType'] != _getMain().MIMETYPE_GA_FOLDER: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_ID, Msg.NOT_AN_ENTITY.format(_getMain().Ent.Singular(_getMain().Ent.DRIVE_FOLDER))) + invalidTodriveUserExit(_gam().Ent.USER, str(e)) + if result['mimeType'] != _gam().MIMETYPE_GA_FOLDER: + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_ID, Msg.NOT_AN_ENTITY.format(_gam().Ent.Singular(_gam().Ent.DRIVE_FOLDER))) if not result['capabilities']['canEdit']: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_ID, Msg.NOT_WRITABLE) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_ID, Msg.NOT_WRITABLE) self.todrive['parentId'] = result['id'] else: try: - results = _getMain().callGAPIpages(drive.files(), 'list', 'files', + results = _gam().callGAPIpages(drive.files(), 'list', 'files', throwReasons=GAPI.DRIVE_USER_THROW_REASONS+[GAPI.INVALID_QUERY], retryReasons=[GAPI.UNKNOWN_ERROR], q=f"name = '{self.todrive['parent']}'", fields='nextPageToken,files(id,mimeType,capabilities(canEdit))', pageSize=1, supportsAllDrives=True) except GAPI.invalidQuery: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_NAME, Msg.NOT_FOUND) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_NAME, Msg.NOT_FOUND) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - invalidTodriveUserExit(_getMain().Ent.USER, str(e)) + invalidTodriveUserExit(_gam().Ent.USER, str(e)) if not results: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_NAME, Msg.NOT_FOUND) - if results[0]['mimeType'] != _getMain().MIMETYPE_GA_FOLDER: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_NAME, Msg.NOT_AN_ENTITY.format(_getMain().Ent.Singular(_getMain().Ent.DRIVE_FOLDER))) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_NAME, Msg.NOT_FOUND) + if results[0]['mimeType'] != _gam().MIMETYPE_GA_FOLDER: + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_NAME, Msg.NOT_AN_ENTITY.format(_gam().Ent.Singular(_gam().Ent.DRIVE_FOLDER))) if not results[0]['capabilities']['canEdit']: - invalidTodriveParentExit(_getMain().Ent.DRIVE_FOLDER_NAME, Msg.NOT_WRITABLE) + invalidTodriveParentExit(_gam().Ent.DRIVE_FOLDER_NAME, Msg.NOT_WRITABLE) self.todrive['parentId'] = results[0]['id'] def SortTitles(self): @@ -1069,9 +1068,9 @@ class CSVPrintFile(): def SetTimestampColumn(self, timestampColumn): self.timestampColumn = timestampColumn if not GC.Values[GC.OUTPUT_TIMEFORMAT]: - self.todaysTime = _getMain().ISOformatTimeStamp(_getMain().todaysTime()) + self.todaysTime = _gam().ISOformatTimeStamp(_gam().todaysTime()) else: - self.todaysTime = _getMain().todaysTime().strftime(GC.Values[GC.OUTPUT_TIMEFORMAT]) + self.todaysTime = _gam().todaysTime().strftime(GC.Values[GC.OUTPUT_TIMEFORMAT]) def SetSortHeaders(self, sortHeaders): self.sortHeaders = sortHeaders @@ -1207,11 +1206,11 @@ class CSVPrintFile(): for filterVal in self.rowFilter: columns = [t for t in self.titlesList if filterVal[0].match(t)] if not columns: - _getMain().stderrWarningMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_OUTPUT_COLUMNS.format(GC.CSV_OUTPUT_ROW_FILTER, filterVal[0].pattern)) + _gam().stderrWarningMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_OUTPUT_COLUMNS.format(GC.CSV_OUTPUT_ROW_FILTER, filterVal[0].pattern)) for filterVal in self.rowDropFilter: columns = [t for t in self.titlesList if filterVal[0].match(t)] if not columns: - _getMain().stderrWarningMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_OUTPUT_COLUMNS.format(GC.CSV_OUTPUT_ROW_DROP_FILTER, filterVal[0].pattern)) + _gam().stderrWarningMsg(Msg.COLUMN_DOES_NOT_MATCH_ANY_OUTPUT_COLUMNS.format(GC.CSV_OUTPUT_ROW_DROP_FILTER, filterVal[0].pattern)) def SetHeaderFilter(self, headerFilter): self.headerFilter = headerFilter @@ -1250,7 +1249,7 @@ class CSVPrintFile(): self.titlesList = [t for t in self.titlesList if self.HeaderFilterMatch(self.headerFilter, t)] self.titlesSet = set(self.titlesList) if not self.titlesSet: - _getMain().systemErrorExit(_getMain().USAGE_ERROR_RC, Msg.NO_COLUMNS_SELECTED_WITH_CSV_OUTPUT_HEADER_FILTER.format(GC.CSV_OUTPUT_HEADER_FILTER, GC.CSV_OUTPUT_HEADER_DROP_FILTER)) + _gam().systemErrorExit(_gam().USAGE_ERROR_RC, Msg.NO_COLUMNS_SELECTED_WITH_CSV_OUTPUT_HEADER_FILTER.format(GC.CSV_OUTPUT_HEADER_FILTER, GC.CSV_OUTPUT_HEADER_DROP_FILTER)) def FilterJSONHeaders(self): if self.headerDropFilter: @@ -1259,14 +1258,14 @@ class CSVPrintFile(): self.JSONtitlesList = [t for t in self.JSONtitlesList if self.HeaderFilterMatch(self.headerFilter, t)] self.JSONtitlesSet = set(self.JSONtitlesList) if not self.JSONtitlesSet: - _getMain().systemErrorExit(_getMain().USAGE_ERROR_RC, Msg.NO_COLUMNS_SELECTED_WITH_CSV_OUTPUT_HEADER_FILTER.format(GC.CSV_OUTPUT_HEADER_FILTER, GC.CSV_OUTPUT_HEADER_DROP_FILTER)) + _gam().systemErrorExit(_gam().USAGE_ERROR_RC, Msg.NO_COLUMNS_SELECTED_WITH_CSV_OUTPUT_HEADER_FILTER.format(GC.CSV_OUTPUT_HEADER_FILTER, GC.CSV_OUTPUT_HEADER_DROP_FILTER)) def writeCSVfile(self, list_type, clearRowFilters=False): def todriveCSVErrorExit(entityValueList, errMsg): - _getMain().systemErrorExit(_getMain().ACTION_FAILED_RC, _getMain().formatKeyValueList(_getMain().Ind.Spaces(), - _getMain().Ent.FormatEntityValueList(entityValueList)+[_getMain().Act.NotPerformed(), errMsg], - _getMain().currentCountNL(0, 0))) + _gam().systemErrorExit(_gam().ACTION_FAILED_RC, _gam().formatKeyValueList(_gam().Ind.Spaces(), + _gam().Ent.FormatEntityValueList(entityValueList)+[_gam().Act.NotPerformed(), errMsg], + _gam().currentCountNL(0, 0))) @staticmethod def itemgetter(*items): @@ -1293,7 +1292,7 @@ class CSVPrintFile(): writer.writerows(self.rows) return True except IOError as e: - _getMain().stderrErrorMsg(e) + _gam().stderrErrorMsg(e) return False def setDialect(lineterminator, noEscapeChar): @@ -1316,40 +1315,40 @@ class CSVPrintFile(): self.sortHeaders = [writerKeyMap[k.lower()] for k in self.sortHeaders if k.lower() in writerKeyMap] def writeCSVToStdout(): - csvFile = _getMain().StringIOobject() + csvFile = _gam().StringIOobject() writerDialect = setDialect('\n', self.noEscapeChar) writer = csv.DictWriter(csvFile, titlesList, extrasaction=extrasaction, **writerDialect) if writeCSVData(writer): try: GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD].write(csvFile.getvalue()) except IOError as e: - _getMain().stderrErrorMsg(_getMain().fdErrorMessage(GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD], 'stdout', e)) - _getMain().setSysExitRC(_getMain().FILE_ERROR_RC) - _getMain().closeFile(csvFile) + _gam().stderrErrorMsg(_gam().fdErrorMessage(GM.Globals[GM.STDOUT][GM.REDIRECT_MULTI_FD], 'stdout', e)) + _gam().setSysExitRC(_gam().FILE_ERROR_RC) + _gam().closeFile(csvFile) def writeCSVToFile(): csvFile = GM.Globals[GM.CSVFILE].get(GM.REDIRECT_FD, None) if not csvFile: - csvFile = _getMain().openFile(GM.Globals[GM.CSVFILE][GM.REDIRECT_NAME], GM.Globals[GM.CSVFILE][GM.REDIRECT_MODE], newline='', + csvFile = _gam().openFile(GM.Globals[GM.CSVFILE][GM.REDIRECT_NAME], GM.Globals[GM.CSVFILE][GM.REDIRECT_MODE], newline='', encoding=GM.Globals[GM.CSVFILE][GM.REDIRECT_ENCODING], errors='backslashreplace', continueOnError=True) if csvFile: writerDialect = setDialect(str(GC.Values[GC.CSV_OUTPUT_LINE_TERMINATOR]), self.noEscapeChar) writer = csv.DictWriter(csvFile, titlesList, extrasaction=extrasaction, **writerDialect) writeCSVData(writer) - _getMain().closeFile(csvFile) + _gam().closeFile(csvFile) def writeCSVToDrive(): numRows = len(self.rows) numColumns = len(titlesList) if numRows == 0 and not self.todrive['uploadnodata']: - _getMain().printKeyValueList([Msg.NO_CSV_DATA_TO_UPLOAD]) - _getMain().setSysExitRC(_getMain().NO_CSV_DATA_TO_UPLOAD_RC) + _gam().printKeyValueList([Msg.NO_CSV_DATA_TO_UPLOAD]) + _gam().setSysExitRC(_gam().NO_CSV_DATA_TO_UPLOAD_RC) return if self.todrive['addsheet'] or self.todrive['updatesheet']: - csvFile = _getMain().TemporaryFile(mode='w+', encoding=_getMain().UTF8) + csvFile = _gam().TemporaryFile(mode='w+', encoding=_gam().UTF8) else: - csvFile = _getMain().StringIOobject() + csvFile = _gam().StringIOobject() writerDialect = setDialect('\n', self.todrive['noescapechar']) writer = csv.DictWriter(csvFile, titlesList, extrasaction=extrasaction, **writerDialect) if writeCSVData(writer): @@ -1375,7 +1374,7 @@ class CSVPrintFile(): if title: title += ' - ' if not self.todrive['timeformat']: - title += _getMain().ISOformatTimeStamp(tdtime) + title += _gam().ISOformatTimeStamp(tdtime) else: title += tdtime.strftime(self.todrive['timeformat']) if self.todrive['sheettimestamp']: @@ -1385,73 +1384,73 @@ class CSVPrintFile(): if sheetTitle: sheetTitle += ' - ' if not self.todrive['sheettimeformat']: - sheetTitle += _getMain().ISOformatTimeStamp(tdtime) + sheetTitle += _gam().ISOformatTimeStamp(tdtime) else: sheetTitle += tdtime.strftime(self.todrive['sheettimeformat']) - action = _getMain().Act.Get() + action = _gam().Act.Get() if not GC.Values[GC.TODRIVE_CLIENTACCESS]: - user, drive = _getMain().buildGAPIServiceObject(_getMain().chooseSaAPI(API.DRIVETD, API.DRIVE3), self.todrive['user']) + user, drive = _gam().buildGAPIServiceObject(_gam().chooseSaAPI(API.DRIVETD, API.DRIVE3), self.todrive['user']) if not drive: - _getMain().closeFile(csvFile) + _gam().closeFile(csvFile) return else: user = self.todrive['user'] - drive = _getMain().buildGAPIObject(API.DRIVE3) + drive = _gam().buildGAPIObject(API.DRIVE3) importSize = csvFile.tell() # Add/Update sheet try: if self.todrive['addsheet'] or self.todrive['updatesheet']: - _getMain().Act.Set(_getMain().Act.CREATE if self.todrive['addsheet'] else _getMain().Act.UPDATE) - result = _getMain().callGAPI(drive.about(), 'get', + _gam().Act.Set(_gam().Act.CREATE if self.todrive['addsheet'] else _gam().Act.UPDATE) + result = _gam().callGAPI(drive.about(), 'get', throwReasons=GAPI.DRIVE_USER_THROW_REASONS, fields='maxImportSizes') - if numRows*numColumns > _getMain().MAX_GOOGLE_SHEET_CELLS or importSize > int(result['maxImportSizes'][_getMain().MIMETYPE_GA_SPREADSHEET]): - todriveCSVErrorExit([_getMain().Ent.USER, user], Msg.RESULTS_TOO_LARGE_FOR_GOOGLE_SPREADSHEET) + if numRows*numColumns > _gam().MAX_GOOGLE_SHEET_CELLS or importSize > int(result['maxImportSizes'][_gam().MIMETYPE_GA_SPREADSHEET]): + todriveCSVErrorExit([_gam().Ent.USER, user], Msg.RESULTS_TOO_LARGE_FOR_GOOGLE_SPREADSHEET) fields = ','.join(['id', 'mimeType', 'webViewLink', 'name', 'capabilities(canEdit)']) body = {'description': self.todrive['description']} if body['description'] is None: - body['description'] = _getMain().Cmd.QuotedArgumentList(_getMain().Cmd.AllArguments()) + body['description'] = _gam().Cmd.QuotedArgumentList(_gam().Cmd.AllArguments()) if not self.todrive['retaintitle']: body['name'] = title - result = _getMain().callGAPI(drive.files(), 'update', + result = _gam().callGAPI(drive.files(), 'update', throwReasons=GAPI.DRIVE_USER_THROW_REASONS+[GAPI.INSUFFICIENT_PERMISSIONS, GAPI.INSUFFICIENT_PARENT_PERMISSIONS, GAPI.FILE_NOT_FOUND, GAPI.UNKNOWN_ERROR], fileId=self.todrive['fileId'], body=body, fields=fields, supportsAllDrives=True) - entityValueList = [_getMain().Ent.USER, user, _getMain().Ent.DRIVE_FILE_ID, self.todrive['fileId']] + entityValueList = [_gam().Ent.USER, user, _gam().Ent.DRIVE_FILE_ID, self.todrive['fileId']] if not result['capabilities']['canEdit']: todriveCSVErrorExit(entityValueList, Msg.NOT_WRITABLE) - if result['mimeType'] != _getMain().MIMETYPE_GA_SPREADSHEET: - todriveCSVErrorExit(entityValueList, f'{Msg.NOT_A} {_getMain().Ent.Singular(_getMain().Ent.SPREADSHEET)}') + if result['mimeType'] != _gam().MIMETYPE_GA_SPREADSHEET: + todriveCSVErrorExit(entityValueList, f'{Msg.NOT_A} {_gam().Ent.Singular(_gam().Ent.SPREADSHEET)}') if not GC.Values[GC.TODRIVE_CLIENTACCESS]: - _, sheet = _getMain().buildGAPIServiceObject(_getMain().chooseSaAPI(API.SHEETSTD, API.SHEETS), user) + _, sheet = _gam().buildGAPIServiceObject(_gam().chooseSaAPI(API.SHEETSTD, API.SHEETS), user) if sheet is None: return else: - sheet = _getMain().buildGAPIObject(API.SHEETS) + sheet = _gam().buildGAPIObject(API.SHEETS) csvFile.seek(0) spreadsheet = None if self.todrive['updatesheet']: for sheetEntity in self.TDSHEET_ENTITY_MAP.values(): if self.todrive[sheetEntity]: - entityValueList = [_getMain().Ent.USER, user, _getMain().Ent.SPREADSHEET, title, self.todrive[sheetEntity]['sheetType'], self.todrive[sheetEntity]['sheetValue']] + entityValueList = [_gam().Ent.USER, user, _gam().Ent.SPREADSHEET, title, self.todrive[sheetEntity]['sheetType'], self.todrive[sheetEntity]['sheetValue']] if spreadsheet is None: - spreadsheet = _getMain().callGAPI(sheet.spreadsheets(), 'get', + spreadsheet = _gam().callGAPI(sheet.spreadsheets(), 'get', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=self.todrive['fileId'], fields='spreadsheetUrl,sheets(properties(sheetId,title),protectedRanges(range(sheetId),requestingUserCanEdit))') - sheetId = _getMain().getSheetIdFromSheetEntity(spreadsheet, self.todrive[sheetEntity]) + sheetId = _gam().getSheetIdFromSheetEntity(spreadsheet, self.todrive[sheetEntity]) if sheetId is None: - if ((sheetEntity != 'sheetEntity') or (self.todrive[sheetEntity]['sheetType'] == _getMain().Ent.SHEET_ID)): + if ((sheetEntity != 'sheetEntity') or (self.todrive[sheetEntity]['sheetType'] == _gam().Ent.SHEET_ID)): todriveCSVErrorExit(entityValueList, Msg.NOT_FOUND) self.todrive['addsheet'] = True else: - if _getMain().protectedSheetId(spreadsheet, sheetId): + if _gam().protectedSheetId(spreadsheet, sheetId): todriveCSVErrorExit(entityValueList, Msg.NOT_WRITABLE) self.todrive[sheetEntity]['sheetId'] = sheetId if self.todrive['addsheet']: body = {'requests': [{'addSheet': {'properties': {'title': sheetTitle, 'sheetType': 'GRID'}}}]} try: - addresult = _getMain().callGAPI(sheet.spreadsheets(), 'batchUpdate', + addresult = _gam().callGAPI(sheet.spreadsheets(), 'batchUpdate', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=self.todrive['fileId'], body=body) self.todrive['sheetEntity'] = {'sheetId': addresult['replies'][0]['addSheet']['properties']['sheetId']} @@ -1484,38 +1483,38 @@ class CSVPrintFile(): body['requests'].append({'copyPaste': {'source': {'sheetId': self.todrive['sheetEntity']['sheetId']}, 'destination': {'sheetId': self.todrive['copySheetEntity']['sheetId']}, 'pasteType': 'PASTE_NORMAL'}}) try: - _getMain().callGAPI(sheet.spreadsheets(), 'batchUpdate', + _gam().callGAPI(sheet.spreadsheets(), 'batchUpdate', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=self.todrive['fileId'], body=body) except (GAPI.notFound, GAPI.forbidden, GAPI.permissionDenied, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.failedPrecondition) as e: todriveCSVErrorExit(entityValueList, str(e)) - _getMain().closeFile(csvFile) + _gam().closeFile(csvFile) # Create/update file else: if GC.Values[GC.TODRIVE_CONVERSION]: - result = _getMain().callGAPI(drive.about(), 'get', + result = _gam().callGAPI(drive.about(), 'get', throwReasons=GAPI.DRIVE_USER_THROW_REASONS, fields='maxImportSizes') - if numRows*len(titlesList) > _getMain().MAX_GOOGLE_SHEET_CELLS or importSize > int(result['maxImportSizes'][_getMain().MIMETYPE_GA_SPREADSHEET]): - _getMain().printKeyValueList([_getMain().WARNING, Msg.RESULTS_TOO_LARGE_FOR_GOOGLE_SPREADSHEET]) + if numRows*len(titlesList) > _gam().MAX_GOOGLE_SHEET_CELLS or importSize > int(result['maxImportSizes'][_gam().MIMETYPE_GA_SPREADSHEET]): + _gam().printKeyValueList([_gam().WARNING, Msg.RESULTS_TOO_LARGE_FOR_GOOGLE_SPREADSHEET]) mimeType = 'text/csv' else: - mimeType = _getMain().MIMETYPE_GA_SPREADSHEET + mimeType = _gam().MIMETYPE_GA_SPREADSHEET else: mimeType = 'text/csv' fields = ','.join(['id', 'mimeType', 'webViewLink']) body = {'description': self.todrive['description'], 'mimeType': mimeType} if body['description'] is None: - body['description'] = _getMain().Cmd.QuotedArgumentList(_getMain().Cmd.AllArguments()) + body['description'] = _gam().Cmd.QuotedArgumentList(_gam().Cmd.AllArguments()) if not self.todrive['fileId'] or not self.todrive['retaintitle']: body['name'] = title try: if not self.todrive['fileId']: - _getMain().Act.Set(_getMain().Act.CREATE) + _gam().Act.Set(_gam().Act.CREATE) body['parents'] = [self.todrive['parentId']] - result = _getMain().callGAPI(drive.files(), 'create', + result = _gam().callGAPI(drive.files(), 'create', bailOnInternalError=True, throwReasons=GAPI.DRIVE_USER_THROW_REASONS+[GAPI.FORBIDDEN, GAPI.INSUFFICIENT_PERMISSIONS, GAPI.INSUFFICIENT_PARENT_PERMISSIONS, GAPI.FILE_NOT_FOUND, GAPI.UNKNOWN_ERROR, GAPI.INTERNAL_ERROR, GAPI.STORAGE_QUOTA_EXCEEDED, @@ -1524,8 +1523,8 @@ class CSVPrintFile(): media_body=googleapiclient.http.MediaIoBaseUpload(io.BytesIO(csvFile.getvalue().encode()), mimetype='text/csv', resumable=True), fields=fields, supportsAllDrives=True) else: - _getMain().Act.Set(_getMain().Act.UPDATE) - result = _getMain().callGAPI(drive.files(), 'update', + _gam().Act.Set(_gam().Act.UPDATE) + result = _gam().callGAPI(drive.files(), 'update', bailOnInternalError=True, throwReasons=GAPI.DRIVE_USER_THROW_REASONS+[GAPI.INSUFFICIENT_PERMISSIONS, GAPI.INSUFFICIENT_PARENT_PERMISSIONS, GAPI.FILE_NOT_FOUND, GAPI.UNKNOWN_ERROR, GAPI.INTERNAL_ERROR], @@ -1535,21 +1534,21 @@ class CSVPrintFile(): fields=fields, supportsAllDrives=True) spreadsheetId = result['id'] except GAPI.internalError as e: - _getMain().entityActionFailedWarning([_getMain().Ent.DRIVE_FILE, body['name']], Msg.UPLOAD_CSV_FILE_INTERNAL_ERROR.format(str(e), str(numRows))) - _getMain().closeFile(csvFile) + _gam().entityActionFailedWarning([_gam().Ent.DRIVE_FILE, body['name']], Msg.UPLOAD_CSV_FILE_INTERNAL_ERROR.format(str(e), str(numRows))) + _gam().closeFile(csvFile) return - _getMain().closeFile(csvFile) + _gam().closeFile(csvFile) if not self.todrive['fileId'] and self.todrive['share']: - _getMain().Act.Set(_getMain().Act.SHARE) + _gam().Act.Set(_gam().Act.SHARE) for share in self.todrive['share']: if share['emailAddress'] != user: try: - _getMain().callGAPI(drive.permissions(), 'create', + _gam().callGAPI(drive.permissions(), 'create', bailOnInternalError=True, throwReasons=GAPI.DRIVE_ACCESS_THROW_REASONS+GAPI.DRIVE3_CREATE_ACL_THROW_REASONS, fileId=spreadsheetId, sendNotificationEmail=False, body=share, fields='', supportsAllDrives=True) - _getMain().entityActionPerformed([_getMain().Ent.USER, user, _getMain().Ent.SPREADSHEET, title, - _getMain().Ent.TARGET_USER, share['emailAddress'], _getMain().Ent.ROLE, share['role']]) + _gam().entityActionPerformed([_gam().Ent.USER, user, _gam().Ent.SPREADSHEET, title, + _gam().Ent.TARGET_USER, share['emailAddress'], _gam().Ent.ROLE, share['role']]) except (GAPI.badRequest, GAPI.invalid, GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.insufficientParentPermissions, GAPI.unknownError, GAPI.ownershipChangeAcrossDomainNotPermitted, GAPI.teamDriveDomainUsersOnlyRestriction, GAPI.teamDriveTeamMembersOnlyRestriction, @@ -1564,22 +1563,22 @@ class CSVPrintFile(): GAPI.cannotModifyInheritedPermission, GAPI.teamDrivesFolderSharingNotSupported, GAPI.invalidLinkVisibility, GAPI.invalidSharingRequest, GAPI.fileNeverWritable, GAPI.abusiveContentRestriction) as e: - _getMain().entityActionFailedWarning([_getMain().Ent.USER, user, _getMain().Ent.SPREADSHEET, title, - _getMain().Ent.TARGET_USER, share['emailAddress'], _getMain().Ent.ROLE, share['role']], + _gam().entityActionFailedWarning([_gam().Ent.USER, user, _gam().Ent.SPREADSHEET, title, + _gam().Ent.TARGET_USER, share['emailAddress'], _gam().Ent.ROLE, share['role']], str(e)) - if ((result['mimeType'] == _getMain().MIMETYPE_GA_SPREADSHEET) and + if ((result['mimeType'] == _gam().MIMETYPE_GA_SPREADSHEET) and (self.todrive['sheetEntity'] or self.todrive['locale'] or self.todrive['timeZone'] or self.todrive['sheettitle'] or self.todrive['cellwrap'] or self.todrive['cellnumberformat'])): if not GC.Values[GC.TODRIVE_CLIENTACCESS]: - _, sheet = _getMain().buildGAPIServiceObject(_getMain().chooseSaAPI(API.SHEETSTD, API.SHEETS), user) + _, sheet = _gam().buildGAPIServiceObject(_gam().chooseSaAPI(API.SHEETSTD, API.SHEETS), user) if sheet is None: return else: - sheet = _getMain().buildGAPIObject(API.SHEETS) + sheet = _gam().buildGAPIObject(API.SHEETS) try: body = {'requests': []} if self.todrive['sheetEntity'] or self.todrive['sheettitle'] or self.todrive['cellwrap']: - spreadsheet = _getMain().callGAPI(sheet.spreadsheets(), 'get', + spreadsheet = _gam().callGAPI(sheet.spreadsheets(), 'get', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=spreadsheetId, fields='sheets/properties') spreadsheet['sheets'][0]['properties']['title'] = sheetTitle @@ -1596,47 +1595,47 @@ class CSVPrintFile(): body['requests'].append({'updateSpreadsheetProperties': {'properties': {'timeZone': self.todrive['timeZone']}, 'fields': 'timeZone'}}) if body['requests']: - _getMain().callGAPI(sheet.spreadsheets(), 'batchUpdate', + _gam().callGAPI(sheet.spreadsheets(), 'batchUpdate', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=spreadsheetId, body=body) except (GAPI.notFound, GAPI.forbidden, GAPI.permissionDenied, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.failedPrecondition, GAPI.teamDriveFileLimitExceeded, GAPI.teamDriveHierarchyTooDeep) as e: - todriveCSVErrorExit([_getMain().Ent.USER, user, _getMain().Ent.SPREADSHEET, title], str(e)) - _getMain().Act.Set(action) + todriveCSVErrorExit([_gam().Ent.USER, user, _gam().Ent.SPREADSHEET, title], str(e)) + _gam().Act.Set(action) file_url = result['webViewLink'] msg_txt = f'{Msg.DATA_UPLOADED_TO_DRIVE_FILE}:\n{file_url}' if not self.todrive['returnidonly']: - _getMain().printKeyValueList([msg_txt]) + _gam().printKeyValueList([msg_txt]) else: if self.todrive['fileId']: - _getMain().writeStdout(f'{self.todrive["fileId"]}\n') + _gam().writeStdout(f'{self.todrive["fileId"]}\n') else: - _getMain().writeStdout(f'{spreadsheetId}\n') + _gam().writeStdout(f'{spreadsheetId}\n') if not self.todrive['subject']: subject = title else: subject = self.todrive['subject'].replace('#file#', title).replace('#sheet#', sheetTitle) if not self.todrive['noemail']: - _getMain().send_email(subject, msg_txt, user, clientAccess=GC.Values[GC.TODRIVE_CLIENTACCESS], msgFrom=self.todrive['from']) + _gam().send_email(subject, msg_txt, user, clientAccess=GC.Values[GC.TODRIVE_CLIENTACCESS], msgFrom=self.todrive['from']) if self.todrive['notify']: for recipient in self.todrive['share']+self.todrive['alert']: if recipient['emailAddress'] != user: - _getMain().send_email(subject, msg_txt, recipient['emailAddress'], clientAccess=GC.Values[GC.TODRIVE_CLIENTACCESS], msgFrom=self.todrive['from']) + _gam().send_email(subject, msg_txt, recipient['emailAddress'], clientAccess=GC.Values[GC.TODRIVE_CLIENTACCESS], msgFrom=self.todrive['from']) if not self.todrive['nobrowser']: webbrowser.open(file_url) except (GAPI.forbidden, GAPI.insufficientPermissions): - _getMain().printWarningMessage(_getMain().INSUFFICIENT_PERMISSIONS_RC, Msg.INSUFFICIENT_PERMISSIONS_TO_PERFORM_TASK) + _gam().printWarningMessage(_gam().INSUFFICIENT_PERMISSIONS_RC, Msg.INSUFFICIENT_PERMISSIONS_TO_PERFORM_TASK) except (GAPI.fileNotFound, GAPI.unknownError, GAPI.internalError, GAPI.storageQuotaExceeded) as e: if not self.todrive['fileId']: - _getMain().entityActionFailedWarning([_getMain().Ent.DRIVE_FOLDER, self.todrive['parentId']], str(e)) + _gam().entityActionFailedWarning([_gam().Ent.DRIVE_FOLDER, self.todrive['parentId']], str(e)) else: - _getMain().entityActionFailedWarning([_getMain().Ent.DRIVE_FILE, self.todrive['fileId']], str(e)) + _gam().entityActionFailedWarning([_gam().Ent.DRIVE_FILE, self.todrive['fileId']], str(e)) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - _getMain().userDriveServiceNotEnabledWarning(user, str(e), 0, 0) + _gam().userDriveServiceNotEnabledWarning(user, str(e), 0, 0) else: - _getMain().closeFile(csvFile) + _gam().closeFile(csvFile) if GM.Globals[GM.CSVFILE][GM.REDIRECT_QUEUE] is not None: GM.Globals[GM.CSVFILE][GM.REDIRECT_QUEUE].put((GM.REDIRECT_QUEUE_NAME, list_type)) @@ -1743,28 +1742,28 @@ class CSVPrintFile(): writeCSVToFile() if self.todrive: writeCSVToDrive() - if GM.Globals[GM.CSVFILE][GM.REDIRECT_MODE] == _getMain().DEFAULT_FILE_APPEND_MODE: + if GM.Globals[GM.CSVFILE][GM.REDIRECT_MODE] == _gam().DEFAULT_FILE_APPEND_MODE: GM.Globals[GM.CSVFILE][GM.REDIRECT_WRITE_HEADER] = False def writeEntityNoHeaderCSVFile(entityType, entityList): csvPF = CSVPrintFile(entityType) - _, _, entityList = _getMain().getEntityArgument(entityList) - if entityType == _getMain().Ent.USER: + _, _, entityList = _gam().getEntityArgument(entityList) + if entityType == _gam().Ent.USER: for entity in entityList: - csvPF.WriteRowNoFilter({entityType: _getMain().normalizeEmailAddressOrUID(entity)}) + csvPF.WriteRowNoFilter({entityType: _gam().normalizeEmailAddressOrUID(entity)}) else: for entity in entityList: csvPF.WriteRowNoFilter({entityType: entity}) GM.Globals[GM.CSVFILE][GM.REDIRECT_WRITE_HEADER] = False - csvPF.writeCSVfile(_getMain().Ent.Plural(entityType)) + csvPF.writeCSVfile(_gam().Ent.Plural(entityType)) def getTodriveOnly(csvPF): - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if csvPF and myarg == 'todrive': csvPF.GetTodriveParameters() else: - _getMain().unknownArgumentExit() + _gam().unknownArgumentExit() DEFAULT_SKIP_OBJECTS = {'kind', 'etag', 'etags', '@type'} @@ -1774,11 +1773,11 @@ def cleanJSON(topStructure, listLimit=None, skipObjects=None, timeObjects=None): if not isinstance(structure, (dict, list)): if key not in timeObjects: if isinstance(structure, str) and GC.Values[GC.CSV_OUTPUT_CONVERT_CR_NL]: - return _getMain().escapeCRsNLs(structure) + return _gam().escapeCRsNLs(structure) return structure if isinstance(structure, str) and not structure.isdigit(): - return _getMain().formatLocalTime(structure) - return _getMain().formatLocalTimestamp(structure) + return _gam().formatLocalTime(structure) + return _gam().formatLocalTimestamp(structure) if isinstance(structure, list): listLen = len(structure) listLen = min(listLen, listLimit or listLen) @@ -1796,16 +1795,16 @@ def flattenJSON(topStructure, flattened=None, if key not in timeObjects: if isinstance(structure, str): if GC.Values[GC.CSV_OUTPUT_CONVERT_CR_NL] and (structure.find('\n') >= 0 or structure.find('\r') >= 0): - flattened[path] = _getMain().escapeCRsNLs(structure) + flattened[path] = _gam().escapeCRsNLs(structure) else: flattened[path] = structure else: flattened[path] = structure else: if isinstance(structure, str) and not structure.isdigit(): - flattened[path] = _getMain().formatLocalTime(structure) + flattened[path] = _gam().formatLocalTime(structure) else: - flattened[path] = _getMain().formatLocalTimestamp(structure) + flattened[path] = _gam().formatLocalTimestamp(structure) elif isinstance(structure, list): listLen = len(structure) listLen = min(listLen, listLimit or listLen) @@ -1841,35 +1840,35 @@ def showJSON(showName, showValue, skipObjects=None, timeObjects=None, if objectName in subSkipObjects: return if objectName is not None: - _getMain().printJSONKey(objectName) + _gam().printJSONKey(objectName) subObjectKey = dictObjectsKey.get(objectName) if isinstance(objectValue, list): if objectName in simpleLists: - _getMain().printJSONValue(' '.join(objectValue)) + _gam().printJSONValue(' '.join(objectValue)) return if len(objectValue) == 1 and isinstance(objectValue[0], (str, bool, float, int)): if objectName is not None: - _getMain().printJSONValue(objectValue[0]) + _gam().printJSONValue(objectValue[0]) else: - _getMain().printKeyValueList([objectValue[0]]) + _gam().printKeyValueList([objectValue[0]]) return if objectName is not None: - _getMain().printBlankLine() - _getMain().Ind.Increment() + _gam().printBlankLine() + _gam().Ind.Increment() for subValue in objectValue: if isinstance(subValue, (str, bool, float, int)): - _getMain().printKeyValueList([subValue]) + _gam().printKeyValueList([subValue]) else: _show(None, subValue, subObjectKey, objectName, level+1, DEFAULT_SKIP_OBJECTS) if objectName is not None: - _getMain().Ind.Decrement() + _gam().Ind.Decrement() elif isinstance(objectValue, dict): if not subObjectKey: subObjectKey = dictObjectsKey.get(subObjectName) indentAfterFirst = unindentAfterLast = False if objectName is not None: - _getMain().printBlankLine() - _getMain().Ind.Increment() + _gam().printBlankLine() + _gam().Ind.Increment() elif level > 0: indentAfterFirst = unindentAfterLast = True subObjects = sorted(objectValue) if sortDictKeys else objectValue.keys() @@ -1891,27 +1890,27 @@ def showJSON(showName, showValue, skipObjects=None, timeObjects=None, if subObject not in subSkipObjects: _show(subObject, objectValue[subObject], subObjectKey, None, level+1, DEFAULT_SKIP_OBJECTS) if indentAfterFirst: - _getMain().Ind.Increment() + _gam().Ind.Increment() indentAfterFirst = False if objectName is not None or ((not indentAfterFirst) and unindentAfterLast): - _getMain().Ind.Decrement() + _gam().Ind.Decrement() else: if objectName not in timeObjects: if isinstance(objectValue, str) and (objectValue.find('\n') >= 0 or objectValue.find('\r') >= 0): if GC.Values[GC.SHOW_CONVERT_CR_NL]: - _getMain().printJSONValue(_getMain().escapeCRsNLs(objectValue)) + _gam().printJSONValue(_gam().escapeCRsNLs(objectValue)) else: - _getMain().printBlankLine() - _getMain().Ind.Increment() - _getMain().printKeyValueList([_getMain().Ind.MultiLineText(objectValue)]) - _getMain().Ind.Decrement() + _gam().printBlankLine() + _gam().Ind.Increment() + _gam().printKeyValueList([_gam().Ind.MultiLineText(objectValue)]) + _gam().Ind.Decrement() else: - _getMain().printJSONValue(objectValue if objectValue is not None else '') + _gam().printJSONValue(objectValue if objectValue is not None else '') else: if isinstance(objectValue, str) and not objectValue.isdigit(): - _getMain().printJSONValue(_getMain().formatLocalTime(objectValue)) + _gam().printJSONValue(_gam().formatLocalTime(objectValue)) else: - _getMain().printJSONValue(_getMain().formatLocalTimestamp(objectValue)) + _gam().printJSONValue(_gam().formatLocalTimestamp(objectValue)) timeObjects = timeObjects or set() simpleLists = simpleLists or set() @@ -1926,12 +1925,12 @@ class FormatJSONQuoteChar(): self.SetQuoteChar(GM.Globals.get(GM.CSV_OUTPUT_QUOTE_CHAR, GC.Values.get(GC.CSV_OUTPUT_QUOTE_CHAR, '"'))) if not formatJSONOnly: return - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if myarg == 'formatjson': self.SetFormatJSON(True) return - _getMain().unknownArgumentExit() + _gam().unknownArgumentExit() def SetCsvPF(self, csvPF): self.csvPF = csvPF @@ -1945,7 +1944,7 @@ class FormatJSONQuoteChar(): if myarg == 'formatjson': self.SetFormatJSON(True) return - _getMain().unknownArgumentExit() + _gam().unknownArgumentExit() def SetQuoteChar(self, quoteChar): self.quoteChar = quoteChar @@ -1954,9 +1953,9 @@ class FormatJSONQuoteChar(): def GetQuoteChar(self, myarg): if self.csvPF and myarg == 'quotechar': - self.SetQuoteChar(_getMain().getCharacter()) + self.SetQuoteChar(_gam().getCharacter()) return - _getMain().unknownArgumentExit() + _gam().unknownArgumentExit() def GetFormatJSONQuoteChar(self, myarg, addTitle=False, noExit=False): if myarg == 'formatjson': @@ -1965,11 +1964,11 @@ class FormatJSONQuoteChar(): self.csvPF.AddJSONTitles('JSON') return True if self.csvPF and myarg == 'quotechar': - self.SetQuoteChar(_getMain().getCharacter()) + self.SetQuoteChar(_gam().getCharacter()) return True if noExit: return False - _getMain().unknownArgumentExit() + _gam().unknownArgumentExit() # Batch processing request_id fields RI_ENTITY = 0 diff --git a/src/gam/util/email.py b/src/gam/util/email.py index f1229a5e..f6d07166 100644 --- a/src/gam/util/email.py +++ b/src/gam/util/email.py @@ -22,67 +22,63 @@ from gamlib import glcfg as GC from gamlib import glgapi as GAPI -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] # Add attachements to an email message def _addAttachmentsToMessage(message, attachments): - gam = _getMain() for attachment in attachments: try: - attachFilename = gam.setFilePath(attachment[0], GC.INPUT_DIR) + attachFilename = _gam().setFilePath(attachment[0], GC.INPUT_DIR) attachContentType, attachEncoding = mimetypes.guess_type(attachFilename) if attachContentType is None or attachEncoding is not None: attachContentType = 'application/octet-stream' main_type, sub_type = attachContentType.split('/', 1) if main_type == 'text': - msg = MIMEText(gam.readFile(attachFilename, 'r', attachment[1]), _subtype=sub_type, _charset=gam.UTF8) + msg = MIMEText(_gam().readFile(attachFilename, 'r', attachment[1]), _subtype=sub_type, _charset=_gam().UTF8) elif main_type == 'image': - msg = MIMEImage(gam.readFile(attachFilename, 'rb'), _subtype=sub_type) + msg = MIMEImage(_gam().readFile(attachFilename, 'rb'), _subtype=sub_type) elif main_type == 'audio': - msg = MIMEAudio(gam.readFile(attachFilename, 'rb'), _subtype=sub_type) + msg = MIMEAudio(_gam().readFile(attachFilename, 'rb'), _subtype=sub_type) elif main_type == 'application': - msg = MIMEApplication(gam.readFile(attachFilename, 'rb'), _subtype=sub_type) + msg = MIMEApplication(_gam().readFile(attachFilename, 'rb'), _subtype=sub_type) else: msg = MIMEBase(main_type, sub_type) - msg.set_payload(gam.readFile(attachFilename, 'rb')) + msg.set_payload(_gam().readFile(attachFilename, 'rb')) msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachFilename)) message.attach(msg) except (IOError, UnicodeDecodeError) as e: - gam.usageErrorExit(f'{attachFilename}: {str(e)}') + _gam().usageErrorExit(f'{attachFilename}: {str(e)}') # Add embedded images to an email message def _addEmbeddedImagesToMessage(message, embeddedImages): - gam = _getMain() for embeddedImage in embeddedImages: try: - imageFilename = gam.setFilePath(embeddedImage[0], GC.INPUT_DIR) + imageFilename = _gam().setFilePath(embeddedImage[0], GC.INPUT_DIR) imageContentType, imageEncoding = mimetypes.guess_type(imageFilename) if imageContentType is None or imageEncoding is not None: imageContentType = 'application/octet-stream' main_type, sub_type = imageContentType.split('/', 1) if main_type == 'image': - msg = MIMEImage(gam.readFile(imageFilename, 'rb'), _subtype=sub_type) + msg = MIMEImage(_gam().readFile(imageFilename, 'rb'), _subtype=sub_type) else: msg = MIMEBase(main_type, sub_type) - msg.set_payload(gam.readFile(imageFilename, 'rb')) + msg.set_payload(_gam().readFile(imageFilename, 'rb')) msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(imageFilename)) msg.add_header('Content-ID', f'<{embeddedImage[1]}>') message.attach(msg) except (IOError, UnicodeDecodeError) as e: - gam.usageErrorExit(f'{imageFilename}: {str(e)}') + _gam().usageErrorExit(f'{imageFilename}: {str(e)}') # Send an email def send_email(msgSubject, msgBody, msgTo, i=0, count=0, clientAccess=False, msgFrom=None, msgReplyTo=None, html=False, charset=None, attachments=None, embeddedImages=None, msgHeaders=None, ccRecipients=None, bccRecipients=None, mailBox=None, threadId=None, action=None): - gam = _getMain() - Act = gam.Act - Ent = gam.Ent + Act = _gam().Act + Ent = _gam().Ent if charset is None: - charset = gam.UTF8 + charset = _gam().UTF8 if action is None: action = Act.SENDEMAIL @@ -96,26 +92,26 @@ def send_email(msgSubject, msgBody, msgTo, i=0, count=0, clientAccess=False, msg toSent.remove(addr) toFailed[addr] = f'{err[0]}: {err[1]}' if toSent: - gam.entityActionPerformed([entityType, ','.join(toSent), Ent.MESSAGE, msgSubject], i, count) + _gam().entityActionPerformed([entityType, ','.join(toSent), Ent.MESSAGE, msgSubject], i, count) for addr, errMsg in toFailed.items(): - gam.entityActionFailedWarning([entityType, addr, Ent.MESSAGE, msgSubject], errMsg, i, count) + _gam().entityActionFailedWarning([entityType, addr, Ent.MESSAGE, msgSubject], errMsg, i, count) def cleanAddr(emailAddr): - match = gam.NAME_EMAIL_ADDRESS_PATTERN.match(emailAddr) + match = _gam().NAME_EMAIL_ADDRESS_PATTERN.match(emailAddr) if match: emailName = match.group(1) - emailAddr = gam.normalizeEmailAddressOrUID(match.group(2), noUid=True, noLower=True) + emailAddr = _gam().normalizeEmailAddressOrUID(match.group(2), noUid=True, noLower=True) return (f'{emailName} <{emailAddr}>', emailAddr) - emailAddr = gam.normalizeEmailAddressOrUID(emailAddr, noUid=True, noLower=True) + emailAddr = _gam().normalizeEmailAddressOrUID(emailAddr, noUid=True, noLower=True) return (emailAddr, emailAddr) if msgFrom is None: - msgFrom = gam._getAdminEmail() + msgFrom = _gam()._getAdminEmail() # Force ASCII for RFC compliance # xmlcharref seems to work to display at least # some unicode in HTML body and is ignored in # plain text body. -# msgBody = msgBody.encode('ascii', 'xmlcharrefreplace').decode(gam.UTF8) +# msgBody = msgBody.encode('ascii', 'xmlcharrefreplace').decode(_gam().UTF8) if not attachments and not embeddedImages: message = MIMEText(msgBody, ['plain', 'html'][html], charset) else: @@ -145,26 +141,26 @@ def send_email(msgSubject, msgBody, msgTo, i=0, count=0, clientAccess=False, msg Act.Set(action) if not GC.Values[GC.SMTP_HOST]: if not clientAccess: - userId, gmail = gam.buildGAPIServiceObject(API.GMAIL, mailBoxAddr) + userId, gmail = _gam().buildGAPIServiceObject(API.GMAIL, mailBoxAddr) if not gmail: Act.Set(parentAction) return else: userId = mailBoxAddr - gmail = gam.buildGAPIObject(API.GMAIL) + gmail = _gam().buildGAPIObject(API.GMAIL) message['To'] = msgTo if msgTo else userId body = {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()} if threadId is not None: body['threadId'] = threadId try: - result = gam.callGAPI(gmail.users().messages(), 'send', + result = _gam().callGAPI(gmail.users().messages(), 'send', throwReasons=[GAPI.SERVICE_NOT_AVAILABLE, GAPI.AUTH_ERROR, GAPI.DOMAIN_POLICY, GAPI.INVALID, GAPI.INVALID_ARGUMENT, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], userId=userId, body=body, fields='id') - gam.entityActionPerformedMessage([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], f"{result['id']}", i, count) + _gam().entityActionPerformedMessage([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], f"{result['id']}", i, count) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy, GAPI.invalid, GAPI.invalidArgument, GAPI.forbidden, GAPI.permissionDenied) as e: - gam.entityActionFailedWarning([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], str(e), i, count) + _gam().entityActionFailedWarning([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], str(e), i, count) else: message['To'] = msgTo if msgTo else mailBoxAddr server = None @@ -175,7 +171,7 @@ def send_email(msgSubject, msgBody, msgTo, i=0, count=0, clientAccess=False, msg server.starttls(context=ssl.create_default_context(cafile=GC.Values[GC.CACERTS_PEM])) if GC.Values[GC.SMTP_USERNAME] and GC.Values[GC.SMTP_PASSWORD]: if isinstance(GC.Values[GC.SMTP_PASSWORD], bytes): - server.login(GC.Values[GC.SMTP_USERNAME], base64.b64decode(GC.Values[GC.SMTP_PASSWORD]).decode(gam.UTF8)) + server.login(GC.Values[GC.SMTP_USERNAME], base64.b64decode(GC.Values[GC.SMTP_PASSWORD]).decode(_gam().UTF8)) else: server.login(GC.Values[GC.SMTP_USERNAME], GC.Values[GC.SMTP_PASSWORD]) result = server.send_message(message) @@ -183,7 +179,7 @@ def send_email(msgSubject, msgBody, msgTo, i=0, count=0, clientAccess=False, msg checkResult(Ent.RECIPIENT_CC, ccRecipients) checkResult(Ent.RECIPIENT_BCC, bccRecipients) except smtplib.SMTPException as e: - gam.entityActionFailedWarning([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], str(e), i, count) + _gam().entityActionFailedWarning([Ent.RECIPIENT, msgTo, Ent.MESSAGE, msgSubject], str(e), i, count) if server: try: server.quit() diff --git a/src/gam/util/entity.py b/src/gam/util/entity.py index f252c371..12196d0f 100644 --- a/src/gam/util/entity.py +++ b/src/gam/util/entity.py @@ -22,13 +22,12 @@ from gamlib import glglobals as GM from gamlib import glmsgs as Msg -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] def getUserEmailFromID(uid, cd): try: - result = _getMain().callGAPI(cd.users(), 'get', + result = _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, userKey=uid, fields='primaryEmail') @@ -39,7 +38,7 @@ def getUserEmailFromID(uid, cd): def getGroupEmailFromID(uid, cd): try: - result = _getMain().callGAPI(cd.groups(), 'get', + result = _gam().callGAPI(cd.groups(), 'get', throwReasons=GAPI.GROUP_GET_THROW_REASONS, retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, groupKey=uid, fields='email') @@ -50,9 +49,9 @@ def getGroupEmailFromID(uid, cd): def getServiceAccountEmailFromID(account_id, sal=None): if sal is None: - sal = _getMain().buildGAPIObject(API.SERVICEACCOUNTLOOKUP) + sal = _gam().buildGAPIObject(API.SERVICEACCOUNTLOOKUP) try: - certs = _getMain().callGAPI(sal.serviceaccounts(), 'lookup', + certs = _gam().callGAPI(sal.serviceaccounts(), 'lookup', throwReasons = [GAPI.BAD_REQUEST, GAPI.NOT_FOUND, GAPI.RESOURCE_NOT_FOUND, GAPI.INVALID_ARGUMENT], account=account_id) except (GAPI.badRequest, GAPI.notFound, GAPI.resourceNotFound, GAPI.invalidArgument): @@ -80,13 +79,13 @@ def convertUIDtoEmailAddressWithType(emailAddressOrUID, cd=None, sal=None, email emailTypes = [emailTypes] if emailTypes != 'any' else ['user', 'group'] if checkForCustomerId and (emailAddressOrUID == GC.Values[GC.CUSTOMER_ID]): return (emailAddressOrUID, 'email') - normalizedEmailAddressOrUID = _getMain().normalizeEmailAddressOrUID(emailAddressOrUID, ciGroupsAPI=ciGroupsAPI) + normalizedEmailAddressOrUID = _gam().normalizeEmailAddressOrUID(emailAddressOrUID, ciGroupsAPI=ciGroupsAPI) if ciGroupsAPI and emailAddressOrUID.startswith('groups/'): return emailAddressOrUID if normalizedEmailAddressOrUID.find('@') > 0 and aliasAllowed: return (normalizedEmailAddressOrUID, 'email') if cd is None: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) if 'user' in emailTypes and 'group' in emailTypes: # Google User IDs *TEND* to be integers while groups tend to have letters # thus we can optimize which check we try first. We'll still check @@ -115,7 +114,7 @@ def convertUIDtoEmailAddressWithType(emailAddressOrUID, cd=None, sal=None, email return (uid, 'group') if 'resource' in emailTypes: try: - result = _getMain().callGAPI(cd.resources().calendars(), 'get', + result = _gam().callGAPI(cd.resources().calendars(), 'get', throwReasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], calendarResourceId=normalizedEmailAddressOrUID, customer=GC.Values[GC.CUSTOMER_ID], fields='resourceEmail') @@ -139,7 +138,7 @@ def convertUIDtoEmailAddress(emailAddressOrUID, cd=None, emailTypes=None, if ciGroupsAPI: if emailAddressOrUID.startswith(NON_EMAIL_MEMBER_PREFIXES): return emailAddressOrUID - normalizedEmailAddressOrUID = _getMain().normalizeEmailAddressOrUID(emailAddressOrUID, ciGroupsAPI=ciGroupsAPI) + normalizedEmailAddressOrUID = _gam().normalizeEmailAddressOrUID(emailAddressOrUID, ciGroupsAPI=ciGroupsAPI) if normalizedEmailAddressOrUID.startswith(NON_EMAIL_MEMBER_PREFIXES): return normalizedEmailAddressOrUID email, _ = convertUIDtoEmailAddressWithType(emailAddressOrUID, cd, None, emailTypes, @@ -148,39 +147,39 @@ def convertUIDtoEmailAddress(emailAddressOrUID, cd=None, emailTypes=None, # Convert email address to User/Group UID; called immediately after getting email address from command line def convertEmailAddressToUID(emailAddressOrUID, cd=None, emailType='user', savedLocation=None): - normalizedEmailAddressOrUID = _getMain().normalizeEmailAddressOrUID(emailAddressOrUID) + normalizedEmailAddressOrUID = _gam().normalizeEmailAddressOrUID(emailAddressOrUID) if normalizedEmailAddressOrUID.find('@') == -1: return normalizedEmailAddressOrUID if cd is None: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) if emailType != 'group': try: - return _getMain().callGAPI(cd.users(), 'get', + return _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=normalizedEmailAddressOrUID, fields='id')['id'] except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.backendError, GAPI.systemError): if emailType == 'user': if savedLocation is not None: - _getMain().Cmd.SetLocation(savedLocation) - _getMain().entityDoesNotExistExit(_getMain().Ent.USER, normalizedEmailAddressOrUID, errMsg=_getMain().getPhraseDNEorSNA(normalizedEmailAddressOrUID)) + _gam().Cmd.SetLocation(savedLocation) + _gam().entityDoesNotExistExit(_gam().Ent.USER, normalizedEmailAddressOrUID, errMsg=_gam().getPhraseDNEorSNA(normalizedEmailAddressOrUID)) try: - return _getMain().callGAPI(cd.groups(), 'get', + return _gam().callGAPI(cd.groups(), 'get', throwReasons=GAPI.GROUP_GET_THROW_REASONS, retryReasons=GAPI.GROUP_GET_RETRY_REASONS, groupKey=normalizedEmailAddressOrUID, fields='id')['id'] except (GAPI.groupNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.systemError): if savedLocation is not None: - _getMain().Cmd.SetLocation(savedLocation) - _getMain().entityDoesNotExistExit([_getMain().Ent.USER, _getMain().Ent.GROUP][emailType == 'group'], normalizedEmailAddressOrUID, errMsg=_getMain().getPhraseDNEorSNA(normalizedEmailAddressOrUID)) + _gam().Cmd.SetLocation(savedLocation) + _gam().entityDoesNotExistExit([_gam().Ent.USER, _gam().Ent.GROUP][emailType == 'group'], normalizedEmailAddressOrUID, errMsg=_gam().getPhraseDNEorSNA(normalizedEmailAddressOrUID)) # Convert User UID from API call to email address def convertUserIDtoEmail(uid, cd=None): primaryEmail = GM.Globals[GM.MAP_USER_ID_TO_NAME].get(uid) if not primaryEmail: if cd is None: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) try: - primaryEmail = _getMain().callGAPI(cd.users(), 'get', + primaryEmail = _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=uid, fields='primaryEmail')['primaryEmail'] except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, @@ -192,13 +191,13 @@ def convertUserIDtoEmail(uid, cd=None): # Convert UID to split email address # Return (foo@bar.com, foo, bar.com) def splitEmailAddressOrUID(emailAddressOrUID): - normalizedEmailAddressOrUID = _getMain().normalizeEmailAddressOrUID(emailAddressOrUID) + normalizedEmailAddressOrUID = _gam().normalizeEmailAddressOrUID(emailAddressOrUID) atLoc = normalizedEmailAddressOrUID.find('@') if atLoc > 0: return (normalizedEmailAddressOrUID, normalizedEmailAddressOrUID[:atLoc], normalizedEmailAddressOrUID[atLoc+1:]) try: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - result = _getMain().callGAPI(cd.users(), 'get', + cd = _gam().buildGAPIObject(API.DIRECTORY) + result = _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=normalizedEmailAddressOrUID, fields='primaryEmail') if 'primaryEmail' in result: @@ -217,9 +216,9 @@ def convertOrgUnitIDtoPath(cd, orgUnitId): orgUnitPath = GM.Globals[GM.MAP_ORGUNIT_ID_TO_NAME].get(orgUnitId) if not orgUnitPath: if cd is None: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) try: - orgUnitPath = _getMain().callGAPI(cd.orgunits(), 'get', + orgUnitPath = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=orgUnitId, fields='orgUnitPath')['orgUnitPath'] except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError, GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): @@ -231,8 +230,8 @@ from util.args import shlexSplitList, shlexSplitListStatus # noqa: E402,F401 - def getQueries(myarg): if myarg in {'query', 'filter'}: - return [_getMain().getString(_getMain().Cmd.OB_QUERY)] - return shlexSplitList(_getMain().getString(_getMain().Cmd.OB_QUERY_LIST)) + return [_gam().getString(_gam().Cmd.OB_QUERY)] + return shlexSplitList(_gam().getString(_gam().Cmd.OB_QUERY_LIST)) def _validateDeviceQuery(entityType, query): if ':' in query: @@ -242,14 +241,14 @@ def _validateDeviceQuery(entityType, query): qfield = '' qvalue = query if (not qfield) or (not qvalue) or ('?' in query): - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.INVALID_DEVICE_QUERY.format(_getMain().Ent.Singular(entityType), query)) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.INVALID_DEVICE_QUERY.format(_gam().Ent.Singular(entityType), query)) def getDeviceQueries(myarg, entityType): if myarg in {'query', 'filter'}: - queries = [_getMain().getString(_getMain().Cmd.OB_QUERY)] + queries = [_gam().getString(_gam().Cmd.OB_QUERY)] else: - queries = shlexSplitList(_getMain().getString(_getMain().Cmd.OB_QUERY_LIST)) + queries = shlexSplitList(_gam().getString(_gam().Cmd.OB_QUERY_LIST)) for query in queries: _validateDeviceQuery(entityType, query) return queries @@ -276,7 +275,7 @@ GROUP_ROLES_MAP = { ALL_GROUP_ROLES = {'MANAGER', 'MEMBER', 'OWNER'} def _getRoleVerification(memberRoles, fields): - if memberRoles and memberRoles.find(_getMain().Ent.ROLE_MEMBER) != -1: + if memberRoles and memberRoles.find(_gam().Ent.ROLE_MEMBER) != -1: return (set(memberRoles.split(',')), None, fields if fields.find('role') != -1 else fields[:-1]+',role)') return (set(), memberRoles, fields) @@ -308,7 +307,7 @@ def _checkMemberIsSuspendedIsArchived(member, isSuspended, isArchived): return _checkMemberStatusIsSuspendedIsArchived(member.get('status', 'UNKNOWN'), isSuspended, isArchived) def _checkMemberRole(member, validRoles): - return not validRoles or member.get('role', _getMain().Ent.ROLE_MEMBER) in validRoles + return not validRoles or member.get('role', _gam().Ent.ROLE_MEMBER) in validRoles def _checkMemberRoleIsSuspendedIsArchived(member, validRoles, isSuspended, isArchived): return _checkMemberRole(member, validRoles) and _checkMemberIsSuspendedIsArchived(member, isSuspended, isArchived) @@ -344,16 +343,16 @@ def getCIGroupMemberRoleFixType(member): if 'type' not in member: if 'id' not in member['preferredMemberKey']: member['preferredMemberKey']['id'] = GC.Values[GC.CUSTOMER_ID] - member['type'] = _getMain().Ent.TYPE_CUSTOMER + member['type'] = _gam().Ent.TYPE_CUSTOMER elif member['preferredMemberKey']['id'] == GC.Values[GC.CUSTOMER_ID]: - member['type'] = _getMain().Ent.TYPE_CUSTOMER + member['type'] = _gam().Ent.TYPE_CUSTOMER else: - member['type'] = _getMain().Ent.TYPE_OTHER + member['type'] = _gam().Ent.TYPE_OTHER roles = {} - memberRoles = member.get('roles', [{'name': _getMain().Ent.ROLE_MEMBER}]) + memberRoles = member.get('roles', [{'name': _gam().Ent.ROLE_MEMBER}]) for role in memberRoles: roles[role['name']] = role - for a_role in [_getMain().Ent.ROLE_OWNER, _getMain().Ent.ROLE_MANAGER, _getMain().Ent.ROLE_MEMBER]: + for a_role in [_gam().Ent.ROLE_OWNER, _gam().Ent.ROLE_MANAGER, _gam().Ent.ROLE_MEMBER]: if a_role in roles: member['role'] = a_role if 'expiryDetail' in roles[a_role]: @@ -372,15 +371,15 @@ def getCIGroupTransitiveMemberRoleFixType(groupName, tmember): member = {'name': f'{groupName}/membershipd/{tname}', 'preferredMemberKey': {'id': tid}} if 'type' not in tmember: if tid == GC.Values[GC.CUSTOMER_ID]: - member['type'] = _getMain().Ent.TYPE_CUSTOMER + member['type'] = _gam().Ent.TYPE_CUSTOMER elif ttype == 'users': - member['type'] = _getMain().Ent.TYPE_USER if not tid.endswith('.iam.gserviceaccount.com') else _getMain().Ent.TYPE_SERVICE_ACCOUNT + member['type'] = _gam().Ent.TYPE_USER if not tid.endswith('.iam.gserviceaccount.com') else _gam().Ent.TYPE_SERVICE_ACCOUNT elif ttype == 'groups': - member['type'] = _getMain().Ent.TYPE_GROUP + member['type'] = _gam().Ent.TYPE_GROUP elif tid.startswith('cbcm-browser.'): - member['type'] = _getMain().Ent.TYPE_CBCM_BROWSER + member['type'] = _gam().Ent.TYPE_CBCM_BROWSER else: - member['type'] = _getMain().Ent.TYPE_OTHER + member['type'] = _gam().Ent.TYPE_OTHER else: member['type'] = tmember['type'] if 'roles' in tmember: @@ -389,14 +388,14 @@ def getCIGroupTransitiveMemberRoleFixType(groupName, tmember): if 'role' in trole: trole['name'] = trole.pop('role') if trole['name'] == 'ADMIN': - trole['name'] = _getMain().Ent.ROLE_MANAGER + trole['name'] = _gam().Ent.ROLE_MANAGER memberRoles.append(trole) else: - memberRoles = [{'name': _getMain().Ent.ROLE_MEMBER}] + memberRoles = [{'name': _gam().Ent.ROLE_MEMBER}] roles = {} for role in memberRoles: roles[role['name']] = role - for a_role in [_getMain().Ent.ROLE_OWNER, _getMain().Ent.ROLE_MANAGER, _getMain().Ent.ROLE_MEMBER]: + for a_role in [_gam().Ent.ROLE_OWNER, _gam().Ent.ROLE_MANAGER, _gam().Ent.ROLE_MEMBER]: if a_role in roles: member['role'] = a_role if 'expiryDetail' in roles[a_role]: @@ -408,46 +407,46 @@ def getCIGroupTransitiveMemberRoleFixType(groupName, tmember): def convertGroupCloudIDToEmail(ci, group, i=0, count=0): if not group.startswith('groups/'): - group = _getMain().normalizeEmailAddressOrUID(group, ciGroupsAPI=True) + group = _gam().normalizeEmailAddressOrUID(group, ciGroupsAPI=True) if not group.startswith('groups/'): return (ci, None, group) if not ci: - ci = _getMain().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) + ci = _gam().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) try: - ciGroup = _getMain().callGAPI(ci.groups(), 'get', + ciGroup = _gam().callGAPI(ci.groups(), 'get', throwReasons=GAPI.CIGROUP_GET_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, name=group, fields='groupKey(id)') return (ci, None, ciGroup['groupKey']['id']) except (GAPI.notFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.permissionDenied, GAPI.serviceNotAvailable) as e: - action = _getMain().Act.Get() - _getMain().Act.Set(_getMain().Act.LOOKUP) - _getMain().entityActionFailedWarning([_getMain().Ent.CLOUD_IDENTITY_GROUP, group, _getMain().Ent.GROUP, None], str(e), i, count) - _getMain().Act.Set(action) + action = _gam().Act.Get() + _gam().Act.Set(_gam().Act.LOOKUP) + _gam().entityActionFailedWarning([_gam().Ent.CLOUD_IDENTITY_GROUP, group, _gam().Ent.GROUP, None], str(e), i, count) + _gam().Act.Set(action) return (ci, None, None) def convertGroupEmailToCloudID(ci, group, i=0, count=0): - group = _getMain().normalizeEmailAddressOrUID(group, ciGroupsAPI=True) + group = _gam().normalizeEmailAddressOrUID(group, ciGroupsAPI=True) if not group.startswith('groups/') and group.find('@') == -1: group = 'groups/'+group if group.startswith('groups/'): ci, _, groupEmail = convertGroupCloudIDToEmail(ci, group, i, count) return (ci, group, groupEmail) if not ci: - ci = _getMain().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) + ci = _gam().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) try: - ciGroup = _getMain().callGAPI(ci.groups(), 'lookup', + ciGroup = _gam().callGAPI(ci.groups(), 'lookup', throwReasons=GAPI.CIGROUP_GET_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, groupKey_id=group, fields='name') return (ci, ciGroup['name'], group) except (GAPI.notFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.failedPrecondition, GAPI.permissionDenied, GAPI.serviceNotAvailable) as e: - action = _getMain().Act.Get() - _getMain().Act.Set(_getMain().Act.LOOKUP) - _getMain().entityActionFailedWarning([_getMain().Ent.CLOUD_IDENTITY_GROUP, group], str(e), i, count) - _getMain().Act.Set(action) + action = _gam().Act.Get() + _gam().Act.Set(_gam().Act.LOOKUP) + _gam().entityActionFailedWarning([_gam().Ent.CLOUD_IDENTITY_GROUP, group], str(e), i, count) + _gam().Act.Set(action) return (ci, None, None) CIGROUP_DISCUSSION_FORUM_LABEL = 'cloudidentity.googleapis.com/groups.discussion_forum' @@ -457,10 +456,10 @@ CIGROUP_LOCKED_LABEL = 'cloudidentity.googleapis.com/groups.locked' def getCIGroupMembershipGraph(ci, member): if not ci: - ci = _getMain().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) + ci = _gam().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) parent = 'groups/-' try: - result = _getMain().callGAPI(ci.groups().memberships(), 'getMembershipGraph', + result = _gam().callGAPI(ci.groups().memberships(), 'getMembershipGraph', throwReasons=GAPI.CIGROUP_LIST_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, parent=parent, query=f"member_key_id == '{member}' && '{CIGROUP_DISCUSSION_FORUM_LABEL}' in labels") @@ -468,23 +467,23 @@ def getCIGroupMembershipGraph(ci, member): except (GAPI.resourceNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.permissionDenied, GAPI.serviceNotAvailable) as e: - action = _getMain().Act.Get() - _getMain().Act.Set(_getMain().Act.LOOKUP) - _getMain().entityActionFailedWarning([_getMain().Ent.CLOUD_IDENTITY_GROUP, parent], str(e)) - _getMain().Act.Set(action) + action = _gam().Act.Get() + _gam().Act.Set(_gam().Act.LOOKUP) + _gam().entityActionFailedWarning([_gam().Ent.CLOUD_IDENTITY_GROUP, parent], str(e)) + _gam().Act.Set(action) return (ci, None) def checkGroupExists(cd, ci, ciGroupsAPI, group, i=0, count=0): - group = _getMain().normalizeEmailAddressOrUID(group, ciGroupsAPI=ciGroupsAPI) + group = _gam().normalizeEmailAddressOrUID(group, ciGroupsAPI=ciGroupsAPI) if not ciGroupsAPI: if not group.startswith('groups/'): try: - result = _getMain().callGAPI(cd.groups(), 'get', + result = _gam().callGAPI(cd.groups(), 'get', throwReasons=GAPI.GROUP_GET_THROW_REASONS, retryReasons=GAPI.GROUP_GET_RETRY_REASONS, groupKey=group, fields='email') return (ci, None, result['email']) except (GAPI.groupNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.systemError): - _getMain().entityUnknownWarning(_getMain().Ent.GROUP, group, i, count) + _gam().entityUnknownWarning(_gam().Ent.GROUP, group, i, count) return (ci, None, None) else: ci, _, groupEmail = convertGroupCloudIDToEmail(ci, group, i, count) @@ -494,14 +493,14 @@ def checkGroupExists(cd, ci, ciGroupsAPI, group, i=0, count=0): group = 'groups/'+group if group.startswith('groups/'): try: - result = _getMain().callGAPI(ci.groups(), 'get', + result = _gam().callGAPI(ci.groups(), 'get', throwReasons=GAPI.CIGROUP_GET_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, name=group, fields='name,groupKey(id)') return (ci, result['name'], result['groupKey']['id']) except (GAPI.notFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.permissionDenied, GAPI.serviceNotAvailable): - _getMain().entityUnknownWarning(_getMain().Ent.CLOUD_IDENTITY_GROUP, group, i, count) + _gam().entityUnknownWarning(_gam().Ent.CLOUD_IDENTITY_GROUP, group, i, count) return (ci, None, None) else: return convertGroupEmailToCloudID(ci, group, i, count) @@ -516,59 +515,59 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA def _showInvalidEntity(entityType, entityName): entityError['entityType'] = entityType entityError[ENTITY_ERROR_INVALID] += 1 - _getMain().printErrorMessage(_getMain().INVALID_ENTITY_RC, _getMain().formatKeyValueList('', [_getMain().Ent.Singular(entityType), entityName, Msg.INVALID], '')) + _gam().printErrorMessage(_gam().INVALID_ENTITY_RC, _gam().formatKeyValueList('', [_gam().Ent.Singular(entityType), entityName, Msg.INVALID], '')) def _addGroupUsersToUsers(group, domains, recursive, includeDerivedMembership): - _getMain().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _getMain().Ent.ROLE_MANAGER_MEMBER_OWNER, group, entityType=_getMain().Ent.GROUP) + _gam().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _gam().Ent.ROLE_MANAGER_MEMBER_OWNER, group, entityType=_gam().Ent.GROUP) validRoles, listRoles, listFields = _getRoleVerification(memberRoles, 'nextPageToken,members(email,type,status)') try: - result = _getMain().callGAPIpages(cd.members(), 'list', 'members', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(cd.members(), 'list', 'members', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=GAPI.MEMBERS_THROW_REASONS, retryReasons=GAPI.MEMBERS_RETRY_REASONS, includeDerivedMembership=includeDerivedMembership, groupKey=group, roles=listRoles, fields=listFields, maxResults=GC.Values[GC.MEMBER_MAX_RESULTS]) except (GAPI.groupNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.invalid, GAPI.forbidden, GAPI.serviceNotAvailable): - _getMain().entityUnknownWarning(_getMain().Ent.GROUP, group) - _incrEntityDoesNotExist(_getMain().Ent.GROUP) + _gam().entityUnknownWarning(_gam().Ent.GROUP, group) + _incrEntityDoesNotExist(_gam().Ent.GROUP) return for member in result: - if member['type'] == _getMain().Ent.TYPE_USER: + if member['type'] == _gam().Ent.TYPE_USER: email = member['email'].lower() if email in entitySet: continue if _checkMemberRoleIsSuspendedIsArchived(member, validRoles, isSuspended, isArchived): if domains: - _, domain = _getMain().splitEmailAddress(email) + _, domain = _gam().splitEmailAddress(email) if domain not in domains: continue entitySet.add(email) entityList.append(email) - elif recursive and member['type'] == _getMain().Ent.TYPE_GROUP: + elif recursive and member['type'] == _gam().Ent.TYPE_GROUP: _addGroupUsersToUsers(member['email'], domains, recursive, includeDerivedMembership) def _addCIGroupUsersToUsers(groupName, groupEmail, recursive): - _getMain().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _getMain().Ent.ROLE_MANAGER_MEMBER_OWNER, groupEmail, entityType=_getMain().Ent.CLOUD_IDENTITY_GROUP) + _gam().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _gam().Ent.ROLE_MANAGER_MEMBER_OWNER, groupEmail, entityType=_gam().Ent.CLOUD_IDENTITY_GROUP) validRoles = _getCIRoleVerification(memberRoles) try: - result = _getMain().callGAPIpages(ci.groups().memberships(), 'list', 'memberships', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(ci.groups().memberships(), 'list', 'memberships', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=GAPI.CIGROUP_LIST_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, parent=groupName, view='FULL', fields='nextPageToken,memberships(name,preferredMemberKey(id),roles(name),type)', pageSize=GC.Values[GC.MEMBER_MAX_RESULTS]) except (GAPI.resourceNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.permissionDenied, GAPI.serviceNotAvailable): - _getMain().entityUnknownWarning(_getMain().Ent.CLOUD_IDENTITY_GROUP, groupEmail) - _incrEntityDoesNotExist(_getMain().Ent.CLOUD_IDENTITY_GROUP) + _gam().entityUnknownWarning(_gam().Ent.CLOUD_IDENTITY_GROUP, groupEmail) + _incrEntityDoesNotExist(_gam().Ent.CLOUD_IDENTITY_GROUP) return for member in result: getCIGroupMemberRoleFixType(member) - if member['type'] == _getMain().Ent.TYPE_USER: + if member['type'] == _gam().Ent.TYPE_USER: email = member.get('preferredMemberKey', {}).get('id', '') if (email and _checkMemberRole(member, validRoles) and email not in entitySet): entitySet.add(email) entityList.append(email) - elif recursive and member['type'] == _getMain().Ent.TYPE_GROUP and _checkMemberRole(member, validRoles): + elif recursive and member['type'] == _gam().Ent.TYPE_GROUP and _checkMemberRole(member, validRoles): _, gname = member['name'].rsplit('/', 1) _addCIGroupUsersToUsers(f'groups/{gname}', f'groups/{gname}', recursive) @@ -578,23 +577,23 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA entityError = {'entityType': None, ENTITY_ERROR_DNE: 0, ENTITY_ERROR_INVALID: 0} entityList = [] entitySet = set() - entityLocation = _getMain().Cmd.Location() - if entityType in {_getMain().Cmd.ENTITY_USER, _getMain().Cmd.ENTITY_USERS}: + entityLocation = _gam().Cmd.Location() + if entityType in {_gam().Cmd.ENTITY_USER, _gam().Cmd.ENTITY_USERS}: if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] and not GC.Values[GC.DOMAIN]: - _getMain().buildGAPIObject(API.DIRECTORY) - result = convertEntityToList(entity, nonListEntityType=entityType == _getMain().Cmd.ENTITY_USER) + _gam().buildGAPIObject(API.DIRECTORY) + result = convertEntityToList(entity, nonListEntityType=entityType == _gam().Cmd.ENTITY_USER) for user in result: - if _getMain().validateEmailAddressOrUID(user): + if _gam().validateEmailAddressOrUID(user): if user not in entitySet: entitySet.add(user) entityList.append(user) else: - _showInvalidEntity(_getMain().Ent.USER, user) + _showInvalidEntity(_gam().Ent.USER, user) if GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY]: return entityList - elif entityType in _getMain().Cmd.ALL_USER_ENTITY_TYPES: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - if entityType == _getMain().Cmd.ENTITY_ALL_USERS and ((isSuspended is not None) or (isArchived is not None)): + elif entityType in _gam().Cmd.ALL_USER_ENTITY_TYPES: + cd = _gam().buildGAPIObject(API.DIRECTORY) + if entityType == _gam().Cmd.ENTITY_ALL_USERS and ((isSuspended is not None) or (isArchived is not None)): if isSuspended is not None: query = f'isSuspended={isSuspended}' if isArchived is not None: @@ -602,37 +601,37 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA else: query = f'isArchived={isArchived}' else: - query = _getMain().Cmd.ALL_USERS_QUERY_MAP[entityType] - _getMain().printGettingAllAccountEntities(_getMain().Ent.USER, query=query) + query = _gam().Cmd.ALL_USERS_QUERY_MAP[entityType] + _gam().printGettingAllAccountEntities(_gam().Ent.USER, query=query) try: - result = _getMain().callGAPIpages(cd.users(), 'list', 'users', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cd.users(), 'list', 'users', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customer=GC.Values[GC.CUSTOMER_ID], query=query, orderBy='email', fields='nextPageToken,users(primaryEmail)', maxResults=GC.Values[GC.USER_MAX_RESULTS]) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) entityList = [user['primaryEmail'] for user in result] - elif entityType == _getMain().Cmd.ENTITY_ALL_USERS_ARCH_OR_SUSP: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + elif entityType == _gam().Cmd.ENTITY_ALL_USERS_ARCH_OR_SUSP: + cd = _gam().buildGAPIObject(API.DIRECTORY) for query in ['isSuspended=True', 'isArchived=True']: - _getMain().printGettingAllAccountEntities(_getMain().Ent.USER, query) + _gam().printGettingAllAccountEntities(_gam().Ent.USER, query) try: - result = _getMain().callGAPIpages(cd.users(), 'list', 'users', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cd.users(), 'list', 'users', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customer=GC.Values[GC.CUSTOMER_ID], query=query, orderBy='email', fields='nextPageToken,users(primaryEmail)', maxResults=GC.Values[GC.USER_MAX_RESULTS]) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) entitySet |= {user['primaryEmail'] for user in result} entityList = sorted(list(entitySet)) - elif entityType in _getMain().Cmd.DOMAIN_ENTITY_TYPES: - if entityType == _getMain().Cmd.ENTITY_DOMAINS and ((isSuspended is not None) or (isArchived is not None)): + elif entityType in _gam().Cmd.DOMAIN_ENTITY_TYPES: + if entityType == _gam().Cmd.ENTITY_DOMAINS and ((isSuspended is not None) or (isArchived is not None)): if isSuspended is not None: query = f'isSuspended={isSuspended}' if isArchived is not None: @@ -640,103 +639,103 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA else: query = f'isArchived={isArchived}' else: - query = _getMain().Cmd.DOMAINS_QUERY_MAP[entityType] - cd = _getMain().buildGAPIObject(API.DIRECTORY) + query = _gam().Cmd.DOMAINS_QUERY_MAP[entityType] + cd = _gam().buildGAPIObject(API.DIRECTORY) domains = convertEntityToList(entity) for domain in domains: - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.USER, domain, query=query, entityType=_getMain().Ent.DOMAIN) + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.USER, domain, query=query, entityType=_gam().Ent.DOMAIN) try: - result = _getMain().callGAPIpages(cd.users(), 'list', 'users', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(cd.users(), 'list', 'users', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=[GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.DOMAIN_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, domain=domain, query=query, orderBy='email', fields='nextPageToken,users(primaryEmail)', maxResults=GC.Values[GC.USER_MAX_RESULTS]) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.domainNotFound, GAPI.forbidden): - _getMain().checkEntityDNEorAccessErrorExit(cd, _getMain().Ent.DOMAIN, domain) - _incrEntityDoesNotExist(_getMain().Ent.DOMAIN) + _gam().checkEntityDNEorAccessErrorExit(cd, _gam().Ent.DOMAIN, domain) + _incrEntityDoesNotExist(_gam().Ent.DOMAIN) continue entityList.extend([user['primaryEmail'] for user in result]) - elif entityType in _getMain().Cmd.GROUP_ENTITY_TYPES or entityType in _getMain().Cmd.GROUPS_ENTITY_TYPES: - isArchived, isSuspended = _getMain().Cmd.GROUPS_QUERY_MAP.get(entityType, (isArchived, isSuspended)) - includeDerivedMembership = entityType in {_getMain().Cmd.ENTITY_GROUP_INDE, _getMain().Cmd.ENTITY_GROUPS_INDE} - cd = _getMain().buildGAPIObject(API.DIRECTORY) - groups = convertEntityToList(entity, nonListEntityType=entityType in _getMain().Cmd.GROUP_ENTITY_TYPES) + elif entityType in _gam().Cmd.GROUP_ENTITY_TYPES or entityType in _gam().Cmd.GROUPS_ENTITY_TYPES: + isArchived, isSuspended = _gam().Cmd.GROUPS_QUERY_MAP.get(entityType, (isArchived, isSuspended)) + includeDerivedMembership = entityType in {_gam().Cmd.ENTITY_GROUP_INDE, _gam().Cmd.ENTITY_GROUPS_INDE} + cd = _gam().buildGAPIObject(API.DIRECTORY) + groups = convertEntityToList(entity, nonListEntityType=entityType in _gam().Cmd.GROUP_ENTITY_TYPES) for group in groups: - if _getMain().validateEmailAddressOrUID(group, checkPeople=False): - group = _getMain().normalizeEmailAddressOrUID(group) - _getMain().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _getMain().Ent.ROLE_MANAGER_MEMBER_OWNER, group, entityType=_getMain().Ent.GROUP) + if _gam().validateEmailAddressOrUID(group, checkPeople=False): + group = _gam().normalizeEmailAddressOrUID(group) + _gam().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _gam().Ent.ROLE_MANAGER_MEMBER_OWNER, group, entityType=_gam().Ent.GROUP) validRoles, listRoles, listFields = _getRoleVerification(memberRoles, 'nextPageToken,members(email,id,type,status)') try: - result = _getMain().callGAPIpages(cd.members(), 'list', 'members', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(cd.members(), 'list', 'members', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=GAPI.MEMBERS_THROW_REASONS, retryReasons=GAPI.MEMBERS_RETRY_REASONS, includeDerivedMembership=includeDerivedMembership, groupKey=group, roles=listRoles, fields=listFields, maxResults=GC.Values[GC.MEMBER_MAX_RESULTS]) except (GAPI.groupNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.invalid, GAPI.forbidden, GAPI.serviceNotAvailable): - _getMain().entityUnknownWarning(_getMain().Ent.GROUP, group) - _incrEntityDoesNotExist(_getMain().Ent.GROUP) + _gam().entityUnknownWarning(_gam().Ent.GROUP, group) + _incrEntityDoesNotExist(_gam().Ent.GROUP) continue for member in result: - email = member['email'].lower() if member['type'] != _getMain().Ent.TYPE_CUSTOMER else member['id'] + email = member['email'].lower() if member['type'] != _gam().Ent.TYPE_CUSTOMER else member['id'] if ((groupMemberType in ('ALL', member['type'])) and - (not includeDerivedMembership or (member['type'] == _getMain().Ent.TYPE_USER)) and + (not includeDerivedMembership or (member['type'] == _gam().Ent.TYPE_USER)) and _checkMemberRoleIsSuspendedIsArchived(member, validRoles, isSuspended, isArchived) and email not in entitySet): entitySet.add(email) entityList.append(email) else: - _showInvalidEntity(_getMain().Ent.GROUP, group) - elif entityType in _getMain().Cmd.GROUP_USERS_ENTITY_TYPES: - isArchived, isSuspended = _getMain().Cmd.GROUP_USERS_QUERY_MAP.get(entityType, (isArchived, isSuspended)) - cd = _getMain().buildGAPIObject(API.DIRECTORY) + _showInvalidEntity(_gam().Ent.GROUP, group) + elif entityType in _gam().Cmd.GROUP_USERS_ENTITY_TYPES: + isArchived, isSuspended = _gam().Cmd.GROUP_USERS_QUERY_MAP.get(entityType, (isArchived, isSuspended)) + cd = _gam().buildGAPIObject(API.DIRECTORY) groups = convertEntityToList(entity) includeDerivedMembership = False domains = [] rolesSet = set() if not noCLArgs: - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if myarg in GROUP_ROLES_MAP: rolesSet.add(GROUP_ROLES_MAP[myarg]) elif myarg == 'primarydomain': domains.append(GC.Values[GC.DOMAIN]) elif myarg == 'domains': - domains.extend(getEntityList(_getMain().Cmd.OB_DOMAIN_NAME_ENTITY)) + domains.extend(getEntityList(_gam().Cmd.OB_DOMAIN_NAME_ENTITY)) elif myarg == 'recursive': recursive = True includeDerivedMembership = False elif myarg == 'includederivedmembership': includeDerivedMembership = True recursive = False - elif entityType == _getMain().Cmd.ENTITY_GROUP_USERS_SELECT and myarg in _getMain().SUSPENDED_ARGUMENTS: - isSuspended = _getMain()._getIsSuspended(myarg) - elif entityType == _getMain().Cmd.ENTITY_GROUP_USERS_SELECT and myarg in _getMain().ARCHIVED_ARGUMENTS: - isArchived = _getMain()._getIsArchived(myarg) + elif entityType == _gam().Cmd.ENTITY_GROUP_USERS_SELECT and myarg in _gam().SUSPENDED_ARGUMENTS: + isSuspended = _gam()._getIsSuspended(myarg) + elif entityType == _gam().Cmd.ENTITY_GROUP_USERS_SELECT and myarg in _gam().ARCHIVED_ARGUMENTS: + isArchived = _gam()._getIsArchived(myarg) elif myarg == 'end': break else: - _getMain().Cmd.Backup() - _getMain().missingArgumentExit('end') + _gam().Cmd.Backup() + _gam().missingArgumentExit('end') if rolesSet: memberRoles = ','.join(sorted(rolesSet)) for group in groups: - if _getMain().validateEmailAddressOrUID(group, checkPeople=False): - _addGroupUsersToUsers(_getMain().normalizeEmailAddressOrUID(group), domains, recursive, includeDerivedMembership) + if _gam().validateEmailAddressOrUID(group, checkPeople=False): + _addGroupUsersToUsers(_gam().normalizeEmailAddressOrUID(group), domains, recursive, includeDerivedMembership) else: - _showInvalidEntity(_getMain().Ent.GROUP, group) - elif entityType in {_getMain().Cmd.ENTITY_CIGROUP, _getMain().Cmd.ENTITY_CIGROUPS}: - ci = _getMain().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) - groups = convertEntityToList(entity, nonListEntityType=entityType in {_getMain().Cmd.ENTITY_CIGROUP}) + _showInvalidEntity(_gam().Ent.GROUP, group) + elif entityType in {_gam().Cmd.ENTITY_CIGROUP, _gam().Cmd.ENTITY_CIGROUPS}: + ci = _gam().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) + groups = convertEntityToList(entity, nonListEntityType=entityType in {_gam().Cmd.ENTITY_CIGROUP}) for group in groups: - if _getMain().validateEmailAddressOrUID(group, checkPeople=False, ciGroupsAPI=True): + if _gam().validateEmailAddressOrUID(group, checkPeople=False, ciGroupsAPI=True): _, name, groupEmail = convertGroupEmailToCloudID(ci, group) - _getMain().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _getMain().Ent.ROLE_MANAGER_MEMBER_OWNER, groupEmail, entityType=_getMain().Ent.CLOUD_IDENTITY_GROUP) + _gam().printGettingAllEntityItemsForWhom(memberRoles if memberRoles else _gam().Ent.ROLE_MANAGER_MEMBER_OWNER, groupEmail, entityType=_gam().Ent.CLOUD_IDENTITY_GROUP) validRoles = _getCIRoleVerification(memberRoles) try: - result = _getMain().callGAPIpages(ci.groups().memberships(), 'list', 'memberships', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(ci.groups().memberships(), 'list', 'memberships', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=GAPI.CIGROUP_LIST_THROW_REASONS, retryReasons=GAPI.CIGROUP_RETRY_REASONS, parent=name, view='FULL', fields='nextPageToken,memberships(preferredMemberKey(id),roles(name),type)', @@ -744,8 +743,8 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA except (GAPI.resourceNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.systemError, GAPI.permissionDenied, GAPI.serviceNotAvailable): - _getMain().entityUnknownWarning(_getMain().Ent.CLOUD_IDENTITY_GROUP, groupEmail) - _incrEntityDoesNotExist(_getMain().Ent.CLOUD_IDENTITY_GROUP) + _gam().entityUnknownWarning(_gam().Ent.CLOUD_IDENTITY_GROUP, groupEmail) + _incrEntityDoesNotExist(_gam().Ent.CLOUD_IDENTITY_GROUP) continue for member in result: getCIGroupMemberRoleFixType(member) @@ -755,14 +754,14 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA entitySet.add(email) entityList.append(email) else: - _showInvalidEntity(_getMain().Ent.CLOUD_IDENTITY_GROUP, groupEmail) - elif entityType in {_getMain().Cmd.ENTITY_CIGROUP_USERS}: - ci = _getMain().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) + _showInvalidEntity(_gam().Ent.CLOUD_IDENTITY_GROUP, groupEmail) + elif entityType in {_gam().Cmd.ENTITY_CIGROUP_USERS}: + ci = _gam().buildGAPIObject(API.CLOUDIDENTITY_GROUPS) groups = convertEntityToList(entity) rolesSet = set() if not noCLArgs: - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if myarg in GROUP_ROLES_MAP: rolesSet.add(GROUP_ROLES_MAP[myarg]) elif myarg == 'recursive': @@ -770,8 +769,8 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA elif myarg == 'end': break else: - _getMain().Cmd.Backup() - _getMain().missingArgumentExit('end') + _gam().Cmd.Backup() + _gam().missingArgumentExit('end') if rolesSet: memberRoles = ','.join(sorted(rolesSet)) for group in groups: @@ -779,41 +778,41 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA if name and groupEmail: _addCIGroupUsersToUsers(name, groupEmail, recursive) else: - _showInvalidEntity(_getMain().Ent.GROUP, group) - elif entityType in _getMain().Cmd.OU_ENTITY_TYPES or entityType in _getMain().Cmd.OUS_ENTITY_TYPES: - isArchived, isSuspended = _getMain().Cmd.OU_QUERY_MAP.get(entityType, (isArchived, isSuspended)) - cd = _getMain().buildGAPIObject(API.DIRECTORY) - ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType in _getMain().Cmd.OU_ENTITY_TYPES) - directlyInOU = entityType in _getMain().Cmd.OU_DIRECT_ENTITY_TYPES - qualifier = Msg.DIRECTLY_IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)) if directlyInOU else Msg.IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)) + _showInvalidEntity(_gam().Ent.GROUP, group) + elif entityType in _gam().Cmd.OU_ENTITY_TYPES or entityType in _gam().Cmd.OUS_ENTITY_TYPES: + isArchived, isSuspended = _gam().Cmd.OU_QUERY_MAP.get(entityType, (isArchived, isSuspended)) + cd = _gam().buildGAPIObject(API.DIRECTORY) + ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType in _gam().Cmd.OU_ENTITY_TYPES) + directlyInOU = entityType in _gam().Cmd.OU_DIRECT_ENTITY_TYPES + qualifier = Msg.DIRECTLY_IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)) if directlyInOU else Msg.IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)) fields = 'nextPageToken,users(primaryEmail,orgUnitPath)' if directlyInOU else 'nextPageToken,users(primaryEmail)' for ou in ous: if ou == 'root': ou = '/' - ou = _getMain().makeOrgUnitPathAbsolute(ou) + ou = _gam().makeOrgUnitPathAbsolute(ou) if ou.startswith('id:'): try: - ou = _getMain().callGAPI(cd.orgunits(), 'get', + ou = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=ou, fields='orgUnitPath')['orgUnitPath'] except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError, GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().checkEntityDNEorAccessErrorExit(cd, _getMain().Ent.ORGANIZATIONAL_UNIT, ou) - _incrEntityDoesNotExist(_getMain().Ent.ORGANIZATIONAL_UNIT) + _gam().checkEntityDNEorAccessErrorExit(cd, _gam().Ent.ORGANIZATIONAL_UNIT, ou) + _incrEntityDoesNotExist(_gam().Ent.ORGANIZATIONAL_UNIT) continue ouLower = ou.lower() - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.USER, ou, qualifier=Msg.IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)), - entityType=_getMain().Ent.ORGANIZATIONAL_UNIT) - pageMessage = _getMain().getPageMessageForWhom() + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.USER, ou, qualifier=Msg.IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)), + entityType=_gam().Ent.ORGANIZATIONAL_UNIT) + pageMessage = _gam().getPageMessageForWhom() usersInOU = 0 try: - feed = _getMain().yieldGAPIpages(cd.users(), 'list', 'users', + feed = _gam().yieldGAPIpages(cd.users(), 'list', 'users', pageMessage=pageMessage, messageAttribute='primaryEmail', throwReasons=[GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, - customer=GC.Values[GC.CUSTOMER_ID], query=_getMain().orgUnitPathQuery(ou, isSuspended, isArchived), orderBy='email', + customer=GC.Values[GC.CUSTOMER_ID], query=_gam().orgUnitPathQuery(ou, isSuspended, isArchived), orderBy='email', fields=fields, maxResults=GC.Values[GC.USER_MAX_RESULTS]) for users in feed: if directlyInOU: @@ -824,20 +823,20 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA else: entityList.extend([user['primaryEmail'] for user in users]) usersInOU += len(users) - _getMain().setGettingAllEntityItemsForWhom(_getMain().Ent.USER, ou, qualifier=qualifier) - _getMain().printGotEntityItemsForWhom(usersInOU) + _gam().setGettingAllEntityItemsForWhom(_gam().Ent.USER, ou, qualifier=qualifier) + _gam().printGotEntityItemsForWhom(usersInOU) except (GAPI.invalidInput, GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError, GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().checkEntityDNEorAccessErrorExit(cd, _getMain().Ent.ORGANIZATIONAL_UNIT, ou) - _incrEntityDoesNotExist(_getMain().Ent.ORGANIZATIONAL_UNIT) - elif entityType in {_getMain().Cmd.ENTITY_QUERY, _getMain().Cmd.ENTITY_QUERIES}: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - queries = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType == _getMain().Cmd.ENTITY_QUERY) + _gam().checkEntityDNEorAccessErrorExit(cd, _gam().Ent.ORGANIZATIONAL_UNIT, ou) + _incrEntityDoesNotExist(_gam().Ent.ORGANIZATIONAL_UNIT) + elif entityType in {_gam().Cmd.ENTITY_QUERY, _gam().Cmd.ENTITY_QUERIES}: + cd = _gam().buildGAPIObject(API.DIRECTORY) + queries = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType == _gam().Cmd.ENTITY_QUERY) for query in queries: - _getMain().printGettingAllAccountEntities(_getMain().Ent.USER, query) + _gam().printGettingAllAccountEntities(_gam().Ent.USER, query) try: - result = _getMain().callGAPIpages(cd.users(), 'list', 'users', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cd.users(), 'list', 'users', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, @@ -845,10 +844,10 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA fields='nextPageToken,users(primaryEmail,suspended,archived)', maxResults=GC.Values[GC.USER_MAX_RESULTS]) except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.invalidInput): - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.INVALID_QUERY) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.INVALID_QUERY) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) for user in result: email = user['primaryEmail'] if ((isSuspended is None or isSuspended == user['suspended']) and @@ -856,29 +855,29 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA email not in entitySet): entitySet.add(email) entityList.append(email) - elif entityType == _getMain().Cmd.ENTITY_LICENSES: + elif entityType == _gam().Cmd.ENTITY_LICENSES: skusList = [] for item in entity.split(','): - productId, sku = _getMain().SKU.getProductAndSKU(item) + productId, sku = _gam().SKU.getProductAndSKU(item) if not productId: - _incrEntityDoesNotExist(_getMain().Ent.SKU) + _incrEntityDoesNotExist(_gam().Ent.SKU) elif (productId, sku) not in skusList: skusList.append((productId, sku)) if skusList: - entityList = _getMain().doPrintLicenses(returnFields=['userId'], skus=skusList) - elif entityType in {_getMain().Cmd.ENTITY_COURSEPARTICIPANTS, _getMain().Cmd.ENTITY_TEACHERS, _getMain().Cmd.ENTITY_STUDENTS}: - croom = _getMain().buildGAPIObject(API.CLASSROOM) + entityList = _gam().doPrintLicenses(returnFields=['userId'], skus=skusList) + elif entityType in {_gam().Cmd.ENTITY_COURSEPARTICIPANTS, _gam().Cmd.ENTITY_TEACHERS, _gam().Cmd.ENTITY_STUDENTS}: + croom = _gam().buildGAPIObject(API.CLASSROOM) if not noListConversion: courseIdList = convertEntityToList(entity) else: courseIdList = [entity] - _, _, coursesInfo = _getMain()._getCoursesOwnerInfo(croom, courseIdList, GC.Values[GC.USE_COURSE_OWNER_ACCESS]) + _, _, coursesInfo = _gam()._getCoursesOwnerInfo(croom, courseIdList, GC.Values[GC.USE_COURSE_OWNER_ACCESS]) for courseId, courseInfo in coursesInfo.items(): try: - if entityType in {_getMain().Cmd.ENTITY_COURSEPARTICIPANTS, _getMain().Cmd.ENTITY_TEACHERS}: - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.TEACHER, _getMain().removeCourseIdScope(courseId), entityType=_getMain().Ent.COURSE) - result = _getMain().callGAPIpages(courseInfo['croom'].courses().teachers(), 'list', 'teachers', - pageMessage=_getMain().getPageMessageForWhom(), + if entityType in {_gam().Cmd.ENTITY_COURSEPARTICIPANTS, _gam().Cmd.ENTITY_TEACHERS}: + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.TEACHER, _gam().removeCourseIdScope(courseId), entityType=_gam().Ent.COURSE) + result = _gam().callGAPIpages(courseInfo['croom'].courses().teachers(), 'list', 'teachers', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=[GAPI.NOT_FOUND, GAPI.BAD_REQUEST, GAPI.SERVICE_NOT_AVAILABLE, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, @@ -889,10 +888,10 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA if email and (email not in entitySet): entitySet.add(email) entityList.append(email) - if entityType in {_getMain().Cmd.ENTITY_COURSEPARTICIPANTS, _getMain().Cmd.ENTITY_STUDENTS}: - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.STUDENT, _getMain().removeCourseIdScope(courseId), entityType=_getMain().Ent.COURSE) - result = _getMain().callGAPIpages(courseInfo['croom'].courses().students(), 'list', 'students', - pageMessage=_getMain().getPageMessageForWhom(), + if entityType in {_gam().Cmd.ENTITY_COURSEPARTICIPANTS, _gam().Cmd.ENTITY_STUDENTS}: + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.STUDENT, _gam().removeCourseIdScope(courseId), entityType=_gam().Ent.COURSE) + result = _gam().callGAPIpages(courseInfo['croom'].courses().students(), 'list', 'students', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=[GAPI.NOT_FOUND, GAPI.BAD_REQUEST, GAPI.SERVICE_NOT_AVAILABLE, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, @@ -904,84 +903,84 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA entitySet.add(email) entityList.append(email) except GAPI.notFound: - _getMain().entityDoesNotExistWarning(_getMain().Ent.COURSE, _getMain().removeCourseIdScope(courseId)) - _incrEntityDoesNotExist(_getMain().Ent.COURSE) + _gam().entityDoesNotExistWarning(_gam().Ent.COURSE, _gam().removeCourseIdScope(courseId)) + _incrEntityDoesNotExist(_gam().Ent.COURSE) except GAPI.serviceNotAvailable as e: - _getMain().entityActionNotPerformedWarning([_getMain().Ent.COURSE, _getMain().removeCourseIdScope(courseId)], str(e)) + _gam().entityActionNotPerformedWarning([_gam().Ent.COURSE, _gam().removeCourseIdScope(courseId)], str(e)) GM.Globals[GM.CLASSROOM_SERVICE_NOT_AVAILABLE] = True break except (GAPI.forbidden, GAPI.permissionDenied, GAPI.badRequest) as e: - _getMain().ClientAPIAccessDeniedExit(str(e)) - elif entityType == _getMain().Cmd.ENTITY_CROS: - _getMain().buildGAPIObject(API.DIRECTORY) + _gam().ClientAPIAccessDeniedExit(str(e)) + elif entityType == _gam().Cmd.ENTITY_CROS: + _gam().buildGAPIObject(API.DIRECTORY) for deviceId in convertEntityToList(entity): if deviceId not in entitySet: entitySet.add(deviceId) entityList.append(deviceId) - elif entityType == _getMain().Cmd.ENTITY_ALL_CROS: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - _getMain().printGettingAllAccountEntities(_getMain().Ent.CROS_DEVICE) + elif entityType == _gam().Cmd.ENTITY_ALL_CROS: + cd = _gam().buildGAPIObject(API.DIRECTORY) + _gam().printGettingAllAccountEntities(_gam().Ent.CROS_DEVICE) try: - result = _getMain().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], fields='nextPageToken,chromeosdevices(deviceId)', maxResults=GC.Values[GC.DEVICE_MAX_RESULTS]) except (GAPI.invalidInput, GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) entityList = [device['deviceId'] for device in result] - elif entityType in {_getMain().Cmd.ENTITY_CROS_QUERY, _getMain().Cmd.ENTITY_CROS_QUERIES, _getMain().Cmd.ENTITY_CROS_SN}: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - queries = convertEntityToList(entity, shlexSplit=entityType == _getMain().Cmd.ENTITY_CROS_QUERIES, - nonListEntityType=entityType == _getMain().Cmd.ENTITY_CROS_QUERY) - if entityType == _getMain().Cmd.ENTITY_CROS_SN: + elif entityType in {_gam().Cmd.ENTITY_CROS_QUERY, _gam().Cmd.ENTITY_CROS_QUERIES, _gam().Cmd.ENTITY_CROS_SN}: + cd = _gam().buildGAPIObject(API.DIRECTORY) + queries = convertEntityToList(entity, shlexSplit=entityType == _gam().Cmd.ENTITY_CROS_QUERIES, + nonListEntityType=entityType == _gam().Cmd.ENTITY_CROS_QUERY) + if entityType == _gam().Cmd.ENTITY_CROS_SN: queries = [f'id:{query}' for query in queries] for query in queries: - _validateDeviceQuery(_getMain().Ent.CROS_DEVICE, query) - _getMain().printGettingAllAccountEntities(_getMain().Ent.CROS_DEVICE, query) + _validateDeviceQuery(_gam().Ent.CROS_DEVICE, query) + _gam().printGettingAllAccountEntities(_gam().Ent.CROS_DEVICE, query) try: - result = _getMain().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], query=query, fields='nextPageToken,chromeosdevices(deviceId)', maxResults=GC.Values[GC.DEVICE_MAX_RESULTS]) except GAPI.invalidInput: - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.INVALID_QUERY) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.INVALID_QUERY) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) for device in result: deviceId = device['deviceId'] if deviceId not in entitySet: entitySet.add(deviceId) entityList.append(deviceId) - elif entityType in _getMain().Cmd.CROS_OU_ENTITY_TYPES or entityType in _getMain().Cmd.CROS_OUS_ENTITY_TYPES: - cd = _getMain().buildGAPIObject(API.DIRECTORY) - ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType in _getMain().Cmd.CROS_OU_ENTITY_TYPES) + elif entityType in _gam().Cmd.CROS_OU_ENTITY_TYPES or entityType in _gam().Cmd.CROS_OUS_ENTITY_TYPES: + cd = _gam().buildGAPIObject(API.DIRECTORY) + ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType in _gam().Cmd.CROS_OU_ENTITY_TYPES) numOus = len(ous) - includeChildOrgunits = entityType in _getMain().Cmd.CROS_OU_CHILDREN_ENTITY_TYPES - allQualifier = Msg.DIRECTLY_IN_THE.format(_getMain().Ent.Choose(_getMain().Ent.ORGANIZATIONAL_UNIT, numOus)) if not includeChildOrgunits else Msg.IN_THE.format(_getMain().Ent.Choose(_getMain().Ent.ORGANIZATIONAL_UNIT, numOus)) - if entityType in _getMain().Cmd.CROS_OU_QUERY_ENTITY_TYPES: - queries = getDeviceQueries('query', _getMain().Ent.CROS_DEVICE) - elif entityType in _getMain().Cmd.CROS_OU_QUERIES_ENTITY_TYPES: - queries = getDeviceQueries('queries', _getMain().Ent.CROS_DEVICE) + includeChildOrgunits = entityType in _gam().Cmd.CROS_OU_CHILDREN_ENTITY_TYPES + allQualifier = Msg.DIRECTLY_IN_THE.format(_gam().Ent.Choose(_gam().Ent.ORGANIZATIONAL_UNIT, numOus)) if not includeChildOrgunits else Msg.IN_THE.format(_gam().Ent.Choose(_gam().Ent.ORGANIZATIONAL_UNIT, numOus)) + if entityType in _gam().Cmd.CROS_OU_QUERY_ENTITY_TYPES: + queries = getDeviceQueries('query', _gam().Ent.CROS_DEVICE) + elif entityType in _gam().Cmd.CROS_OU_QUERIES_ENTITY_TYPES: + queries = getDeviceQueries('queries', _gam().Ent.CROS_DEVICE) else: queries = [None] for ou in ous: if ou == 'root': ou = '/' - ou = _getMain().makeOrgUnitPathAbsolute(ou) - oneQualifier = Msg.DIRECTLY_IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)) if not includeChildOrgunits else Msg.IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)) + ou = _gam().makeOrgUnitPathAbsolute(ou) + oneQualifier = Msg.DIRECTLY_IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)) if not includeChildOrgunits else Msg.IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)) for query in queries: - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.CROS_DEVICE, ou, - query=query, qualifier=oneQualifier, entityType=_getMain().Ent.ORGANIZATIONAL_UNIT) + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.CROS_DEVICE, ou, + query=query, qualifier=oneQualifier, entityType=_gam().Ent.ORGANIZATIONAL_UNIT) try: - result = _getMain().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(cd.chromeosdevices(), 'list', 'chromeosdevices', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=[GAPI.INVALID_INPUT, GAPI.INVALID_ORGUNIT, GAPI.ORGUNIT_NOT_FOUND, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, @@ -989,11 +988,11 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA orgUnitPath=ou, includeChildOrgunits=includeChildOrgunits, fields='nextPageToken,chromeosdevices(deviceId)', maxResults=GC.Values[GC.DEVICE_MAX_RESULTS]) except GAPI.invalidInput: - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.INVALID_QUERY) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.INVALID_QUERY) except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden): - _getMain().checkEntityDNEorAccessErrorExit(cd, _getMain().Ent.ORGANIZATIONAL_UNIT, ou) - _incrEntityDoesNotExist(_getMain().Ent.ORGANIZATIONAL_UNIT) + _gam().checkEntityDNEorAccessErrorExit(cd, _gam().Ent.ORGANIZATIONAL_UNIT, ou) + _incrEntityDoesNotExist(_gam().Ent.ORGANIZATIONAL_UNIT) continue if query is None: entityList.extend([device['deviceId'] for device in result]) @@ -1003,78 +1002,78 @@ def getItemsToModify(entityType, entity, memberRoles=None, isSuspended=None, isA if deviceId not in entitySet: entitySet.add(deviceId) entityList.append(deviceId) - _getMain().Ent.SetGettingQualifier(_getMain().Ent.CROS_DEVICE, allQualifier) - _getMain().Ent.SetGettingForWhom(','.join(ous)) - _getMain().printGotEntityItemsForWhom(len(entityList)) - elif entityType == _getMain().Cmd.ENTITY_BROWSER: + _gam().Ent.SetGettingQualifier(_gam().Ent.CROS_DEVICE, allQualifier) + _gam().Ent.SetGettingForWhom(','.join(ous)) + _gam().printGotEntityItemsForWhom(len(entityList)) + elif entityType == _gam().Cmd.ENTITY_BROWSER: result = convertEntityToList(entity) for deviceId in result: if deviceId not in entitySet: entitySet.add(deviceId) entityList.append(deviceId) - elif entityType in {_getMain().Cmd.ENTITY_BROWSER_OU, _getMain().Cmd.ENTITY_BROWSER_OUS}: - cbcm = _getMain().buildGAPIObject(API.CBCM) - customerId = _getMain()._getCustomerIdNoC() - ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType == _getMain().Cmd.ENTITY_BROWSER_OU) + elif entityType in {_gam().Cmd.ENTITY_BROWSER_OU, _gam().Cmd.ENTITY_BROWSER_OUS}: + cbcm = _gam().buildGAPIObject(API.CBCM) + customerId = _gam()._getCustomerIdNoC() + ous = convertEntityToList(entity, shlexSplit=True, nonListEntityType=entityType == _gam().Cmd.ENTITY_BROWSER_OU) numOus = len(ous) - allQualifier = Msg.DIRECTLY_IN_THE.format(_getMain().Ent.Choose(_getMain().Ent.ORGANIZATIONAL_UNIT, numOus)) - oneQualifier = Msg.DIRECTLY_IN_THE.format(_getMain().Ent.Singular(_getMain().Ent.ORGANIZATIONAL_UNIT)) + allQualifier = Msg.DIRECTLY_IN_THE.format(_gam().Ent.Choose(_gam().Ent.ORGANIZATIONAL_UNIT, numOus)) + oneQualifier = Msg.DIRECTLY_IN_THE.format(_gam().Ent.Singular(_gam().Ent.ORGANIZATIONAL_UNIT)) for ou in ous: - ou = _getMain().makeOrgUnitPathAbsolute(ou) - _getMain().printGettingAllEntityItemsForWhom(_getMain().Ent.CHROME_BROWSER, ou, qualifier=oneQualifier, entityType=_getMain().Ent.ORGANIZATIONAL_UNIT) + ou = _gam().makeOrgUnitPathAbsolute(ou) + _gam().printGettingAllEntityItemsForWhom(_gam().Ent.CHROME_BROWSER, ou, qualifier=oneQualifier, entityType=_gam().Ent.ORGANIZATIONAL_UNIT) try: - result = _getMain().callGAPIpages(cbcm.chromebrowsers(), 'list', 'browsers', - pageMessage=_getMain().getPageMessageForWhom(), + result = _gam().callGAPIpages(cbcm.chromebrowsers(), 'list', 'browsers', + pageMessage=_gam().getPageMessageForWhom(), throwReasons=[GAPI.BAD_REQUEST, GAPI.INVALID_ORGUNIT, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customer=customerId, orgUnitPath=ou, projection='BASIC', orderBy='id', sortOrder='ASCENDING', fields='nextPageToken,browsers(deviceId)') except (GAPI.badRequest, GAPI.invalidOrgunit, GAPI.forbidden): - _getMain().checkEntityDNEorAccessErrorExit(None, _getMain().Ent.ORGANIZATIONAL_UNIT, ou) - _incrEntityDoesNotExist(_getMain().Ent.ORGANIZATIONAL_UNIT) + _gam().checkEntityDNEorAccessErrorExit(None, _gam().Ent.ORGANIZATIONAL_UNIT, ou) + _incrEntityDoesNotExist(_gam().Ent.ORGANIZATIONAL_UNIT) continue entityList.extend([browser['deviceId'] for browser in result]) - _getMain().Ent.SetGettingQualifier(_getMain().Ent.CHROME_BROWSER, allQualifier) - _getMain().Ent.SetGettingForWhom(','.join(ous)) - _getMain().printGotEntityItemsForWhom(len(entityList)) - elif entityType in {_getMain().Cmd.ENTITY_BROWSER_QUERY, _getMain().Cmd.ENTITY_BROWSER_QUERIES}: - cbcm = _getMain().buildGAPIObject(API.CBCM) - customerId = _getMain()._getCustomerIdNoC() - queries = convertEntityToList(entity, shlexSplit=entityType == _getMain().Cmd.ENTITY_BROWSER_QUERIES, - nonListEntityType=entityType == _getMain().Cmd.ENTITY_BROWSER_QUERY) + _gam().Ent.SetGettingQualifier(_gam().Ent.CHROME_BROWSER, allQualifier) + _gam().Ent.SetGettingForWhom(','.join(ous)) + _gam().printGotEntityItemsForWhom(len(entityList)) + elif entityType in {_gam().Cmd.ENTITY_BROWSER_QUERY, _gam().Cmd.ENTITY_BROWSER_QUERIES}: + cbcm = _gam().buildGAPIObject(API.CBCM) + customerId = _gam()._getCustomerIdNoC() + queries = convertEntityToList(entity, shlexSplit=entityType == _gam().Cmd.ENTITY_BROWSER_QUERIES, + nonListEntityType=entityType == _gam().Cmd.ENTITY_BROWSER_QUERY) for query in queries: - _getMain().printGettingAllAccountEntities(_getMain().Ent.CHROME_BROWSER, query) + _gam().printGettingAllAccountEntities(_gam().Ent.CHROME_BROWSER, query) try: - result = _getMain().callGAPIpages(cbcm.chromebrowsers(), 'list', 'browsers', - pageMessage=_getMain().getPageMessage(), + result = _gam().callGAPIpages(cbcm.chromebrowsers(), 'list', 'browsers', + pageMessage=_gam().getPageMessage(), throwReasons=[GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN], retryReasons=GAPI.SERVICE_NOT_AVAILABLE_RETRY_REASONS, customer=customerId, query=query, projection='BASIC', orderBy='id', sortOrder='ASCENDING', fields='nextPageToken,browsers(deviceId)') except GAPI.invalidInput: - _getMain().Cmd.Backup() - _getMain().usageErrorExit(Msg.INVALID_QUERY) + _gam().Cmd.Backup() + _gam().usageErrorExit(Msg.INVALID_QUERY) except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden) as e: - _getMain().accessErrorExitNonDirectory(API.CBCM, str(e)) + _gam().accessErrorExitNonDirectory(API.CBCM, str(e)) for device in result: deviceId = device['deviceId'] if deviceId not in entitySet: entitySet.add(deviceId) entityList.append(deviceId) else: - _getMain().systemErrorExit(_getMain().UNKNOWN_ERROR_RC, 'getItemsToModify coding error') + _gam().systemErrorExit(_gam().UNKNOWN_ERROR_RC, 'getItemsToModify coding error') for errorType in [ENTITY_ERROR_DNE, ENTITY_ERROR_INVALID]: if entityError[errorType] > 0: - _getMain().Cmd.SetLocation(entityLocation-1) - _getMain().writeStderr(_getMain().Cmd.CommandLineWithBadArgumentMarked(False)) + _gam().Cmd.SetLocation(entityLocation-1) + _gam().writeStderr(_gam().Cmd.CommandLineWithBadArgumentMarked(False)) count = entityError[errorType] if errorType == ENTITY_ERROR_DNE: - _getMain().stderrErrorMsg(Msg.BAD_ENTITIES_IN_SOURCE.format(count, _getMain().Ent.Choose(entityError['entityType'], count), + _gam().stderrErrorMsg(Msg.BAD_ENTITIES_IN_SOURCE.format(count, _gam().Ent.Choose(entityError['entityType'], count), Msg.DO_NOT_EXIST if count != 1 else Msg.DOES_NOT_EXIST)) - sys.exit(_getMain().ENTITY_DOES_NOT_EXIST_RC) + sys.exit(_gam().ENTITY_DOES_NOT_EXIST_RC) else: - _getMain().stderrErrorMsg(Msg.BAD_ENTITIES_IN_SOURCE.format(count, Msg.INVALID, _getMain().Ent.Choose(entityError['entityType'], count))) - sys.exit(_getMain().INVALID_ENTITY_RC) + _gam().stderrErrorMsg(Msg.BAD_ENTITIES_IN_SOURCE.format(count, Msg.INVALID, _gam().Ent.Choose(entityError['entityType'], count))) + sys.exit(_gam().INVALID_ENTITY_RC) return entityList def splitEntityList(entity, dataDelimiter): @@ -1093,38 +1092,38 @@ def splitEntityListShlex(entity, dataDelimiter): def fileDataErrorExit(filename, row, itemName, value, errMessage): if itemName: - _getMain().systemErrorExit(_getMain().DATA_ERROR_RC, - _getMain().formatKeyValueList('', - [_getMain().Ent.Singular(_getMain().Ent.FILE), filename, - _getMain().Ent.Singular(_getMain().Ent.ROW), row, - _getMain().Ent.Singular(_getMain().Ent.ITEM), itemName, - _getMain().Ent.Singular(_getMain().Ent.VALUE), value, + _gam().systemErrorExit(_gam().DATA_ERROR_RC, + _gam().formatKeyValueList('', + [_gam().Ent.Singular(_gam().Ent.FILE), filename, + _gam().Ent.Singular(_gam().Ent.ROW), row, + _gam().Ent.Singular(_gam().Ent.ITEM), itemName, + _gam().Ent.Singular(_gam().Ent.VALUE), value, errMessage], '')) else: - _getMain().systemErrorExit(_getMain().DATA_ERROR_RC, - _getMain().formatKeyValueList('', - [_getMain().Ent.Singular(_getMain().Ent.FILE), filename, - _getMain().Ent.Singular(_getMain().Ent.ROW), row, - _getMain().Ent.Singular(_getMain().Ent.VALUE), value, + _gam().systemErrorExit(_gam().DATA_ERROR_RC, + _gam().formatKeyValueList('', + [_gam().Ent.Singular(_gam().Ent.FILE), filename, + _gam().Ent.Singular(_gam().Ent.ROW), row, + _gam().Ent.Singular(_gam().Ent.VALUE), value, errMessage], '')) # def getEntitiesFromFile(shlexSplit, returnSet=False): - filename = _getMain().getString(_getMain().Cmd.OB_FILE_NAME) + filename = _gam().getString(_gam().Cmd.OB_FILE_NAME) filenameLower = filename.lower() if filenameLower not in {'gcsv', 'gdoc', 'gcscsv', 'gcsdoc'}: - encoding = _getMain().getCharSet() - filename = _getMain().setFilePath(filename, GC.INPUT_DIR) - f = _getMain().openFile(filename, encoding=encoding, stripUTFBOM=True) + encoding = _gam().getCharSet() + filename = _gam().setFilePath(filename, GC.INPUT_DIR) + f = _gam().openFile(filename, encoding=encoding, stripUTFBOM=True) elif filenameLower in {'gcsv', 'gdoc'}: - f = _getMain().getGDocData(filenameLower) - _getMain().getCharSet() + f = _gam().getGDocData(filenameLower) + _gam().getCharSet() else: #filenameLower in {'gcscsv', 'gcsdoc'}: - f = _getMain().getStorageFileData(filenameLower) - _getMain().getCharSet() - dataDelimiter = _getMain().getDelimiter() + f = _gam().getStorageFileData(filenameLower) + _gam().getCharSet() + dataDelimiter = _gam().getDelimiter() entitySet = set() entityList = [] i = 0 @@ -1141,33 +1140,33 @@ def getEntitiesFromFile(shlexSplit, returnSet=False): if item and (item not in entitySet): entitySet.add(item) entityList.append(item) - _getMain().closeFile(f) + _gam().closeFile(f) return entityList if not returnSet else entitySet # def getEntitiesFromCSVFile(shlexSplit, returnSet=False): - fileFieldName = _getMain().getString(_getMain().Cmd.OB_FILE_NAME_FIELD_NAME) + fileFieldName = _gam().getString(_gam().Cmd.OB_FILE_NAME_FIELD_NAME) if platform.system() == 'Windows' and not fileFieldName.startswith('-:'): drive, fileFieldName = os.path.splitdrive(fileFieldName) else: drive = '' if fileFieldName.find(':') == -1: - _getMain().Cmd.Backup() - _getMain().invalidArgumentExit(_getMain().Cmd.OB_FILE_NAME_FIELD_NAME) + _gam().Cmd.Backup() + _gam().invalidArgumentExit(_gam().Cmd.OB_FILE_NAME_FIELD_NAME) fileFieldNameList = fileFieldName.split(':') filename = drive+fileFieldNameList[0] - f, csvFile, fieldnames = _getMain().openCSVFileReader(filename) + f, csvFile, fieldnames = _gam().openCSVFileReader(filename) for fieldName in fileFieldNameList[1:]: if fieldName not in fieldnames: - _getMain().csvFieldErrorExit(fieldName, fieldnames, backupArg=True, checkForCharset=True) - matchFields, skipFields = _getMain().getMatchSkipFields(fieldnames) - dataDelimiter = _getMain().getDelimiter() + _gam().csvFieldErrorExit(fieldName, fieldnames, backupArg=True, checkForCharset=True) + matchFields, skipFields = _gam().getMatchSkipFields(fieldnames) + dataDelimiter = _gam().getDelimiter() entitySet = set() entityList = [] i = 1 for row in csvFile: i += 1 - if _getMain().checkMatchSkipFields(row, None, matchFields, skipFields): + if _gam().checkMatchSkipFields(row, None, matchFields, skipFields): for fieldName in fileFieldNameList[1:]: if shlexSplit: splitStatus, itemList = splitEntityListShlex(row[fieldName].strip(), dataDelimiter) @@ -1180,7 +1179,7 @@ def getEntitiesFromCSVFile(shlexSplit, returnSet=False): if item and (item not in entitySet): entitySet.add(item) entityList.append(item) - _getMain().closeFile(f) + _gam().closeFile(f) return entityList if not returnSet else entitySet # @@ -1191,28 +1190,28 @@ def getEntitiesFromCSVFile(shlexSplit, returnSet=False): def getEntitiesFromCSVbyField(): def getKeyFieldInfo(keyword, required, globalKeyField): - if not _getMain().checkArgumentPresent(keyword, required=required): + if not _gam().checkArgumentPresent(keyword, required=required): GM.Globals[globalKeyField] = None return (None, None, None, None) - keyField = GM.Globals[globalKeyField] = _getMain().getString(_getMain().Cmd.OB_FIELD_NAME) + keyField = GM.Globals[globalKeyField] = _gam().getString(_gam().Cmd.OB_FIELD_NAME) if keyField not in fieldnames: - _getMain().csvFieldErrorExit(keyField, fieldnames, backupArg=True) - if _getMain().checkArgumentPresent('keypattern'): - keyPattern = _getMain().getREPattern() + _gam().csvFieldErrorExit(keyField, fieldnames, backupArg=True) + if _gam().checkArgumentPresent('keypattern'): + keyPattern = _gam().getREPattern() else: keyPattern = None - if _getMain().checkArgumentPresent('keyvalue'): - keyValue = _getMain().getString(_getMain().Cmd.OB_STRING) + if _gam().checkArgumentPresent('keyvalue'): + keyValue = _gam().getString(_gam().Cmd.OB_STRING) else: keyValue = keyField - keyDelimiter = _getMain().getDelimiter() + keyDelimiter = _gam().getDelimiter() return (keyField, keyPattern, keyValue, keyDelimiter) def getKeyList(row, keyField, keyPattern, keyValue, keyDelimiter, matchFields, skipFields): item = row[keyField].strip() if not item: return [] - if not _getMain().checkMatchSkipFields(row, None, matchFields, skipFields): + if not _gam().checkMatchSkipFields(row, None, matchFields, skipFields): return [] if keyPattern: keyList = [keyPattern.sub(keyValue, keyItem.strip()) for keyItem in splitEntityList(item, keyDelimiter)] @@ -1220,20 +1219,20 @@ def getEntitiesFromCSVbyField(): keyList = [re.sub(keyField, keyItem.strip(), keyValue) for keyItem in splitEntityList(item, keyDelimiter)] return [key for key in keyList if key] - filename = _getMain().getString(_getMain().Cmd.OB_FILE_NAME) - f, csvFile, fieldnames = _getMain().openCSVFileReader(filename) + filename = _gam().getString(_gam().Cmd.OB_FILE_NAME) + f, csvFile, fieldnames = _gam().openCSVFileReader(filename) mainKeyField, mainKeyPattern, mainKeyValue, mainKeyDelimiter = getKeyFieldInfo('keyfield', True, GM.CSV_KEY_FIELD) subKeyField, subKeyPattern, subKeyValue, subKeyDelimiter = getKeyFieldInfo('subkeyfield', False, GM.CSV_SUBKEY_FIELD) - matchFields, skipFields = _getMain().getMatchSkipFields(fieldnames) - if _getMain().checkArgumentPresent('datafield'): + matchFields, skipFields = _gam().getMatchSkipFields(fieldnames) + if _gam().checkArgumentPresent('datafield'): if GM.Globals[GM.CSV_DATA_DICT]: - _getMain().csvDataAlreadySavedErrorExit() - GM.Globals[GM.CSV_DATA_FIELD] = _getMain().getString(_getMain().Cmd.OB_FIELD_NAME, checkBlank=True) + _gam().csvDataAlreadySavedErrorExit() + GM.Globals[GM.CSV_DATA_FIELD] = _gam().getString(_gam().Cmd.OB_FIELD_NAME, checkBlank=True) dataFields = GM.Globals[GM.CSV_DATA_FIELD].split(':') for dataField in dataFields: if dataField not in fieldnames: - _getMain().csvFieldErrorExit(dataField, fieldnames, backupArg=True) - dataDelimiter = _getMain().getDelimiter() + _gam().csvFieldErrorExit(dataField, fieldnames, backupArg=True) + dataDelimiter = _gam().getDelimiter() else: GM.Globals[GM.CSV_DATA_FIELD] = None dataFields = [] @@ -1300,7 +1299,7 @@ def getEntitiesFromCSVbyField(): if dataValue not in csvDataKeys[mainKey][subKey]: csvDataKeys[mainKey][subKey].add(dataValue) GM.Globals[GM.CSV_DATA_DICT][mainKey][subKey].append(dataValue) - _getMain().closeFile(f) + _gam().closeFile(f) return entityList # Typically used to map courseparticipants to students or teachers @@ -1313,161 +1312,161 @@ def getEntityArgument(entityList): if entityList is None: return (0, 0, entityList) if isinstance(entityList, dict): - clLoc = _getMain().Cmd.Location() - _getMain().Cmd.SetLocation(GM.Globals[GM.ENTITY_CL_DELAY_START]) + clLoc = _gam().Cmd.Location() + _gam().Cmd.SetLocation(GM.Globals[GM.ENTITY_CL_DELAY_START]) entityList = getItemsToModify(**entityList) - _getMain().Cmd.SetLocation(clLoc) + _gam().Cmd.SetLocation(clLoc) return (0, len(entityList), entityList) def getEntityToModify(defaultEntityType=None, browserAllowed=False, crosAllowed=False, userAllowed=True, typeMap=None, isSuspended=None, isArchived=None, groupMemberType='USER', delayGet=False): if GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY]: crosAllowed = False - selectorChoices = _getMain().Cmd.SERVICE_ACCOUNT_ONLY_ENTITY_SELECTORS[:] + selectorChoices = _gam().Cmd.SERVICE_ACCOUNT_ONLY_ENTITY_SELECTORS[:] else: - selectorChoices = _getMain().Cmd.BASE_ENTITY_SELECTORS[:] + selectorChoices = _gam().Cmd.BASE_ENTITY_SELECTORS[:] if userAllowed: - selectorChoices += _getMain().Cmd.USER_ENTITY_SELECTORS+_getMain().Cmd.USER_CSVDATA_ENTITY_SELECTORS + selectorChoices += _gam().Cmd.USER_ENTITY_SELECTORS+_gam().Cmd.USER_CSVDATA_ENTITY_SELECTORS if crosAllowed: - selectorChoices += _getMain().Cmd.CROS_ENTITY_SELECTORS+_getMain().Cmd.CROS_CSVDATA_ENTITY_SELECTORS + selectorChoices += _gam().Cmd.CROS_ENTITY_SELECTORS+_gam().Cmd.CROS_CSVDATA_ENTITY_SELECTORS if browserAllowed: - selectorChoices = _getMain().Cmd.BROWSER_ENTITY_SELECTORS - entitySelector = _getMain().getChoice(selectorChoices, defaultChoice=None) + selectorChoices = _gam().Cmd.BROWSER_ENTITY_SELECTORS + entitySelector = _gam().getChoice(selectorChoices, defaultChoice=None) if entitySelector: choices = [] - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_ALL: + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_ALL: if userAllowed: - choices += _getMain().Cmd.USER_ENTITY_SELECTOR_ALL_SUBTYPES + choices += _gam().Cmd.USER_ENTITY_SELECTOR_ALL_SUBTYPES if crosAllowed: - choices += _getMain().Cmd.CROS_ENTITY_SELECTOR_ALL_SUBTYPES - entityType = _getMain().Cmd.ENTITY_SELECTOR_ALL_SUBTYPES_MAP[_getMain().getChoice(choices)] + choices += _gam().Cmd.CROS_ENTITY_SELECTOR_ALL_SUBTYPES + entityType = _gam().Cmd.ENTITY_SELECTOR_ALL_SUBTYPES_MAP[_gam().getChoice(choices)] if not delayGet: - return (_getMain().Cmd.ENTITY_USERS if entityType != _getMain().Cmd.ENTITY_ALL_CROS else _getMain().Cmd.ENTITY_CROS, + return (_gam().Cmd.ENTITY_USERS if entityType != _gam().Cmd.ENTITY_ALL_CROS else _gam().Cmd.ENTITY_CROS, getItemsToModify(entityType, None)) - GM.Globals[GM.ENTITY_CL_DELAY_START] = _getMain().Cmd.Location() - _getMain().buildGAPIObject(API.DIRECTORY) - return (_getMain().Cmd.ENTITY_USERS if entityType != _getMain().Cmd.ENTITY_ALL_CROS else _getMain().Cmd.ENTITY_CROS, + GM.Globals[GM.ENTITY_CL_DELAY_START] = _gam().Cmd.Location() + _gam().buildGAPIObject(API.DIRECTORY) + return (_gam().Cmd.ENTITY_USERS if entityType != _gam().Cmd.ENTITY_ALL_CROS else _gam().Cmd.ENTITY_CROS, {'entityType': entityType, 'entity': None}) if userAllowed: - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_FILE: - return (_getMain().Cmd.ENTITY_USERS, getItemsToModify(_getMain().Cmd.ENTITY_USERS, getEntitiesFromFile(False))) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSV, _getMain().Cmd.ENTITY_SELECTOR_CSVFILE]: - return (_getMain().Cmd.ENTITY_USERS, getItemsToModify(_getMain().Cmd.ENTITY_USERS, getEntitiesFromCSVFile(False))) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_FILE: + return (_gam().Cmd.ENTITY_USERS, getItemsToModify(_gam().Cmd.ENTITY_USERS, getEntitiesFromFile(False))) + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSV, _gam().Cmd.ENTITY_SELECTOR_CSVFILE]: + return (_gam().Cmd.ENTITY_USERS, getItemsToModify(_gam().Cmd.ENTITY_USERS, getEntitiesFromCSVFile(False))) if crosAllowed: - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CROSFILE: - return (_getMain().Cmd.ENTITY_CROS, getEntitiesFromFile(False)) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CROSCSV, _getMain().Cmd.ENTITY_SELECTOR_CROSCSVFILE]: - return (_getMain().Cmd.ENTITY_CROS, getEntitiesFromCSVFile(False)) - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CROSFILE_SN: - return (_getMain().Cmd.ENTITY_CROS, getItemsToModify(_getMain().Cmd.ENTITY_CROS_SN, getEntitiesFromFile(False))) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CROSCSV_SN, _getMain().Cmd.ENTITY_SELECTOR_CROSCSVFILE_SN]: - return (_getMain().Cmd.ENTITY_CROS, getItemsToModify(_getMain().Cmd.ENTITY_CROS_SN, getEntitiesFromCSVFile(False))) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CROSFILE: + return (_gam().Cmd.ENTITY_CROS, getEntitiesFromFile(False)) + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CROSCSV, _gam().Cmd.ENTITY_SELECTOR_CROSCSVFILE]: + return (_gam().Cmd.ENTITY_CROS, getEntitiesFromCSVFile(False)) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CROSFILE_SN: + return (_gam().Cmd.ENTITY_CROS, getItemsToModify(_gam().Cmd.ENTITY_CROS_SN, getEntitiesFromFile(False))) + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CROSCSV_SN, _gam().Cmd.ENTITY_SELECTOR_CROSCSVFILE_SN]: + return (_gam().Cmd.ENTITY_CROS, getItemsToModify(_gam().Cmd.ENTITY_CROS_SN, getEntitiesFromCSVFile(False))) if browserAllowed: - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_FILE: - return (_getMain().Cmd.ENTITY_BROWSER, getEntitiesFromFile(False)) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSV, _getMain().Cmd.ENTITY_SELECTOR_CSVFILE]: - return (_getMain().Cmd.ENTITY_BROWSER, getEntitiesFromCSVFile(False)) - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_DATAFILE: + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_FILE: + return (_gam().Cmd.ENTITY_BROWSER, getEntitiesFromFile(False)) + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSV, _gam().Cmd.ENTITY_SELECTOR_CSVFILE]: + return (_gam().Cmd.ENTITY_BROWSER, getEntitiesFromCSVFile(False)) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_DATAFILE: if userAllowed: - choices += _getMain().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_getMain().Cmd.ENTITY_USERS] + choices += _gam().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_gam().Cmd.ENTITY_USERS] if crosAllowed: - choices += _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES - entityType = mapEntityType(_getMain().getChoice(choices), typeMap) - return (_getMain().Cmd.ENTITY_USERS if entityType not in _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _getMain().Cmd.ENTITY_CROS, - getItemsToModify(entityType, getEntitiesFromFile(shlexSplit=entityType in _getMain().Cmd.OUS_ENTITY_TYPES | {_getMain().Cmd.ENTITY_CROS_OUS, _getMain().Cmd.ENTITY_CROS_OUS_AND_CHILDREN}))) - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CSVDATAFILE: + choices += _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES + entityType = mapEntityType(_gam().getChoice(choices), typeMap) + return (_gam().Cmd.ENTITY_USERS if entityType not in _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _gam().Cmd.ENTITY_CROS, + getItemsToModify(entityType, getEntitiesFromFile(shlexSplit=entityType in _gam().Cmd.OUS_ENTITY_TYPES | {_gam().Cmd.ENTITY_CROS_OUS, _gam().Cmd.ENTITY_CROS_OUS_AND_CHILDREN}))) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CSVDATAFILE: if userAllowed: - choices += _getMain().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_getMain().Cmd.ENTITY_USERS] + choices += _gam().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_gam().Cmd.ENTITY_USERS] if crosAllowed: - choices += _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES - entityType = mapEntityType(_getMain().getChoice(choices), typeMap) - return (_getMain().Cmd.ENTITY_USERS if entityType not in _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _getMain().Cmd.ENTITY_CROS, - getItemsToModify(entityType, getEntitiesFromCSVFile(shlexSplit=entityType in _getMain().Cmd.OUS_ENTITY_TYPES | {_getMain().Cmd.ENTITY_CROS_OUS, _getMain().Cmd.ENTITY_CROS_OUS_AND_CHILDREN}))) - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CSVKMD: + choices += _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES + entityType = mapEntityType(_gam().getChoice(choices), typeMap) + return (_gam().Cmd.ENTITY_USERS if entityType not in _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _gam().Cmd.ENTITY_CROS, + getItemsToModify(entityType, getEntitiesFromCSVFile(shlexSplit=entityType in _gam().Cmd.OUS_ENTITY_TYPES | {_gam().Cmd.ENTITY_CROS_OUS, _gam().Cmd.ENTITY_CROS_OUS_AND_CHILDREN}))) + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CSVKMD: if userAllowed: - choices += _getMain().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_getMain().Cmd.ENTITY_USERS] + choices += _gam().Cmd.USER_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_gam().Cmd.ENTITY_USERS] if crosAllowed: - choices += _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES - entityType = mapEntityType(_getMain().getChoice(choices, choiceAliases=_getMain().Cmd.ENTITY_ALIAS_MAP), typeMap) - return (_getMain().Cmd.ENTITY_USERS if entityType not in _getMain().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _getMain().Cmd.ENTITY_CROS, + choices += _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES + entityType = mapEntityType(_gam().getChoice(choices, choiceAliases=_gam().Cmd.ENTITY_ALIAS_MAP), typeMap) + return (_gam().Cmd.ENTITY_USERS if entityType not in _gam().Cmd.CROS_ENTITY_SELECTOR_DATAFILE_CSVKMD_SUBTYPES else _gam().Cmd.ENTITY_CROS, getItemsToModify(entityType, getEntitiesFromCSVbyField())) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSVDATA, _getMain().Cmd.ENTITY_SELECTOR_CROSCSVDATA]: - _getMain().checkDataField() - return (_getMain().Cmd.ENTITY_USERS if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CSVDATA else _getMain().Cmd.ENTITY_CROS, + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSVDATA, _gam().Cmd.ENTITY_SELECTOR_CROSCSVDATA]: + _gam().checkDataField() + return (_gam().Cmd.ENTITY_USERS if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CSVDATA else _gam().Cmd.ENTITY_CROS, GM.Globals[GM.CSV_DATA_DICT]) entityChoices = [] if userAllowed: - entityChoices += _getMain().Cmd.USER_ENTITIES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_getMain().Cmd.ENTITY_USER, _getMain().Cmd.ENTITY_USERS] + entityChoices += _gam().Cmd.USER_ENTITIES if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY] else [_gam().Cmd.ENTITY_USER, _gam().Cmd.ENTITY_USERS] if crosAllowed: - entityChoices += _getMain().Cmd.CROS_ENTITIES + entityChoices += _gam().Cmd.CROS_ENTITIES if browserAllowed: - entityChoices += _getMain().Cmd.BROWSER_ENTITIES - entityType = mapEntityType(_getMain().getChoice(entityChoices, choiceAliases=_getMain().Cmd.ENTITY_ALIAS_MAP, defaultChoice=defaultEntityType), typeMap) + entityChoices += _gam().Cmd.BROWSER_ENTITIES + entityType = mapEntityType(_gam().getChoice(entityChoices, choiceAliases=_gam().Cmd.ENTITY_ALIAS_MAP, defaultChoice=defaultEntityType), typeMap) if not entityType: - _getMain().invalidChoiceExit(_getMain().Cmd.Current(), selectorChoices+entityChoices, False) - if entityType not in _getMain().Cmd.CROS_ENTITIES+_getMain().Cmd.BROWSER_ENTITIES: - entityClass = _getMain().Cmd.ENTITY_USERS - if entityType == _getMain().Cmd.ENTITY_OAUTHUSER: - return (entityClass, [_getMain()._getAdminEmail()]) - entityItem = _getMain().getString(_getMain().Cmd.OB_USER_ENTITY, minLen=0) - elif entityType in _getMain().Cmd.CROS_ENTITIES: - entityClass = _getMain().Cmd.ENTITY_CROS - entityItem = _getMain().getString(_getMain().Cmd.OB_CROS_ENTITY, minLen=0) + _gam().invalidChoiceExit(_gam().Cmd.Current(), selectorChoices+entityChoices, False) + if entityType not in _gam().Cmd.CROS_ENTITIES+_gam().Cmd.BROWSER_ENTITIES: + entityClass = _gam().Cmd.ENTITY_USERS + if entityType == _gam().Cmd.ENTITY_OAUTHUSER: + return (entityClass, [_gam()._getAdminEmail()]) + entityItem = _gam().getString(_gam().Cmd.OB_USER_ENTITY, minLen=0) + elif entityType in _gam().Cmd.CROS_ENTITIES: + entityClass = _gam().Cmd.ENTITY_CROS + entityItem = _gam().getString(_gam().Cmd.OB_CROS_ENTITY, minLen=0) else: - entityClass = _getMain().Cmd.ENTITY_BROWSER - entityItem = _getMain().getString(_getMain().Cmd.OB_BROWSER_ENTITY, minLen=0) + entityClass = _gam().Cmd.ENTITY_BROWSER + entityItem = _gam().getString(_gam().Cmd.OB_BROWSER_ENTITY, minLen=0) if not delayGet: - if entityClass == _getMain().Cmd.ENTITY_USERS: + if entityClass == _gam().Cmd.ENTITY_USERS: return (entityClass, getItemsToModify(entityType, entityItem, isSuspended=isSuspended, isArchived=isArchived, groupMemberType=groupMemberType)) return (entityClass, getItemsToModify(entityType, entityItem)) - GM.Globals[GM.ENTITY_CL_DELAY_START] = _getMain().Cmd.Location() + GM.Globals[GM.ENTITY_CL_DELAY_START] = _gam().Cmd.Location() if not GC.Values[GC.USER_SERVICE_ACCOUNT_ACCESS_ONLY]: - _getMain().buildGAPIObject(API.DIRECTORY) - if entityClass == _getMain().Cmd.ENTITY_USERS: - if entityType in _getMain().Cmd.GROUP_USERS_ENTITY_TYPES | {_getMain().Cmd.ENTITY_CIGROUP_USERS}: + _gam().buildGAPIObject(API.DIRECTORY) + if entityClass == _gam().Cmd.ENTITY_USERS: + if entityType in _gam().Cmd.GROUP_USERS_ENTITY_TYPES | {_gam().Cmd.ENTITY_CIGROUP_USERS}: # Skip over sub-arguments - while _getMain().Cmd.ArgumentsRemaining(): - myarg = _getMain().getArgument() + while _gam().Cmd.ArgumentsRemaining(): + myarg = _gam().getArgument() if myarg in GROUP_ROLES_MAP or myarg in {'primarydomain', 'recursive', 'includederivedmembership'}: pass elif myarg == 'domains': - _getMain().Cmd.Advance() - elif ((entityType == _getMain().Cmd.ENTITY_GROUP_USERS_SELECT) and - (myarg in _getMain().SUSPENDED_ARGUMENTS) or (myarg in _getMain().ARCHIVED_ARGUMENTS)): + _gam().Cmd.Advance() + elif ((entityType == _gam().Cmd.ENTITY_GROUP_USERS_SELECT) and + (myarg in _gam().SUSPENDED_ARGUMENTS) or (myarg in _gam().ARCHIVED_ARGUMENTS)): if myarg in {'issuspended', 'isarchived'}: - if _getMain().Cmd.PeekArgumentPresent(_getMain().TRUE_VALUES) or _getMain().Cmd.PeekArgumentPresent(_getMain().FALSE_VALUES): - _getMain().Cmd.Advance() + if _gam().Cmd.PeekArgumentPresent(_gam().TRUE_VALUES) or _gam().Cmd.PeekArgumentPresent(_gam().FALSE_VALUES): + _gam().Cmd.Advance() elif myarg == 'end': break else: - _getMain().Cmd.Backup() - _getMain().missingArgumentExit('end') + _gam().Cmd.Backup() + _gam().missingArgumentExit('end') return (entityClass, {'entityType': entityType, 'entity': entityItem, 'isSuspended': isSuspended, 'isArchived': isArchived, 'groupMemberType': groupMemberType}) - if entityClass == _getMain().Cmd.ENTITY_CROS: - if entityType in {_getMain().Cmd.ENTITY_CROS_OU_QUERY, _getMain().Cmd.ENTITY_CROS_OU_AND_CHILDREN_QUERY, _getMain().Cmd.ENTITY_CROS_OUS_QUERY, _getMain().Cmd.ENTITY_CROS_OUS_AND_CHILDREN_QUERY, - _getMain().Cmd.ENTITY_CROS_OU_QUERIES, _getMain().Cmd.ENTITY_CROS_OU_AND_CHILDREN_QUERIES, _getMain().Cmd.ENTITY_CROS_OUS_QUERIES, _getMain().Cmd.ENTITY_CROS_OUS_AND_CHILDREN_QUERIES}: - _getMain().Cmd.Advance() + if entityClass == _gam().Cmd.ENTITY_CROS: + if entityType in {_gam().Cmd.ENTITY_CROS_OU_QUERY, _gam().Cmd.ENTITY_CROS_OU_AND_CHILDREN_QUERY, _gam().Cmd.ENTITY_CROS_OUS_QUERY, _gam().Cmd.ENTITY_CROS_OUS_AND_CHILDREN_QUERY, + _gam().Cmd.ENTITY_CROS_OU_QUERIES, _gam().Cmd.ENTITY_CROS_OU_AND_CHILDREN_QUERIES, _gam().Cmd.ENTITY_CROS_OUS_QUERIES, _gam().Cmd.ENTITY_CROS_OUS_AND_CHILDREN_QUERIES}: + _gam().Cmd.Advance() return (entityClass, {'entityType': entityType, 'entity': entityItem}) def getEntitySelector(): - return _getMain().getChoice(_getMain().Cmd.ENTITY_LIST_SELECTORS, defaultChoice=None) + return _gam().getChoice(_gam().Cmd.ENTITY_LIST_SELECTORS, defaultChoice=None) def getEntitySelection(entitySelector, shlexSplit): - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_FILE]: + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_FILE]: return getEntitiesFromFile(shlexSplit) - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSV, _getMain().Cmd.ENTITY_SELECTOR_CSVFILE]: + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSV, _gam().Cmd.ENTITY_SELECTOR_CSVFILE]: return getEntitiesFromCSVFile(shlexSplit) - if entitySelector == _getMain().Cmd.ENTITY_SELECTOR_CSVKMD: + if entitySelector == _gam().Cmd.ENTITY_SELECTOR_CSVKMD: return getEntitiesFromCSVbyField() - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSVSUBKEY]: - _getMain().checkSubkeyField() + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSVSUBKEY]: + _gam().checkSubkeyField() return GM.Globals[GM.CSV_DATA_DICT] - if entitySelector in [_getMain().Cmd.ENTITY_SELECTOR_CSVDATA]: - _getMain().checkDataField() + if entitySelector in [_gam().Cmd.ENTITY_SELECTOR_CSVDATA]: + _gam().checkDataField() return GM.Globals[GM.CSV_DATA_DICT] return [] @@ -1475,10 +1474,10 @@ def getEntityList(item, shlexSplit=False): entitySelector = getEntitySelector() if entitySelector: return getEntitySelection(entitySelector, shlexSplit) - return convertEntityToList(_getMain().getString(item, minLen=0), shlexSplit=shlexSplit) + return convertEntityToList(_gam().getString(item, minLen=0), shlexSplit=shlexSplit) def getNormalizedEmailAddressEntity(shlexSplit=False, noUid=True, noLower=False): - return [_getMain().normalizeEmailAddressOrUID(emailAddress, noUid=noUid, noLower=noLower) for emailAddress in getEntityList(_getMain().Cmd.OB_EMAIL_ADDRESS_ENTITY, shlexSplit)] + return [_gam().normalizeEmailAddressOrUID(emailAddress, noUid=noUid, noLower=noLower) for emailAddress in getEntityList(_gam().Cmd.OB_EMAIL_ADDRESS_ENTITY, shlexSplit)] def getUserObjectEntity(clObject, itemType, shlexSplit=False): entity = {'item': itemType, 'list': getEntityList(clObject, shlexSplit), 'dict': None} @@ -1491,14 +1490,14 @@ def _validateUserGetObjectList(user, i, count, entity, api=API.GMAIL, showAction entityList = entity['dict'][user] else: entityList = entity['list'] - user, svc = _getMain().buildGAPIServiceObject(api, user, i, count) + user, svc = _gam().buildGAPIServiceObject(api, user, i, count) if not svc: return (user, None, [], 0) jcount = len(entityList) if showAction: - _getMain().entityPerformActionNumItems([_getMain().Ent.USER, user], jcount, entity['item'], i, count) + _gam().entityPerformActionNumItems([_gam().Ent.USER, user], jcount, entity['item'], i, count) if jcount == 0: - _getMain().setSysExitRC(_getMain().NO_ENTITIES_FOUND_RC) + _gam().setSysExitRC(_gam().NO_ENTITIES_FOUND_RC) return (user, svc, entityList, jcount) def _validateUserGetMessageIds(user, i, count, entity): @@ -1509,35 +1508,35 @@ def _validateUserGetMessageIds(user, i, count, entity): entityList = entity['list'] else: entityList = [] - user, gmail = _getMain().buildGAPIServiceObject(API.GMAIL, user, i, count) + user, gmail = _gam().buildGAPIServiceObject(API.GMAIL, user, i, count) if not gmail: return (user, None, None) return (user, gmail, entityList) def checkUserExists(cd, user, entityType=None, i=0, count=0): if entityType is None: - entityType = _getMain().Ent.USER - user = _getMain().normalizeEmailAddressOrUID(user) + entityType = _gam().Ent.USER + user = _gam().normalizeEmailAddressOrUID(user) try: - return _getMain().callGAPI(cd.users(), 'get', + return _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=user, fields='primaryEmail')['primaryEmail'] except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.backendError, GAPI.systemError): - _getMain().entityUnknownWarning(entityType, user, i, count) + _gam().entityUnknownWarning(entityType, user, i, count) return None def checkUserSuspended(cd, user, entityType=None, i=0, count=0): if entityType is None: - entityType = _getMain().Ent.USER - user = _getMain().normalizeEmailAddressOrUID(user) + entityType = _gam().Ent.USER + user = _gam().normalizeEmailAddressOrUID(user) try: - return _getMain().callGAPI(cd.users(), 'get', + return _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=user, fields='suspended')['suspended'] except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden, GAPI.badRequest, GAPI.backendError, GAPI.systemError): - _getMain().entityUnknownWarning(entityType, user, i, count) + _gam().entityUnknownWarning(entityType, user, i, count) return None def _getCustomerId(): @@ -1567,7 +1566,7 @@ def _getCustomersCustomerIdWithC(): def _getDomainList(cd, customer, fields): from gam.util.access import accessErrorExit, ClientAPIAccessDeniedExit try: - return _getMain().callGAPIitems(cd.domains(), 'list', 'domains', + return _gam().callGAPIitems(cd.domains(), 'list', 'domains', throwReasons=[GAPI.BAD_REQUEST, GAPI.NOT_FOUND, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], customer=customer, fields=fields) @@ -1580,9 +1579,9 @@ def setTrueCustomerId(cd=None, forceUpdate=False): from gam.util.access import ClientAPIAccessDeniedExit if GC.Values[GC.CUSTOMER_ID] == GC.MY_CUSTOMER or forceUpdate: if not cd: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) try: - customerInfo = _getMain().callGAPI(cd.customers(), 'get', + customerInfo = _gam().callGAPI(cd.customers(), 'get', throwReasons=[GAPI.BAD_REQUEST, GAPI.INVALID_INPUT, GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN, GAPI.PERMISSION_DENIED], customerKey=GC.MY_CUSTOMER, @@ -1610,7 +1609,7 @@ NOT_ME_IN_OWNERS = "not " + ME_IN_OWNERS NOT_ME_IN_OWNERS_AND = NOT_ME_IN_OWNERS + " and " def _getEntityMimeType(fileEntry): - Ent = _getMain().Ent + Ent = _gam().Ent if fileEntry['mimeType'] == MIMETYPE_GA_FOLDER: return Ent.DRIVE_FOLDER if fileEntry['mimeType'].startswith(MIMETYPE_GA_3P_SHORTCUT): @@ -1622,7 +1621,7 @@ def _getEntityMimeType(fileEntry): return Ent.DRIVE_FOLDER_SHORTCUT if fileEntry['shortcutDetails']['targetMimeType'] == MIMETYPE_GA_FOLDER else Ent.DRIVE_FILE_SHORTCUT def _getTargetEntityMimeType(fileEntry): - Ent = _getMain().Ent + Ent = _gam().Ent return Ent.DRIVE_FOLDER if fileEntry['shortcutDetails']['targetMimeType'] == MIMETYPE_GA_FOLDER else Ent.DRIVE_FILE QUERY_SHORTCUTS_MAP = { diff --git a/src/gam/util/gdoc.py b/src/gam/util/gdoc.py index 30290e11..1e0ce349 100644 --- a/src/gam/util/gdoc.py +++ b/src/gam/util/gdoc.py @@ -24,8 +24,7 @@ from gamlib import glglobals as GM from gamlib import glmsgs as Msg -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] GDOC_FORMAT_MIME_TYPES = { @@ -37,39 +36,39 @@ GDOC_FORMAT_MIME_TYPES = { # gdoc | def getGDocData(gformat): mimeType = GDOC_FORMAT_MIME_TYPES[gformat] - user = _getMain().getEmailAddress() - fileIdEntity = _getMain().getDriveFileEntity(queryShortcutsOK=False) + user = _gam().getEmailAddress() + fileIdEntity = _gam().getDriveFileEntity(queryShortcutsOK=False) if not GC.Values[GC.COMMANDDATA_CLIENTACCESS]: - _, drive = _getMain().buildGAPIServiceObject(API.DRIVE3, user) + _, drive = _gam().buildGAPIServiceObject(API.DRIVE3, user) else: - drive = _getMain().buildGAPIObject(API.DRIVE3) + drive = _gam().buildGAPIObject(API.DRIVE3) if not drive: sys.exit(GM.Globals[GM.SYSEXITRC]) - _, _, jcount = _getMain()._validateUserGetFileIDs(user, 0, 0, fileIdEntity, drive=drive) + _, _, jcount = _gam()._validateUserGetFileIDs(user, 0, 0, fileIdEntity, drive=drive) if jcount == 0: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user], Msg.NO_ENTITIES_FOUND.format(Ent.Singular(Ent.DRIVE_FILE))) + _gam().getGDocSheetDataFailedExit([Ent.USER, user], Msg.NO_ENTITIES_FOUND.format(Ent.Singular(Ent.DRIVE_FILE))) if jcount > 1: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user], Msg.MULTIPLE_ENTITIES_FOUND.format(Ent.Plural(Ent.DRIVE_FILE), jcount, ','.join(fileIdEntity['list']))) + _gam().getGDocSheetDataFailedExit([Ent.USER, user], Msg.MULTIPLE_ENTITIES_FOUND.format(Ent.Plural(Ent.DRIVE_FILE), jcount, ','.join(fileIdEntity['list']))) fileId = fileIdEntity['list'][0] f = None try: - result = _getMain().callGAPI(drive.files(), 'get', + result = _gam().callGAPI(drive.files(), 'get', throwReasons=GAPI.DRIVE_GET_THROW_REASONS, fileId=fileId, fields='name,mimeType,exportLinks', supportsAllDrives=True) # Google Doc if 'exportLinks' in result: if mimeType not in result['exportLinks']: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], Msg.INVALID_MIMETYPE.format(result['mimeType'], mimeType)) - f = TemporaryFile(mode='w+', encoding=_getMain().UTF8) + f = TemporaryFile(mode='w+', encoding=_gam().UTF8) _, content = drive._http.request(uri=result['exportLinks'][mimeType], method='GET') - f.write(content.decode(_getMain().UTF8_SIG)) + f.write(content.decode(_gam().UTF8_SIG)) f.seek(0) return f # Drive File if result['mimeType'] != mimeType: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], Msg.INVALID_MIMETYPE.format(result['mimeType'], mimeType)) fb = TemporaryFile(mode='wb+') request = drive.files().get_media(fileId=fileId) @@ -77,63 +76,63 @@ def getGDocData(gformat): done = False while not done: _, done = downloader.next_chunk() - f = TemporaryFile(mode='w+', encoding=_getMain().UTF8) + f = TemporaryFile(mode='w+', encoding=_gam().UTF8) fb.seek(0) - f.write(fb.read().decode(_getMain().UTF8_SIG)) + f.write(fb.read().decode(_gam().UTF8_SIG)) fb.close() f.seek(0) return f except GAPI.fileNotFound: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DOCUMENT, fileId], Msg.DOES_NOT_EXIST) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DOCUMENT, fileId], Msg.DOES_NOT_EXIST) except (IOError, httplib2.HttpLib2Error, google.auth.exceptions.TransportError, RuntimeError) as e: if f: f.close() - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DOCUMENT, fileId], str(e)) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DOCUMENT, fileId], str(e)) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - _getMain().userDriveServiceNotEnabledWarning(user, str(e)) + _gam().userDriveServiceNotEnabledWarning(user, str(e)) sys.exit(GM.Globals[GM.SYSEXITRC]) HTML_TITLE_PATTERN = re.compile(r'.*(.+)') # gsheet | def getGSheetData(): - user = _getMain().getEmailAddress() - fileIdEntity = _getMain().getDriveFileEntity(queryShortcutsOK=False) - sheetEntity = _getMain().getSheetEntity(False) + user = _gam().getEmailAddress() + fileIdEntity = _gam().getDriveFileEntity(queryShortcutsOK=False) + sheetEntity = _gam().getSheetEntity(False) if not GC.Values[GC.COMMANDDATA_CLIENTACCESS]: - user, drive = _getMain().buildGAPIServiceObject(API.DRIVE3, user) + user, drive = _gam().buildGAPIServiceObject(API.DRIVE3, user) else: - drive = _getMain().buildGAPIObject(API.DRIVE3) + drive = _gam().buildGAPIObject(API.DRIVE3) if not drive: sys.exit(GM.Globals[GM.SYSEXITRC]) - _, _, jcount = _getMain()._validateUserGetFileIDs(user, 0, 0, fileIdEntity, drive=drive) + _, _, jcount = _gam()._validateUserGetFileIDs(user, 0, 0, fileIdEntity, drive=drive) if jcount == 0: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user], Msg.NO_ENTITIES_FOUND.format(Ent.Singular(Ent.DRIVE_FILE))) + _gam().getGDocSheetDataFailedExit([Ent.USER, user], Msg.NO_ENTITIES_FOUND.format(Ent.Singular(Ent.DRIVE_FILE))) if jcount > 1: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user], Msg.MULTIPLE_ENTITIES_FOUND.format(Ent.Plural(Ent.DRIVE_FILE), jcount, ','.join(fileIdEntity['list']))) + _gam().getGDocSheetDataFailedExit([Ent.USER, user], Msg.MULTIPLE_ENTITIES_FOUND.format(Ent.Plural(Ent.DRIVE_FILE), jcount, ','.join(fileIdEntity['list']))) if not GC.Values[GC.COMMANDDATA_CLIENTACCESS]: - _, sheet = _getMain().buildGAPIServiceObject(API.SHEETS, user) + _, sheet = _gam().buildGAPIServiceObject(API.SHEETS, user) else: - sheet = _getMain().buildGAPIObject(API.SHEETS) + sheet = _gam().buildGAPIObject(API.SHEETS) if not sheet: sys.exit(GM.Globals[GM.SYSEXITRC]) fileId = fileIdEntity['list'][0] f = None try: - result = _getMain().callGAPI(drive.files(), 'get', + result = _gam().callGAPI(drive.files(), 'get', throwReasons=GAPI.DRIVE_GET_THROW_REASONS, fileId=fileId, fields='name,mimeType', supportsAllDrives=True) - if result['mimeType'] != _getMain().MIMETYPE_GA_SPREADSHEET: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], - Msg.INVALID_MIMETYPE.format(result['mimeType'], _getMain().MIMETYPE_GA_SPREADSHEET)) - spreadsheet = _getMain().callGAPI(sheet.spreadsheets(), 'get', + if result['mimeType'] != _gam().MIMETYPE_GA_SPREADSHEET: + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.DRIVE_FILE, result['name']], + Msg.INVALID_MIMETYPE.format(result['mimeType'], _gam().MIMETYPE_GA_SPREADSHEET)) + spreadsheet = _gam().callGAPI(sheet.spreadsheets(), 'get', throwReasons=GAPI.SHEETS_ACCESS_THROW_REASONS, spreadsheetId=fileId, fields='spreadsheetUrl,sheets(properties(sheetId,title))') - sheetId = _getMain().getSheetIdFromSheetEntity(spreadsheet, sheetEntity) + sheetId = _gam().getSheetIdFromSheetEntity(spreadsheet, sheetEntity) if sheetId is None: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], Msg.NOT_FOUND) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], Msg.NOT_FOUND) spreadsheetUrl = f'{re.sub("/edit.*$", "/export", spreadsheet["spreadsheetUrl"])}?format=csv&id={fileId}&gid={sheetId}' - f = TemporaryFile(mode='w+', encoding=_getMain().UTF8) + f = TemporaryFile(mode='w+', encoding=_gam().UTF8) if GC.Values[GC.DEBUG_LEVEL] > 0: sys.stderr.write(f'Debug: spreadsheetUrl: {spreadsheetUrl}\n') triesLimit = 3 @@ -144,25 +143,25 @@ def getGSheetData(): break tg = HTML_TITLE_PATTERN.match(content[0:600].decode('utf-8')) errMsg = tg.group(1) if tg else 'Unknown error' - _getMain().getGDocSheetDataRetryWarning([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], errMsg, n, triesLimit) + _gam().getGDocSheetDataRetryWarning([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], errMsg, n, triesLimit) time.sleep(20) else: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], errMsg) - f.write(content.decode(_getMain().UTF8_SIG)) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, result['name'], sheetEntity['sheetType'], sheetEntity['sheetValue']], errMsg) + f.write(content.decode(_gam().UTF8_SIG)) f.seek(0) return f except GAPI.fileNotFound: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId], Msg.DOES_NOT_EXIST) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId], Msg.DOES_NOT_EXIST) except (GAPI.notFound, GAPI.forbidden, GAPI.permissionDenied, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.badRequest, GAPI.invalid, GAPI.invalidArgument, GAPI.failedPrecondition) as e: - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId, sheetEntity['sheetType'], sheetEntity['sheetValue']], str(e)) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId, sheetEntity['sheetType'], sheetEntity['sheetValue']], str(e)) except (IOError, httplib2.HttpLib2Error) as e: if f: f.close() - _getMain().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId, sheetEntity['sheetType'], sheetEntity['sheetValue']], str(e)) + _gam().getGDocSheetDataFailedExit([Ent.USER, user, Ent.SPREADSHEET, fileId, sheetEntity['sheetType'], sheetEntity['sheetValue']], str(e)) except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e: - _getMain().userDriveServiceNotEnabledWarning(user, str(e)) + _gam().userDriveServiceNotEnabledWarning(user, str(e)) sys.exit(GM.Globals[GM.SYSEXITRC]) @@ -173,15 +172,15 @@ BUCKET_OBJECT_PATTERNS = [ ] def getBucketObjectName(): - Cmd = _getMain().Cmd - uri = _getMain().getString(Cmd.OB_STRING) + Cmd = _gam().Cmd + uri = _gam().getString(Cmd.OB_STRING) for pattern in BUCKET_OBJECT_PATTERNS: mg = re.search(pattern['pattern'], uri) if mg: bucket = mg.group(1) s_object = mg.group(2) if not pattern['unquote'] else unquote(mg.group(2)) return (bucket, s_object, f'{bucket}/{s_object}') - _getMain().systemErrorExit(_getMain().ACTION_NOT_PERFORMED_RC, f'Invalid : {uri}') + _gam().systemErrorExit(_gam().ACTION_NOT_PERFORMED_RC, f'Invalid : {uri}') GCS_FORMAT_MIME_TYPES = { 'gcscsv': 'text/csv', @@ -193,17 +192,17 @@ GCS_FORMAT_MIME_TYPES = { def getStorageFileData(gcsformat, returnData=True): mimeType = GCS_FORMAT_MIME_TYPES[gcsformat] bucket, s_object, bucketObject = getBucketObjectName() - s = _getMain().buildGAPIObject(API.STORAGEREAD) + s = _gam().buildGAPIObject(API.STORAGEREAD) try: - result = _getMain().callGAPI(s.objects(), 'get', + result = _gam().callGAPI(s.objects(), 'get', throwReasons=[GAPI.NOT_FOUND, GAPI.FORBIDDEN], bucket=bucket, object=s_object, projection='noAcl', fields='contentType') except GAPI.notFound: - _getMain().entityDoesNotExistExit(Ent.CLOUD_STORAGE_FILE, bucketObject) + _gam().entityDoesNotExistExit(Ent.CLOUD_STORAGE_FILE, bucketObject) except GAPI.forbidden as e: - _getMain().entityActionFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], str(e)) + _gam().entityActionFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], str(e)) if result['contentType'] != mimeType: - _getMain().getGDocSheetDataFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], + _gam().getGDocSheetDataFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], Msg.INVALID_MIMETYPE.format(result['contentType'], mimeType)) fb = TemporaryFile(mode='wb+') try: @@ -214,58 +213,58 @@ def getStorageFileData(gcsformat, returnData=True): _, done = downloader.next_chunk() fb.seek(0) if returnData: - data = fb.read().decode(_getMain().UTF8) + data = fb.read().decode(_gam().UTF8) fb.close() return data - f = TemporaryFile(mode='w+', encoding=_getMain().UTF8) - f.write(fb.read().decode(_getMain().UTF8_SIG)) + f = TemporaryFile(mode='w+', encoding=_gam().UTF8) + f.write(fb.read().decode(_gam().UTF8_SIG)) fb.close() f.seek(0) return f except googleapiclient.http.HttpError as e: - mg = _getMain().HTTP_ERROR_PATTERN.match(str(e)) - _getMain().getGDocSheetDataFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], mg.group(1) if mg else str(e)) + mg = _gam().HTTP_ERROR_PATTERN.match(str(e)) + _gam().getGDocSheetDataFailedExit([Ent.CLOUD_STORAGE_FILE, bucketObject], mg.group(1) if mg else str(e)) # def openCSVFileReader(filename, fieldnames=None): - Cmd = _getMain().Cmd + Cmd = _gam().Cmd filenameLower = filename.lower() if filenameLower == 'gsheet': f = getGSheetData() - _getMain().getCharSet() + _gam().getCharSet() elif filenameLower in {'gcsv', 'gdoc'}: f = getGDocData(filenameLower) - _getMain().getCharSet() + _gam().getCharSet() elif filenameLower in {'gcscsv', 'gcsdoc'}: f = getStorageFileData(filenameLower, False) - _getMain().getCharSet() + _gam().getCharSet() else: - encoding = _getMain().getCharSet() - filename = _getMain().setFilePath(filename, GC.INPUT_DIR) - f = _getMain().openFile(filename, mode=_getMain().DEFAULT_CSV_READ_MODE, encoding=encoding) - if _getMain().checkArgumentPresent('warnifnodata'): + encoding = _gam().getCharSet() + filename = _gam().setFilePath(filename, GC.INPUT_DIR) + f = _gam().openFile(filename, mode=_gam().DEFAULT_CSV_READ_MODE, encoding=encoding) + if _gam().checkArgumentPresent('warnifnodata'): loc = f.tell() try: if not f.readline() or not f.readline(): - _getMain().stderrWarningMsg(_getMain().fileErrorMessage(filename, Msg.NO_CSV_FILE_DATA_FOUND)) - sys.exit(_getMain().NO_ENTITIES_FOUND_RC) + _gam().stderrWarningMsg(_gam().fileErrorMessage(filename, Msg.NO_CSV_FILE_DATA_FOUND)) + sys.exit(_gam().NO_ENTITIES_FOUND_RC) f.seek(loc) except (IOError, UnicodeDecodeError, UnicodeError) as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, _getMain().fileErrorMessage(filename, e)) - if _getMain().checkArgumentPresent('columndelimiter'): - columnDelimiter = _getMain().getCharacter() + _gam().systemErrorExit(_gam().FILE_ERROR_RC, _gam().fileErrorMessage(filename, e)) + if _gam().checkArgumentPresent('columndelimiter'): + columnDelimiter = _gam().getCharacter() else: columnDelimiter = GC.Values[GC.CSV_INPUT_COLUMN_DELIMITER] - if _getMain().checkArgumentPresent('noescapechar'): - noEscapeChar = _getMain().getBoolean() + if _gam().checkArgumentPresent('noescapechar'): + noEscapeChar = _gam().getBoolean() else: noEscapeChar = GC.Values[GC.CSV_INPUT_NO_ESCAPE_CHAR] - if _getMain().checkArgumentPresent('quotechar'): - quotechar = _getMain().getCharacter() + if _gam().checkArgumentPresent('quotechar'): + quotechar = _gam().getCharacter() else: quotechar = GC.Values[GC.CSV_INPUT_QUOTE_CHAR] - if not _getMain().checkArgumentPresent('endcsv') and _getMain().checkArgumentPresent('fields'): - fieldnames = _getMain().shlexSplitList(_getMain().getString(Cmd.OB_FIELD_NAME_LIST)) + if not _gam().checkArgumentPresent('endcsv') and _gam().checkArgumentPresent('fields'): + fieldnames = _gam().shlexSplitList(_gam().getString(Cmd.OB_FIELD_NAME_LIST)) try: csvFile = csv.DictReader(f, fieldnames=fieldnames, delimiter=columnDelimiter, @@ -273,4 +272,4 @@ def openCSVFileReader(filename, fieldnames=None): quotechar=quotechar) return (f, csvFile, csvFile.fieldnames if csvFile.fieldnames is not None else []) except (csv.Error, UnicodeDecodeError, UnicodeError) as e: - _getMain().systemErrorExit(_getMain().FILE_ERROR_RC, e) + _gam().systemErrorExit(_gam().FILE_ERROR_RC, e) diff --git a/src/gam/util/orgunits.py b/src/gam/util/orgunits.py index c11c3d83..aa8834cd 100644 --- a/src/gam/util/orgunits.py +++ b/src/gam/util/orgunits.py @@ -11,12 +11,11 @@ from gamlib import glgapi as GAPI from gamlib import glglobals as GM -def _getMain(): - return sys.modules['gam'] +_gam = lambda: sys.modules['gam'] def getOrgUnitItem(pathOnly=False, absolutePath=True, cd=None): - Cmd = _getMain().Cmd + Cmd = _gam().Cmd if Cmd.ArgumentsRemaining(): path = Cmd.Current().strip() if path == 'root': @@ -24,40 +23,40 @@ def getOrgUnitItem(pathOnly=False, absolutePath=True, cd=None): if path: if pathOnly and (path.startswith('id:') or path.startswith('uid:')) and cd is not None: try: - result = _getMain().callGAPI(cd.orgunits(), 'get', + result = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=path, fields='orgUnitPath') Cmd.Advance() if absolutePath: - return _getMain().makeOrgUnitPathAbsolute(result['orgUnitPath']) - return _getMain().makeOrgUnitPathRelative(result['orgUnitPath']) + return _gam().makeOrgUnitPathAbsolute(result['orgUnitPath']) + return _gam().makeOrgUnitPathRelative(result['orgUnitPath']) except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError, GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().checkEntityAFDNEorAccessErrorExit(cd, _getMain().Ent.ORGANIZATIONAL_UNIT, path) - _getMain().invalidArgumentExit(Cmd.OB_ORGUNIT_PATH) + _gam().checkEntityAFDNEorAccessErrorExit(cd, _gam().Ent.ORGANIZATIONAL_UNIT, path) + _gam().invalidArgumentExit(Cmd.OB_ORGUNIT_PATH) Cmd.Advance() if absolutePath: - return _getMain().makeOrgUnitPathAbsolute(path) - return _getMain().makeOrgUnitPathRelative(path) - _getMain().missingArgumentExit([Cmd.OB_ORGUNIT_ITEM, Cmd.OB_ORGUNIT_PATH][pathOnly]) + return _gam().makeOrgUnitPathAbsolute(path) + return _gam().makeOrgUnitPathRelative(path) + _gam().missingArgumentExit([Cmd.OB_ORGUNIT_ITEM, Cmd.OB_ORGUNIT_PATH][pathOnly]) def getTopLevelOrgId(cd, parentOrgUnitPath): - Ent = _getMain().Ent + Ent = _gam().Ent if parentOrgUnitPath != '/': try: - result = _getMain().callGAPI(cd.orgunits(), 'get', + result = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, - customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=_getMain().encodeOrgUnitPath(_getMain().makeOrgUnitPathRelative(parentOrgUnitPath)), + customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=_gam().encodeOrgUnitPath(_gam().makeOrgUnitPathRelative(parentOrgUnitPath)), fields='orgUnitId') return result['orgUnitId'] except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError): return None except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().checkEntityAFDNEorAccessErrorExit(cd, Ent.ORGANIZATIONAL_UNIT, parentOrgUnitPath) + _gam().checkEntityAFDNEorAccessErrorExit(cd, Ent.ORGANIZATIONAL_UNIT, parentOrgUnitPath) return None try: - result = _getMain().callGAPI(cd.orgunits(), 'list', + result = _gam().callGAPI(cd.orgunits(), 'list', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath='/', type='allIncludingParent', fields='organizationUnits(orgUnitId,orgUnitPath)') @@ -68,18 +67,18 @@ def getTopLevelOrgId(cd, parentOrgUnitPath): except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError): return None except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().checkEntityAFDNEorAccessErrorExit(cd, Ent.ORGANIZATIONAL_UNIT, parentOrgUnitPath) + _gam().checkEntityAFDNEorAccessErrorExit(cd, Ent.ORGANIZATIONAL_UNIT, parentOrgUnitPath) return None def getOrgUnitId(cd=None, orgUnit=None): - Ent = _getMain().Ent + Ent = _gam().Ent if cd is None: - cd = _getMain().buildGAPIObject(API.DIRECTORY) + cd = _gam().buildGAPIObject(API.DIRECTORY) if orgUnit is None: orgUnit = getOrgUnitItem() try: if orgUnit == '/': - result = _getMain().callGAPI(cd.orgunits(), 'list', + result = _gam().callGAPI(cd.orgunits(), 'list', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath='/', type='children', fields='organizationUnits(parentOrgUnitId,parentOrgUnitPath)') @@ -89,35 +88,35 @@ def getOrgUnitId(cd=None, orgUnit=None): if topLevelOrgId: return (orgUnit, topLevelOrgId) return (orgUnit, '/') #Bogus but should never happen - result = _getMain().callGAPI(cd.orgunits(), 'get', + result = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, - customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=_getMain().encodeOrgUnitPath(_getMain().makeOrgUnitPathRelative(orgUnit)), + customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=_gam().encodeOrgUnitPath(_gam().makeOrgUnitPathRelative(orgUnit)), fields='orgUnitId,orgUnitPath') return (result['orgUnitPath'], result['orgUnitId']) except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError): - _getMain().entityDoesNotExistExit(Ent.ORGANIZATIONAL_UNIT, orgUnit) + _gam().entityDoesNotExistExit(Ent.ORGANIZATIONAL_UNIT, orgUnit) except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) def getAllParentOrgUnitsForUser(cd, user): - Ent = _getMain().Ent + Ent = _gam().Ent try: - result = _getMain().callGAPI(cd.users(), 'get', + result = _gam().callGAPI(cd.users(), 'get', throwReasons=GAPI.USER_GET_THROW_REASONS, userKey=user, fields='orgUnitPath', projection='basic') except (GAPI.userNotFound, GAPI.domainNotFound, GAPI.domainCannotUseApis, GAPI.forbidden): - _getMain().entityDoesNotExistExit(Ent.USER, user) + _gam().entityDoesNotExistExit(Ent.USER, user) except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) parentPath = result['orgUnitPath'] if parentPath == '/': orgUnitPath, orgUnitId = getOrgUnitId(cd, '/') return {orgUnitId: orgUnitPath} - parentPath = _getMain().encodeOrgUnitPath(_getMain().makeOrgUnitPathRelative(parentPath)) + parentPath = _gam().encodeOrgUnitPath(_gam().makeOrgUnitPathRelative(parentPath)) orgUnits = {} while True: try: - result = _getMain().callGAPI(cd.orgunits(), 'get', + result = _gam().callGAPI(cd.orgunits(), 'get', throwReasons=GAPI.ORGUNIT_GET_THROW_REASONS, customerId=GC.Values[GC.CUSTOMER_ID], orgUnitPath=parentPath, fields='orgUnitId,orgUnitPath,parentOrgUnitId') @@ -126,9 +125,9 @@ def getAllParentOrgUnitsForUser(cd, user): break parentPath = result['parentOrgUnitId'] except (GAPI.invalidOrgunit, GAPI.orgunitNotFound, GAPI.backendError): - _getMain().entityDoesNotExistExit(Ent.ORGANIZATIONAL_UNIT, parentPath) + _gam().entityDoesNotExistExit(Ent.ORGANIZATIONAL_UNIT, parentPath) except (GAPI.badRequest, GAPI.invalidCustomerId, GAPI.loginRequired): - _getMain().accessErrorExit(cd) + _gam().accessErrorExit(cd) return orgUnits def _getOrgunitsOrgUnitIdPath(cd, orgUnit):