darkfire514 commited on
Commit
46e3043
·
verified ·
1 Parent(s): dab7df9

Update openspace/grounding/core/quality/store.py

Browse files
openspace/grounding/core/quality/store.py CHANGED
@@ -22,6 +22,98 @@ try:
22
  except ImportError:
23
  libsql = None
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  from .types import ToolQualityRecord, ExecutionRecord, DescriptionQuality
26
  from openspace.utils.logging import Logger
27
  from openspace.config.constants import PROJECT_ROOT
@@ -90,8 +182,9 @@ class QualityStore:
90
  turso_token = os.environ.get("TURSO_AUTH_TOKEN")
91
 
92
  if turso_url and libsql is not None:
93
- self._conn = libsql.connect(turso_url, auth_token=turso_token)
94
- self._conn.row_factory = sqlite3.Row
 
95
  logger.debug(f"QualityStore ready (Turso) at {turso_url}")
96
  else:
97
  self._conn = sqlite3.connect(
 
22
  except ImportError:
23
  libsql = None
24
 
25
+ class _LibsqlCursorProxy:
26
+ def __init__(self, cursor, conn_proxy):
27
+ self._cursor = cursor
28
+ self._conn_proxy = conn_proxy
29
+
30
+ def execute(self, *args, **kwargs):
31
+ self._cursor.execute(*args, **kwargs)
32
+ return self
33
+
34
+ def executescript(self, *args, **kwargs):
35
+ self._cursor.executescript(*args, **kwargs)
36
+ return self
37
+
38
+ def fetchone(self):
39
+ row = self._cursor.fetchone()
40
+ if row is not None and self._conn_proxy.row_factory:
41
+ return self._conn_proxy.row_factory(self, row)
42
+ return row
43
+
44
+ def fetchall(self):
45
+ rows = self._cursor.fetchall()
46
+ if self._conn_proxy.row_factory:
47
+ return [self._conn_proxy.row_factory(self, row) for row in rows]
48
+ return rows
49
+
50
+ @property
51
+ def description(self):
52
+ return getattr(self._cursor, "description", [])
53
+
54
+ @property
55
+ def rowcount(self):
56
+ return getattr(self._cursor, "rowcount", -1)
57
+
58
+ @property
59
+ def lastrowid(self):
60
+ return getattr(self._cursor, "lastrowid", None)
61
+
62
+ class _LibsqlConnectionProxy:
63
+ def __init__(self, conn):
64
+ self._conn = conn
65
+ self.row_factory = None
66
+
67
+ def execute(self, *args, **kwargs):
68
+ cursor = self.cursor()
69
+ return cursor.execute(*args, **kwargs)
70
+
71
+ def executescript(self, *args, **kwargs):
72
+ cursor = self.cursor()
73
+ return cursor.executescript(*args, **kwargs)
74
+
75
+ def cursor(self):
76
+ return _LibsqlCursorProxy(self._conn.cursor(), self)
77
+
78
+ def commit(self):
79
+ if hasattr(self._conn, "commit"):
80
+ return self._conn.commit()
81
+
82
+ def rollback(self):
83
+ if hasattr(self._conn, "rollback"):
84
+ return self._conn.rollback()
85
+
86
+ def close(self):
87
+ if hasattr(self._conn, "close"):
88
+ return self._conn.close()
89
+
90
+ class _RowProxy:
91
+ def __init__(self, row, description):
92
+ self._row = row
93
+ self._description = description
94
+ self._col_map = {col[0]: idx for idx, col in enumerate(description)}
95
+
96
+ def __getitem__(self, item):
97
+ if isinstance(item, int):
98
+ return self._row[item]
99
+ if item in self._col_map:
100
+ return self._row[self._col_map[item]]
101
+ raise KeyError(item)
102
+
103
+ def keys(self):
104
+ return self._col_map.keys()
105
+
106
+ def __iter__(self):
107
+ return iter(self._row)
108
+
109
+ def __len__(self):
110
+ return len(self._row)
111
+
112
+ def _dict_factory(cursor, row):
113
+ if hasattr(cursor, "description") and cursor.description:
114
+ return _RowProxy(row, cursor.description)
115
+ return row
116
+
117
  from .types import ToolQualityRecord, ExecutionRecord, DescriptionQuality
118
  from openspace.utils.logging import Logger
119
  from openspace.config.constants import PROJECT_ROOT
 
182
  turso_token = os.environ.get("TURSO_AUTH_TOKEN")
183
 
184
  if turso_url and libsql is not None:
185
+ raw_conn = libsql.connect(turso_url, auth_token=turso_token)
186
+ self._conn = _LibsqlConnectionProxy(raw_conn)
187
+ self._conn.row_factory = _dict_factory
188
  logger.debug(f"QualityStore ready (Turso) at {turso_url}")
189
  else:
190
  self._conn = sqlite3.connect(