Skip to content
Permalink
Browse files

Remove qgsfunction from qgis.utils.

Import from qgis.core so we don't break API
  • Loading branch information
NathanW2 committed Nov 29, 2014
1 parent e14b7e4 commit 7942325fd758b384353189926aa3ab660c8b5476
Showing with 1 addition and 57 deletions.
  1. +1 −57 python/utils.py
@@ -29,7 +29,7 @@
"""

from PyQt4.QtCore import QCoreApplication, QLocale
from qgis.core import QGis, QgsExpression, QgsMessageLog
from qgis.core import QGis, QgsExpression, QgsMessageLog, qgsfunction
from string import Template
import sys
import traceback
@@ -412,62 +412,6 @@ def closeProjectMacro():
mod.closeProject()


def qgsfunction(args, group, **kwargs):
"""
Decorator function used to define a user expression function.
Custom functions should take (values, feature, parent) as args,
they can also shortcut naming feature and parent args by using *args
if they are not needed in the function.
Functions should return a value compatible with QVariant
Eval errors can be raised using parent.setEvalErrorString()
Functions must be unregistered when no longer needed using
QgsExpression.unregisterFunction
Example:
@qgsfunction(2, 'test'):
def add(values, feature, parent):
pass
Will create and register a function in QgsExpression called 'add' in the
'test' group that takes two arguments.
or not using feature and parent:
@qgsfunction(2, 'test'):
def add(values, *args):
pass
"""
helptemplate = Template("""<h3>$name function</h3><br>$doc""")

class QgsExpressionFunction(QgsExpression.Function):
def __init__(self, name, args, group, helptext='', usesgeometry=False):
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry)

def func(self, values, feature, parent):
pass

def wrapper(func):
name = kwargs.get('name', func.__name__)
usesgeometry = kwargs.get('usesgeometry', False)
help = func.__doc__ or ''
help = help.strip()
if args == 0 and not name[0] == '$':
name = '${0}'.format(name)
func.__name__ = name
help = helptemplate.safe_substitute(name=name, doc=help)
f = QgsExpressionFunction(name, args, group, help, usesgeometry)
f.func = func
register = kwargs.get('register', True)
if register:
QgsExpression.registerFunction(f)
return f

return wrapper

#######################
# SERVER PLUGINS
#

0 comments on commit 7942325

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