Skip to content

Commit

Permalink
Remove qgsfunction from qgis.utils.
Browse files Browse the repository at this point in the history
Import from qgis.core so we don't break API
  • Loading branch information
NathanW2 committed Nov 29, 2014
1 parent e14b7e4 commit 7942325
Showing 1 changed file with 1 addition and 57 deletions.
58 changes: 1 addition & 57 deletions python/utils.py
Expand Up @@ -29,7 +29,7 @@
""" """


from PyQt4.QtCore import QCoreApplication, QLocale 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 from string import Template
import sys import sys
import traceback import traceback
Expand Down Expand Up @@ -412,62 +412,6 @@ def closeProjectMacro():
mod.closeProject() 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 # SERVER PLUGINS
# #
Expand Down

0 comments on commit 7942325

Please sign in to comment.