Skip to content
Snippets Groups Projects
Commit e341f9e8 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Refactored json_default, implemented to_json method to Idea classes

parent c5f35bc7
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,6 @@ from idea import lite
from idea import valid
import json
from jsonschema import Draft4Validator
import typedcol
import timeit
raw_idea = {
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016, CESNET, z. s. p. o.
# Copyright (c) since 2016, CESNET, z. s. p. o.
# Use of this source is governed by an ISC license, see LICENSE file.
__version__ = '0.1.7'
__version__ = '0.1.8'
__author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>'
......@@ -336,10 +336,14 @@ class IdeaBase(typedcols.TypedDict):
allow_unknown = True
typedef = {}
def json_default(o):
if isinstance(o, typedcols.TypedDict):
return o.data
elif isinstance(o, typedcols.TypedList):
return o.data
else:
raise ValueError(o)
@staticmethod
def json_default(o):
if isinstance(o, typedcols.TypedDict):
return o.data
elif isinstance(o, typedcols.TypedList):
return o.data
else:
raise ValueError(o)
def to_json(self, *args, **kwargs):
return json.dumps(self, default=self.json_default, *args, **kwargs)
......@@ -232,15 +232,16 @@ class Idea(base.IdeaBase):
typedcols.typed_list("AttachList", AttachDict),
typedcols.typed_list("NodeList", NodeDict))
def json_default(o):
if isinstance(o, (ipranges.IPBase, uuid.UUID)):
return str(o)
elif isinstance(o, datetime.datetime):
return o.isoformat() + "Z"
elif isinstance(o, datetime.timedelta):
hours = o.seconds // 3600
mins = (o.seconds % 3600) // 60
secs = o.seconds % 60
return "%s%02i:%02i:%02i%s" % (("%iD" % o.days) if o.days else "", hours, mins, secs, (".%i" % o.microseconds) if o.microseconds else "")
else:
return base.json_default(o)
@staticmethod
def json_default(o):
if isinstance(o, (ipranges.IPBase, uuid.UUID)):
return str(o)
elif isinstance(o, datetime.datetime):
return o.isoformat() + "Z"
elif isinstance(o, datetime.timedelta):
hours = o.seconds // 3600
mins = (o.seconds % 3600) // 60
secs = o.seconds % 60
return "%s%02i:%02i:%02i%s" % (("%iD" % o.days) if o.days else "", hours, mins, secs, (".%i" % o.microseconds) if o.microseconds else "")
else:
return base.IdeaBase.json_default(o)
......@@ -11,7 +11,6 @@ import struct
import socket
from .base import unicode
from .base import json_default
def Version(s):
......
......@@ -17,6 +17,8 @@ from setuptools import setup, find_packages
from codecs import open
from os import path
import idea
here = path.abspath(path.dirname(__file__))
# Get the long description from the README file
......@@ -25,7 +27,8 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
setup(
name = 'idea-format',
version = '0.1.7',
#version = '0.1.8',
version = idea.__version__,
description = 'Python library for working with IDEA messages.',
long_description = long_description,
classifiers = [
......
......@@ -4,7 +4,6 @@
# Copyright (c) 2016, CESNET, z. s. p. o.
# Use of this source is governed by an ISC license, see LICENSE file.
import typedcol
import unittest
import json
import difflib
......@@ -80,13 +79,13 @@ class TestIdea(unittest.TestCase):
def testLiteIdea(self):
idea = lite.Idea(raw_idea)
orig = json.dumps(raw_idea, indent=4, sort_keys=True)
new = json.dumps(idea, indent=4, sort_keys=True, default=lite.json_default)
new = idea.to_json(indent=4, sort_keys=True)
self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))]))
def testValidIdea(self):
idea = valid.Idea(raw_idea)
orig = json.dumps(raw_idea, indent=4, sort_keys=True)
new = json.dumps(idea, indent=4, sort_keys=True, default=valid.json_default)
new = idea.to_json(indent=4, sort_keys=True)
self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))]))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment