From f8b1cd9178a18cfb158f95243922e49d182039f3 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 23 Jul 2022 00:02:10 -0700 Subject: [PATCH] Rename hyphen_range to dotdot_range a..b so we can have negatives. Hyphen range doesn't allow negative numbers. The dot-dot syntax is used by git, ruby, and others so I think it's a good pick. --- etiquette/helpers.py | 63 ++++++++++--------- etiquette/photodb.py | 4 +- etiquette/searchhelpers.py | 8 +-- .../etiquette_flask/templates/photo.html | 4 +- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 1eae8b0..fd951ca 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -7,6 +7,7 @@ import datetime import hashlib import mimetypes import os +import re import PIL.Image import typing import zipstream @@ -181,6 +182,37 @@ def decollide_names(things, namer): def dict_to_tuple(d) -> tuple: return tuple(sorted(d.items())) +def dotdot_range(s) -> tuple: + ''' + Given a string like '1..3', return numbers (1, 3) representing lower + and upper bounds. + + Supports bytestring.parsebytes and hh:mm:ss format, for example + '1k..2k', '10:00..20:00', '4gib..' + ''' + s = s.strip() + s = s.replace(' ', '') + if not s: + return (None, None) + + parts = s.split('..') + parts = [part.strip() or None for part in parts] + + if len(parts) == 1: + (low, high) = (parts[0], None) + elif len(parts) == 2: + (low, high) = parts + else: + raise ValueError('Too many dots.') + + low = parse_unit_string(low) + high = parse_unit_string(high) + + if low is not None and high is not None and low > high: + raise exceptions.MinMaxOutOfOrder(range=s, min=low, max=high) + + return (low, high) + def generate_image_thumbnail(filepath, width, height) -> PIL.Image: if not os.path.isfile(filepath): raise FileNotFoundError(filepath) @@ -268,37 +300,6 @@ def hash_photoset(photos) -> str: return hasher.hexdigest() -def hyphen_range(s) -> tuple: - ''' - Given a string like '1-3', return numbers (1, 3) representing lower - and upper bounds. - - Supports bytestring.parsebytes and hh:mm:ss format, for example - '1k-2k', '10:00-20:00', '4gib-' - ''' - s = s.strip() - s = s.replace(' ', '') - if not s: - return (None, None) - - parts = s.split('-') - parts = [part.strip() or None for part in parts] - - if len(parts) == 1: - (low, high) = (parts[0], None) - elif len(parts) == 2: - (low, high) = parts - else: - raise ValueError('Too many hyphens.') - - low = parse_unit_string(low) - high = parse_unit_string(high) - - if low is not None and high is not None and low > high: - raise exceptions.MinMaxOutOfOrder(range=s, min=low, max=high) - - return (low, high) - def is_xor(*args) -> bool: ''' Return True if and only if one arg is truthy. diff --git a/etiquette/photodb.py b/etiquette/photodb.py index f8e4428..629e0a2 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -451,7 +451,7 @@ class PDBPhotoMixin: ''' PHOTO PROPERTIES area, width, height, ratio, bytes, duration: - A hyphen_range string representing min and max. Or just a number + A dotdot_range string representing min and max. Or just a number for lower bound. TAGS AND FILTERS @@ -460,7 +460,7 @@ class PDBPhotoMixin: usernames. created: - A hyphen_range string respresenting min and max. Or just a number + A dotdot_range string respresenting min and max. Or just a number for lower bound. extension: diff --git a/etiquette/searchhelpers.py b/etiquette/searchhelpers.py index 12b003a..20029aa 100644 --- a/etiquette/searchhelpers.py +++ b/etiquette/searchhelpers.py @@ -76,9 +76,9 @@ def expand_mmf(tag_musts, tag_mays, tag_forbids): def minmax(key, value, minimums, maximums, warning_bag=None): ''' - Dissects a hyphenated range string and inserts the correct k:v pair into + Dissects a dotdot_range string and inserts the correct k:v pair into both minimums and maximums. - ('area', '100-200', {}, {}) + ('area', '100..200', {}, {}) --> {'area': 100}, {'area': 200} (MODIFIED IN PLACE) ''' @@ -96,7 +96,7 @@ def minmax(key, value, minimums, maximums, warning_bag=None): return try: - (low, high) = helpers.hyphen_range(value) + (low, high) = helpers.dotdot_range(value) except ValueError as exc: if warning_bag: @@ -106,7 +106,7 @@ def minmax(key, value, minimums, maximums, warning_bag=None): else: raise - except exceptions.OutOfOrder as exc: + except exceptions.MinMaxOutOfOrder as exc: if warning_bag: warning_bag.add(exc) return diff --git a/frontends/etiquette_flask/templates/photo.html b/frontends/etiquette_flask/templates/photo.html index 326cdef..b43e406 100644 --- a/frontends/etiquette_flask/templates/photo.html +++ b/frontends/etiquette_flask/templates/photo.html @@ -224,9 +224,9 @@