chiark / gitweb /
plugins: Support user configuration of default values
[cura.git] / Cura / util / pluginInfo.py
index 77b103d31b2aa48a53f5e0bf24561aee968b9ffd..9005388882e2fb5272790388c2b920a87777f8b6 100644 (file)
@@ -10,6 +10,7 @@ import traceback
 import platform
 import re
 import tempfile
+import ConfigParser
 import cPickle as pickle
 
 from Cura.util import profile
@@ -18,6 +19,10 @@ from Cura.util import resources
 _pluginList = None
 
 class pluginInfo(object):
+       """
+       Plugin information object. Used to keep track of information about the available plugins in this installation of Cura.
+       Each plugin as meta-data associated with it which can be retrieved from this class.
+       """
        def __init__(self, dirname, filename):
                self._dirname = dirname
                self._filename = filename
@@ -48,6 +53,27 @@ class pluginInfo(object):
                                # else:
                                #       print "Unknown item in plugin meta data: %s %s" % (line[0], line[1])
 
+                self._readSettings(os.path.join(profile.getBasePath()))
+                for plugindir in getPluginBasePaths():
+                        self._readSettings(plugindir)
+
+        def _readSettings(self, indir):
+                settings_file = os.path.join(indir, 'plugin_defaults.ini')
+                plugin_basename = re.sub(r'\.pyc?','',os.path.basename(self._filename))
+                if not os.path.exists(settings_file):
+                        return
+                cfg = ConfigParser.ConfigParser()
+                try:
+                        cfg.read(settings_file)
+                except:
+                        traceback.print_exc()
+                if not cfg.has_section(plugin_basename):
+                        return
+                for param in self._params:
+                        name = param['name']
+                        if cfg.has_option(plugin_basename, name):
+                                param['default'] = cfg.get(plugin_basename, name)
+
        def getFilename(self):
                return self._filename
 
@@ -75,6 +101,9 @@ def getPostProcessPluginConfig():
 def setPostProcessPluginConfig(config):
        profile.putProfileSetting('plugin_config', pickle.dumps(config))
 
+def overridePostProcessPluginConfig(config):
+       profile.setTempOverride('plugin_config', pickle.dumps(config))
+
 def getPluginBasePaths():
        ret = []
        if platform.system() != "Windows":
@@ -90,24 +119,24 @@ def getPluginList(pluginType):
        if _pluginList is None:
                _pluginList = []
                for basePath in getPluginBasePaths():
-                       for filename in os.listdir(basePath):
-                               if filename.startswith('.'):
-                                       continue
-                               if filename.startswith('_'):
-                                       continue
-                               if os.path.isdir(os.path.join(basePath, filename)):
-                                       if os.path.exists(os.path.join(basePath, filename, 'script.py')):
-                                               _pluginList.append(pluginInfo(basePath, os.path.join(filename, 'script.py')))
-                               elif filename.endswith('.py'):
-                                       _pluginList.append(pluginInfo(basePath, filename))
+                       if os.path.isdir(basePath):
+                               for filename in os.listdir(basePath):
+                                       if filename.startswith('.'):
+                                               continue
+                                       if filename.startswith('_'):
+                                               continue
+                                       if os.path.isdir(os.path.join(basePath, filename)):
+                                               if os.path.exists(os.path.join(basePath, filename, 'script.py')):
+                                                       _pluginList.append(pluginInfo(basePath, os.path.join(filename, 'script.py')))
+                                       elif filename.endswith('.py'):
+                                               _pluginList.append(pluginInfo(basePath, filename))
        ret = []
        for plugin in _pluginList:
                if plugin.getType() == pluginType:
                        ret.append(plugin)
        return ret
 
-def runPostProcessingPlugins(engineResult):
-       pluginConfigList = getPostProcessPluginConfig()
+def runPostProcessingPlugins(engineResult, pluginConfigList):
        pluginList = getPluginList('postprocess')
 
        tempfilename = None
@@ -124,8 +153,14 @@ def runPostProcessingPlugins(engineResult):
                if tempfilename is None:
                        f = tempfile.NamedTemporaryFile(prefix='CuraPluginTemp', delete=False)
                        tempfilename = f.name
-                       f.write(engineResult.getGCode())
+                       gcode = engineResult.getGCode()
+                       while True:
+                               data = gcode.read(16 * 1024)
+                               if len(data) == 0:
+                                       break
+                               f.write(data)
                        f.close()
+                       del gcode
 
                locals = {'filename': tempfilename}
                for param in plugin.getParams():
@@ -143,11 +178,18 @@ def runPostProcessingPlugins(engineResult):
                try:
                        execfile(pythonFile, locals)
                except:
+                       traceback.print_exc()
                        locationInfo = traceback.extract_tb(sys.exc_info()[2])[-1]
                        return "%s: '%s' @ %s:%s:%d" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]), os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
        if tempfilename is not None:
                f = open(tempfilename, "r")
-               engineResult.setGCode(f.read())
+               engineResult.setGCode("")
+               import gc
+               gc.collect()
+               data = f.read(4096)
+               while len(data) > 0:
+                       engineResult._gcodeData.write(data)
+                       data = f.read(4096)
                f.close()
                os.unlink(tempfilename)
        return None