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