From 53599211992e0fe0462770c84f33217fede49ba3 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:00:21 +0530 Subject: [PATCH] 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. --- .../google-workspace/scripts/google_api.py | 24 ++++++------------- .../google-workspace/scripts/setup.py | 15 +++--------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/skills/productivity/google-workspace/scripts/google_api.py b/skills/productivity/google-workspace/scripts/google_api.py index 207f8c7374..2a5c662a65 100644 --- a/skills/productivity/google-workspace/scripts/google_api.py +++ b/skills/productivity/google-workspace/scripts/google_api.py @@ -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]: - granted = _normalize_scope_values(_load_token_payload().get("scopes") or _load_token_payload().get("scope")) - if not granted: + try: + payload = json.loads(TOKEN_PATH.read_text()) + except Exception: 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) diff --git a/skills/productivity/google-workspace/scripts/setup.py b/skills/productivity/google-workspace/scripts/setup.py index be27e1d355..52a07427df 100644 --- a/skills/productivity/google-workspace/scripts/setup.py +++ b/skills/productivity/google-workspace/scripts/setup.py @@ -56,24 +56,15 @@ REDIRECT_URI = "http://localhost:1" def _load_token_payload(path: Path = TOKEN_PATH) -> dict: try: return json.loads(path.read_text()) - except FileNotFoundError: - return {} 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_from_payload(payload: dict) -> list[str]: - granted = _normalize_scope_values(payload.get("scopes") or payload.get("scope")) - if not granted: + 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)