chiark / gitweb /
Add feature which checks for newer releases on startup.
[cura.git] / Cura / util / profile.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import os, traceback, math, re, zlib, base64, time, sys, platform, glob, string, stat
5 import cPickle as pickle
6 if sys.version_info[0] < 3:
7         import ConfigParser
8 else:
9         import configparser as ConfigParser
10
11 from Cura.util import resources
12 from Cura.util import version
13
14 #########################################################
15 ## Default settings when none are found.
16 #########################################################
17
18 #Single place to store the defaults, so we have a consistent set of default settings.
19 profileDefaultSettings = {
20         'nozzle_size': '0.4',
21         'layer_height': '0.2',
22         'wall_thickness': '0.8',
23         'solid_layer_thickness': '0.6',
24         'fill_density': '20',
25         'skirt_line_count': '1',
26         'skirt_gap': '3.0',
27         'print_speed': '50',
28         'print_temperature': '220',
29         'print_bed_temperature': '70',
30         'support': 'None',
31         'filament_diameter': '2.89',
32         'filament_density': '1.00',
33         'retraction_min_travel': '5.0',
34         'retraction_enable': 'False',
35         'retraction_speed': '40.0',
36         'retraction_amount': '4.5',
37         'retraction_extra': '0.0',
38         'retract_on_jumps_only': 'True',
39         'travel_speed': '150',
40         'max_z_speed': '3.0',
41         'bottom_layer_speed': '20',
42         'cool_min_layer_time': '5',
43         'fan_enabled': 'True',
44         'fan_layer': '1',
45         'fan_speed': '100',
46         'fan_speed_max': '100',
47         'model_scale': '1.0',
48         'flip_x': 'False',
49         'flip_y': 'False',
50         'flip_z': 'False',
51         'swap_xz': 'False',
52         'swap_yz': 'False',
53         'model_rotate_base': '0',
54         'model_multiply_x': '1',
55         'model_multiply_y': '1',
56         'extra_base_wall_thickness': '0.0',
57         'sequence': 'Loops > Perimeter > Infill',
58         'force_first_layer_sequence': 'True',
59         'infill_type': 'Line',
60         'solid_top': 'True',
61         'fill_overlap': '15',
62         'support_rate': '50',
63         'support_distance': '0.5',
64         'support_dual_extrusion': 'False',
65         'joris': 'False',
66         'enable_skin': 'False',
67         'enable_raft': 'False',
68         'cool_min_feedrate': '10',
69         'bridge_speed': '100',
70         'raft_margin': '5',
71         'raft_base_material_amount': '100',
72         'raft_interface_material_amount': '100',
73         'bottom_thickness': '0.3',
74         'hop_on_move': 'False',
75         'plugin_config': '',
76         'object_center_x': '-1',
77         'object_center_y': '-1',
78         
79         'gcode_extension': 'gcode',
80         'alternative_center': '',
81         'clear_z': '0.0',
82         'extruder': '0',
83 }
84 alterationDefault = {
85 #######################################################################################
86         'start.gcode': """;Sliced {filename} at: {day} {date} {time}
87 ;Basic settings: Layer height: {layer_height} Walls: {wall_thickness} Fill: {fill_density}
88 ;Print time: {print_time}
89 ;Filament used: {filament_amount}m {filament_weight}g
90 ;Filament cost: {filament_cost}
91 G21        ;metric values
92 G90        ;absolute positioning
93 M107       ;start with the fan off
94
95 G28 X0 Y0  ;move X/Y to min endstops
96 G28 Z0     ;move Z to min endstops
97 G92 X0 Y0 Z0 E0         ;reset software position to front/left/z=0.0
98
99 G1 Z15.0 F{max_z_speed} ;move the platform down 15mm
100
101 G92 E0                  ;zero the extruded length
102 G1 F200 E3              ;extrude 3mm of feed stock
103 G92 E0                  ;zero the extruded length again
104 G1 F{travel_speed}
105 """,
106 #######################################################################################
107         'end.gcode': """;End GCode
108 M104 S0                     ;extruder heater off
109 M140 S0                     ;heated bed heater off (if you have it)
110
111 G91                                    ;relative positioning
112 G1 E-1 F300                            ;retract the filament a bit before lifting the nozzle, to release some of the pressure
113 G1 Z+0.5 E-5 X-20 Y-20 F{travel_speed} ;move Z up a bit and retract filament even more
114 G28 X0 Y0                              ;move X/Y to min endstops, so the head is out of the way
115
116 M84                         ;steppers off
117 G90                         ;absolute positioning
118 """,
119 #######################################################################################
120         'support_start.gcode': '',
121         'support_end.gcode': '',
122         'cool_start.gcode': '',
123         'cool_end.gcode': '',
124         'replace.csv': '',
125 #######################################################################################
126         'nextobject.gcode': """;Move to next object on the platform. clear_z is the minimal z height we need to make sure we do not hit any objects.
127 G92 E0
128
129 G91                                    ;relative positioning
130 G1 E-1 F300                            ;retract the filament a bit before lifting the nozzle, to release some of the pressure
131 G1 Z+0.5 E-5 F{travel_speed}           ;move Z up a bit and retract filament even more
132 G90                                    ;absolute positioning
133
134 G1 Z{clear_z} F{max_z_speed}
135 G92 E0
136 G1 X{object_center_x} Y{object_center_x} F{travel_speed}
137 G1 F200 E6
138 G92 E0
139 """,
140 #######################################################################################
141         'switchExtruder.gcode': """;Switch between the current extruder and the next extruder, when printing with multiple extruders.
142 G92 E0
143 G1 E-15 F5000
144 G92 E0
145 T{extruder}
146 G1 E15 F5000
147 G92 E0
148 """,
149 }
150 preferencesDefaultSettings = {
151         'startMode': 'Simple',
152         'lastFile': os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'resources', 'example', 'UltimakerRobot_support.stl')),
153         'machine_width': '205',
154         'machine_depth': '205',
155         'machine_height': '200',
156         'machine_type': 'unknown',
157         'ultimaker_extruder_upgrade': 'False',
158         'has_heated_bed': 'False',
159         'extruder_amount': '1',
160         'extruder_offset_x1': '-22.0',
161         'extruder_offset_y1': '0.0',
162         'extruder_offset_x2': '0.0',
163         'extruder_offset_y2': '0.0',
164         'extruder_offset_x3': '0.0',
165         'extruder_offset_y3': '0.0',
166         'filament_density': '1300',
167         'steps_per_e': '0',
168         'serial_port': 'AUTO',
169         'serial_port_auto': '',
170         'serial_baud': 'AUTO',
171         'serial_baud_auto': '',
172         'slicer': 'Cura (Skeinforge based)',
173         'save_profile': 'False',
174         'filament_cost_kg': '0',
175         'filament_cost_meter': '0',
176         'sdpath': '',
177         'sdshortnames': 'False',
178         'check_for_updates': 'True',
179
180         'planner_always_autoplace': 'True',
181         'extruder_head_size_min_x': '75.0',
182         'extruder_head_size_min_y': '18.0',
183         'extruder_head_size_max_x': '18.0',
184         'extruder_head_size_max_y': '35.0',
185         'extruder_head_size_height': '60.0',
186         
187         'model_colour': '#72CB30',
188         'model_colour2': '#CB3030',
189         'model_colour3': '#DDD93C',
190         'model_colour4': '#4550D3',
191 }
192
193 #########################################################
194 ## Profile and preferences functions
195 #########################################################
196
197 ## Profile functions
198 def getDefaultProfilePath():
199         if platform.system() == "Windows":
200                 basePath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
201                 #If we have a frozen python install, we need to step out of the library.zip
202                 if hasattr(sys, 'frozen'):
203                         basePath = os.path.normpath(os.path.join(basePath, ".."))
204         else:
205                 basePath = os.path.expanduser('~/.cura/%s' % version.getVersion(False))
206         if not os.path.isdir(basePath):
207                 os.makedirs(basePath)
208         return os.path.join(basePath, 'current_profile.ini')
209
210 def loadGlobalProfile(filename):
211         #Read a configuration file as global config
212         global globalProfileParser
213         globalProfileParser = ConfigParser.ConfigParser()
214         globalProfileParser.read(filename)
215
216 def resetGlobalProfile():
217         #Read a configuration file as global config
218         global globalProfileParser
219         globalProfileParser = ConfigParser.ConfigParser()
220
221         if getPreference('machine_type') == 'ultimaker':
222                 putProfileSetting('nozzle_size', '0.4')
223                 if getPreference('ultimaker_extruder_upgrade') == 'True':
224                         putProfileSetting('retraction_enable', 'True')
225         else:
226                 putProfileSetting('nozzle_size', '0.5')
227
228 def saveGlobalProfile(filename):
229         #Save the current profile to an ini file
230         globalProfileParser.write(open(filename, 'w'))
231
232 def loadGlobalProfileFromString(options):
233         global globalProfileParser
234         globalProfileParser = ConfigParser.ConfigParser()
235         globalProfileParser.add_section('profile')
236         globalProfileParser.add_section('alterations')
237         options = base64.b64decode(options)
238         options = zlib.decompress(options)
239         (profileOpts, alt) = options.split('\f', 1)
240         for option in profileOpts.split('\b'):
241                 if len(option) > 0:
242                         (key, value) = option.split('=', 1)
243                         globalProfileParser.set('profile', key, value)
244         for option in alt.split('\b'):
245                 if len(option) > 0:
246                         (key, value) = option.split('=', 1)
247                         globalProfileParser.set('alterations', key, value)
248
249 def getGlobalProfileString():
250         global globalProfileParser
251         if not globals().has_key('globalProfileParser'):
252                 loadGlobalProfile(getDefaultProfilePath())
253         
254         p = []
255         alt = []
256         tempDone = []
257         if globalProfileParser.has_section('profile'):
258                 for key in globalProfileParser.options('profile'):
259                         if key in tempOverride:
260                                 p.append(key + "=" + tempOverride[key])
261                                 tempDone.append(key)
262                         else:
263                                 p.append(key + "=" + globalProfileParser.get('profile', key))
264         if globalProfileParser.has_section('alterations'):
265                 for key in globalProfileParser.options('alterations'):
266                         if key in tempOverride:
267                                 p.append(key + "=" + tempOverride[key])
268                                 tempDone.append(key)
269                         else:
270                                 alt.append(key + "=" + globalProfileParser.get('alterations', key))
271         for key in tempOverride:
272                 if key not in tempDone:
273                         p.append(key + "=" + tempOverride[key])
274         ret = '\b'.join(p) + '\f' + '\b'.join(alt)
275         ret = base64.b64encode(zlib.compress(ret, 9))
276         return ret
277
278 def getProfileSetting(name):
279         if name in tempOverride:
280                 return unicode(tempOverride[name], "utf-8")
281         #Check if we have a configuration file loaded, else load the default.
282         if not globals().has_key('globalProfileParser'):
283                 loadGlobalProfile(getDefaultProfilePath())
284         if not globalProfileParser.has_option('profile', name):
285                 if name in profileDefaultSettings:
286                         default = profileDefaultSettings[name]
287                 else:
288                         print("Missing default setting for: '" + name + "'")
289                         profileDefaultSettings[name] = ''
290                         default = ''
291                 if not globalProfileParser.has_section('profile'):
292                         globalProfileParser.add_section('profile')
293                 globalProfileParser.set('profile', name, str(default))
294                 #print(name + " not found in profile, so using default: " + str(default))
295                 return default
296         return globalProfileParser.get('profile', name)
297
298 def getProfileSettingFloat(name):
299         try:
300                 setting = getProfileSetting(name).replace(',', '.')
301                 return float(eval(setting, {}, {}))
302         except (ValueError, SyntaxError, TypeError):
303                 return 0.0
304
305 def putProfileSetting(name, value):
306         #Check if we have a configuration file loaded, else load the default.
307         if not globals().has_key('globalProfileParser'):
308                 loadGlobalProfile(getDefaultProfilePath())
309         if not globalProfileParser.has_section('profile'):
310                 globalProfileParser.add_section('profile')
311         globalProfileParser.set('profile', name, str(value))
312
313 def isProfileSetting(name):
314         if name in profileDefaultSettings:
315                 return True
316         return False
317
318 ## Preferences functions
319 global globalPreferenceParser
320 globalPreferenceParser = None
321
322 def getPreferencePath():
323         if platform.system() == "Windows":
324                 basePath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
325                 #If we have a frozen python install, we need to step out of the library.zip
326                 if hasattr(sys, 'frozen'):
327                         basePath = os.path.normpath(os.path.join(basePath, ".."))
328         else:
329                 basePath = os.path.expanduser('~/.cura/%s' % version.getVersion(False))
330         if not os.path.isdir(basePath):
331                 os.makedirs(basePath)
332         return os.path.join(basePath, 'preferences.ini')
333
334 def getPreferenceFloat(name):
335         try:
336                 setting = getPreference(name).replace(',', '.')
337                 return float(eval(setting, {}, {}))
338         except (ValueError, SyntaxError, TypeError):
339                 return 0.0
340
341 def getPreferenceColour(name):
342         colorString = getPreference(name)
343         return [float(int(colorString[1:3], 16)) / 255, float(int(colorString[3:5], 16)) / 255, float(int(colorString[5:7], 16)) / 255, 1.0]
344
345 def getPreference(name):
346         if name in tempOverride:
347                 return unicode(tempOverride[name])
348         global globalPreferenceParser
349         if globalPreferenceParser is None:
350                 globalPreferenceParser = ConfigParser.ConfigParser()
351                 globalPreferenceParser.read(getPreferencePath())
352         if not globalPreferenceParser.has_option('preference', name):
353                 if name in preferencesDefaultSettings:
354                         default = preferencesDefaultSettings[name]
355                 else:
356                         print("Missing default setting for: '" + name + "'")
357                         preferencesDefaultSettings[name] = ''
358                         default = ''
359                 if not globalPreferenceParser.has_section('preference'):
360                         globalPreferenceParser.add_section('preference')
361                 globalPreferenceParser.set('preference', name, str(default))
362                 #print(name + " not found in preferences, so using default: " + str(default))
363                 return default
364         return unicode(globalPreferenceParser.get('preference', name), "utf-8")
365
366 def putPreference(name, value):
367         #Check if we have a configuration file loaded, else load the default.
368         global globalPreferenceParser
369         if globalPreferenceParser == None:
370                 globalPreferenceParser = ConfigParser.ConfigParser()
371                 globalPreferenceParser.read(getPreferencePath())
372         if not globalPreferenceParser.has_section('preference'):
373                 globalPreferenceParser.add_section('preference')
374         globalPreferenceParser.set('preference', name, unicode(value).encode("utf-8"))
375         globalPreferenceParser.write(open(getPreferencePath(), 'w'))
376
377 def isPreference(name):
378         if name in preferencesDefaultSettings:
379                 return True
380         return False
381
382 ## Temp overrides for multi-extruder slicing and the project planner.
383 tempOverride = {}
384 def setTempOverride(name, value):
385         tempOverride[name] = unicode(value).encode("utf-8")
386 def clearTempOverride(name):
387         del tempOverride[name]
388 def resetTempOverride():
389         tempOverride.clear()
390
391 #########################################################
392 ## Utility functions to calculate common profile values
393 #########################################################
394 def calculateEdgeWidth():
395         wallThickness = getProfileSettingFloat('wall_thickness')
396         nozzleSize = getProfileSettingFloat('nozzle_size')
397         
398         if wallThickness < nozzleSize:
399                 return wallThickness
400
401         lineCount = int(wallThickness / nozzleSize + 0.0001)
402         lineWidth = wallThickness / lineCount
403         lineWidthAlt = wallThickness / (lineCount + 1)
404         if lineWidth > nozzleSize * 1.5:
405                 return lineWidthAlt
406         return lineWidth
407
408 def calculateLineCount():
409         wallThickness = getProfileSettingFloat('wall_thickness')
410         nozzleSize = getProfileSettingFloat('nozzle_size')
411         
412         if wallThickness < nozzleSize:
413                 return 1
414
415         lineCount = int(wallThickness / nozzleSize + 0.0001)
416         lineWidth = wallThickness / lineCount
417         lineWidthAlt = wallThickness / (lineCount + 1)
418         if lineWidth > nozzleSize * 1.5:
419                 return lineCount + 1
420         return lineCount
421
422 def calculateSolidLayerCount():
423         layerHeight = getProfileSettingFloat('layer_height')
424         solidThickness = getProfileSettingFloat('solid_layer_thickness')
425         return int(math.ceil(solidThickness / layerHeight - 0.0001))
426
427 def getMachineCenterCoords():
428         return [getPreferenceFloat('machine_width') / 2, getPreferenceFloat('machine_depth') / 2]
429
430 def getObjectMatrix():
431         rotate = getProfileSettingFloat('model_rotate_base')
432         rotate = rotate / 180.0 * math.pi
433         scaleX = getProfileSettingFloat('model_scale')
434         scaleY = getProfileSettingFloat('model_scale')
435         scaleZ = getProfileSettingFloat('model_scale')
436         if getProfileSetting('flipX') == 'True':
437                 scaleX = -scaleX
438         if getProfileSetting('flipY') == 'True':
439                 scaleY = -scaleY
440         if getProfileSetting('flipZ') == 'True':
441                 scaleZ = -scaleZ
442         mat00 = math.cos(rotate) * scaleX
443         mat01 =-math.sin(rotate) * scaleY
444         mat10 = math.sin(rotate) * scaleX
445         mat11 = math.cos(rotate) * scaleY
446
447         mat = [mat00,mat10,0, mat01,mat11,0, 0,0,scaleZ]
448         if getProfileSetting('swap_xz') == 'True':
449                 mat = mat[6:9] + mat[3:6] + mat[0:3]
450         if getProfileSetting('swap_yz') == 'True':
451                 mat = mat[0:3] + mat[6:9] + mat[3:6]
452         return mat
453
454 #########################################################
455 ## Alteration file functions
456 #########################################################
457 def replaceTagMatch(m):
458         pre = m.group(1)
459         tag = m.group(2)
460         if tag == 'time':
461                 return pre + time.strftime('%H:%M:%S').encode('utf-8', 'replace')
462         if tag == 'date':
463                 return pre + time.strftime('%d %b %Y').encode('utf-8', 'replace')
464         if tag == 'day':
465                 return pre + time.strftime('%a').encode('utf-8', 'replace')
466         if tag == 'print_time':
467                 return pre + '#P_TIME#'
468         if tag == 'filament_amount':
469                 return pre + '#F_AMNT#'
470         if tag == 'filament_weight':
471                 return pre + '#F_WGHT#'
472         if tag == 'filament_cost':
473                 return pre + '#F_COST#'
474         if pre == 'F' and tag in ['print_speed', 'retraction_speed', 'travel_speed', 'max_z_speed', 'bottom_layer_speed', 'cool_min_feedrate']:
475                 f = getProfileSettingFloat(tag) * 60
476         elif isProfileSetting(tag):
477                 f = getProfileSettingFloat(tag)
478         elif isPreference(tag):
479                 f = getProfileSettingFloat(tag)
480         else:
481                 return '%s?%s?' % (pre, tag)
482         if (f % 1) == 0:
483                 return pre + str(int(f))
484         return pre + str(f)
485
486 def replaceGCodeTags(filename, gcodeInt):
487         f = open(filename, 'r+')
488         data = f.read(2048)
489         data = data.replace('#P_TIME#', ('%5d:%02d' % (int(gcodeInt.totalMoveTimeMinute / 60), int(gcodeInt.totalMoveTimeMinute % 60)))[-8:])
490         data = data.replace('#F_AMNT#', ('%8.2f' % (gcodeInt.extrusionAmount / 1000))[-8:])
491         data = data.replace('#F_WGHT#', ('%8.2f' % (gcodeInt.calculateWeight() * 1000))[-8:])
492         cost = gcodeInt.calculateCost()
493         if cost is None:
494                 cost = 'Unknown'
495         data = data.replace('#F_COST#', ('%8s' % (cost.split(' ')[0]))[-8:])
496         f.seek(0)
497         f.write(data)
498         f.close()
499
500 ### Get aleration raw contents. (Used internally in Cura)
501 def getAlterationFile(filename):
502         #Check if we have a configuration file loaded, else load the default.
503         if not globals().has_key('globalProfileParser'):
504                 loadGlobalProfile(getDefaultProfilePath())
505         
506         if not globalProfileParser.has_option('alterations', filename):
507                 if filename in alterationDefault:
508                         default = alterationDefault[filename]
509                 else:
510                         print("Missing default alteration for: '" + filename + "'")
511                         alterationDefault[filename] = ''
512                         default = ''
513                 if not globalProfileParser.has_section('alterations'):
514                         globalProfileParser.add_section('alterations')
515                 #print("Using default for: %s" % (filename))
516                 globalProfileParser.set('alterations', filename, default)
517         return unicode(globalProfileParser.get('alterations', filename), "utf-8")
518
519 def setAlterationFile(filename, value):
520         #Check if we have a configuration file loaded, else load the default.
521         if not globals().has_key('globalProfileParser'):
522                 loadGlobalProfile(getDefaultProfilePath())
523         if not globalProfileParser.has_section('alterations'):
524                 globalProfileParser.add_section('alterations')
525         globalProfileParser.set('alterations', filename, value.encode("utf-8"))
526         saveGlobalProfile(getDefaultProfilePath())
527
528 ### Get the alteration file for output. (Used by Skeinforge)
529 def getAlterationFileContents(filename):
530         prefix = ''
531         postfix = ''
532         alterationContents = getAlterationFile(filename)
533         if filename == 'start.gcode':
534                 #For the start code, hack the temperature and the steps per E value into it. So the temperature is reached before the start code extrusion.
535                 #We also set our steps per E here, if configured.
536                 eSteps = getPreferenceFloat('steps_per_e')
537                 if eSteps > 0:
538                         prefix += 'M92 E%f\n' % (eSteps)
539                 temp = getProfileSettingFloat('print_temperature')
540                 bedTemp = 0
541                 if getPreference('has_heated_bed') == 'True':
542                         bedTemp = getProfileSettingFloat('print_bed_temperature')
543                 
544                 if bedTemp > 0 and not '{print_bed_temperature}' in alterationContents:
545                         prefix += 'M140 S%f\n' % (bedTemp)
546                 if temp > 0 and not '{print_temperature}' in alterationContents:
547                         prefix += 'M109 S%f\n' % (temp)
548                 if bedTemp > 0 and not '{print_bed_temperature}' in alterationContents:
549                         prefix += 'M190 S%f\n' % (bedTemp)
550         elif filename == 'end.gcode':
551                 #Append the profile string to the end of the GCode, so we can load it from the GCode file later.
552                 postfix = ';CURA_PROFILE_STRING:%s\n' % (getGlobalProfileString())
553         elif filename == 'replace.csv':
554                 #Always remove the extruder on/off M codes. These are no longer needed in 5D printing.
555                 prefix = 'M101\nM103\n'
556         elif filename == 'support_start.gcode' or filename == 'support_end.gcode':
557                 #Add support start/end code 
558                 if getProfileSetting('support_dual_extrusion') == 'True' and int(getPreference('extruder_amount')) > 1:
559                         if filename == 'support_start.gcode':
560                                 setTempOverride('extruder', '1')
561                         else:
562                                 setTempOverride('extruder', '0')
563                         alterationContents = getAlterationFileContents('switchExtruder.gcode')
564                         clearTempOverride('extruder')
565                 else:
566                         alterationContents = ''
567         return unicode(prefix + re.sub("(.)\{([^\}]*)\}", replaceTagMatch, alterationContents).rstrip() + '\n' + postfix).strip().encode('utf-8') + '\n'
568
569 ###### PLUGIN #####
570
571 def getPluginConfig():
572         try:
573                 return pickle.loads(getProfileSetting('plugin_config'))
574         except:
575                 return []
576
577 def setPluginConfig(config):
578         putProfileSetting('plugin_config', pickle.dumps(config))
579
580 def getPluginBasePaths():
581         ret = []
582         if platform.system() != "Windows":
583                 ret.append(os.path.expanduser('~/.cura/plugins/'))
584         if platform.system() == "Darwin" and hasattr(sys, 'frozen'):
585                 ret.append(os.path.normpath(os.path.join(resources.resourceBasePath, "Cura/plugins")))
586         else:
587                 ret.append(os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'plugins')))
588         return ret
589
590 def getPluginList():
591         ret = []
592         for basePath in getPluginBasePaths():
593                 for filename in glob.glob(os.path.join(basePath, '*.py')):
594                         filename = os.path.basename(filename)
595                         if filename.startswith('_'):
596                                 continue
597                         with open(os.path.join(basePath, filename), "r") as f:
598                                 item = {'filename': filename, 'name': None, 'info': None, 'type': None, 'params': []}
599                                 for line in f:
600                                         line = line.strip()
601                                         if not line.startswith('#'):
602                                                 break
603                                         line = line[1:].split(':', 1)
604                                         if len(line) != 2:
605                                                 continue
606                                         if line[0].upper() == 'NAME':
607                                                 item['name'] = line[1].strip()
608                                         elif line[0].upper() == 'INFO':
609                                                 item['info'] = line[1].strip()
610                                         elif line[0].upper() == 'TYPE':
611                                                 item['type'] = line[1].strip()
612                                         elif line[0].upper() == 'DEPEND':
613                                                 pass
614                                         elif line[0].upper() == 'PARAM':
615                                                 m = re.match('([a-zA-Z]*)\(([a-zA-Z_]*)(?::([^\)]*))?\) +(.*)', line[1].strip())
616                                                 if m is not None:
617                                                         item['params'].append({'name': m.group(1), 'type': m.group(2), 'default': m.group(3), 'description': m.group(4)})
618                                         else:
619                                                 print "Unknown item in effect meta data: %s %s" % (line[0], line[1])
620                                 if item['name'] != None and item['type'] == 'postprocess':
621                                         ret.append(item)
622         return ret
623
624 def runPostProcessingPlugins(gcodefilename):
625         pluginConfigList = getPluginConfig()
626         pluginList = getPluginList()
627         
628         for pluginConfig in pluginConfigList:
629                 plugin = None
630                 for pluginTest in pluginList:
631                         if pluginTest['filename'] == pluginConfig['filename']:
632                                 plugin = pluginTest
633                 if plugin is None:
634                         continue
635                 
636                 pythonFile = None
637                 for basePath in getPluginBasePaths():
638                         testFilename = os.path.join(basePath, pluginConfig['filename'])
639                         if os.path.isfile(testFilename):
640                                 pythonFile = testFilename
641                 if pythonFile is None:
642                         continue
643                 
644                 locals = {'filename': gcodefilename}
645                 for param in plugin['params']:
646                         value = param['default']
647                         if param['name'] in pluginConfig['params']:
648                                 value = pluginConfig['params'][param['name']]
649                         
650                         if param['type'] == 'float':
651                                 try:
652                                         value = float(value)
653                                 except:
654                                         value = float(param['default'])
655                         
656                         locals[param['name']] = value
657                 try:
658                         execfile(pythonFile, locals)
659                 except:
660                         locationInfo = traceback.extract_tb(sys.exc_info()[2])[-1]
661                         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])
662         return None
663
664 def getSDcardDrives():
665         drives = ['']
666         if platform.system() == "Windows":
667                 from ctypes import windll
668                 bitmask = windll.kernel32.GetLogicalDrives()
669                 for letter in string.uppercase:
670                         if bitmask & 1:
671                                 drives.append(letter + ':/')
672                         bitmask >>= 1
673         if platform.system() == "Darwin":
674                 drives = []
675                 for volume in glob.glob('/Volumes/*'):
676                         if stat.S_ISLNK(os.lstat(volume).st_mode):
677                                 continue
678                         drives.append(volume)
679         return drives