Skip to content

Commit 35fc290

Browse files
committedAug 12, 2015
[FEATURE] Add with edit(layer): to python
Example: with edit(layer): f=layer.getFeatures().next() f[0]=5 layer.updateFeature(f) This will automatically call commitChanges() in the end. If any exception occurs, it will rollBack() all the changes.
1 parent 45d4dbe commit 35fc290

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
 

‎python/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ def __hash__(self):
7272
except ImportError:
7373
pass
7474

75+
# Define a `with edit(layer)` statement
76+
77+
class edit:
78+
def __init__(self,layer):
79+
self.layer = layer
80+
81+
def __enter__(self):
82+
assert self.layer.startEditing()
83+
return self.layer
84+
85+
def __exit__(self, ex_type, ex_value, traceback):
86+
if ex_type is None:
87+
assert self.layer.commitChanges()
88+
return True
89+
else:
90+
self.layer.rollBack()
91+
return False
92+
93+
from qgis import core
94+
core.edit = edit
95+
7596

7697
def mapping_feature(feature):
7798
geom = feature.geometry()

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
5252
ADD_PYTHON_TEST(PyQgsShapefileProvider test_provider_shapefile.py)
5353
ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py)
5454
ADD_PYTHON_TEST(PyQgsVectorColorRamp test_qgsvectorcolorramp.py)
55+
ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
5556

5657
IF (NOT WIN32)
5758
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for some syntactic sugar in python
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Matthias Kuhn'
10+
__date__ = '12.8.2015'
11+
__copyright__ = 'Copyright 2015, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis
16+
17+
from utilities import (unittest,
18+
TestCase,
19+
getQgisTestApp
20+
)
21+
from qgis.core import (edit,
22+
QgsFeature,
23+
QgsGeometry,
24+
QgsVectorLayer
25+
)
26+
27+
getQgisTestApp()
28+
29+
class TestSyntacticSugar(TestCase):
30+
31+
def testEdit(self):
32+
"""Test `with edit(layer):` code"""
33+
34+
ml=QgsVectorLayer("Point?crs=epsg:4236&field=id:integer&field=value:double",
35+
"test_data", "memory")
36+
# Data as list of x, y, id, value
37+
assert ml.isValid()
38+
fields=ml.fields()
39+
40+
# Check insert
41+
with edit(ml):
42+
feat=QgsFeature(fields)
43+
feat['id']=1
44+
feat['value']=0.9
45+
assert ml.addFeature(feat)
46+
47+
assert ml.dataProvider().getFeatures().next()['value']==0.9
48+
49+
# Check update
50+
with edit(ml):
51+
f = ml.getFeatures().next()
52+
f['value']=9.9
53+
assert ml.updateFeature(f)
54+
55+
assert ml.dataProvider().getFeatures().next()['value']==9.9
56+
57+
# Check for rollBack after exceptions
58+
with self.assertRaises(NameError):
59+
with edit(ml):
60+
f = ml.getFeatures().next()
61+
f['value']=3.8
62+
crashycrash()
63+
64+
assert ml.dataProvider().getFeatures().next()['value']==9.9
65+
assert ml.getFeatures().next()['value']==9.9
66+
67+
# Check for `as`
68+
with edit(ml) as l:
69+
f = l.getFeatures().next()
70+
f['value']=10
71+
assert l.updateFeature(f)
72+
73+
assert ml.dataProvider().getFeatures().next()['value']==10
74+
75+
if __name__ == "__main__":
76+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.