Skip to content

Commit 78f18c6

Browse files
committed
🎨 Run black, prettier, isort, flake8
1 parent 418f65e commit 78f18c6

File tree

3 files changed

+759
-630
lines changed

3 files changed

+759
-630
lines changed

octoprint_file_check/__init__.py

Lines changed: 103 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,122 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, division, print_function, unicode_literals
33

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"
55
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
66

7-
import octoprint.plugin
8-
import octoprint.events
9-
10-
import sarge
117
import io
128
import re
139

14-
from flask_babel import gettext
15-
1610
# noinspection PyCompatibility
1711
from concurrent.futures import ThreadPoolExecutor
1812

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+
)
105109

106110

107111
__plugin_name__ = "File Check"
108112
__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+
)
112118

113119
__plugin_implementation__ = FileCheckPlugin()
114120
__plugin_hooks__ = {
115-
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
121+
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
116122
}

octoprint_file_check/static/js/file_check.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
* Author: Gina Häußge
55
* License: AGPLv3
66
*/
7-
$(function() {
7+
$(function () {
88
function FileCheckViewModel(parameters) {
99
var self = this;
1010

1111
var issues = {
12-
travel_speed: gettext("Your sliced file still contains a place holder <code>{travel_speed}</code>. "
13-
+ "This is a common issue when using old start/stop GCODE code snippets in current versions of "
14-
+ "Cura, as the placeholder name switched to <code>{speed_travel}</code> at some point and "
15-
+ "no longer gets replaced in current versions. You need to fix this in your slicer and reslice "
16-
+ "your file, do not print it like it is as it will cause issues!")
17-
}
12+
travel_speed: gettext(
13+
"Your sliced file still contains a place holder <code>{travel_speed}</code>. " +
14+
"This is a common issue when using old start/stop GCODE code snippets in current versions of " +
15+
"Cura, as the placeholder name switched to <code>{speed_travel}</code> at some point and " +
16+
"no longer gets replaced in current versions. You need to fix this in your slicer and reslice " +
17+
"your file, do not print it like it is as it will cause issues!"
18+
)
19+
};
1820

19-
self.onDataUpdaterPluginMessage = function(plugin, data) {
21+
self.onDataUpdaterPluginMessage = function (plugin, data) {
2022
if (plugin !== "file_check") {
2123
return;
2224
}
@@ -26,16 +28,22 @@ $(function() {
2628
return;
2729
}
2830

29-
var faq = "<a href='https://faq.octoprint.org/file-check-"
30-
+ data["type"].replace("_", "-") + "' target='_blank' rel='noreferrer noopener'>"
31-
+ gettext("Read more...") + "</a>";
31+
var faq =
32+
"<a href='https://faq.octoprint.org/file-check-" +
33+
data["type"].replace("_", "-") +
34+
"' target='_blank' rel='noreferrer noopener'>" +
35+
gettext("Read more...") +
36+
"</a>";
3237

3338
new PNotify({
34-
title: _.sprintf(gettext('File Check detected issues with %(storage)s:%(path)s!'), data),
39+
title: _.sprintf(
40+
gettext("File Check detected issues with %(storage)s:%(path)s!"),
41+
data
42+
),
3543
text: "<p>" + message + "</p><p>" + faq + "</p>",
3644
hide: false
3745
});
38-
}
46+
};
3947
}
4048

4149
OCTOPRINT_VIEWMODELS.push({

0 commit comments

Comments
 (0)