Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save NathanW2/a51303bcb7bb0390b8db to your computer and use it in GitHub Desktop.

Select an option

Save NathanW2/a51303bcb7bb0390b8db to your computer and use it in GitHub Desktop.
import os
import sys
import glob
import expressions
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")
# TODO Look for the startup.py and execfile it
expressions.load = load_user_expressions
expressions.load(expressionspath)
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
>>qgis.user
<module 'qgis.user' from '/home/nathan/dev/qgis/build/output/python/qgis/user.py'>
>>qgis.user.expressions
<module 'expressions' from '/home/nathan/.qgis2/python/expressions/__init__.pyc'>
>>qgis.user.expressions.load
<function load_user_expressions at 0x7f7163cde8c0>
>>dir(qgis.user.expressions)
['MyFunctions', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'load', 'scratch']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment