Skip to content

Commit

Permalink
Added handling of mediatimestamp TimeRange types
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesba committed Aug 1, 2018
1 parent 4448fb6 commit 979cf56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
12 changes: 9 additions & 3 deletions mediajson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from six import string_types
import re

from mediatimestamp import Timestamp, TimeOffset
from mediatimestamp import Timestamp, TimeOffset, TimeRange

__all__ = ["dump", "dumps", "load", "loads",
"encode_value", "decode_value",
Expand Down Expand Up @@ -92,6 +92,8 @@ def encode_value(o, return_no_encode=True):
return o.to_tai_sec_nsec()
elif isinstance(o, TimeOffset):
return o.to_sec_nsec()
elif isinstance(o, TimeRange):
return o.to_sec_nsec_range()
elif isinstance(o, Fraction):
return {"numerator": o.numerator,
"denominator": o.denominator}
Expand All @@ -114,10 +116,14 @@ def decode_value(o):
if re.match(UUID_REGEX,
o):
return uuid.UUID(o)
elif re.match(r'\d+:\d+', o):
elif re.match(r'^\d+:\d+$', o):
return Timestamp.from_tai_sec_nsec(o)
elif re.match(r'(\+|-)\d+:\d+', o):
elif re.match(r'^(\+|-)\d+:\d+$', o):
return TimeOffset.from_sec_nsec(o)
elif re.match(r'^(\(|\[)?(\d+:\d+)?_(\d+:\d+)?(\)|\])?$', o):
return TimeRange.from_str(o)
elif o == "()":
return TimeRange.never()
return o


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Basic metadata
name = 'mediajson'
version = '1.0.0-dev2'
version = '1.0.0-dev3'
description = 'A JSON serialiser and parser for python that supports extensions convenient for our media grain formats'
url = 'https://github.com/bbc/rd-apmm-python-lib-mediajson'
author = u'James P. Weaver'
Expand Down Expand Up @@ -49,7 +49,7 @@ def find_packages(path, base=""):
# This is where you list packages which are required
packages_required = [
"six",
"mediatimestamp >= 1.0.0-dev1"
"mediatimestamp >= 1.0.0-dev4"
]

# This is where you list locations for packages not
Expand Down
18 changes: 15 additions & 3 deletions tests/test_mediajson.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import json
from six import StringIO
from uuid import UUID
from mediatimestamp import Timestamp
from mediatimestamp import Timestamp, TimeRange
from fractions import Fraction

import mediajson
Expand Down Expand Up @@ -48,12 +48,24 @@
"decimal": 0.44,
"uuid": UUID("b8b4a34f-3293-11e8-89c0-acde48001122"),
"rational": Fraction(30000, 1001),
"timestamp": Timestamp.from_sec_nsec("417798915:0")
"timestamp": Timestamp.from_sec_nsec("417798915:0"),
"timeranges": [TimeRange(Timestamp(417798915, 0), Timestamp(417798916, 999), TimeRange.INCLUSIVE),
TimeRange(Timestamp(417798915, 0), Timestamp(417798916, 999), TimeRange.EXCLUSIVE),
TimeRange(Timestamp(417798915, 0), Timestamp(417798916, 999), TimeRange.INCLUDE_START),
TimeRange(Timestamp(417798915, 0), Timestamp(417798916, 999), TimeRange.INCLUDE_END),
TimeRange.never(),
TimeRange.eternity(),
TimeRange.from_start(Timestamp(417798915, 0), TimeRange.INCLUSIVE),
TimeRange.from_start(Timestamp(417798915, 0), TimeRange.EXCLUSIVE),
TimeRange.from_end(Timestamp(417798915, 0), TimeRange.INCLUSIVE),
TimeRange.from_end(Timestamp(417798915, 0), TimeRange.EXCLUSIVE)]
}

MEDIAJSON_STRING = '{"foo": "bar", "baz": ["boop", "beep"], "boggle": {"cat": "\\u732b", "kitten": "\\u5b50\\u732b"}, '\
'"numeric": 25, "boolean": true, "decimal": 0.44, "uuid": "b8b4a34f-3293-11e8-89c0-acde48001122", '\
'"rational": {"numerator": 30000, "denominator": 1001}, "timestamp": "417798915:0"}'
'"rational": {"numerator": 30000, "denominator": 1001}, "timestamp": "417798915:0",'\
'"timeranges": ["[417798915:0_417798916:999]", "(417798915:0_417798916:999)", "[417798915:0_417798916:999)", '\
'"(417798915:0_417798916:999]", "()", "_", "[417798915:0_", "(417798915:0_", "_417798915:0]", "_417798915:0)"]}'


class TestJSON(unittest.TestCase):
Expand Down

0 comments on commit 979cf56

Please sign in to comment.