Extract code to function search_contents_generic.

master
voussoir 2020-01-25 00:36:50 -08:00
parent 265e106079
commit 08782fdaf9
1 changed files with 30 additions and 25 deletions

View File

@ -30,6 +30,35 @@ def all_terms_match(search_text, terms, match_function):
) )
return matches return matches
def search_contents_generic(filepath, content_args):
try:
with open(filepath.absolute_path, 'r') as handle:
text = handle.read()
except UnicodeDecodeError:
try:
with open(filepath.absolute_path, 'r', encoding='utf-8') as handle:
text = handle.read()
except UnicodeDecodeError:
#safeprint.safeprint(filepath.absolute_path)
#traceback.print_exc()
return
except Exception:
safeprint.safeprint(filepath.absolute_path)
traceback.print_exc()
return
content_args['text'] = text
content_args['line_numbers'] = True
results = search(**content_args)
results = list(results)
if not results:
return
yield filepath.absolute_path
yield from results
yield ''
def search( def search(
*, *,
yes_all=None, yes_all=None,
@ -117,32 +146,8 @@ def search(
filepath = pathclass.Path(search_object) filepath = pathclass.Path(search_object)
if not filepath.is_file: if not filepath.is_file:
continue continue
try:
with open(filepath.absolute_path, 'r') as handle:
text = handle.read()
except UnicodeDecodeError:
try:
with open(filepath.absolute_path, 'r', encoding='utf-8') as handle:
text = handle.read()
except UnicodeDecodeError:
#safeprint.safeprint(filepath.absolute_path)
#traceback.print_exc()
continue
except Exception:
safeprint.safeprint(filepath.absolute_path)
traceback.print_exc()
continue
content_args['text'] = text yield from search_contents_generic(filepath, content_args)
content_args['line_numbers'] = True
results = search(**content_args)
results = list(results)
if not results:
continue
yield filepath.absolute_path
yield from results
yield ''
def argparse_to_dict(args): def argparse_to_dict(args):
text = args.text text = args.text