Skip to content

Commit 3108ca4

Browse files
committed
Allow pattern search to return match instead of just boolean
1 parent 2003329 commit 3108ca4

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

adafruit_shell.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def reconfig(self, file, pattern, replacement):
282282
# Not found; append (silently)
283283
self.write_text_file(file, replacement, append=True)
284284

285-
def pattern_search(self, location, pattern, multi_line=False):
285+
def pattern_search(self, location, pattern, multi_line=False, return_match=False):
286286
"""
287287
Similar to grep, but uses pure python
288288
multi_line will search the entire file as a large text glob,
@@ -296,13 +296,17 @@ def pattern_search(self, location, pattern, multi_line=False):
296296
if self.exists(location) and not self.isdir(location):
297297
if multi_line:
298298
with open(location, "r+", encoding="utf-8") as file:
299-
if re.search(pattern, file.read(), flags=re.DOTALL):
299+
match = re.search(pattern, file.read(), flags=re.DOTALL)
300+
if match:
300301
found = True
301302
else:
302303
for line in fileinput.FileInput(location):
303-
if re.search(pattern, line):
304+
match = re.search(pattern, line)
305+
if match:
304306
found = True
305-
307+
break
308+
if return_match:
309+
return match
306310
return found
307311

308312
def pattern_replace(self, location, pattern, replace="", multi_line=False):

0 commit comments

Comments
 (0)