mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
Allows for adding on the fly functions to the expression engine. Functions are saved in qgis2\python\expressions. New qgis.user module in Python. The qgis.user.expressions package points to the qgis2\python\expressions package in the users home
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import os
|
|
import sys
|
|
import glob
|
|
|
|
from qgis.core import QgsApplication
|
|
|
|
def load_user_expressions(path):
|
|
"""
|
|
Load all user expressions from the given paths
|
|
"""
|
|
#Loop all py files and import them
|
|
modules = glob.glob(path + "/*.py")
|
|
names = [os.path.basename(f)[:-3] for f in modules]
|
|
for name in names:
|
|
if name == "__init__":
|
|
continue
|
|
# As user expression functions should be registed with qgsfunction
|
|
# just importing the file is enough to get it to load the functions into QGIS
|
|
__import__("expressions.{0}".format(name), locals(), globals())
|
|
|
|
|
|
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
|
|
expressionspath = os.path.join(userpythonhome, "expressions")
|
|
startuppy = os.path.join(userpythonhome, "startup.py")
|
|
|
|
# exec startup script
|
|
if os.path.exists(startuppy):
|
|
execfile(startuppy, locals(), globals())
|
|
|
|
if not os.path.exists(expressionspath):
|
|
os.makedirs(expressionspath)
|
|
|
|
initfile = os.path.join(expressionspath, "__init__.py")
|
|
if not os.path.exists(initfile):
|
|
open(initfile, "w").close()
|
|
|
|
import expressions
|
|
|
|
expressions.load = load_user_expressions
|
|
expressions.load(expressionspath)
|
|
|