mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-16 08:28:52 +01:00
53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
import re
|
|
|
|
LINK_SPLIT = re.compile(',\s*(?=[<])')
|
|
LINK_SEG_SPLIT = re.compile(';\s*')
|
|
LINK_URL = re.compile('<(.*)>')
|
|
LINK_PROP = re.compile('([\w]+)="([^"]+)')
|
|
|
|
|
|
#=================================================================
|
|
class MementoUtils(object):
|
|
@staticmethod
|
|
def parse_links(link_header, def_name='timemap'):
|
|
links = LINK_SPLIT.split(link_header)
|
|
results = {}
|
|
mementos = []
|
|
|
|
for link in links:
|
|
props = LINK_SEG_SPLIT.split(link)
|
|
m = LINK_URL.match(props[0])
|
|
if not m:
|
|
raise Exception('Invalid Link Url: ' + props[0])
|
|
|
|
result = dict(url=m.group(1))
|
|
key = ''
|
|
is_mem = False
|
|
|
|
for prop in props[1:]:
|
|
m = LINK_PROP.match(prop)
|
|
if not m:
|
|
raise Exception('Invalid prop ' + prop)
|
|
|
|
name = m.group(1)
|
|
value = m.group(2)
|
|
|
|
if name == 'rel':
|
|
if 'memento' in value:
|
|
is_mem = True
|
|
result[name] = value
|
|
elif value == 'self':
|
|
key = def_name
|
|
else:
|
|
key = value
|
|
else:
|
|
result[name] = value
|
|
|
|
if key:
|
|
results[key] = result
|
|
elif is_mem:
|
|
mementos.append(result)
|
|
|
|
results['mementos'] = mementos
|
|
return results
|