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 getPluginBasePaths():
84 if platform.system() != "Windows":
85 ret.append(os.path.expanduser('~/.cura/plugins/'))
86 if platform.system() == "Darwin" and hasattr(sys, 'frozen'):
87 ret.append(os.path.normpath(os.path.join(resources.resourceBasePath, "plugins")))
89 ret.append(os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'plugins')))
92 def getPluginList(pluginType):
94 if _pluginList is None:
96 for basePath in getPluginBasePaths():
97 if os.path.isdir(basePath):
98 for filename in os.listdir(basePath):
99 if filename.startswith('.'):
101 if filename.startswith('_'):
103 if os.path.isdir(os.path.join(basePath, filename)):
104 if os.path.exists(os.path.join(basePath, filename, 'script.py')):
105 _pluginList.append(pluginInfo(basePath, os.path.join(filename, 'script.py')))
106 elif filename.endswith('.py'):
107 _pluginList.append(pluginInfo(basePath, filename))
109 for plugin in _pluginList:
110 if plugin.getType() == pluginType:
114 def runPostProcessingPlugins(engineResult):
115 pluginConfigList = getPostProcessPluginConfig()
116 pluginList = getPluginList('postprocess')
119 for pluginConfig in pluginConfigList:
121 for pluginTest in pluginList:
122 if pluginTest.getFilename() == pluginConfig['filename']:
127 pythonFile = plugin.getFullFilename()
129 if tempfilename is None:
130 f = tempfile.NamedTemporaryFile(prefix='CuraPluginTemp', delete=False)
131 tempfilename = f.name
132 gcode = engineResult.getGCode()
134 data = gcode.read(16 * 1024)
141 locals = {'filename': tempfilename}
142 for param in plugin.getParams():
143 value = param['default']
144 if param['name'] in pluginConfig['params']:
145 value = pluginConfig['params'][param['name']]
147 if param['type'] == 'float':
151 value = float(param['default'])
153 locals[param['name']] = value
155 execfile(pythonFile, locals)
157 locationInfo = traceback.extract_tb(sys.exc_info()[2])[-1]
158 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])
159 if tempfilename is not None:
160 f = open(tempfilename, "r")
161 engineResult.setGCode("")
166 engineResult._gcodeData.write(data)
169 os.unlink(tempfilename)