|
| 1 | +# coding=utf-8 |
| 2 | +from __future__ import absolute_import |
| 3 | +from __future__ import unicode_literals |
| 4 | + |
| 5 | +import octoprint.plugin |
| 6 | +import octoprint.filemanager |
| 7 | +import octoprint.filemanager.util |
| 8 | +import octoprint.util |
| 9 | +import os |
| 10 | + |
| 11 | +class ThumbnailProcessor(octoprint.filemanager.util.LineProcessorStream): |
| 12 | + |
| 13 | + def __init__(self, fileBufferedReader, thumbnail_data, path, logger): |
| 14 | + super(ThumbnailProcessor, self).__init__(fileBufferedReader) |
| 15 | + self._thumbnail_data = thumbnail_data |
| 16 | + self._collecting_data = False |
| 17 | + self._logger = logger |
| 18 | + self._path = path |
| 19 | + |
| 20 | + def process_line(self, origLine): |
| 21 | + if not len(origLine): |
| 22 | + return None |
| 23 | + |
| 24 | + isBytesLineForPy3 = type(origLine) is bytes and not (type(origLine) is str) |
| 25 | + line = octoprint.util.to_unicode(origLine, errors="replace") |
| 26 | + line = line.lstrip() |
| 27 | + |
| 28 | + if (len(line) != 0 and line.startswith("; thumbnail end")): |
| 29 | + self._collecting_data = False |
| 30 | + if len(self._thumbnail_data) > 0: |
| 31 | + import base64 |
| 32 | + with open(self._path, "wb") as fh: |
| 33 | + fh.write(base64.b64decode(self._thumbnail_data)) |
| 34 | + |
| 35 | + if (len(line) != 0 and self._collecting_data == True): |
| 36 | + self._thumbnail_data += line.replace("; ","") |
| 37 | + |
| 38 | + if (len(line) != 0 and line.startswith("; thumbnail begin")): |
| 39 | + if os.path.exists(self._path): |
| 40 | + os.remove(self._path) |
| 41 | + self._collecting_data = True |
| 42 | + |
| 43 | + line = origLine |
| 44 | + |
| 45 | + if (isBytesLineForPy3 and type(line) is str): |
| 46 | + # line = line.encode('utf8') |
| 47 | + # line = line.encode('ISO-8859-1') |
| 48 | + line = octoprint.util.to_bytes(line, errors="replace") |
| 49 | + else: |
| 50 | + if (isBytesLineForPy3 == False): |
| 51 | + # do nothing, because we don't modify the line |
| 52 | + if (type(line) is unicode): |
| 53 | + line = octoprint.util.to_native_str(line) |
| 54 | + return line |
| 55 | + |
| 56 | +class PrusaslicerthumbnailsPlugin(octoprint.plugin.SettingsPlugin, |
| 57 | + octoprint.plugin.AssetPlugin, |
| 58 | + octoprint.plugin.TemplatePlugin): |
| 59 | + |
| 60 | + def __init__(self): |
| 61 | + self._fileRemovalTimer = None |
| 62 | + self._fileRemovalLastDeleted = None |
| 63 | + self._fileRemovalLastAdded = None |
| 64 | + self._waitForAnalysis = False |
| 65 | + self._analysis_active = False |
| 66 | + self._thumbnail_data = "" |
| 67 | + |
| 68 | + ##~~ SettingsPlugin mixin |
| 69 | + |
| 70 | + def get_settings_defaults(self): |
| 71 | + return dict( |
| 72 | + installed=True, |
| 73 | + inline_thumbnail=False, |
| 74 | + scale_inline_thumbnail=False, |
| 75 | + inline_thumbnail_scale_value="50", |
| 76 | + align_inline_thumbnail=False, |
| 77 | + inline_thumbnail_align_value="left" |
| 78 | + ) |
| 79 | + |
| 80 | + ##~~ AssetPlugin mixin |
| 81 | + |
| 82 | + def get_assets(self): |
| 83 | + return dict( |
| 84 | + js=["js/prusaslicerthumbnails.js"], |
| 85 | + css=["css/prusaslicerthumbnails.css"] |
| 86 | + ) |
| 87 | + |
| 88 | + ##~~ TemplatePlugin mixin |
| 89 | + |
| 90 | + def get_template_configs(self): |
| 91 | + return [ |
| 92 | + dict(type="settings", custom_bindings=False, template="prusaslicerthumbnails_settings.jinja2"), |
| 93 | + ] |
| 94 | + |
| 95 | + ##~~ preprocessor hook |
| 96 | + |
| 97 | + def thumbnail_extractor(self, path, file_object, links=None, printer_profile=None, allow_overwrite=True, *args, **kwargs): |
| 98 | + if not octoprint.filemanager.valid_file_type(path, type="gcode"): |
| 99 | + return file_object |
| 100 | + |
| 101 | + thumbnail_filename = self.get_plugin_data_folder() + "/" + path.replace(".gcode",".png") |
| 102 | + return octoprint.filemanager.util.StreamWrapper(file_object.filename, ThumbnailProcessor(file_object.stream(), self._thumbnail_data, thumbnail_filename, self._logger)) |
| 103 | + # return file_object |
| 104 | + |
| 105 | + ##~~ Routes hook |
| 106 | + def route_hook(self, server_routes, *args, **kwargs): |
| 107 | + from octoprint.server.util.tornado import LargeResponseHandler, UrlProxyHandler, path_validation_factory |
| 108 | + from octoprint.util import is_hidden_path |
| 109 | + return [ |
| 110 | + (r"thumbnail/(.*)", LargeResponseHandler, dict(path=self.get_plugin_data_folder(), |
| 111 | + as_attachment=False, |
| 112 | + path_validation=path_validation_factory(lambda path: not is_hidden_path(path),status_code=404))) |
| 113 | + ] |
| 114 | + |
| 115 | + ##~~ Softwareupdate hook |
| 116 | + |
| 117 | + def get_update_information(self): |
| 118 | + return dict( |
| 119 | + prusaslicerthumbnails=dict( |
| 120 | + displayName="Prusa Slicer Thumbnails", |
| 121 | + displayVersion=self._plugin_version, |
| 122 | + |
| 123 | + # version check: github repository |
| 124 | + type="github_release", |
| 125 | + user="jneilliii", |
| 126 | + repo="OctoPrint-PrusaSlicerThumbnails", |
| 127 | + current=self._plugin_version, |
| 128 | + |
| 129 | + # update method: pip |
| 130 | + pip="https://github.com/jneilliii/OctoPrint-PrusaSlicerThumbnails/archive/{target_version}.zip" |
| 131 | + ) |
| 132 | + ) |
| 133 | + |
| 134 | +__plugin_name__ = "Prusa Slicer Thumbnails" |
| 135 | +__plugin_pythoncompat__ = ">=2.7,<4" # python 2 and 3 |
| 136 | + |
| 137 | +def __plugin_load__(): |
| 138 | + global __plugin_implementation__ |
| 139 | + __plugin_implementation__ = PrusaslicerthumbnailsPlugin() |
| 140 | + |
| 141 | + global __plugin_hooks__ |
| 142 | + __plugin_hooks__ = { |
| 143 | + "octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information, |
| 144 | + "octoprint.filemanager.preprocessor": __plugin_implementation__.thumbnail_extractor, |
| 145 | + "octoprint.server.http.routes": __plugin_implementation__.route_hook |
| 146 | + } |
| 147 | + |
0 commit comments