h2gis 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of h2gis might be problematic. Click here for more details.

h2gis/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .h2gisconnector import H2gisConnector
2
+
3
+ __all__ = ["H2gisConnector"]
4
+
@@ -0,0 +1,170 @@
1
+ import ctypes
2
+ import os
3
+ import platform
4
+
5
+
6
+ # --- Opaque struct placeholders for GraalVM native API ---
7
+ class GraalIsolate(ctypes.Structure):
8
+ _fields_ = []
9
+
10
+
11
+ class GraalIsolateThread(ctypes.Structure):
12
+ _fields_ = []
13
+
14
+
15
+ class GraalIsolateParams(ctypes.Structure):
16
+ _fields_ = []
17
+
18
+
19
+ GraalIsolate_p = ctypes.POINTER(GraalIsolate)
20
+ GraalIsolateThread_p = ctypes.POINTER(GraalIsolateThread)
21
+
22
+
23
+ class H2gisConnector:
24
+ """
25
+ Python wrapper for the native H2GIS library compiled with GraalVM.
26
+ """
27
+
28
+ def __init__(self, lib_path=None):
29
+ """
30
+ Initialize the H2GIS connector and create a GraalVM isolate.
31
+
32
+ :param lib_path: Optional path to the native .so/.dll/.dylib library
33
+ """
34
+ if lib_path is None:
35
+ lib_path = self._default_library_path()
36
+
37
+ self.lib = ctypes.CDLL(lib_path)
38
+ self._setup_c_function_signatures()
39
+
40
+ self.isolate = GraalIsolate_p()
41
+ self.thread = GraalIsolateThread_p()
42
+ self.connection = 0
43
+
44
+ params = GraalIsolateParams()
45
+ result = self.lib.graal_create_isolate(
46
+ ctypes.byref(params),
47
+ ctypes.byref(self.isolate),
48
+ ctypes.byref(self.thread)
49
+ )
50
+ if result != 0:
51
+ raise RuntimeError(f"Failed to create GraalVM isolate (code {result})")
52
+
53
+ def _default_library_path(self):
54
+ """Find default library path relative to this file."""
55
+ base_dir = os.path.dirname(__file__)
56
+ system = platform.system()
57
+ if system == "Windows":
58
+ libname = "h2gis.dll"
59
+ else:
60
+ libname = "h2gis.so"
61
+
62
+ return os.path.join(base_dir, "lib", libname)
63
+
64
+ def _setup_c_function_signatures(self):
65
+ """Declare argument and return types for native functions."""
66
+ self.lib.graal_create_isolate.argtypes = [
67
+ ctypes.POINTER(GraalIsolateParams),
68
+ ctypes.POINTER(GraalIsolate_p),
69
+ ctypes.POINTER(GraalIsolateThread_p),
70
+ ]
71
+ self.lib.graal_create_isolate.restype = ctypes.c_int
72
+
73
+ self.lib.graal_tear_down_isolate.argtypes = [GraalIsolateThread_p]
74
+ self.lib.graal_tear_down_isolate.restype = ctypes.c_int
75
+
76
+ self.lib.h2gis_connect.argtypes = [
77
+ GraalIsolateThread_p,
78
+ ctypes.c_char_p,
79
+ ctypes.c_char_p,
80
+ ctypes.c_char_p,
81
+ ]
82
+ self.lib.h2gis_connect.restype = ctypes.c_long
83
+
84
+ self.lib.h2gis_execute_update.argtypes = [
85
+ GraalIsolateThread_p,
86
+ ctypes.c_long,
87
+ ctypes.c_char_p,
88
+ ]
89
+ self.lib.h2gis_execute_update.restype = ctypes.c_int
90
+
91
+ self.lib.h2gis_execute.argtypes = [
92
+ GraalIsolateThread_p,
93
+ ctypes.c_long,
94
+ ctypes.c_char_p,
95
+ ]
96
+ self.lib.h2gis_execute.restype = ctypes.c_long
97
+
98
+ self.lib.h2gis_fetch_row.argtypes = [
99
+ GraalIsolateThread_p,
100
+ ctypes.c_long,
101
+ ]
102
+ self.lib.h2gis_fetch_row.restype = ctypes.c_char_p
103
+
104
+ self.lib.h2gis_close_query.argtypes = [
105
+ GraalIsolateThread_p,
106
+ ctypes.c_long,
107
+ ]
108
+ self.lib.h2gis_close_query.restype = None
109
+
110
+ self.lib.h2gis_close_connection.argtypes = [
111
+ GraalIsolateThread_p,
112
+ ctypes.c_long,
113
+ ]
114
+ self.lib.h2gis_close_connection.restype = None
115
+
116
+ def connect(self, db_file: str, username="sa", password=""):
117
+ """Connect to a H2GIS database file."""
118
+ self.connection = self.lib.h2gis_connect(
119
+ self.thread,
120
+ db_file.encode("utf-8"),
121
+ username.encode("utf-8"),
122
+ password.encode("utf-8"),
123
+ )
124
+ if self.connection == 0:
125
+ raise RuntimeError("Failed to connect to H2GIS database.")
126
+
127
+ def execute(self, sql: str) -> int:
128
+ """Execute INSERT/UPDATE/DELETE statement and return affected row count."""
129
+ return self.lib.h2gis_execute_update(
130
+ self.thread,
131
+ self.connection,
132
+ sql.encode("utf-8"),
133
+ )
134
+
135
+ def fetch(self, sql: str) -> list[str]:
136
+ """Execute SELECT statement and return list of rows."""
137
+ handle = self.lib.h2gis_execute(
138
+ self.thread,
139
+ self.connection,
140
+ sql.encode("utf-8"),
141
+ )
142
+ if handle == 0:
143
+ raise RuntimeError("Query execution failed")
144
+
145
+ rows = []
146
+ while True:
147
+ row = self.lib.h2gis_fetch_row(self.thread, handle)
148
+ if not row:
149
+ break
150
+ rows.append(row.decode("utf-8"))
151
+
152
+ self.lib.h2gis_close_query(self.thread, handle)
153
+ return rows
154
+
155
+ def close(self):
156
+ """Close the database connection."""
157
+ if self.connection:
158
+ self.lib.h2gis_close_connection(self.thread, self.connection)
159
+ self.connection = 0
160
+
161
+ def __del__(self):
162
+ """Destructor to clean up isolate and resources."""
163
+ try:
164
+ self.close()
165
+ except Exception:
166
+ pass
167
+ try:
168
+ self.lib.graal_tear_down_isolate(self.thread)
169
+ except Exception:
170
+ pass
h2gis/lib/h2gis.dll ADDED
Binary file
h2gis/lib/h2gis.so ADDED
Binary file
@@ -0,0 +1,32 @@
1
+ Metadata-Version: 2.4
2
+ Name: h2gis
3
+ Version: 0.0.1
4
+ Summary: A Python library to use an H2GIS database through a native GraalVM bridge.
5
+ Home-page: https://github.com/yourusername/h2gis
6
+ Author: Lemap
7
+ Author-email: you@example.com
8
+ License: LGPL-3.0
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.7
13
+ Description-Content-Type: text/markdown
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: license
21
+ Dynamic: requires-python
22
+ Dynamic: summary
23
+
24
+ # h2gis-python
25
+
26
+ A lib thal allows to interract with a h2gis lib. Do not forget when you create an H2gisConnection object to put the lib.so full path as a parameter. Without this, it will not work.
27
+
28
+ To insall run :
29
+ ```
30
+ python setup.py bdist_wheel
31
+ pip install dist/h2gis-0.0.1-py3-none-any.whl --force-reinstall
32
+ ```
@@ -0,0 +1,8 @@
1
+ h2gis/__init__.py,sha256=gP0ROK44u2DgEBG_fUr7ruseel-ep9Y7iH7F6S5dvwc,74
2
+ h2gis/h2gisconnector.py,sha256=-OfAM7JCbiTx6SR-UXeZpZhJJKcyqoHZbsrob-uVvOE,5114
3
+ h2gis/lib/h2gis.dll,sha256=oi8N-Li8nK2UbIsWizLmpjKx6Oa8pyA6pnZl97f5cgc,68620288
4
+ h2gis/lib/h2gis.so,sha256=f32Rz1MCpN-0_OJaqqWdpD5Lbxy7C0hS0MkwzJLK3oQ,68792104
5
+ h2gis-0.0.1.dist-info/METADATA,sha256=vUIQ2k0Kgb7AFLEGeg1eOp1K52JLDV7S26H3sB80Mbk,996
6
+ h2gis-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ h2gis-0.0.1.dist-info/top_level.txt,sha256=FDWZmvmOrdCecn3GWaYs7N_SrzvMlUVf6smIelL1_E0,6
8
+ h2gis-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ h2gis