Skip to content

Commit 313fd47

Browse files
committed
🐛 Fix false positives for hotwords in comments
Closes #1
1 parent a17304c commit 313fd47

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

octoprint_file_check/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,24 @@ def _validate_file(self, storage, path, file_type):
6464
if self._search_through_file(path_on_disk, "{travel_speed}"):
6565
self._notify("travel_speed", storage, path)
6666

67-
def _search_through_file(self, path, term):
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+
6874
try:
6975
# try native grep
70-
result = sarge.run(["grep", "-q", re.escape(term), path])
76+
result = sarge.run(["grep", "-q", pattern, path])
7177
return result.returncode == 0
7278
except ValueError as exc:
7379
if 'Command not found' in str(exc):
7480
try:
7581
# try python only approach
7682
with io.open(path, mode="r", encoding="utf8", errors="replace") as f:
7783
for line in f:
78-
if term in line:
84+
if term in line and (incl_comments or compiled.match(line)):
7985
return True
8086
return False
8187
except:

0 commit comments

Comments
 (0)