From 52ac62d6345ca1db27368b19d197f968448ae436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fik=C3=B3=20Gy=C3=B6rgyi?= Date: Mon, 6 Dec 2021 21:10:36 +0100 Subject: [PATCH] * Handling wif files with no "color" field in warp & weft sections. Test file: https://www.handweaving.net/draft-detail/62968/threading-draft-from-divisional-profile-tieup-draft-8776-2005-2015 * Fix to avoid pymongo error: "NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None" --- api/chalicelib/util/database.py | 3 ++- api/chalicelib/util/wif.py | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/api/chalicelib/util/database.py b/api/chalicelib/util/database.py index c36e698..436a5a2 100644 --- a/api/chalicelib/util/database.py +++ b/api/chalicelib/util/database.py @@ -6,6 +6,7 @@ db = None def get_db(): global db - if not db: + + if db is None: db = MongoClient(os.environ['MONGO_URL'])[os.environ['MONGO_DATABASE']] return db diff --git a/api/chalicelib/util/wif.py b/api/chalicelib/util/wif.py index 7c9f345..2f8b331 100644 --- a/api/chalicelib/util/wif.py +++ b/api/chalicelib/util/wif.py @@ -129,10 +129,15 @@ def loads(wif_file): draft['warp'] = {} draft['warp']['shafts'] = weaving.getint('shafts') draft['warp']['threading'] = [] - draft['warp']['defaultColour'] = draft['colours'][warp.getint('color')-1] - warp_colour_index = warp.getint('color') - 1 - # In case of no color table or colour index out of bounds - draft['warp']['defaultColour'] = draft['colours'][warp_colour_index] if len(draft['colours']) > warp_colour_index else draft['colours'][0] + + + if warp.get('color'): + warp_colour_index = warp.getint('color') - 1 + draft['warp']['defaultColour'] = draft['colours'][warp_colour_index] + + else: + # In case of no color table or colour index out of bounds + draft['warp']['defaultColour'] = draft['colours'][0] for x in threading: shaft = threading[x] @@ -155,9 +160,13 @@ def loads(wif_file): draft['weft'] = {} draft['weft']['treadles'] = weaving.getint('treadles') draft['weft']['treadling'] = [] - weft_colour_index = weft.getint('color') - 1 - # In case of no color table or colour index out of bounds - draft['weft']['defaultColour'] = draft['colours'][weft_colour_index] if len(draft['colours']) > weft_colour_index else draft['colours'][1] + + if weft.get('color'): + weft_colour_index = weft.getint('color') - 1 + draft['weft']['defaultColour'] = draft['colours'][weft_colour_index] + else: + # In case of no color table or colour index out of bounds + draft['weft']['defaultColour'] = draft['colours'][1] for x in treadling: shaft = treadling[x] -- 2.45.2