def lcc_transform(raw): """ Transform the lcc search field value :param str raw: :rtype: str """ # e.g. lcc:[NC1 TO NC1000] to lcc:[NC-0001.00000000 TO NC-1000.00000000] # for proper range search m = re_range.match(raw) if m: lcc_range = [m.group('start').strip(), m.group('end').strip()] normed = normalize_lcc_range(*lcc_range) return f'[{normed[0] or lcc_range[0]} TO {normed[1] or lcc_range[1]}]' elif '*' in raw and not raw.startswith('*'): # Marshals human repr into solr repr # lcc:A720* should become A--0720* parts = raw.split('*', 1) lcc_prefix = normalize_lcc_prefix(parts[0]) return (lcc_prefix or parts[0]) + '*' + parts[1] else: normed = short_lcc_to_sortable_lcc(raw.strip('"')) if normed: use_quotes = ' ' in normed or raw.startswith('"') return ('"%s"' if use_quotes else '%s*') % normed # If none of the transforms took return raw
def test_normalize_lcc_prefix(prefix, normed, name): assert normalize_lcc_prefix(prefix) == normed