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