find_spatialite() utility function, closes #135

This commit is contained in:
Simon Willison 2020-08-21 13:30:02 -07:00
commit bf4c6b7c82
3 changed files with 38 additions and 0 deletions

View file

@ -1298,3 +1298,23 @@ For example:
# [age] INTEGER,
# [thumbnail] BLOB
# )
.. _find_spatialite:
Finding SpatiaLite
==================
The ``find_spatialite()`` function searches for the `SpatiaLite <https://www.gaia-gis.it/fossil/libspatialite/index>`__ SQLite extension in some common places. It returns a string path to the location, or ``None`` if SpatiaLite was not found.
You can use it in code like this:
.. code-block:: python
from sqlite_utils import Database
from sqlite_utils.utils import find_spatialite
db = Database("mydb.db")
spatialite = find_spatialite()
if spatialite:
db.conn.enable_load_extension(True)
db.conn.load_extension(spatialite)

View file

@ -1,4 +1,5 @@
import base64
import os
try:
import pysqlite3 as sqlite3
@ -10,6 +11,11 @@ except ImportError:
OperationalError = sqlite3.OperationalError
SPATIALITE_PATHS = (
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
"/usr/local/lib/mod_spatialite.dylib",
)
def suggest_column_types(records):
all_column_types = {}
@ -74,3 +80,10 @@ def decode_base64_values(doc):
if not to_fix:
return doc
return dict(doc, **{k: base64.b64decode(doc[k]["encoded"]) for k in to_fix})
def find_spatialite():
for path in SPATIALITE_PATHS:
if os.path.exists(path):
return path
return None

View file

@ -20,3 +20,8 @@ def test_decode_base64_values(input, expected, should_be_is):
assert actual is input
else:
assert actual == expected
def test_find_spatialite():
spatialite = utils.find_spatialite()
assert spatialite is None or isinstance(spatialite, str)