Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[pyqgis] add method to retrieve metaEnum from an enum value or type (#…
- Loading branch information
Showing
6 changed files
with
130 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
metaenum.py | ||
--------------------- | ||
Date : May 2018 | ||
Copyright : (C) 2018 by Denis Rouzaud | ||
Email : denis@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
|
||
def metaEnumFromValue(enumValue, raiseException=True, baseClass=None): | ||
return metaEnumFromType(enumValue.__class__, raiseException, baseClass) | ||
|
||
|
||
def metaEnumFromType(enumClass, raiseException=True, baseClass=None): | ||
if enumClass == int: | ||
if raiseException: | ||
raise TypeError("enumClass is an int, while it should be an enum") | ||
else: | ||
return None | ||
|
||
if baseClass is None: | ||
try: | ||
baseClass = enumClass.baseClass | ||
return metaEnumFromType(enumClass, raiseException, baseClass) | ||
except AttributeError: | ||
if raiseException: | ||
raise ValueError("Enum type does not implement baseClass method. Provide the base class as argument.") | ||
|
||
try: | ||
idx = baseClass.staticMetaObject.indexOfEnumerator(enumClass.__name__) | ||
meta_enum = baseClass.staticMetaObject.enumerator(idx) | ||
except AttributeError: | ||
if raiseException: | ||
raise TypeError("could not get the metaEnum for {}".format(enumClass.__name__)) | ||
meta_enum = None | ||
|
||
return meta_enum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for core additions | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Denis Rouzaud' | ||
__date__ = '15.5.2018' | ||
__copyright__ = 'Copyright 2015, The QGIS Project' | ||
# This will get replaced with a git SHA1 when you do a git archive | ||
__revision__ = '$Format:%H$' | ||
|
||
import qgis # NOQA | ||
import os | ||
|
||
from qgis.testing import unittest, start_app | ||
from qgis.core import metaEnumFromValue, metaEnumFromType, QgsTolerance, QgsMapLayer | ||
import sip | ||
|
||
start_app() | ||
|
||
|
||
class TestCoreAdditions(unittest.TestCase): | ||
|
||
def testEnum(self): | ||
me = metaEnumFromValue(QgsTolerance.Pixels) | ||
self.assertIsNotNone(me) | ||
self.assertEqual(me.valueToKey(QgsTolerance.Pixels), 'Pixels') | ||
|
||
# if using same variable twice (e.g. me = me2), this seg faults | ||
me2 = metaEnumFromValue(QgsTolerance.Pixels, True, QgsTolerance) | ||
self.assertIsNotNone(me) | ||
self.assertEqual(me2.valueToKey(QgsTolerance.Pixels), 'Pixels') | ||
|
||
# do not provide an int | ||
with self.assertRaises(TypeError): | ||
metaEnumFromValue(1) | ||
|
||
# QgsMapLayer.LayerType is not a Q_ENUM | ||
with self.assertRaises(ValueError): | ||
metaEnumFromValue(QgsMapLayer.LayerType) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |