From 414c81127f6436a9c97f1c6ade625d1113283050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dias=20Conde=20Azevedo?= Date: Tue, 7 Jun 2022 17:33:38 +0100 Subject: [PATCH] fix: review fixes --- src/ripe/base.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/ripe/base.py b/src/ripe/base.py index f96e9be..1327f0f 100644 --- a/src/ripe/base.py +++ b/src/ripe/base.py @@ -64,9 +64,12 @@ def _query_to_spec(cls, query): description = options.get("description", None) initials = options.get("initials", None) engraving = options.get("engraving", None) - initials_extra = options.get("initials_extra", []) + gender = options.get("gender", None) + size = options.get("size", None) + meta = options.get("meta", []) tuples = options.get("p", []) tuples = tuples if isinstance(tuples, list) else [tuples] + initials_extra = options.get("initials_extra", []) initials_extra = ( initials_extra if isinstance(initials_extra, list) else [initials_extra] ) @@ -87,11 +90,17 @@ def _query_to_spec(cls, query): spec["version"] = version if description: spec["description"] = description + if gender: + spec["gender"] = gender + if size: + spec["size"] = size + if meta: + spec["meta"] = cls._normalize_meta(meta) return spec @classmethod def _unpack_query(cls, query): - query = query[1:] if query[0] == "?" else query + query = query.strip("?") parts = appier.split_unescape(query, "&") options = dict() for part in parts: @@ -131,6 +140,41 @@ def _parts_to_parts_m(cls, parts): parts_m[name] = dict(material=material, color=color) return parts_m + @classmethod + def _normalize_meta(cls, meta): + meta_d = {} + meta_l = ( + [ + appier.split_unescape(element, ":", 2) + if element.startswith("$") + else appier.split_unescape(element, ":", 1) + for element in meta + ] + if meta + else [] + ) + for parts in meta_l: + if len(parts) == 2: + parts = None, parts[0], parts[1] + type, key, value = parts + if key in meta_d: + old = meta_d[key] + is_sequence = isinstance(old, (list, tuple)) + if not is_sequence: + old = [old] + old.append(value) + value = old + if type == "$list" and not isinstance(value, list): + value = [value] + if type == "$int": + value = int(value) + if type == "$float": + value = float(value) + if type == "$bool": + value = value in ("1", "true", "True") + meta_d[key] = value + return meta_d + def __init__(self, *args, **kwargs): appier.API.__init__(self, *args, **kwargs) self.base_url = appier.conf("RIPE_BASE_URL", RIPE_BASE_URL)