jinruiyang Claude Opus 4.5 commited on
Commit ·
6319ca0
1
Parent(s): d14d8e4
Fix gspread import - move to module level
Browse filesCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- ir/sheets_storage.py +18 -3
ir/sheets_storage.py
CHANGED
|
@@ -11,6 +11,16 @@ from datetime import datetime
|
|
| 11 |
from typing import Optional, Dict, Any, List
|
| 12 |
import logging
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Google Sheets configuration
|
|
@@ -25,13 +35,16 @@ _spreadsheet = None
|
|
| 25 |
|
| 26 |
def _get_credentials():
|
| 27 |
"""Get Google credentials from environment variable."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
creds_json = os.environ.get("GOOGLE_SHEETS_CREDENTIALS")
|
| 29 |
if not creds_json:
|
| 30 |
logger.warning("GOOGLE_SHEETS_CREDENTIALS not set")
|
| 31 |
return None
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
from google.oauth2.service_account import Credentials
|
| 35 |
creds_dict = json.loads(creds_json)
|
| 36 |
scopes = [
|
| 37 |
"https://www.googleapis.com/auth/spreadsheets",
|
|
@@ -50,8 +63,10 @@ def _get_spreadsheet():
|
|
| 50 |
if _spreadsheet is not None:
|
| 51 |
return _spreadsheet
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
-
import gspread
|
| 55 |
creds = _get_credentials()
|
| 56 |
if creds is None:
|
| 57 |
return None
|
|
@@ -168,7 +183,7 @@ def save_notes_to_sheets(
|
|
| 168 |
|
| 169 |
def is_sheets_enabled() -> bool:
|
| 170 |
"""Check if Google Sheets storage is configured and working."""
|
| 171 |
-
return os.environ.get("GOOGLE_SHEETS_CREDENTIALS") is not None
|
| 172 |
|
| 173 |
|
| 174 |
def test_connection() -> Dict[str, Any]:
|
|
|
|
| 11 |
from typing import Optional, Dict, Any, List
|
| 12 |
import logging
|
| 13 |
|
| 14 |
+
# Import gspread at module level
|
| 15 |
+
try:
|
| 16 |
+
import gspread
|
| 17 |
+
from google.oauth2.service_account import Credentials
|
| 18 |
+
GSPREAD_AVAILABLE = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
GSPREAD_AVAILABLE = False
|
| 21 |
+
gspread = None
|
| 22 |
+
Credentials = None
|
| 23 |
+
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
# Google Sheets configuration
|
|
|
|
| 35 |
|
| 36 |
def _get_credentials():
|
| 37 |
"""Get Google credentials from environment variable."""
|
| 38 |
+
if not GSPREAD_AVAILABLE:
|
| 39 |
+
logger.warning("gspread not installed")
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
creds_json = os.environ.get("GOOGLE_SHEETS_CREDENTIALS")
|
| 43 |
if not creds_json:
|
| 44 |
logger.warning("GOOGLE_SHEETS_CREDENTIALS not set")
|
| 45 |
return None
|
| 46 |
|
| 47 |
try:
|
|
|
|
| 48 |
creds_dict = json.loads(creds_json)
|
| 49 |
scopes = [
|
| 50 |
"https://www.googleapis.com/auth/spreadsheets",
|
|
|
|
| 63 |
if _spreadsheet is not None:
|
| 64 |
return _spreadsheet
|
| 65 |
|
| 66 |
+
if not GSPREAD_AVAILABLE:
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
try:
|
|
|
|
| 70 |
creds = _get_credentials()
|
| 71 |
if creds is None:
|
| 72 |
return None
|
|
|
|
| 183 |
|
| 184 |
def is_sheets_enabled() -> bool:
|
| 185 |
"""Check if Google Sheets storage is configured and working."""
|
| 186 |
+
return GSPREAD_AVAILABLE and os.environ.get("GOOGLE_SHEETS_CREDENTIALS") is not None
|
| 187 |
|
| 188 |
|
| 189 |
def test_connection() -> Dict[str, Any]:
|