Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility function with tests to pad datetime digits and validate #586

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ipwb/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ def resolveMemento(urir, datetime):

@app.route('/memento/<regex("[0-9]{1,14}"):datetime>/<path:urir>')
def showMemento(urir, datetime):
try:
datetime = ipwbUtils.padDigits14(datetime, validate=True)
except ValueError as e:
msg = 'Expected a 4-14 digits valid datetime: {}'.format(datetime)
return Response(msg, status=400)
resolvedMemento = resolveMemento(urir, datetime)

# resolved to a 404, flask Response object returned instead of tuple
Expand Down
19 changes: 19 additions & 0 deletions ipwb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
log.setLevel(logging.ERROR)


dtPattern = re.compile("^(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?$")


def isDaemonAlive(hostAndPort="{0}:{1}".format(IPFSAPI_HOST, IPFSAPI_PORT)):
"""Ensure that the IPFS daemon is running via HTTP before proceeding"""
client = ipfsapi.Client(IPFSAPI_HOST, IPFSAPI_PORT)
Expand Down Expand Up @@ -160,6 +163,22 @@ def getRFC1123OfNow():
return d.strftime('%a, %d %b %Y %H:%M:%S GMT')


def padDigits14(dtstr, validate=False):
'''Pad datetime to make a 14-digit string and optionally validate it'''
match = dtPattern.match(dtstr)
if match:
Y = match.group(1)
m = match.group(2) or '01'
d = match.group(3) or '01'
H = match.group(4) or '00'
M = match.group(5) or '00'
S = match.group(6) or '00'
dtstr = '{}{}{}{}{}{}'.format(Y, m, d, H, M, S)
if validate:
datetime.datetime.strptime(dtstr, '%Y%m%d%H%M%S')
return dtstr


def fetchRemoteFile(path):
try:
r = requests.get(path)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

from ipwb import util


@pytest.mark.parametrize('expected,input', [
('', ''),
('foo', 'foo'),
('18', '18'),
('20181', '20181'),
('20181126134257.123', '20181126134257.123'),
('20180101000000', '2018'),
('20181101000000', '201811'),
('20181126000000', '20181126'),
('20181126130000', '2018112613'),
('20181126134200', '201811261342'),
('20181126134257', '20181126134257'),
])
def test_padDigits14(expected, input):
assert expected == util.padDigits14(input)


@pytest.mark.parametrize('input', [
'',
'foo',
'18',
'20181',
'201800',
'20180132',
'2018010226',
'20180102263127',
'20181126134257.123',
])
def test_padDigits14_inalid(input):
with pytest.raises(ValueError):
util.padDigits14(input, validate=True)