-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmsu-shell
executable file
·90 lines (72 loc) · 2.46 KB
/
tmsu-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import argparse
import subprocess
import sys
import typing
def get_tags(path_db) -> typing.List[str]:
proc = subprocess.run(['tmsu', '-D', path_db, 'tags'],
stdout=subprocess.PIPE)
return proc.stdout.decode('utf8').rstrip().split('\n')
def print_tags(path_db: str, fname: str, tags: typing.List[str]):
proc = subprocess.run(['tmsu', '-D', path_db, 'tags', fname],
stdout=subprocess.PIPE)
ret = proc.stdout.decode('utf8').rstrip().split(': ')
current_tags: typing.Set[str] = set()
if len(ret) != 1:
current_tags = set(ret[1].split())
print(fname)
for idx, tag in enumerate(tags):
mark = ' '
if tag in current_tags:
mark = '*'
print(f'{mark} [{idx}] {tag}')
def set_tags(path_db: str, fname: str, tags: typing.List[str],
line: str, remove=False):
new_tags = []
for item in line.strip().split():
if item.startswith('!'):
new_tags.append(item[1:])
continue
try:
new_tags.append(tags[int(item)])
except ValueError:
print(f' Not number: {item}')
pass
except IndexError:
print(f' Invalid number: {item}')
pass
print("Selected tags: ", new_tags)
if len(new_tags) == 0:
return
cmd = 'tag'
if remove:
cmd = 'untag'
proc = subprocess.run(['tmsu', '-D', path_db, cmd, fname] + new_tags,
stdout=subprocess.PIPE)
print(proc.stdout.decode('utf8'))
def operation(path_db: str) -> None:
fname = ''
for line in iter(sys.stdin.readline, ""):
if line.startswith("'"):
tags = get_tags(path_db)
fname = line[1:-3]
print(fname)
print_tags(path_db, fname, tags)
elif len(fname) > 0 and len(line) > 1:
tags = get_tags(path_db)
if line.startswith('l'):
print_tags(path_db, fname, tags)
elif line.startswith('u'):
print("Untag")
set_tags(path_db, fname, tags, line[1:], True)
else:
set_tags(path_db, fname, tags, line)
def get_opts() -> argparse.Namespace:
oparser = argparse.ArgumentParser()
oparser.add_argument("--db", "-D", required=True)
return oparser.parse_args()
def main() -> None:
opts = get_opts()
operation(opts.db)
if __name__ == '__main__':
main()