chiark / gitweb /
plugins: Support user configuration of default values
[cura.git] / Cura / util / pluginInfo.py
index 05b50a4f64fd71ea32350982e17781aaaedd521d..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
@@ -52,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
 
@@ -79,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":
@@ -111,8 +136,7 @@ def getPluginList(pluginType):
                        ret.append(plugin)
        return ret
 
-def runPostProcessingPlugins(engineResult):
-       pluginConfigList = getPostProcessPluginConfig()
+def runPostProcessingPlugins(engineResult, pluginConfigList):
        pluginList = getPluginList('postprocess')
 
        tempfilename = None
@@ -131,11 +155,12 @@ def runPostProcessingPlugins(engineResult):
                        tempfilename = f.name
                        gcode = engineResult.getGCode()
                        while True:
-                               data = gcode.read()
+                               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():
@@ -153,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