1# -*- coding: utf-8 -*-

2

3"""

4***************************************************************************

5 edit.py

6 ---------------------

7 Date : May 2018

8 Copyright : (C) 2018 by Denis Rouzaud

9 Email : denis@opengis.ch

10***************************************************************************

11* *

12* This program is free software; you can redistribute it and/or modify *

13* it under the terms of the GNU General Public License as published by *

14* the Free Software Foundation; either version 2 of the License, or *

15* (at your option) any later version. *

16* *

17***************************************************************************

18"""

19

20 from builtins import object

21

22

23 class QgsEditError(Exception):

24

25 def __init__(self, value):

26 self.value = value

27

28 def __str__(self):

29 return repr(self.value)

30

31

32 class edit(object):

33

34 def __init__(self, layer):

35 self.layer = layer

36

37 def __enter__(self):

38 assert self.layer.startEditing()

39 return self.layer

40

41 def __exit__(self, ex_type, ex_value, traceback):

42 if ex_type is None:

43 if not self.layer.commitChanges():

44 raise QgsEditError(self.layer.commitErrors())

45 return True

46 else:

47 self.layer.rollBack()

48 return False