-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
62 lines (49 loc) · 1.91 KB
/
cli.py
File metadata and controls
62 lines (49 loc) · 1.91 KB
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
# cli.py
import argparse
import json
from pathlib import Path
from .indexer import VaultIndexer
from .orm import Note, Link
def main():
parser = argparse.ArgumentParser(description="Obsidian Vault SQL")
parser.add_argument('--db', default='vault.db', help='Database path')
sub = parser.add_subparsers(dest='cmd', required=True)
# Sync command
sync_p = sub.add_parser('sync', help='Index vault')
sync_p.add_argument('vault_path', nargs='?', default='.')
# Query command using ORM
query_p = sub.add_parser('query', help='Query vault')
query_p.add_argument('model', choices=['note', 'link', 'attachment'])
query_p.add_argument('--filter', '-f', action='append', help='Filter: key=value')
query_p.add_argument('--exclude', '-e', action='append', help='Exclude: key=value')
query_p.add_argument('--limit', '-n', type=int, default=20)
query_p.add_argument('--json', action='store_true')
args = parser.parse_args()
if args.cmd == 'sync':
indexer = VaultIndexer(args.vault_path, args.db)
indexer.sync()
elif args.cmd == 'query':
model = {'note': Note, 'link': Link}[args.model]
qs = model.objects.all()
if args.filter:
for f in args.filter:
key, val = f.split('=', 1)
qs = qs.filter(**{key: val})
if args.exclude:
for e in args.exclude:
key, val = e.split('=', 1)
qs = qs.exclude(**{key: val})
qs = qs[:args.limit]
# Output
data = [{
'id': obj.pk,
'path': getattr(obj, 'path', None),
'title': getattr(obj, 'title', None),
} for obj in qs]
if args.json:
print(json.dumps(data, indent=2, default=str))
else:
for item in data:
print(f"{item['id']}: {item.get('title') or item.get('path')}")
if __name__ == '__main__':
main()