mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-28 06:51:16 +08:00
refactor: simplify scope validation helpers in google workspace scripts
Fix double file read bug in google_api.py _missing_scopes(), consolidate redundant _normalize_scope_values into callers, merge duplicate except blocks.
This commit is contained in:
@@ -44,25 +44,15 @@ SCOPES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def _load_token_payload() -> dict:
|
|
||||||
try:
|
|
||||||
return json.loads(TOKEN_PATH.read_text())
|
|
||||||
except Exception:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_scope_values(values) -> set[str]:
|
|
||||||
if not values:
|
|
||||||
return set()
|
|
||||||
if isinstance(values, str):
|
|
||||||
values = values.split()
|
|
||||||
return {str(value).strip() for value in values if str(value).strip()}
|
|
||||||
|
|
||||||
|
|
||||||
def _missing_scopes() -> list[str]:
|
def _missing_scopes() -> list[str]:
|
||||||
granted = _normalize_scope_values(_load_token_payload().get("scopes") or _load_token_payload().get("scope"))
|
try:
|
||||||
if not granted:
|
payload = json.loads(TOKEN_PATH.read_text())
|
||||||
|
except Exception:
|
||||||
return []
|
return []
|
||||||
|
raw = payload.get("scopes") or payload.get("scope")
|
||||||
|
if not raw:
|
||||||
|
return []
|
||||||
|
granted = {s.strip() for s in (raw.split() if isinstance(raw, str) else raw) if s.strip()}
|
||||||
return sorted(scope for scope in SCOPES if scope not in granted)
|
return sorted(scope for scope in SCOPES if scope not in granted)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -56,24 +56,15 @@ REDIRECT_URI = "http://localhost:1"
|
|||||||
def _load_token_payload(path: Path = TOKEN_PATH) -> dict:
|
def _load_token_payload(path: Path = TOKEN_PATH) -> dict:
|
||||||
try:
|
try:
|
||||||
return json.loads(path.read_text())
|
return json.loads(path.read_text())
|
||||||
except FileNotFoundError:
|
|
||||||
return {}
|
|
||||||
except Exception:
|
except Exception:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def _normalize_scope_values(values) -> set[str]:
|
|
||||||
if not values:
|
|
||||||
return set()
|
|
||||||
if isinstance(values, str):
|
|
||||||
values = values.split()
|
|
||||||
return {str(value).strip() for value in values if str(value).strip()}
|
|
||||||
|
|
||||||
|
|
||||||
def _missing_scopes_from_payload(payload: dict) -> list[str]:
|
def _missing_scopes_from_payload(payload: dict) -> list[str]:
|
||||||
granted = _normalize_scope_values(payload.get("scopes") or payload.get("scope"))
|
raw = payload.get("scopes") or payload.get("scope")
|
||||||
if not granted:
|
if not raw:
|
||||||
return []
|
return []
|
||||||
|
granted = {s.strip() for s in (raw.split() if isinstance(raw, str) else raw) if s.strip()}
|
||||||
return sorted(scope for scope in SCOPES if scope not in granted)
|
return sorted(scope for scope in SCOPES if scope not in granted)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user