2 The plugin module contains information about the plugins found for Cura.
3 It keeps track of a list of installed plugins and the information contained within.
5 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
13 import cPickle as pickle
15 from Cura.util import profile
16 from Cura.util import resources
20 class pluginInfo(object):
22 Plugin information object. Used to keep track of information about the available plugins in this installation of Cura.
23 Each plugin as meta-data associated with it which can be retrieved from this class.
25 def __init__(self, dirname, filename):
26 self._dirname = dirname
27 self._filename = filename
28 self._name = os.path.splitext(os.path.basename(filename))[0]
29 self._type = 'unknown'
32 with open(os.path.join(dirname, filename), "r") as f:
35 if not line.startswith('#'):
37 line = line[1:].split(':', 1)
40 if line[0].upper() == 'NAME':
41 self._name = line[1].strip()
42 elif line[0].upper() == 'INFO':
43 self._info = line[1].strip()
44 elif line[0].upper() == 'TYPE':
45 self._type = line[1].strip()
46 elif line[0].upper() == 'DEPEND':
48 elif line[0].upper() == 'PARAM':
49 m = re.match('([a-zA-Z][a-zA-Z0-9_]*)\(([a-zA-Z_]*)(?::([^\)]*))?\) +(.*)', line[1].strip())
51 self._params.append({'name': m.group(1), 'type': m.group(2), 'default': m.group(3), 'description': m.group(4)})
53 # print "Unknown item in plugin meta data: %s %s" % (line[0], line[1])
55 def getFilename(self):
58 def getFullFilename(self):
59 return os.path.join(self._dirname, self._filename)
73 def getPostProcessPluginConfig():
75 return pickle.loads(str(profile.getProfileSetting('plugin_config')))
79 def setPostProcessPluginConfig(config):
80 profile.putProfileSetting('plugin_config', pickle.dumps(config))
82 def overridePostProcessPluginConfig(config):
83 profile.setTempOverride('plugin_config', pickle.dumps(config))
85 def getPluginBasePaths():
87 if platform.system() != "Windows":
88 ret.append(os.path.expanduser('~/.cura/plugins/'))
89 if platform.system() == "Darwin" and hasattr(sys, 'frozen'):
90 ret.append(os.path.normpath(os.path.join(resources.resourceBasePath, "plugins")))
92 ret.append(os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'plugins')))
95 def getPluginList(pluginType):
97 if _pluginList is None:
99 for basePath in getPluginBasePaths():
100 if os.path.isdir(basePath):
101 for filename in os.listdir(basePath):
102 if filename.startswith('.'):
104 if filename.startswith('_'):
106 if os.path.isdir(os.path.join(basePath, filename)):
107 if os.path.exists(os.path.join(basePath, filename, 'script.py')):
108 _pluginList.append(pluginInfo(basePath, os.path.join(filename, 'script.py')))
109 elif filename.endswith('.py'):
110 _pluginList.append(pluginInfo(basePath, filename))
112 for plugin in _pluginList:
113 if plugin.getType() == pluginType:
117 def runPostProcessingPlugins(engineResult, pluginConfigList):
118 pluginList = getPluginList('postprocess')
121 for pluginConfig in pluginConfigList:
123 for pluginTest in pluginList:
124 if pluginTest.getFilename() == pluginConfig['filename']:
129 pythonFile = plugin.getFullFilename()
131 if tempfilename is None:
132 f = tempfile.NamedTemporaryFile(prefix='CuraPluginTemp', delete=False)
133 tempfilename = f.name
134 gcode = engineResult.getGCode()
136 data = gcode.read(16 * 1024)
143 locals = {'filename': tempfilename}
144 for param in plugin.getParams():
145 value = param['default']
146 if param['name'] in pluginConfig['params']:
147 value = pluginConfig['params'][param['name']]
149 if param['type'] == 'float':
153 value = float(param['default'])
155 locals[param['name']] = value
157 execfile(pythonFile, locals)
159 locationInfo = traceback.extract_tb(sys.exc_info()[2])[-1]
160 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])
161 if tempfilename is not None:
162 f = open(tempfilename, "r")
163 engineResult.setGCode("")
168 engineResult._gcodeData.write(data)
171 os.unlink(tempfilename)