Skip to content

Commit 01be1e4

Browse files
committed
Allow finding multiple items
1 parent 0a414a2 commit 01be1e4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

adafruit_shell.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ def reconfig(self, file, pattern, replacement):
384384
# Not found; append (silently)
385385
self.write_text_file(file, replacement, append=True)
386386

387-
def pattern_search(self, location, pattern, multi_line=False, return_match=False):
387+
# pylint: disable=too-many-arguments
388+
def pattern_search(
389+
self, location, pattern, multi_line=False, return_match=False, find_all=False
390+
):
388391
"""
389392
Similar to grep, but uses pure python
390393
multi_line will search the entire file as a large text glob,
@@ -394,16 +397,17 @@ def pattern_search(self, location, pattern, multi_line=False, return_match=False
394397
"""
395398
location = self.path(location)
396399
found = False
400+
search_function = re.findall if find_all else re.search
397401

398402
if self.exists(location) and not self.isdir(location):
399403
if multi_line:
400404
with open(location, "r+", encoding="utf-8") as file:
401-
match = re.search(pattern, file.read(), flags=re.DOTALL)
405+
match = search_function(pattern, file.read(), flags=re.DOTALL)
402406
if match:
403407
found = True
404408
else:
405409
for line in fileinput.FileInput(location):
406-
match = re.search(pattern, line)
410+
match = search_function(pattern, line)
407411
if match:
408412
found = True
409413
break

0 commit comments

Comments
 (0)