-
Notifications
You must be signed in to change notification settings - Fork 0
/
picky.py
50 lines (41 loc) · 1.14 KB
/
picky.py
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
import os
import shutil
import json
class Tags(dict):
def __init__(self, path, *args, **kwargs):
""" Returns the given JSON export from Picky
as a (path, [tags])-dict
"""
if os.path.exists(path or ''):
self.update(json.load(open(path, 'rb')))
self.update(*args)
self.update(kwargs)
self.path = path
def save(self):
json.dump(self, open(self.path, 'w'))
def find(self, f=lambda v: True):
return {k: v for k, v in self.items() if f(v)}
def merge(*tags):
""" Returns a dict of the merged Tags.
"""
m = {}
for t in tags:
for k, v in t.items():
m.setdefault(k, []).extend(v)
return m
# Merging two sets of Tags:
# t1 = Tags(None, {'1.jpg': ['cat'], '2.jpg': ['dog']})
# t2 = Tags(None, {'1.jpg': ['hat']})
# t3 = Tags(None, merge(t1, t2))
# print(t3)
# Finding a subset of Tags:
# t1 = Tags(None, {
# '1.jpg': ['+'],
# '2.jpg': ['+'],
# '3.jpg': ['-'],
# })
# for f1 in t1.find(lambda tags: '+' in tags):
# f2 = os.path.basename(f1)
# f2 = os.path.join('subset', f2)
# shutil.copy(f1, f2)
# print(f1)