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.
This commit is contained in:
voussoir 2022-07-23 00:02:10 -07:00
parent 6dd4cfe59b
commit f8b1cd9178
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
4 changed files with 40 additions and 39 deletions

View file

@ -7,6 +7,7 @@ import datetime
import hashlib import hashlib
import mimetypes import mimetypes
import os import os
import re
import PIL.Image import PIL.Image
import typing import typing
import zipstream import zipstream
@ -181,6 +182,37 @@ def decollide_names(things, namer):
def dict_to_tuple(d) -> tuple: def dict_to_tuple(d) -> tuple:
return tuple(sorted(d.items())) 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: def generate_image_thumbnail(filepath, width, height) -> PIL.Image:
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
raise FileNotFoundError(filepath) raise FileNotFoundError(filepath)
@ -268,37 +300,6 @@ def hash_photoset(photos) -> str:
return hasher.hexdigest() 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: def is_xor(*args) -> bool:
''' '''
Return True if and only if one arg is truthy. Return True if and only if one arg is truthy.

View file

@ -451,7 +451,7 @@ class PDBPhotoMixin:
''' '''
PHOTO PROPERTIES PHOTO PROPERTIES
area, width, height, ratio, bytes, duration: 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. for lower bound.
TAGS AND FILTERS TAGS AND FILTERS
@ -460,7 +460,7 @@ class PDBPhotoMixin:
usernames. usernames.
created: 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. for lower bound.
extension: extension:

View file

@ -76,9 +76,9 @@ def expand_mmf(tag_musts, tag_mays, tag_forbids):
def minmax(key, value, minimums, maximums, warning_bag=None): 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. both minimums and maximums.
('area', '100-200', {}, {}) ('area', '100..200', {}, {})
--> -->
{'area': 100}, {'area': 200} (MODIFIED IN PLACE) {'area': 100}, {'area': 200} (MODIFIED IN PLACE)
''' '''
@ -96,7 +96,7 @@ def minmax(key, value, minimums, maximums, warning_bag=None):
return return
try: try:
(low, high) = helpers.hyphen_range(value) (low, high) = helpers.dotdot_range(value)
except ValueError as exc: except ValueError as exc:
if warning_bag: if warning_bag:
@ -106,7 +106,7 @@ def minmax(key, value, minimums, maximums, warning_bag=None):
else: else:
raise raise
except exceptions.OutOfOrder as exc: except exceptions.MinMaxOutOfOrder as exc:
if warning_bag: if warning_bag:
warning_bag.add(exc) warning_bag.add(exc)
return return

View file

@ -224,9 +224,9 @@
<!-- BEFORE & AFTER SEARCH LINKS --> <!-- BEFORE & AFTER SEARCH LINKS -->
<div id="before_after_links"> <div id="before_after_links">
<a href="/search?created=-{{photo.created_unix}}">&larr;Before</a> <a href="/search?created=..{{photo.created_unix}}">&larr;Before</a>
<span> | </span> <span> | </span>
<a href="/search?created={{photo.created_unix}}-&orderby=created-asc">After&rarr;</a> <a href="/search?created={{photo.created_unix}}..&orderby=created-asc">After&rarr;</a>
</div> </div>
</div> </div>