This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
thicket_db.py
378 lines (318 loc) · 12.7 KB
/
thicket_db.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# ThicketDB: Laubwerk Plants database for Thicket Blender Add-on
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Copyright (C) 2020 Darren Hart <[email protected]>
import argparse
from collections import deque
import glob
import hashlib
import json
import logging
import os
from pathlib import Path
from subprocess import Popen, PIPE
import sys
import textwrap
try:
import laubwerk as lbw
from . import logger
except ImportError:
# Likely running as a subprocess, these will be added in main()
pass
# <pep8 compliant>
SCHEMA_VERSION = 2
def md5sum(filename):
md5 = hashlib.md5()
with open(filename, mode="rb") as f:
buf = f.read(4096)
while buf:
md5.update(buf)
buf = f.read(4096)
return md5.hexdigest()
class DBQualifier:
def __init__(self, db, name):
self.name = name
self.label = db.get_label(name)
class DBModel:
def __init__(self, db, name, m_rec, plant_preview):
self.name = name
self.label = db.get_label(self.name)
self.qualifiers = [DBQualifier(db, q) for q in m_rec["qualifiers"]]
self._default_qualifier = DBQualifier(db, m_rec["default_qualifier"])
self.preview = m_rec["preview"]
if self.preview == "":
self.preview = plant_preview
def get_qualifier(self, name=None):
""" Return the requested qualifier or the default qualifier if None or not found """
if name is not None:
for q in self.qualifiers:
if q.name == name:
return q
return self._default_qualifier
class DBPlant:
def __init__(self, db, name):
self.name = name
p_rec = db._db["plants"][name]
self.md5 = p_rec["md5"]
self.filepath = p_rec["filepath"]
self.label = db.get_label(self.name)
preview = p_rec["preview"]
self.models = [DBModel(db, m, p_rec["models"][m], preview) for m in p_rec["models"]]
def_m = p_rec["default_model"]
self._default_model = DBModel(db, def_m, p_rec["models"][def_m], preview)
self.preview = preview
def get_model(self, name=None):
""" Return the requested model or the default model if None or not found """
if name is not None:
for m in self.models:
if m.name == name:
return m
return self._default_model
class DBIter:
def __init__(self, db):
self._items = []
self._index = 0
for name in db._db["plants"]:
self._items.append(DBPlant(db, name))
self._items.sort(key=lambda plant: plant.name)
def __next__(self):
if self._index < len(self._items):
item = self._items[self._index]
self._index += 1
return item
raise StopIteration
class ThicketDBOldSchemaError(Exception):
# TODO: include current and read schema version
pass
class ThicketDB:
""" Thicket Database Interface """
def __init__(self, db_filename, locale="en-US", python=sys.executable, create=False):
global SCHEMA_VERSION
self._db_filename = db_filename
self.locale = locale.replace("_", "-")
self.python = python
try:
with open(db_filename, "r", encoding="utf-8") as f:
self._db = json.load(f)
if self._db["info"]["schema_version"] < SCHEMA_VERSION:
logger.warning("Unknown database schema version")
raise ThicketDBOldSchemaError
except FileNotFoundError:
if create:
self.initialize()
self.save()
else:
raise FileNotFoundError
except json.decoder.JSONDecodeError as e:
logger.critical("JSONDecodeError while loading database: %s" % e)
def __iter__(self):
return DBIter(self)
def initialize(self):
global SCHEMA_VERSION
self._db = {}
self._db["info"] = {}
self._db["labels"] = {}
self._db["plants"] = {}
self._db["info"]["sdk_version"] = lbw.version
self._db["info"]["sdk_major"] = lbw.version_info.major
self._db["info"]["sdk_minor"] = lbw.version_info.minor
self._db["info"]["sdk_micro"] = lbw.version_info.micro
self._db["info"]["schema_version"] = SCHEMA_VERSION
def save(self):
with open(self._db_filename, "w", encoding="utf-8") as f:
json.dump(self._db, f, ensure_ascii=False, indent=4)
def print_info(self):
info = self._db["info"]
print("Laubwerk Version: %s" % info["sdk_version"])
print("\tmajor: %s" % info["sdk_major"])
print("\tminor: %s" % info["sdk_minor"])
print("\tmicro: %s" % info["sdk_micro"])
print("Loaded %d plants:" % self.plant_count())
def update_labels(self, labels):
self._db["labels"].update(labels)
def get_label(self, key, locale=None):
if locale:
locale = locale.replace("_", "-")
else:
locale = self.locale
try:
if locale in self._db["labels"][key]:
return self._db["labels"][key][locale]
elif locale[:2] in self._db["labels"][key]:
return self._db["labels"][key][locale[:2]]
return key
except KeyError:
return key
def get_plant(self, filepath=None, name=None):
if name:
if name not in self._db["plants"]:
name = None
if name is None and filepath:
for n in self._db["plants"]:
if self._db["plants"][n]["filepath"] == filepath:
name = n
if name:
return DBPlant(self, name)
return None
def add_plant(self, filepath):
p_rec = ThicketDB.parse_plant(filepath)
self._db["plants"][p_rec["name"]] = p_rec["plant"]
self.update_labels(p_rec["labels"])
def plant_count(self):
return len(self._db["plants"])
def build(self, plants_dir, sdk_path):
self.initialize()
# FIXME: .gz is optional
plant_files = glob.glob(plants_dir + "/*/*.lbw.gz")
num_plants = len(plant_files)
num_jobs = os.cpu_count()
if not num_jobs:
num_jobs = 4
jobs = deque()
log_level = logging.getLevelName(logger.level)
logger.info("Parsing %d plants using %d parallel jobs" % (num_plants, num_jobs))
while len(plant_files) > 0 or len(jobs) > 0:
# Keep up to num_jobs jobs running
while len(jobs) < num_jobs and len(plant_files) > 0:
f = plant_files.pop()
logger.debug("Parsing: %s" % f)
job = Popen([self.python, __file__, "-f", f, "-s", sdk_path, "-l", log_level, "parse_plant"],
stdout=PIPE)
jobs.append(job)
# Wait for the oldest job to complete
job = jobs.popleft()
outs, errs = job.communicate()
try:
p_rec = json.loads(outs)
self._db["plants"][p_rec["plant"]["name"]] = p_rec["plant"]
self.update_labels(p_rec["labels"])
logger.info('Added "%s"' % p_rec["plant"]["name"])
except json.decoder.JSONDecodeError as e:
logger.error("JSONDecodeError while parsing %s: %s" % (f, e))
if len(plant_files) > 0:
logger.error("Exited worker loop with %d plant files remaining" % len(plant_files))
if len(jobs) > 0:
logger.error("Exited worker loop with %d jobs still running" % len(jobs))
self.save()
logger.info("Processed %d/%d plants" % (self.plant_count(), num_plants))
def read(self):
self.print_info()
for plant in self:
print("%s (%s)" % (plant.name, plant.label))
print("\tfile: %s" % plant.filepath)
print("\tmd5: %s" % plant.md5)
m = plant.get_model()
print("\tdefault_model: %s (%s)" % (m.name, m.label))
print("\tmodels:")
for m in plant.models:
print("\t\t%s (%s) %s" % (m.name, m.get_qualifier().label,
[q.name for q in m.qualifiers]))
# Class methods
def parse_plant(filepath):
p = lbw.load(filepath)
p_rec = {}
plant = {}
plant["name"] = p.name
plant["filepath"] = filepath
plant["md5"] = md5sum(filepath)
plant["default_model"] = p.default_model.name
preview_stem = p.name.replace(" ", "_").replace(".", "")
preview_path = Path(filepath).parent.absolute() / (preview_stem + ".png")
if not preview_path.is_file():
logger.warning("Preview not found: %s" % preview_path)
preview_path = ""
plant["preview"] = str(preview_path)
labels = {}
p_labels = {}
# Store only the first label per locale
for label in p.labels.items():
p_labels[label[0]] = label[1][0]
labels[p.name] = p_labels
models = {}
i = 0
for m in p.models:
m_rec = {}
seasons = []
q_labels = {}
for q in m.qualifiers:
seasons.append(q)
q_labels[q] = {}
for q_lang in m.qualifier_labels[q].items():
q_labels[q][q_lang[0]] = q_lang[1][0]
labels.update(q_labels)
m_rec["index"] = i
m_rec["qualifiers"] = seasons
m_rec["default_qualifier"] = m.default_qualifier
preview_path = Path(filepath).parent.absolute() / "models" / (preview_stem + "_" + m.name + ".png")
if not preview_path.is_file():
logger.warning("Preview not found: %s" % preview_path)
preview_path = ""
m_rec["preview"] = str(preview_path)
models[m.name] = m_rec
m_labels = {}
# FIXME: highly redundant
for label in m.labels.items():
m_labels[label[0]] = label[1][0]
labels[m.name] = m_labels
i = i + 1
plant["models"] = models
p_rec["plant"] = plant
p_rec["labels"] = labels
return p_rec
def parse_plant_json(filepath):
p_rec = ThicketDB.parse_plant(filepath)
print(json.dumps(p_rec))
def main():
global lbw, logger
argParse = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
Thicket Database Tool
Commands:
read read and print the db contents (requires -d)
build scan plants path and add all plants to a new db (requires -d -p -s)
parse_plant read a plant file and print the plant record json (requires -f -s)
'''))
argParse.add_argument("cmd", choices=["read", "build", "parse_plant"])
argParse.add_argument("-d", help="database filename")
argParse.add_argument("-f", help="Laubwerk Plant filename (lbw.gz)")
argParse.add_argument("-l", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO", help="logger level")
argParse.add_argument("-p", help="Laubwerk Plants path")
argParse.add_argument("-s", help="Laubwerk Python SDK path")
args = argParse.parse_args()
logging.basicConfig(format="%(levelname)s: thicket_db: %(message)s", level=args.l)
logger = logging.getLogger()
if args.s:
# If the SDK path was specified, attempt to import the Laubwerk SDK The
# build and parse_plant commands require the Laubwerk SDK which may or
# may not be in the sys.path depending on the OS, environment, and how
# it was called (from Blender, as a subprocess, or via the command
# line).
if args.s not in sys.path:
sys.path.append(args.s)
import laubwerk as lbw
cmd = args.cmd
if cmd == "read" and args.d:
db = ThicketDB(args.d, create=False)
db.read()
elif cmd == "build" and args.d and args.p and lbw:
db = ThicketDB(args.d, create=True)
db.build(args.p, args.s)
elif cmd == "parse_plant" and args.f and lbw:
ThicketDB.parse_plant_json(args.f)
else:
argParse.print_help()
return 1
if __name__ == "__main__":
main()