File: extensions.py

package info (click to toggle)
json-schema-validator 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 308 kB
  • ctags: 129
  • sloc: python: 2,256; makefile: 21; sh: 2
file content (94 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of json-schema-validator.
#
# json-schema-validator is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# json-schema-validator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with json-schema-validator.  If not, see <http://www.gnu.org/licenses/>.

"""
Date-time extension, allows to serialize and deserialize datetime
objects in a consistent way. Implements equivalent of schema:

{
    "type": "string",
    "format": "date-time"
}
"""

from datetime import datetime, timedelta
import re


class datetime_extension(object):
    """
    Proxy class for serializing datetime.datetime objects.

    The serialization is a JSON string. Date is encoded
    using the ISO 8601 format:
        YYYY-MM-DDThh:mm:ssZ

    That is:
        * Four digit year code
        * Dash
        * Two digit month code
        * Dash
        * Two digit day code
        * Capital letter 'T' - time stamp indicator
        * Two digit hour code
        * Colon
        * Two digit minute code
        * Colon
        * Two digit seconds code
        * Capital letter 'Z' - Zulu (UTC) time zone indicator
    """

    FORMAT = "%Y-%m-%dT%H:%M:%SZ"

    @classmethod
    def to_json(cls, obj):
        return obj.strftime(cls.FORMAT)

    @classmethod
    def from_json(cls, doc):
        return datetime.strptime(doc, cls.FORMAT)


class timedelta_extension(object):
    """
    Proxy for serializing datetime.timedelta instances
    """
    PATTERN = re.compile("^(\d+)d (\d+)s (\d+)us$")

    @classmethod
    def to_json(cls, obj):
        """
        Serialize wrapped datetime.timedelta instance to a string the
        with the following format:
            [DAYS]d [SECONDS]s [MICROSECONDS]us
        """
        return "{0}d {1}s {2}us".format(
                obj.days, obj.seconds, obj.microseconds)

    @classmethod
    def from_json(cls, doc):
        """
        Deserialize JSON document (string) to datetime.timedelta instance
        """
        if not isinstance(doc, basestring):
            raise TypeError("JSON document must be a string")
        match = cls.PATTERN.match(doc)
        if not match:
            raise ValueError("JSON document must match expected pattern")
        days, seconds, microseconds = map(int, match.groups())
        return timedelta(days, seconds, microseconds)