Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Petrounias committed Nov 19, 2015
1 parent dbe59e6 commit 728cb41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion json_schema_toolkit/__init__.py
Expand Up @@ -40,5 +40,5 @@
__author__ = (u"Alexis Petrounias <www.petrounias.org>", )


VERSION = (1, 0, 0, 'alpha', 1)
VERSION = (1, 0, 0, 'beta', 1)

62 changes: 30 additions & 32 deletions json_schema_toolkit/document/__init__.py
@@ -1,9 +1,9 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
# This document is free and open-source software, subject to the OSI-approved
# BSD license below.
#
# Copyright (c) 2013 Alexis Petrounias <www.petrounias.org>,
# Copyright (c) 2013 - 2015 by Alexis Petrounias <www.petrounias.org>,
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,8 +34,8 @@
"""
"""

__status__ = "alpha"
__version__ = "1.0.0a1"
__status__ = "beta"
__version__ = "1.0.0b1"
__maintainer__ = (u"Alexis Petrounias <www.petrounias.org>", )
__author__ = (u"Alexis Petrounias <www.petrounias.org>", )

Expand Down Expand Up @@ -89,18 +89,13 @@ def __delitem__(self, key):

class JSONDocument(Document):
"""
A custom validator can be provided. It shall implement the following
function
validate(schema, value)
"""

def __init__(self, value, validator=None):
def __init__(self, value, validator = None):
# set _fields before __getattribute__ is processed
self._fields = {}
super(JSONDocument, self).__init__(value,
self._generate_schema(), validator)
super(JSONDocument, self).__init__(value, self._generate_schema(),
validator = validator)

def _generate_schema(self):
base = {
Expand All @@ -115,7 +110,7 @@ def _generate_schema(self):
self._fields[name] = field
return base

def __getattribute__(self, name) :
def __getattribute__(self, name):
_fields = Document.__getattribute__(self, '_fields')
if name in _fields:
if isinstance(_fields[name], JSONObjectField) or \
Expand Down Expand Up @@ -182,7 +177,6 @@ def __init__(self, title = None, description = None, default = None,
self.implementation = implementation or JSONDocumentFragment

def _generate_schema(self):

SCHEMA = {
'type' : self.TYPE if not self.null else [ self.TYPE, 'null', ],
'title' : self.title,
Expand All @@ -195,12 +189,11 @@ def _generate_schema(self):
'__field' : self,
'__fragment_cls' : self.implementation,
}

if self.enum is not None:
SCHEMA['enum']=self.enum

return SCHEMA



class JSONBooleanField(JSONDocumentField):
"""
"""
Expand Down Expand Up @@ -270,7 +263,8 @@ class JSONStringField(JSONDocumentField):

def __init__(self, title = None, description = None, default = None,
optional = False, null = False, pattern = None, content = None,
enum = None, implementation = None, min_length = None, max_length = None):
enum = None, implementation = None, min_length = None,
max_length = None):
super(JSONStringField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern, content = content, enum = enum,
Expand Down Expand Up @@ -325,8 +319,8 @@ def __init__(self, title = None, description = None, default = None,
enum = None, implementation = None):
super(JSONDateField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern or self.PATTERN, content = content, enum = enum,
implementation = implementation)
null = null, pattern = pattern or self.PATTERN, content = content,
enum = enum, implementation = implementation)


class JSONTimeField(JSONDocumentField):
Expand All @@ -347,8 +341,8 @@ def __init__(self, title = None, description = None, default = None,
enum = None, implementation = None):
super(JSONTimeField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern or self.PATTERN, content = content, enum = enum,
implementation = implementation)
null = null, pattern = pattern or self.PATTERN, content = content,
enum = enum, implementation = implementation)


class JSONTimeDeltaField(JSONDocumentField):
Expand All @@ -367,11 +361,6 @@ def __init__(self, title = None, description = None, default = None,
null = null, pattern = pattern or self.PATTERN, content = content,
enum = enum, implementation = implementation)

def _generate_schema(self) :
schema = super(JSONTimeDeltaField, self)._generate_schema()
schema['pattern'] = self.PATTERN
return schema


class JSONObjectField(JSONDocumentField):
"""
Expand Down Expand Up @@ -416,13 +405,15 @@ def _generate_schema(self):
self.content]
return schema


class JSONEmailField(JSONStringField):
"""
"""

def __init__(self, title = None, description = None, default = None,
optional = False, null = False, pattern = None, content = None,
enum = None, implementation = None, min_length = None, max_length = None):
enum = None, implementation = None, min_length = None,
max_length = None):
super(JSONEmailField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern, content = content, enum = enum,
Expand All @@ -434,24 +425,29 @@ def _generate_schema(self):
schema['format'] = 'email'
return schema


class JSONIPAddressField(JSONStringField):
"""
"""

DEFAULT_PROTOCOL = 'ipv4'

def __init__(self, title = None, description = None, default = None,
optional = False, null = False, pattern = None, content = None,
enum = None, protocol=None, implementation = None):
super(JSONIPAddressField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern, content = content, enum = enum,
implementation = implementation)
self.protocol = protocol if protocol is not None else 'ipv4'
self.protocol = protocol if protocol is not None else \
self.DEFAULT_PROTOCOL

def _generate_schema(self):
schema = super(JSONIPAddressField, self)._generate_schema()
schema['format'] = self.protocol
return schema


class JSONSlugField(JSONStringField):
"""
"""
Expand All @@ -467,9 +463,11 @@ def __init__(self, title = None, description = None, default = None,
enum = enum, implementation = implementation, min_length = min_length,
max_length = max_length)


class JSONURLField(JSONStringField):
"""
"""

# Pattern based on django's URLValidator regex pattern
PATTERN = (r"^(http|ftp)s?://(([A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)"
"+([A-Za-z]{2,6}\.?|[A-Za-z0-9-]{2,}\.?)|localhost|\d{1,3}\."
Expand All @@ -481,7 +479,7 @@ def __init__(self, title = None, description = None, default = None,
enum = None, implementation = None, min_length = None, max_length = None):
super(JSONURLField, self).__init__(title = title,
description = description, default = default, optional = optional,
null = null, pattern = pattern or self.PATTERN, content = content, enum = enum,
implementation = implementation, min_length = min_length,
max_length = max_length)
null = null, pattern = pattern or self.PATTERN, content = content,
enum = enum, implementation = implementation,
min_length = min_length, max_length = max_length)

0 comments on commit 728cb41

Please sign in to comment.