Changeset View
Changeset View
Standalone View
Standalone View
swh/search/utils.py
Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | try: | ||||
datetime.strptime(date_str, "%Y-%m-%d") | datetime.strptime(date_str, "%Y-%m-%d") | ||||
return True | return True | ||||
except Exception: | except Exception: | ||||
try: | try: | ||||
iso8601.parse_date(date_str) | iso8601.parse_date(date_str) | ||||
return True | return True | ||||
except Exception: | except Exception: | ||||
return False | return False | ||||
def to_raw(obj): | |||||
"""Makes the object directly injectable into the | |||||
query language. | |||||
For strings, appends \\ before special characters like ', ", and \\ | |||||
For arrays, applies the same transformation on each element, joins the | |||||
elements and returns a string-like representation of the list. | |||||
>>> to_raw("foo ' bar") | |||||
'"foo \\\' bar"' | |||||
>>> to_raw([r"foo ' bar", r"bar \\\' baz", r'foo " baz']) | |||||
'["foo \\\' bar", "bar \\\\\\\' baz", "foo \\" baz"]' | |||||
""" | |||||
if type(obj) == list: | |||||
items = [to_raw(item) for item in obj] | |||||
return "[" + ", ".join(items) + "]" | |||||
elif type(obj) == str: | |||||
return ( | |||||
'"' | |||||
+ obj.translate({ord("'"): r"\'", ord('"'): r"\"", ord("\\"): r"\\",}) | |||||
+ '"' | |||||
) | |||||
else: | |||||
raise Exception(f"Unexpected item type {type(obj)}") | |||||
vlorentz: call it `unescape` |
call it unescape