|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 3 | + |
| 4 | +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' |
| 5 | +__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License" |
| 6 | + |
| 7 | +import octoprint.plugin |
| 8 | +import octoprint.events |
| 9 | + |
| 10 | +import sarge |
| 11 | +import io |
| 12 | +import re |
| 13 | + |
| 14 | +from flask_babel import gettext |
| 15 | + |
| 16 | +# noinspection PyCompatibility |
| 17 | +from concurrent.futures import ThreadPoolExecutor |
| 18 | + |
| 19 | +class FileCheckPlugin(octoprint.plugin.AssetPlugin, |
| 20 | + octoprint.plugin.EventHandlerPlugin): |
| 21 | + def __init__(self): |
| 22 | + self._executor = ThreadPoolExecutor() |
| 23 | + |
| 24 | + ##~~ AssetPlugin API |
| 25 | + |
| 26 | + def get_assets(self): |
| 27 | + return dict(js=("js/file_check.js",)) |
| 28 | + |
| 29 | + ##~~ EventHandlerPlugin API |
| 30 | + |
| 31 | + def on_event(self, event, payload): |
| 32 | + if event == octoprint.events.Events.FILE_ADDED: |
| 33 | + self._executor.submit(self._validate_file, payload["storage"], payload["path"], payload["type"]) |
| 34 | + |
| 35 | + ##~~ SoftwareUpdate hook |
| 36 | + |
| 37 | + def get_update_information(self): |
| 38 | + return dict( |
| 39 | + file_check=dict( |
| 40 | + displayName="File Check Plugin", |
| 41 | + displayVersion=self._plugin_version, |
| 42 | + |
| 43 | + # version check: github repository |
| 44 | + type="github_release", |
| 45 | + user="OctoPrint", |
| 46 | + repo="OctoPrint-FileCheck", |
| 47 | + current=self._plugin_version, |
| 48 | + |
| 49 | + # update method: pip |
| 50 | + pip="https://github.com/OctoPrint/OctoPrint-FileCheck/archive/{target_version}.zip" |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + ##~~ Internal logic & helpers |
| 55 | + |
| 56 | + def _validate_file(self, storage, path, file_type): |
| 57 | + try: |
| 58 | + path_on_disk = self._file_manager.path_on_disk(storage, path) |
| 59 | + except NotImplementedError: |
| 60 | + # storage doesn't support path_on_disk, ignore |
| 61 | + return |
| 62 | + |
| 63 | + if file_type[-1] == "gcode": |
| 64 | + if self._search_through_file(path_on_disk, "{travel_speed}"): |
| 65 | + self._notify("travel_speed", storage, path) |
| 66 | + |
| 67 | + def _search_through_file(self, path, term): |
| 68 | + try: |
| 69 | + # try native grep |
| 70 | + result = sarge.run(["grep", "-q", re.escape(term), path]) |
| 71 | + return result.returncode == 0 |
| 72 | + except ValueError as exc: |
| 73 | + if 'Command not found' in str(exc): |
| 74 | + try: |
| 75 | + # try python only approach |
| 76 | + with io.open(path, mode="r", encoding="utf8", errors="replace") as f: |
| 77 | + for line in f: |
| 78 | + if term in line: |
| 79 | + return True |
| 80 | + return False |
| 81 | + except: |
| 82 | + self._logger.exception("Something unexpectedly went wrong while trying to " |
| 83 | + "search for {} in {} via native python".format(term, path)) |
| 84 | + else: |
| 85 | + self._logger.exception("Something unexpectedly went wrong while trying to " |
| 86 | + "search for {} in {} via grep".format(term, path)) |
| 87 | + |
| 88 | + return False |
| 89 | + |
| 90 | + def _notify(self, notification_type, storage, path): |
| 91 | + self._logger.warning("File check identified an issue: {} for {}:{}, see " |
| 92 | + "https://faq.octoprint.org/file-check-{} for details".format(notification_type, |
| 93 | + storage, |
| 94 | + path, |
| 95 | + notification_type.replace("_", "-"))) |
| 96 | + self._plugin_manager.send_plugin_message(self._identifier, dict(type=notification_type, |
| 97 | + storage=storage, |
| 98 | + path=path)) |
| 99 | + |
| 100 | + |
| 101 | +__plugin_name__ = "File Check" |
| 102 | +__plugin_pycompat__ = ">2.7,<4" |
| 103 | +__plugin_disabling_discouraged__ = gettext("Without this plugin OctoPrint will no longer be able to " |
| 104 | + "check if uploaded files contain common problems and inform you " |
| 105 | + "about that fact.") |
| 106 | + |
| 107 | +__plugin_implementation__ = FileCheckPlugin() |
| 108 | +__plugin_hooks__ = { |
| 109 | + "octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information |
| 110 | +} |
0 commit comments