Skip to content

Commit

Permalink
Better handling error and simplified logic (ver 1.0.10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ǝuıɥsuooɯ committed Nov 15, 2017
1 parent 4d852a3 commit c224e46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
30 changes: 17 additions & 13 deletions octoprint_multi_colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
import contextlib
from shutil import copyfile
import os

class MultiColorsPlugin(octoprint.plugin.AssetPlugin,
octoprint.plugin.SimpleApiPlugin,
Expand Down Expand Up @@ -61,25 +62,28 @@ def on_api_command(self, command, data):
self.save_gcode(data.get('gcode'))
self.save_regex(data.get('find_string'))

if data.get('duplicate'):
old_gcode_file = os.path.join(self._settings.global_get_basefolder('uploads'), data.get('file'))
gcode_file = os.path.join(self._settings.global_get_basefolder('uploads'), self.rename(data.get('file')) )
if gcode_file != old_gcode_file:
copyfile(old_gcode_file, gcode_file)
else:
gcode_file = os.path.join(self._settings.global_get_basefolder('uploads'), data.get('file'))
gcode_file = os.path.join(self._settings.global_get_basefolder('uploads'), data.get('file') )
gcode_file_multi = os.path.join(self._settings.global_get_basefolder('uploads'), self.rename(data.get('file')) )
work_copy = "%s.tmp"%gcode_file

self._logger.info("File to modify '%s'"%gcode_file)
copyfile(gcode_file, work_copy)

ret, message = self.inject_gcode(gcode_file, data.get('layers').replace(",", " ").split(), data.get('find_string'), data.get('gcode'))
ret, message = self.inject_gcode(work_copy, data.get('layers').replace(",", " ").split(), data.get('find_string'), data.get('gcode'))

if ret == "error":
return jsonify(dict(status=ret, message=message, file=data.get('file')) )
else:
return jsonify(dict(status=ret, message=message, file=self.rename(data.get('file'))) )
if ret != "error":
if data.get('duplicate'):
copyfile(work_copy, gcode_file_multi)
else:
copyfile(work_copy, gcode_file)

os.remove( work_copy )

return jsonify(dict(status=ret, message=message))

def inject_gcode(self, file, layers, find_string, gcode):
try:
self._logger.info("File to modify '%s'"%file)

marker = "; multi color"
line_found = False
with open(file, "r") as f:
Expand Down
6 changes: 3 additions & 3 deletions octoprint_multi_colors/static/js/multi_colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ $(function() {
function(data){
new PNotify({title:"Colors", text:data.message, type: data.status});
self.filesViewModel.requestData({force:true});
//if (data.status != "error") {
//self.filesViewModel.loadFile({origin:"local", path:data.file});
//}
if (data.status != "error" and !self.duplicate()) {
self.filesViewModel.loadFile({origin:"local", self.filename});
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint MultiColors"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.0.9"
plugin_version = "1.0.10"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit c224e46

Please sign in to comment.