From a3f9cdc3032e4b0b5cd43c34a6221168efb9f710 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Sat, 4 Jul 2026 05:57:12 -0400 Subject: [PATCH] more fixes for phase 3 --- src/gam/util/api.py | 13 -------- src/gam/util/args.py | 4 --- src/gam/util/display.py | 73 ++--------------------------------------- src/gam/util/errors.py | 59 ++++----------------------------- src/gam/util/fileio.py | 12 ++----- 5 files changed, 10 insertions(+), 151 deletions(-) diff --git a/src/gam/util/api.py b/src/gam/util/api.py index 36a17c1e..c99078a6 100644 --- a/src/gam/util/api.py +++ b/src/gam/util/api.py @@ -67,13 +67,6 @@ _DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds -def _getEnt(): - return sys.modules['gam'].Ent - -def _getInd(): - return sys.modules['gam'].Ind - - def handleServerError(e): errMsg = str(e) if 'setting tls' not in errMsg: @@ -164,7 +157,6 @@ def transportCreateRequest(httpObj=None): return transportAgentRequest(httpObj) def doGAMCheckForUpdates(forceCheck): - Ind = _getInd() def _gamLatestVersionNotAvailable(): if forceCheck: systemErrorExit(NETWORK_ERROR_RC, Msg.GAM_LATEST_VERSION_NOT_AVAILABLE) @@ -273,7 +265,6 @@ class signjwtSignJwt(google.auth.crypt.Signer): def handleOAuthTokenError(e, softErrors, displayError=False, i=0, count=0): from util.access import APIAccessDeniedExit, ClientAPIAccessDeniedExit, SvcAcctAPIAccessDeniedExit - Ent = _getEnt() errMsg = str(e).replace('.', '') if ((errMsg in API.OAUTH2_TOKEN_ERRORS) or errMsg.startswith('Invalid response') or @@ -923,7 +914,6 @@ def callGDataPages(service, function, softErrors=False, throwErrors=None, retryErrors=None, uri=None, **kwargs): - Ent = _getEnt() if throwErrors is None: throwErrors = [] if retryErrors is None: @@ -1227,7 +1217,6 @@ def callGAPI(service, function, systemErrorExit(GOOGLE_API_ERROR_RC, str(e)) def _showGAPIpagesResult(results, pageItems, totalItems, pageMessage, messageAttribute, entityType): - Ent = _getEnt() showMessage = pageMessage.replace(TOTAL_ITEMS_MARKER, str(totalItems)) if pageItems: if messageAttribute: @@ -1302,7 +1291,6 @@ def callGAPIpages(service, function, items, throwReasons=None, retryReasons=None, pageArgsInBody=False, **kwargs): - Ent = _getEnt() if throwReasons is None: throwReasons = [] if retryReasons is None: @@ -1335,7 +1323,6 @@ def yieldGAPIpages(service, function, items, throwReasons=None, retryReasons=None, pageArgsInBody=False, **kwargs): - Ent = _getEnt() if throwReasons is None: throwReasons = [] if retryReasons is None: diff --git a/src/gam/util/args.py b/src/gam/util/args.py index bfdb547f..c5a3f23d 100644 --- a/src/gam/util/args.py +++ b/src/gam/util/args.py @@ -102,10 +102,6 @@ from util.errors import ( from util.fileio import readFile from gam.var import Cmd, Ent -# Lazy accessor for Ent (runtime instance) -def _getEnt(): - return sys.modules['gam'].Ent - # Lazy accessor for main module # --- Constants duplicated from __init__.py --- diff --git a/src/gam/util/display.py b/src/gam/util/display.py index f1419449..2719ff33 100644 --- a/src/gam/util/display.py +++ b/src/gam/util/display.py @@ -13,6 +13,7 @@ from gamlib import glmsgs as Msg from gam.var import Act, Ent, Ind +from util.args import escapeCRsNLs from util.output import ( currentCountNL, formatKeyValueList, @@ -35,40 +36,26 @@ LAST_ITEM_MARKER = '%%last_item%%' TOTAL_ITEMS_MARKER = '%%total_items%%' -def _getEnt(): - """Get the Ent instance from the main module (lazy accessor).""" - return sys.modules['gam'].Ent - - -def _getEscapeCRsNLs(): - """Get the escapeCRsNLs function from the main module (lazy accessor).""" - return sys.modules['gam'].escapeCRsNLs - # --- Warnings --- def badRequestWarning(entityType, itemType, itemValue): - Ent = _getEnt() printWarningMessage(BAD_REQUEST_RC, f'{Msg.GOT} 0 {Ent.Plural(entityType)}: {Msg.INVALID} {Ent.Singular(itemType)} - {itemValue}') def emptyQuery(query, entityType): - Ent = _getEnt() return f'{Ent.Singular(Ent.QUERY)} ({query}) {Msg.NO_ENTITIES_FOUND.format(Ent.Plural(entityType))}' def invalidQuery(query): - Ent = _getEnt() return f'{Ent.Singular(Ent.QUERY)} ({query}) {Msg.INVALID}' def invalidMember(query): - Ent = _getEnt() if query: badRequestWarning(Ent.GROUP, Ent.QUERY, invalidQuery(query)) return True return False def invalidUserSchema(schema): - Ent = _getEnt() if isinstance(schema, list): return f'{Ent.Singular(Ent.USER_SCHEMA)} ({",".join(schema)}) {Msg.INVALID}' return f'{Ent.Singular(Ent.USER_SCHEMA)} {schema}) {Msg.INVALID}' @@ -77,7 +64,6 @@ def invalidUserSchema(schema): # --- Service Not Enabled Warnings --- def userServiceNotEnabledWarning(entityName, service, i=0, count=0): - Ent = _getEnt() setSysExitRC(SERVICE_NOT_APPLICABLE_RC) writeStderr(formatKeyValueList(Ind.Spaces(), [Ent.Singular(Ent.USER), entityName, Msg.SERVICE_NOT_ENABLED.format(service)], @@ -104,7 +90,7 @@ def userDriveServiceNotEnabledWarning(user, errMessage, i=0, count=0): if errMessage.find('Drive apps') >= 0 or errMessage.find('Active session is invalid') >= 0: userServiceNotEnabledWarning(user, 'Drive', i, count) else: - entityActionNotPerformedWarning([_getEnt().USER, user], errMessage, i, count) + entityActionNotPerformedWarning([Ent.USER, user], errMessage, i, count) def userKeepServiceNotEnabledWarning(entityName, i=0, count=0): userServiceNotEnabledWarning(entityName, 'Keep', i, count) @@ -128,70 +114,60 @@ def userYouTubeServiceNotEnabledWarning(entityName, i=0, count=0): # --- Entity Warning Functions --- def entityServiceNotApplicableWarning(entityType, entityName, i=0, count=0): - Ent = _getEnt() setSysExitRC(SERVICE_NOT_APPLICABLE_RC) writeStderr(formatKeyValueList(Ind.Spaces(), [Ent.Singular(entityType), entityName, Msg.SERVICE_NOT_APPLICABLE], currentCountNL(i, count))) def entityDoesNotExistWarning(entityType, entityName, i=0, count=0): - Ent = _getEnt() setSysExitRC(ENTITY_DOES_NOT_EXIST_RC) writeStderr(formatKeyValueList(Ind.Spaces(), [Ent.Singular(entityType), entityName, Msg.DOES_NOT_EXIST], currentCountNL(i, count))) def entityListDoesNotExistWarning(entityValueList, i=0, count=0): - Ent = _getEnt() setSysExitRC(ENTITY_DOES_NOT_EXIST_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Msg.DOES_NOT_EXIST], currentCountNL(i, count))) def entityDoesNotHaveItemWarning(entityValueList, i=0, count=0): - Ent = _getEnt() setSysExitRC(ENTITY_DOES_NOT_EXIST_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Msg.DOES_NOT_EXIST], currentCountNL(i, count))) def entityDuplicateWarning(entityValueList, i=0, count=0): - Ent = _getEnt() setSysExitRC(ENTITY_DUPLICATE_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.Failed(), Msg.DUPLICATE], currentCountNL(i, count))) def entityActionFailedWarning(entityValueList, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_FAILED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.Failed(), errMessage], currentCountNL(i, count))) def entityModifierItemValueListActionFailedWarning(entityValueList, modifier, infoTypeValueList, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_FAILED_RC) writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', None]+Ent.FormatEntityValueList(infoTypeValueList)+[Act.Failed(), errMessage], currentCountNL(i, count))) def entityModifierActionFailedWarning(entityValueList, modifier, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_FAILED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', Act.Failed(), errMessage], currentCountNL(i, count))) def entityModifierNewValueActionFailedWarning(entityValueList, modifier, newValue, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_FAILED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', newValue, Act.Failed(), errMessage], currentCountNL(i, count))) def entityNumEntitiesActionFailedWarning(entityType, entityName, itemType, itemCount, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_FAILED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), [Ent.Singular(entityType), entityName, @@ -200,33 +176,28 @@ def entityNumEntitiesActionFailedWarning(entityType, entityName, itemType, itemC currentCountNL(i, count))) def entityActionNotPerformedWarning(entityValueList, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_NOT_PERFORMED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.NotPerformed(), errMessage], currentCountNL(i, count))) def entityItemValueListActionNotPerformedWarning(entityValueList, infoTypeValueList, errMessage, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.NotPerformed(), '']+Ent.FormatEntityValueList(infoTypeValueList)+[errMessage], currentCountNL(i, count))) def entityModifierItemValueListActionNotPerformedWarning(entityValueList, modifier, infoTypeValueList, errMessage, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.NotPerformed()} {modifier}', None]+Ent.FormatEntityValueList(infoTypeValueList)+[errMessage], currentCountNL(i, count))) def entityNumEntitiesActionNotPerformedWarning(entityValueList, itemType, itemCount, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_NOT_PERFORMED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Ent.Choose(itemType, itemCount), itemCount, Act.NotPerformed(), errMessage], currentCountNL(i, count))) def entityBadRequestWarning(entityValueList, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(BAD_REQUEST_RC) writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[ERROR, errMessage], @@ -236,7 +207,6 @@ def entityBadRequestWarning(entityValueList, errMessage, i=0, count=0): # --- Getting / Paging Display --- def printGettingAllAccountEntities(entityType, query='', qualifier='', accountType=None): - Ent = _getEnt() if accountType is None: accountType = Ent.ACCOUNT if GC.Values[GC.SHOW_GETTINGS]: @@ -249,12 +219,10 @@ def printGettingAllAccountEntities(entityType, query='', qualifier='', accountTy writeStderr(f'{Msg.GETTING_ALL} {Ent.PluralGetting()}{Ent.GettingPreQualifier()}{Ent.MayTakeTime(accountType)}\n') def printGotAccountEntities(count): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: writeStderr(f'{Msg.GOT} {count} {Ent.ChooseGetting(count)}{Ent.GettingPostQualifier()}\n') def setGettingAllEntityItemsForWhom(entityItem, forWhom, query='', qualifier=''): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: if query: Ent.SetGettingQuery(entityItem, query) @@ -265,30 +233,25 @@ def setGettingAllEntityItemsForWhom(entityItem, forWhom, query='', qualifier='') Ent.SetGettingForWhom(forWhom) def printGettingAllEntityItemsForWhom(entityItem, forWhom, i=0, count=0, query='', qualifier='', entityType=None): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: setGettingAllEntityItemsForWhom(entityItem, forWhom, query=query, qualifier=qualifier) writeStderr(f'{Msg.GETTING_ALL} {Ent.PluralGetting()}{Ent.GettingPreQualifier()} {Msg.FOR} {forWhom}{Ent.MayTakeTime(entityType)}{currentCountNL(i, count)}') def printGotEntityItemsForWhom(count): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: writeStderr(f'{Msg.GOT} {count} {Ent.ChooseGetting(count)}{Ent.GettingPostQualifier()} {Msg.FOR} {Ent.GettingForWhom()}\n') def printGettingEntityItem(entityType, entityItem, i=0, count=0): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: writeStderr(f'{Msg.GETTING} {Ent.Singular(entityType)} {entityItem}{currentCountNL(i, count)}') def printGettingEntityItemForWhom(entityItem, forWhom, i=0, count=0): - Ent = _getEnt() if GC.Values[GC.SHOW_GETTINGS]: Ent.SetGetting(entityItem) Ent.SetGettingForWhom(forWhom) writeStderr(f'{Msg.GETTING} {Ent.PluralGetting()} {Msg.FOR} {forWhom}{currentCountNL(i, count)}') def stderrEntityMessage(entityValueList, message, i=0, count=0): - Ent = _getEnt() writeStderr(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[message], currentCountNL(i, count))) @@ -310,7 +273,6 @@ def getPageMessage(showFirstLastItems=False, showDate=None): return pageMessage def getPageMessageForWhom(forWhom=None, showFirstLastItems=False, showDate=None, clearLastGotMsgLen=True): - Ent = _getEnt() if not GC.Values[GC.SHOW_GETTINGS]: return None if forWhom: @@ -348,7 +310,6 @@ def printKeyValueDict(kvDict): writeStdout(formatKeyValueList(Ind.Spaces(), [key, value], '\n')) def printKeyValueWithCRsNLs(key, value): - escapeCRsNLs = _getEscapeCRsNLs() if value.find('\n') >= 0 or value.find('\r') >= 0: if GC.Values[GC.SHOW_CONVERT_CR_NL]: printKeyValueList([key, escapeCRsNLs(value)]) @@ -367,25 +328,21 @@ def printJSONValue(value): writeStdout(formatKeyValueList(' ', [value], '\n')) def printEntity(entityValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList), currentCountNL(i, count))) def printEntityMessage(entityValueList, message, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[message], currentCountNL(i, count))) def printEntitiesCount(entityType, entityList): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), [Ent.Plural(entityType), None if entityList is None else f'({len(entityList)})'], '\n')) def printEntityKVList(entityValueList, infoKVList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+infoKVList, currentCountNL(i, count))) @@ -394,111 +351,93 @@ def printEntityKVList(entityValueList, infoKVList, i=0, count=0): # --- performAction / entityPerformAction --- def performAction(entityType, entityValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), [f'{Act.ToPerform()} {Ent.Singular(entityType)} {entityValue}'], currentCountNL(i, count))) def performActionNumItems(itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), [f'{Act.ToPerform()} {itemCount} {Ent.Choose(itemType, itemCount)}'], currentCountNL(i, count))) def performActionModifierNumItems(modifier, itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), [f'{Act.ToPerform()} {modifier} {itemCount} {Ent.Choose(itemType, itemCount)}'], currentCountNL(i, count))) def actionPerformedNumItems(itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStderr(formatKeyValueList(Ind.Spaces(), [f'{itemCount} {Ent.Choose(itemType, itemCount)} {Act.Performed()} '], currentCountNL(i, count))) def actionFailedNumItems(itemCount, itemType, errMessage, i=0, count=0): - Ent = _getEnt() writeStderr(formatKeyValueList(Ind.Spaces(), [f'{itemCount} {Ent.Choose(itemType, itemCount)} {Act.Failed()}: {errMessage} '], currentCountNL(i, count))) def actionNotPerformedNumItemsWarning(itemCount, itemType, errMessage, i=0, count=0): - Ent = _getEnt() setSysExitRC(ACTION_NOT_PERFORMED_RC) writeStderr(formatKeyValueList(Ind.Spaces(), [Ent.Choose(itemType, itemCount), itemCount, Act.NotPerformed(), errMessage], currentCountNL(i, count))) def entityPerformAction(entityValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()}'], currentCountNL(i, count))) def entityPerformActionNumItems(entityValueList, itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {itemCount} {Ent.Choose(itemType, itemCount)}'], currentCountNL(i, count))) def entityPerformActionModifierNumItems(entityValueList, modifier, itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier} {itemCount} {Ent.Choose(itemType, itemCount)}'], currentCountNL(i, count))) def entityPerformActionNumItemsModifier(entityValueList, itemCount, itemType, modifier, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {itemCount} {Ent.Choose(itemType, itemCount)} {modifier}'], currentCountNL(i, count))) def entityPerformActionSubItemModifierNumItems(entityValueList, subitemType, modifier, itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {Ent.Plural(subitemType)} {modifier} {itemCount} {Ent.Choose(itemType, itemCount)}'], currentCountNL(i, count))) def entityPerformActionSubItemModifierNumItemsModifierNewValue(entityValueList, subitemType, modifier1, itemCount, itemType, modifier2, newValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+ [f'{Act.ToPerform()} {Ent.Plural(subitemType)} {modifier1} {itemCount} {Ent.Choose(itemType, itemCount)} {modifier2}', newValue], currentCountNL(i, count))) def entityPerformActionModifierNumItemsModifier(entityValueList, modifier1, itemCount, itemType, modifier2, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier1} {itemCount} {Ent.Choose(itemType, itemCount)} {modifier2}'], currentCountNL(i, count))) def entityPerformActionModifierItemValueList(entityValueList, modifier, infoTypeValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', None]+Ent.FormatEntityValueList(infoTypeValueList), currentCountNL(i, count))) def entityPerformActionModifierNewValue(entityValueList, modifier, newValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', newValue], currentCountNL(i, count))) def entityPerformActionModifierNewValueItemValueList(entityValueList, modifier, newValue, infoTypeValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.ToPerform()} {modifier}', newValue]+Ent.FormatEntityValueList(infoTypeValueList), currentCountNL(i, count))) def entityPerformActionItemValue(entityValueList, itemType, itemValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.ToPerform(), None, Ent.Singular(itemType), itemValue], currentCountNL(i, count))) def entityPerformActionInfo(entityValueList, infoValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.ToPerform(), infoValue], currentCountNL(i, count))) @@ -507,13 +446,11 @@ def entityPerformActionInfo(entityValueList, infoValue, i=0, count=0): # --- entityActionPerformed / entityModifier --- def entityActionPerformed(entityValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.Performed()], currentCountNL(i, count))) def entityActionPerformedMessage(entityValueList, message, i=0, count=0): - Ent = _getEnt() if message: writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.Performed(), message], @@ -524,37 +461,31 @@ def entityActionPerformedMessage(entityValueList, message, i=0, count=0): currentCountNL(i, count))) def entityNumItemsActionPerformed(entityValueList, itemCount, itemType, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{itemCount} {Ent.Choose(itemType, itemCount)} {Act.Performed()}'], currentCountNL(i, count))) def entityModifierActionPerformed(entityValueList, modifier, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.Performed()} {modifier}', None], currentCountNL(i, count))) def entityModifierItemValueListActionPerformed(entityValueList, modifier, infoTypeValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.Performed()} {modifier}', None]+Ent.FormatEntityValueList(infoTypeValueList), currentCountNL(i, count))) def entityModifierNewValueActionPerformed(entityValueList, modifier, newValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.Performed()} {modifier}', newValue], currentCountNL(i, count))) def entityModifierNewValueItemValueListActionPerformed(entityValueList, modifier, newValue, infoTypeValueList, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.Performed()} {modifier}', newValue]+Ent.FormatEntityValueList(infoTypeValueList), currentCountNL(i, count))) def entityModifierNewValueKeyValueActionPerformed(entityValueList, modifier, newValue, infoKey, infoValue, i=0, count=0): - Ent = _getEnt() writeStdout(formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[f'{Act.Performed()} {modifier}', newValue, infoKey, infoValue], currentCountNL(i, count))) diff --git a/src/gam/util/errors.py b/src/gam/util/errors.py index 41f1074b..f97f5fad 100644 --- a/src/gam/util/errors.py +++ b/src/gam/util/errors.py @@ -9,6 +9,12 @@ from gamlib import glmsgs as Msg from gam.var import Act, Cmd, Ent, Ind +from gam.constants import ( + ACTION_FAILED_RC, CLIENT_SECRETS_JSON_REQUIRED_RC, + ENTITY_DOES_NOT_EXIST_RC, ENTITY_IS_NOT_UNIQUE_RC, + INVALID_JSON_RC, OAUTH2_TXT_REQUIRED_RC, + OAUTH2SERVICE_JSON_REQUIRED_RC, USAGE_ERROR_RC, +) from util.output import ( currentCountNL, formatKeyValueList, @@ -18,87 +24,40 @@ from util.output import ( writeStderr, ) -# Lazy accessor for Ent (runtime instance) -def _getEnt(): - return sys.modules['gam'].Ent - -# Constants imported from __init__.py at module level via lazy accessor -def _getConst(name): - return getattr(sys.modules['gam'], name) - -# --- Return code constants (imported lazily) --- -ACTION_FAILED_RC = None -CLIENT_SECRETS_JSON_REQUIRED_RC = None -ENTITY_DOES_NOT_EXIST_RC = None -ENTITY_IS_NOT_UNIQUE_RC = None -INVALID_JSON_RC = None -OAUTH2_TXT_REQUIRED_RC = None -OAUTH2SERVICE_JSON_REQUIRED_RC = None -USAGE_ERROR_RC = None - -def _ensureConstants(): - global ACTION_FAILED_RC, CLIENT_SECRETS_JSON_REQUIRED_RC, ENTITY_DOES_NOT_EXIST_RC - global ENTITY_IS_NOT_UNIQUE_RC, INVALID_JSON_RC, OAUTH2_TXT_REQUIRED_RC - global OAUTH2SERVICE_JSON_REQUIRED_RC, USAGE_ERROR_RC - if ACTION_FAILED_RC is None: - m = sys.modules['gam'] - ACTION_FAILED_RC = m.ACTION_FAILED_RC - CLIENT_SECRETS_JSON_REQUIRED_RC = m.CLIENT_SECRETS_JSON_REQUIRED_RC - ENTITY_DOES_NOT_EXIST_RC = m.ENTITY_DOES_NOT_EXIST_RC - ENTITY_IS_NOT_UNIQUE_RC = m.ENTITY_IS_NOT_UNIQUE_RC - INVALID_JSON_RC = m.INVALID_JSON_RC - OAUTH2_TXT_REQUIRED_RC = m.OAUTH2_TXT_REQUIRED_RC - OAUTH2SERVICE_JSON_REQUIRED_RC = m.OAUTH2SERVICE_JSON_REQUIRED_RC - USAGE_ERROR_RC = m.USAGE_ERROR_RC - # --- Credential file errors --- def invalidClientSecretsJsonExit(errMsg): - _ensureConstants() - Ent = _getEnt() stderrErrorMsg(Msg.DOES_NOT_EXIST_OR_HAS_INVALID_FORMAT.format(Ent.Singular(Ent.CLIENT_SECRETS_JSON_FILE), GC.Values[GC.CLIENT_SECRETS_JSON], errMsg)) writeStderr(Msg.INSTRUCTIONS_CLIENT_SECRETS_JSON) systemErrorExit(CLIENT_SECRETS_JSON_REQUIRED_RC, None) def invalidOauth2serviceJsonExit(errMsg): - _ensureConstants() - Ent = _getEnt() stderrErrorMsg(Msg.DOES_NOT_EXIST_OR_HAS_INVALID_FORMAT.format(Ent.Singular(Ent.OAUTH2SERVICE_JSON_FILE), GC.Values[GC.OAUTH2SERVICE_JSON], errMsg)) writeStderr(Msg.INSTRUCTIONS_OAUTH2SERVICE_JSON) systemErrorExit(OAUTH2SERVICE_JSON_REQUIRED_RC, None) def invalidOauth2TxtExit(errMsg): - _ensureConstants() - Ent = _getEnt() stderrErrorMsg(Msg.DOES_NOT_EXIST_OR_HAS_INVALID_FORMAT.format(Ent.Singular(Ent.OAUTH2_TXT_FILE), GC.Values[GC.OAUTH2_TXT], errMsg)) writeStderr(Msg.EXECUTE_GAM_OAUTH_CREATE) systemErrorExit(OAUTH2_TXT_REQUIRED_RC, None) def expiredRevokedOauth2TxtExit(): - _ensureConstants() - Ent = _getEnt() stderrErrorMsg(Msg.IS_EXPIRED_OR_REVOKED.format(Ent.Singular(Ent.OAUTH2_TXT_FILE), GC.Values[GC.OAUTH2_TXT])) writeStderr(Msg.EXECUTE_GAM_OAUTH_CREATE) systemErrorExit(OAUTH2_TXT_REQUIRED_RC, None) def invalidDiscoveryJsonExit(fileName, errMsg): - _ensureConstants() - Ent = _getEnt() stderrErrorMsg(Msg.DOES_NOT_EXIST_OR_HAS_INVALID_FORMAT.format(Ent.Singular(Ent.DISCOVERY_JSON_FILE), fileName, errMsg)) systemErrorExit(INVALID_JSON_RC, None) # --- Entity error exits --- def entityActionFailedExit(entityValueList, errMsg, i=0, count=0): - _ensureConstants() - Ent = _getEnt() systemErrorExit(ACTION_FAILED_RC, formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.Failed(), errMsg], currentCountNL(i, count))) def entityDoesNotExistExit(entityType, entityName, i=0, count=0, errMsg=None): - _ensureConstants() - Ent = _getEnt() Cmd.Backup() writeStderr(Cmd.CommandLineWithBadArgumentMarked(False)) systemErrorExit(ENTITY_DOES_NOT_EXIST_RC, formatKeyValueList(Ind.Spaces(), @@ -106,8 +65,6 @@ def entityDoesNotExistExit(entityType, entityName, i=0, count=0, errMsg=None): currentCountNL(i, count))) def entityDoesNotHaveItemExit(entityValueList, i=0, count=0): - _ensureConstants() - Ent = _getEnt() Cmd.Backup() writeStderr(Cmd.CommandLineWithBadArgumentMarked(False)) systemErrorExit(ENTITY_DOES_NOT_EXIST_RC, formatKeyValueList(Ind.Spaces(), @@ -115,8 +72,6 @@ def entityDoesNotHaveItemExit(entityValueList, i=0, count=0): currentCountNL(i, count))) def entityIsNotUniqueExit(entityType, entityName, valueType, valueList, i=0, count=0): - _ensureConstants() - Ent = _getEnt() Cmd.Backup() writeStderr(Cmd.CommandLineWithBadArgumentMarked(False)) systemErrorExit(ENTITY_IS_NOT_UNIQUE_RC, formatKeyValueList(Ind.Spaces(), @@ -126,7 +81,6 @@ def entityIsNotUniqueExit(entityType, entityName, valueType, valueList, i=0, cou # --- Usage/argument errors --- def usageErrorExit(message, extraneous=False): - _ensureConstants() writeStderr(Cmd.CommandLineWithBadArgumentMarked(extraneous)) stderrErrorMsg(message) FN_GAMCOMMANDS_TXT = _getConst('FN_GAMCOMMANDS_TXT') @@ -178,7 +132,6 @@ def deprecatedArgumentExit(argument): usageErrorExit(f'{Cmd.ARGUMENT_ERROR_NAMES[Cmd.ARGUMENT_DEPRECATED][1]}: <{argument}>') def deprecatedCommandExit(): - _ensureConstants() systemErrorExit(USAGE_ERROR_RC, Msg.SITES_COMMAND_DEPRECATED.format(Cmd.CommandDeprecated())) # Choices is the valid set of choices that was expected diff --git a/src/gam/util/fileio.py b/src/gam/util/fileio.py index ed8b1ed3..2e1e3525 100644 --- a/src/gam/util/fileio.py +++ b/src/gam/util/fileio.py @@ -48,10 +48,6 @@ WARNING_PREFIX = WARNING + ': ' _ENT_FILE = glentity.GamEntity.FILE -def _getEnt(): - """Get the Ent instance from the main module (lazy accessor).""" - return sys.modules['gam'].Ent - def cleanFilename(filename): return sanitize_filename(filename, '_') @@ -82,7 +78,6 @@ def cleanFilepath(filepath): return sanitize_filepath(filepath, platform='auto') def fileErrorMessage(filename, e, entityType=_ENT_FILE): - Ent = _getEnt() return f'{Ent.Singular(entityType)}: {filename}, {str(e)}' @@ -209,7 +204,6 @@ def deleteFile(filename, continueOnError=False, displayError=True): systemErrorExit(FILE_ERROR_RC, fileErrorMessage(filename, e)) def getGDocSheetDataRetryWarning(entityValueList, errMsg, i=0, count=0): - Ent = _getEnt() action = Act.Get() Act.Set(Act.RETRIEVE_DATA) stderrWarningMsg(formatKeyValueList(Ind.Spaces(), @@ -218,7 +212,6 @@ def getGDocSheetDataRetryWarning(entityValueList, errMsg, i=0, count=0): Act.Set(action) def getGDocSheetDataFailedExit(entityValueList, errMsg, i=0, count=0): - Ent = _getEnt() Act.Set(Act.RETRIEVE_DATA) systemErrorExit(ACTION_FAILED_RC, formatKeyValueList(Ind.Spaces(), Ent.FormatEntityValueList(entityValueList)+[Act.NotPerformed(), errMsg], @@ -265,9 +258,8 @@ def openGAMCommandLog(Globals, name): systemErrorExit(CONFIG_ERROR_RC, Msg.LOGGING_INITIALIZATION_ERROR.format(str(e))) def writeGAMCommandLog(Globals, logCmd, sysRC): - import sys as _sys # noqa: PLC0415 - gam = _sys.modules['gam'] - Globals[GM.CMDLOG_LOGGER].info(f'{gam.currentISOformatTimeStamp()},{sysRC},{logCmd}') + from util.args import currentISOformatTimeStamp + Globals[GM.CMDLOG_LOGGER].info(f'{currentISOformatTimeStamp()},{sysRC},{logCmd}') def closeGAMCommandLog(Globals): try: