-
Notifications
You must be signed in to change notification settings - Fork 4
/
meta.py
executable file
·139 lines (110 loc) · 4.57 KB
/
meta.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# Script for generating clean metadata files for superdirt and friends.
# (c) 2021 Alex McLean and contributors. Distributed under the terms of
# GPLv3, see LICENSE for details.
import argparse
import pathlib
import os,sys
import json
import itertools
import getpass
version = "clean-0.1"
meta_subpath = "_soundmeta"
exts = {'.mp3': 'sample',
'.wav': 'sample',
'.scd': 'supercollider'
}
def merge(a, b):
if isinstance(a, dict) and isinstance(b, dict):
d = dict(a)
d.update({k: merge(a.get(k, None), b[k]) for k in b})
return d
if isinstance(a, list) and isinstance(b, list):
return [merge(x, y) for x, y in itertools.zip_longest(a, b)]
return a if b is None else b
def isSound(path):
if os.path.isfile(path):
extension = os.path.splitext(path)[1].lower()
if extension in exts.keys():
return True
return False
def makeMeta(shortname, defaultMeta, pack_folder, subfolder, write):
subpath = os.path.join(pack_folder, subfolder)
metapath = os.path.join(pack_folder, meta_subpath)
if not os.path.exists(metapath):
os.makedirs(metapath)
metafile = os.path.join(metapath, shortname + ".json")
sounds = [fn for fn in os.listdir(subpath) if isSound(os.path.join(subpath, fn))]
sounds.sort()
sound_types = set(map(lambda x: exts[os.path.splitext(x)[1].lower()], sounds))
if len(sound_types) > 1:
defaultMeta['sound_type'] = "mixed"
else:
defaultMeta['sound_type'] = sound_types.pop()
defaultMeta['sounds'] = list(map(lambda x: {'filename': os.path.join(subfolder,x), 'shortname': os.path.splitext(x)[0], 'description': ''}, sounds))
if (os.path.exists(metafile)):
with open(metafile) as json_file:
meta = json.load(json_file)
meta = merge(defaultMeta,meta)
else:
meta = defaultMeta
if write:
with open(metafile, 'w') as json_file:
json.dump(meta, json_file, sort_keys=True, indent=2)
return(meta)
def getArgs():
parser = argparse.ArgumentParser(description='Create metadata for a sample folder.')
parser.add_argument('pack_folder', type=str)
parser.add_argument('--quiet', default=False, action="store_true")
parser.add_argument('--sample-subfolder', dest="subfolder", default="", type=str)
parser.add_argument('--shortname', type=str,
help='Identifier for the sampleset. Used to name the metadata file and to reference the sampleset elsewhere. Defaults to the name of the containing folder.'
)
parser.add_argument('--maintainer', type=str,
help='Name of maintainer'
)
parser.add_argument('--email', type=str,
help='Email of maintainer'
)
parser.add_argument('--copyright', type=str,
help='Copyright statement, e.g. (c) Gregory Coleman, 1969'
)
parser.add_argument('--license', type=str,
help='e.g. CC0 for creative commons zero (public domain)'
)
parser.add_argument('--provenance', type=str,
help='Where the samples came from, e.g. a freesound.org URL'
)
parser.add_argument('--description', type=str,
help='Description of a sample pack'
)
parser.add_argument('--write', default=False,
help='write/re-write the metadata with default values', action="store_true"
)
args = parser.parse_args()
if (not os.path.exists(args.pack_folder)):
sys.exit("Couldn't open '" + args.pack_folder + "'")
subpath = os.path.join(args.pack_folder, args.subfolder)
if (not os.path.exists(subpath)):
sys.exit("Couldn't open '" + subpath + "'")
return args
args = getArgs()
pack_folder = args.pack_folder
if args.shortname:
shortname = args.shortname
else:
shortname = os.path.basename(os.path.normpath(os.path.join(args.pack_folder, args.subfolder)))
defaultMeta = {
'metadata-format': version,
'shortname': shortname,
'license': args.license if args.license else "unknown",
'copyright': args.copyright if args.copyright else "unknown",
'provenance': args.provenance if args.provenance else "",
'maintainer': args.maintainer if args.maintainer else getpass.getuser(),
'email': args.email if args.email else "",
'description': args.description if args.description else "",
}
meta = makeMeta(shortname, defaultMeta, pack_folder, args.subfolder, args.write)
if not args.quiet:
print(json.dumps(meta, sort_keys=True, indent=2))
exit(0)