Skip to content
Permalink
Browse files

[pyqgis] add method to retrieve metaEnum from an enum value or type (#…

  • Loading branch information
3nids committed May 18, 2018
1 parent ae89b4f commit deccf205cfc50f2b8570c9ce186a85225985b7b2
@@ -28,6 +28,7 @@

from .additions.edit import edit, QgsEditError
from .additions.fromfunction import fromFunction
from .additions.metaenum import metaEnumFromType, metaEnumFromValue
from .additions.processing import processing_output_layer_repr, processing_source_repr
from .additions.projectdirtyblocker import ProjectDirtyBlocker
from .additions.qgsdefaultvalue import _isValid
@@ -52,25 +53,25 @@
# -----------------
# DO NOT EDIT BELOW
# These are automatically added by calling sipify.pl script
QgsTolerance.UnitType.parentClass = lambda: QgsTolerance
QgsAuthManager.MessageLevel.parentClass = lambda: QgsAuthManager
QgsDataItem.Type.parentClass = lambda: QgsDataItem
QgsDataItem.State.parentClass = lambda: QgsDataItem
QgsLayerItem.LayerType.parentClass = lambda: QgsLayerItem
QgsDataProvider.DataCapability.parentClass = lambda: QgsDataProvider
QgsDataSourceUri.SslMode.parentClass = lambda: QgsDataSourceUri
QgsNetworkContentFetcherRegistry.FetchingMode.parentClass = lambda: QgsNetworkContentFetcherRegistry
QgsSnappingConfig.SnappingMode.parentClass = lambda: QgsSnappingConfig
QgsSnappingConfig.SnappingType.parentClass = lambda: QgsSnappingConfig
QgsUnitTypes.DistanceUnit.parentClass = lambda: QgsUnitTypes
QgsUnitTypes.AreaUnit.parentClass = lambda: QgsUnitTypes
QgsUnitTypes.AngleUnit.parentClass = lambda: QgsUnitTypes
QgsUnitTypes.RenderUnit.parentClass = lambda: QgsUnitTypes
QgsUnitTypes.LayoutUnit.parentClass = lambda: QgsUnitTypes
QgsVectorSimplifyMethod.SimplifyHint.parentClass = lambda: QgsVectorSimplifyMethod
QgsVectorSimplifyMethod.SimplifyAlgorithm.parentClass = lambda: QgsVectorSimplifyMethod
QgsRasterProjector.Precision.parentClass = lambda: QgsRasterProjector
QgsAbstractGeometry.SegmentationToleranceType.parentClass = lambda: QgsAbstractGeometry
QgsGeometry.BufferSide.parentClass = lambda: QgsGeometry
QgsGeometry.EndCapStyle.parentClass = lambda: QgsGeometry
QgsGeometry.JoinStyle.parentClass = lambda: QgsGeometry
QgsTolerance.UnitType.baseClass = QgsTolerance
QgsAuthManager.MessageLevel.baseClass = QgsAuthManager
QgsDataItem.Type.baseClass = QgsDataItem
QgsDataItem.State.baseClass = QgsDataItem
QgsLayerItem.LayerType.baseClass = QgsLayerItem
QgsDataProvider.DataCapability.baseClass = QgsDataProvider
QgsDataSourceUri.SslMode.baseClass = QgsDataSourceUri
QgsNetworkContentFetcherRegistry.FetchingMode.baseClass = QgsNetworkContentFetcherRegistry
QgsSnappingConfig.SnappingMode.baseClass = QgsSnappingConfig
QgsSnappingConfig.SnappingType.baseClass = QgsSnappingConfig
QgsUnitTypes.DistanceUnit.baseClass = QgsUnitTypes
QgsUnitTypes.AreaUnit.baseClass = QgsUnitTypes
QgsUnitTypes.AngleUnit.baseClass = QgsUnitTypes
QgsUnitTypes.RenderUnit.baseClass = QgsUnitTypes
QgsUnitTypes.LayoutUnit.baseClass = QgsUnitTypes
QgsVectorSimplifyMethod.SimplifyHint.baseClass = QgsVectorSimplifyMethod
QgsVectorSimplifyMethod.SimplifyAlgorithm.baseClass = QgsVectorSimplifyMethod
QgsRasterProjector.Precision.baseClass = QgsRasterProjector
QgsAbstractGeometry.SegmentationToleranceType.baseClass = QgsAbstractGeometry
QgsGeometry.BufferSide.baseClass = QgsGeometry
QgsGeometry.EndCapStyle.baseClass = QgsGeometry
QgsGeometry.JoinStyle.baseClass = QgsGeometry
@@ -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
@@ -29,13 +29,13 @@
# -----------------
# DO NOT EDIT BELOW
# These are automatically added by calling sipify.pl script
QgsAuthSettingsWidget.WarningType.parentClass = lambda: QgsAuthSettingsWidget
QgsColorButton.Behavior.parentClass = lambda: QgsColorButton
QgsColorTextWidget.ColorTextFormat.parentClass = lambda: QgsColorTextWidget
QgsFilterLineEdit.ClearMode.parentClass = lambda: QgsFilterLineEdit
QgsFloatingWidget.AnchorPoint.parentClass = lambda: QgsFloatingWidget
QgsFontButton.Mode.parentClass = lambda: QgsFontButton
QgsMapToolIdentify.IdentifyMode.parentClass = lambda: QgsMapToolIdentify
QgsAttributeTableFilterModel.FilterMode.parentClass = lambda: QgsAttributeTableFilterModel
QgsAttributeTableFilterModel.ColumnType.parentClass = lambda: QgsAttributeTableFilterModel
QgsDualView.ViewMode.parentClass = lambda: QgsDualView
QgsAuthSettingsWidget.WarningType.baseClass = QgsAuthSettingsWidget
QgsColorButton.Behavior.baseClass = QgsColorButton
QgsColorTextWidget.ColorTextFormat.baseClass = QgsColorTextWidget
QgsFilterLineEdit.ClearMode.baseClass = QgsFilterLineEdit
QgsFloatingWidget.AnchorPoint.baseClass = QgsFloatingWidget
QgsFontButton.Mode.baseClass = QgsFontButton
QgsMapToolIdentify.IdentifyMode.baseClass = QgsMapToolIdentify
QgsAttributeTableFilterModel.FilterMode.baseClass = QgsAttributeTableFilterModel
QgsAttributeTableFilterModel.ColumnType.baseClass = QgsAttributeTableFilterModel
QgsDualView.ViewMode.baseClass = QgsDualView
@@ -614,7 +614,7 @@ sub detect_non_method_member{
}
if ($LINE =~ m/Q_ENUM\(\s*(\w+)\s*\)/ ){
if ($LINE !~ m/SIP_SKIP/){
my $enum_helper = "$ACTUAL_CLASS.$1.parentClass = lambda: $ACTUAL_CLASS";
my $enum_helper = "$ACTUAL_CLASS.$1.baseClass = $ACTUAL_CLASS";
dbg_info("Q_ENUM $enum_helper");
if ($python_output ne ''){
my $pl;
@@ -8,6 +8,7 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
ENDIF (WITH_SERVER)

ADD_PYTHON_TEST(PyCoreAdittions test_core_additions.py)
ADD_PYTHON_TEST(PyQgsActionManager test_qgsactionmanager.py)
ADD_PYTHON_TEST(PyQgsAFSProvider test_provider_afs.py)
ADD_PYTHON_TEST(PyQgsAggregateCalculator test_qgsaggregatecalculator.py)
@@ -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()

0 comments on commit deccf20

Please sign in to comment.
You can’t perform that action at this time.