Skip to content

Commit f8f72dc

Browse files
committed
Write qgis_resources according to current extension schema
1 parent 0382c1e commit f8f72dc

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

examples/small_world.gpkg

-324 KB
Binary file not shown.

qgis_plugin/qgpkg/qgpkg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def info(self):
8585
pass
8686

8787
try:
88-
rows = list(cur.execute('''SELECT name, type FROM qgis_resources'''))
88+
rows = list(cur.execute('''SELECT name, mime_type FROM qgis_resources'''))
8989
if len(rows) > 0:
9090
print("QGIS recources:")
9191
for row in rows:
92-
print(row['name'] + row['type'])
92+
print(row['name'] + ' (%s)' % row['mime_type'])
9393
except sqlite3.Error:
9494
pass
9595

qgis_plugin/qgpkg/qgpkg_qgis.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sqlite3
88
import tempfile
9+
import mimetypes
910
import logging
1011
from qgis.core import QgsProject
1112
from PyQt4.QtCore import QFileInfo
@@ -122,14 +123,15 @@ def write(self, project_path):
122123
except sqlite3.OperationalError:
123124
self.c.execute(
124125
"""CREATE TABLE IF NOT EXISTS qgis_resources
125-
(name TEXT, type TEXT, blob BLOB)""")
126+
(name TEXT, mime_type TEXT, content BLOB)""")
126127
for image in images:
127128
with open(image, 'rb') as input_file:
128129
blob = input_file.read()
129-
name, type = os.path.splitext(os.path.basename(image))
130+
name = os.path.basename(image)
131+
mime_type = mimetypes.MimeTypes().guess_type(name)[0]
130132
self.conn.execute(
131133
"""INSERT INTO qgis_resources \
132-
VALUES(?, ?, ?)""", (name, type, sqlite3.Binary(blob)))
134+
VALUES(?, ?, ?)""", (name, mime_type, sqlite3.Binary(blob)))
133135
self.log(logging.DEBUG, u"Image %s was saved" % name)
134136
self.conn.commit()
135137

@@ -195,11 +197,10 @@ def read(self, gpkg_path):
195197

196198
# and the image will be saved in the same folder as the project
197199
if images:
198-
self.c.execute("SELECT name, type, blob FROM qgis_resources")
200+
self.c.execute("SELECT name, mime_type, content FROM qgis_resources")
199201
images = self.c.fetchall()
200202
for img in images:
201-
name, type, blob = img
202-
img_name = name + type
203+
img_name, mime_type, blob = img
203204
img_path = os.path.join(tmp_folder, img_name)
204205
with open(img_path, 'wb') as file:
205206
file.write(blob)

qgisgpkg/qgpkg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def info(self):
8585
pass
8686

8787
try:
88-
rows = list(cur.execute('''SELECT name, type FROM qgis_resources'''))
88+
rows = list(cur.execute('''SELECT name, mime_type FROM qgis_resources'''))
8989
if len(rows) > 0:
9090
print("QGIS recources:")
9191
for row in rows:
92-
print(row['name'] + row['type'])
92+
print(row['name'] + ' (%s)' % row['mime_type'])
9393
except sqlite3.Error:
9494
pass
9595

qgisgpkg/qgpkg_qgis.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sqlite3
88
import tempfile
9+
import mimetypes
910
import logging
1011
from qgis.core import QgsProject
1112
from PyQt4.QtCore import QFileInfo
@@ -122,14 +123,15 @@ def write(self, project_path):
122123
except sqlite3.OperationalError:
123124
self.c.execute(
124125
"""CREATE TABLE IF NOT EXISTS qgis_resources
125-
(name TEXT, type TEXT, blob BLOB)""")
126+
(name TEXT, mime_type TEXT, content BLOB)""")
126127
for image in images:
127128
with open(image, 'rb') as input_file:
128129
blob = input_file.read()
129-
name, type = os.path.splitext(os.path.basename(image))
130+
name = os.path.basename(image)
131+
mime_type = mimetypes.MimeTypes().guess_type(name)[0]
130132
self.conn.execute(
131133
"""INSERT INTO qgis_resources \
132-
VALUES(?, ?, ?)""", (name, type, sqlite3.Binary(blob)))
134+
VALUES(?, ?, ?)""", (name, mime_type, sqlite3.Binary(blob)))
133135
self.log(logging.DEBUG, u"Image %s was saved" % name)
134136
self.conn.commit()
135137

@@ -195,11 +197,10 @@ def read(self, gpkg_path):
195197

196198
# and the image will be saved in the same folder as the project
197199
if images:
198-
self.c.execute("SELECT name, type, blob FROM qgis_resources")
200+
self.c.execute("SELECT name, mime_type, content FROM qgis_resources")
199201
images = self.c.fetchall()
200202
for img in images:
201-
name, type, blob = img
202-
img_name = name + type
203+
img_name, mime_type, blob = img
203204
img_path = os.path.join(tmp_folder, img_name)
204205
with open(img_path, 'wb') as file:
205206
file.write(blob)

tests/data/qgis.png

-8.28 KB
Loading

tests/test_readwrite.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_write():
4848
QGIS projects:
4949
small_world.qgs
5050
QGIS recources:
51-
qgis.png"""
51+
qgis.png (image/png)"""
5252
assert output == info
5353

5454
conn = sqlite3.connect(gpkg_path)
@@ -58,7 +58,7 @@ def test_write():
5858
assert_equals('small_world.qgs', curs.fetchone()[0], 'small_world.qgs not found')
5959

6060
curs.execute('SELECT name FROM qgis_resources')
61-
assert_equals('qgis', curs.fetchone()[0], 'Image not found')
61+
assert_equals('qgis.png', curs.fetchone()[0], 'Image not found')
6262

6363
curs.execute("SELECT scope FROM gpkg_extensions WHERE extension_name = 'qgis'")
6464
assert_equals('read-write', curs.fetchone()[0], 'Extension registration missing')

0 commit comments

Comments
 (0)