2015-01-12 18:14:30 +10:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
2015-05-30 17:29:45 +02:00
|
|
|
import traceback
|
2015-01-12 18:14:30 +10:00
|
|
|
|
2015-05-30 17:29:45 +02:00
|
|
|
from PyQt4.QtCore import QCoreApplication
|
|
|
|
from qgis.core import QgsApplication, QgsMessageLog
|
2015-01-12 18:14:30 +10:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2015-01-12 18:14:30 +10:00
|
|
|
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
|
2015-05-30 17:29:45 +02:00
|
|
|
# As user expression functions should be registered with qgsfunction
|
2015-01-12 18:14:30 +10:00
|
|
|
# just importing the file is enough to get it to load the functions into QGIS
|
2015-05-30 17:29:45 +02:00
|
|
|
try:
|
|
|
|
__import__("expressions.{0}".format(name), locals(), globals())
|
|
|
|
except:
|
|
|
|
error = traceback.format_exc()
|
|
|
|
msgtitle = QCoreApplication.translate("UserExpressions", "User expressions")
|
2015-05-31 00:42:13 +02:00
|
|
|
msg = QCoreApplication.translate("UserExpressions", "The user expression {0} is not valid").format(name)
|
2015-08-22 14:29:41 +02:00
|
|
|
QgsMessageLog.logMessage(msg + "\n" + error, msgtitle, QgsMessageLog.WARNING)
|
2015-01-12 18:14:30 +10:00
|
|
|
|
|
|
|
|
|
|
|
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
|
|
|
|
expressionspath = os.path.join(userpythonhome, "expressions")
|
|
|
|
startuppy = os.path.join(userpythonhome, "startup.py")
|
|
|
|
|
2015-01-20 09:54:30 +10:00
|
|
|
sys.path.append(userpythonhome)
|
|
|
|
|
2015-01-12 18:14:30 +10:00
|
|
|
# exec startup script
|
|
|
|
if os.path.exists(startuppy):
|
2015-08-22 14:29:41 +02:00
|
|
|
exec(compile(open(startuppy).read(), startuppy, 'exec'), locals(), globals())
|
2015-01-12 18:14:30 +10:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2015-07-04 21:04:40 +10:00
|
|
|
template = """\"\"\"
|
|
|
|
Define new functions using @qgsfunction. feature and parent must always be the
|
|
|
|
last args. Use args=-1 to pass a list of values as arguments
|
|
|
|
\"\"\"
|
|
|
|
|
|
|
|
from qgis.core import *
|
|
|
|
from qgis.gui import *
|
|
|
|
|
|
|
|
@qgsfunction(args='auto', group='Custom')
|
|
|
|
def func(value1, feature, parent):
|
|
|
|
return value1
|
|
|
|
"""
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2015-06-26 19:53:52 +10:00
|
|
|
try:
|
|
|
|
import expressions
|
2015-06-01 14:44:54 +10:00
|
|
|
|
2015-06-26 19:53:52 +10:00
|
|
|
expressions.load = load_user_expressions
|
|
|
|
expressions.load(expressionspath)
|
2015-07-04 21:04:40 +10:00
|
|
|
expressions.template = template
|
2015-06-26 19:53:52 +10:00
|
|
|
except ImportError:
|
|
|
|
# We get a import error and crash for some reason even if we make the expressions package
|
|
|
|
# TODO Fix the crash on first load with no expressions folder
|
|
|
|
# But for now it's not the end of the world if it doesn't laod the first time
|
|
|
|
pass
|