Skip to content

Commit

Permalink
desktop.filetag: use more returns and less nested conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-fg committed Dec 7, 2023
1 parent 651456f commit e9ce6af
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions desktop/filetag
Original file line number Diff line number Diff line change
Expand Up @@ -356,45 +356,48 @@ def main(args=None):
raise
else: p_db_tmp.rename(p_db)

if opts.tags:
print_func = lambda *line: print(*line, end='\n' if not opts.print0 else '\0')
try:
with TagDB(p_db) as db:

if not opts.path:
tagsets = list(set(ts.split('+')) for ts in ' '.join(opts.tags).split())
for p_str in db.lookup_tags(tagsets):
if opts.existing and not os.path.exists(p_str): continue
print_func(p_str)

else:
paths = set()
for p_str in opts.tags:
p = pl.Path(p_str).resolve()
if opts.existing and not p.exists(): continue
paths.add(str(p))
for p_str, tags in db.lookup_paths(paths):
print_func(p_str, '::', ' '.join(tags))
paths.remove(p_str)
if paths: # check if tags are missing due to filtering lists
for p_str in paths:
for p_scan in p_scan_list:
p = str(p_scan.resolve())
if not p_str.startswith(f'{p}/'): continue
p_str = p_str[len(p):]
break
else: continue # can still be a symlink, but whatever
p_chunks = p_str.split('/')
for n in range(1, len(p_chunks)):
p_dir = '/'.join(p_chunks[:n]) + '/'
if not path_filter.check(p_dir):
p_err( f'{p_str}: parent dir {p_dir!r}'
f' is ignored due to filtering (scan root: {p_scan})' )
break
else:
if not path_filter.check(p_str):
p_err('{p_str}: path ignored due to filtering (scan root: {p_scan})')

except BrokenPipeError: return

if __name__ == '__main__': sys.exit(main())
if not opts.tags: return

print_func = lambda *line: print(*line, end='\n' if not opts.print0 else '\0')
with TagDB(p_db) as db:

if not opts.path:
tagsets = list(set(ts.split('+')) for ts in ' '.join(opts.tags).split())
for p_str in db.lookup_tags(tagsets):
if opts.existing and not os.path.exists(p_str): continue
print_func(p_str)
return

paths = set()
for p_str in opts.tags:
p = pl.Path(p_str).resolve()
if opts.existing and not p.exists(): continue
paths.add(str(p))
for p_str, tags in db.lookup_paths(paths):
print_func(p_str, '::', ' '.join(tags))
paths.remove(p_str)
if not paths: return # check if tags are missing due to filtering lists

for p_str in paths:
for p_scan in p_scan_list:
p = str(p_scan.resolve())
if not p_str.startswith(f'{p}/'): continue
p_str = p_str[len(p):]
break
else: continue # can still be a symlink, but whatever
p_chunks = p_str.split('/')
for n in range(1, len(p_chunks)):
p_dir = '/'.join(p_chunks[:n]) + '/'
if not path_filter.check(p_dir):
p_err( f'{p_str}: parent dir {p_dir!r}'
f' is ignored due to filtering (scan root: {p_scan})' )
break
else:
if not path_filter.check(p_str):
p_err('{p_str}: path ignored due to filtering (scan root: {p_scan})')

if __name__ == '__main__':
try: sys.exit(main())
except BrokenPipeError: # stdout pipe closed
os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
sys.exit(1)

0 comments on commit e9ce6af

Please sign in to comment.