-
Notifications
You must be signed in to change notification settings - Fork 107
Adds schema methods to Option and Config classes. #646
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
# freeseer - vga/presentation capture software | ||
# | ||
# Copyright (C) 2011, 2013 Free and Open Source Software Learning Centre | ||
# Copyright (C) 2011, 2013, 2014 Free and Open Source Software Learning Centre | ||
# http://fosslc.org | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
|
@@ -30,6 +30,7 @@ | |
|
||
class StringOption(Option): | ||
"""Represents a string value.""" | ||
SCHEMA_TYPE = 'string' | ||
|
||
def is_valid(self, value): | ||
return isinstance(value, str) or hasattr(value, '__str__') | ||
|
@@ -43,6 +44,7 @@ def decode(self, value): | |
|
||
class IntegerOption(Option): | ||
"""Represents an integer number value.""" | ||
SCHEMA_TYPE = 'integer' | ||
|
||
def is_valid(self, value): | ||
return isinstance(value, int) | ||
|
@@ -59,6 +61,7 @@ def decode(self, value): | |
|
||
class FloatOption(Option): | ||
"""Represents a floating point number value.""" | ||
SCHEMA_TYPE = 'number' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't been following this project closely so perhaps I'm missing something, but would this be better as 'float'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSON schema spec doesn't actually include floats. Instead, it has 'number' for any numbers, both whole and fractional, and 'integer' for just integers. It's described here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that's a handy page. Carry on. |
||
|
||
def is_valid(self, value): | ||
return isinstance(value, float) | ||
|
@@ -75,6 +78,7 @@ def decode(self, value): | |
|
||
class BooleanOption(Option): | ||
"""Represents a boolean value.""" | ||
SCHEMA_TYPE = 'boolean' | ||
|
||
def is_valid(self, value): | ||
return isinstance(value, bool) | ||
|
@@ -88,6 +92,7 @@ def decode(self, value): | |
|
||
class FolderOption(Option): | ||
"""Represents the path to a folder.""" | ||
SCHEMA_TYPE = 'string' | ||
|
||
def __init__(self, default=Option.NotSpecified, auto_create=False): | ||
self.auto_create = auto_create | ||
|
@@ -119,6 +124,7 @@ def presentation(self, value): | |
|
||
class ChoiceOption(StringOption): | ||
"""Represents a selection from a pre-defined list of strings.""" | ||
SCHEMA_TYPE = 'enum' | ||
|
||
def __init__(self, choices, default=Option.NotSpecified): | ||
self.choices = choices | ||
|
@@ -133,3 +139,9 @@ def decode(self, value): | |
return choice | ||
else: | ||
raise InvalidDecodeValueError(value) | ||
|
||
def schema(self): | ||
schema = {'enum': self.choices} | ||
if self.default != Option.NotSpecified: | ||
schema['default'] = self.default | ||
return schema |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# freeseer - vga/presentation capture software | ||
# | ||
# Copyright (C) 2014 Free and Open Source Software Learning Centre | ||
# http://fosslc.org | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program 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 General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
# For support, questions, suggestions or any other inquiries, visit: | ||
# http://wiki.github.com/Freeseer/freeseer/ | ||
|
||
import unittest | ||
|
||
from jsonschema import validate | ||
from jsonschema import ValidationError | ||
|
||
from freeseer.framework.config.options import FloatOption | ||
from freeseer.tests.framework.config.options import OptionTest | ||
|
||
|
||
class TestFloatOptionNoDefault(unittest.TestCase, OptionTest): | ||
"""Tests FloatOption without a default value.""" | ||
|
||
valid_success = [x / 10.0 for x in xrange(-100, 100)] | ||
|
||
encode_success = zip(valid_success, map(str, valid_success)) | ||
|
||
decode_success = zip(map(str, valid_success), valid_success) | ||
decode_failure = [ | ||
'hello', | ||
'1world', | ||
'test2', | ||
] | ||
|
||
def setUp(self): | ||
self.option = FloatOption() | ||
|
||
def test_schema(self): | ||
"""Tests FloatOption schema method.""" | ||
self.assertRaises(ValidationError, validate, 'error', self.option.schema()) | ||
self.assertIsNone(validate(5.5, self.option.schema())) | ||
self.assertDictEqual(self.option.schema(), {'type': 'number'}) | ||
|
||
|
||
class TestFloatOptionWithDefault(TestFloatOptionNoDefault): | ||
"""Tests FloatOption with a default value.""" | ||
|
||
def setUp(self): | ||
self.option = FloatOption(1234.5) | ||
|
||
def test_default(self): | ||
"""Tests that the default was set correctly.""" | ||
self.assertEqual(self.option.default, 1234.5) | ||
|
||
def test_schema(self): | ||
"""Tests FloatOption schema method.""" | ||
self.assertRaises(ValidationError, validate, 'error', self.option.schema()) | ||
self.assertIsNone(validate(5.0, self.option.schema())) | ||
self.assertDictEqual(self.option.schema(), {'default': 1234.5, 'type': 'number'}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add these to the schema directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally tried having 'required': [] as part of the schema, and filling it as needed when options are required. But json-schema validate() throws exceptions if a schema it receives as input has an empty list of 'required'.