Skip to content

Instantly share code, notes, and snippets.

@anabarasan
Created August 12, 2016 10:30
Show Gist options
  • Save anabarasan/f2fb07f996c2e845bdf9dc6ddf973071 to your computer and use it in GitHub Desktop.
Save anabarasan/f2fb07f996c2e845bdf9dc6ddf973071 to your computer and use it in GitHub Desktop.
[Boolean]
type: boolean
[Integer]
type: integer
[Real]
type: float
[String]
type: string
[Hostname]
type: string
pattern: ^[a-z0-9].eng.velocix.com$
[IpAddress]
type: string
pattern: "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
[multipleIpAddress]
type: array
itemType: string
pattern: "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
import ConfigParser
import unittest
def ConfigSectionMap(config, section):
sectionMap = {}
options = config.options(section)
for option in options:
try:
sectionMap[option] = config.get(section, option)
except:
sectionMap[option] = None
return sectionMap
post_data = {
"title": "Base",
"type": "factory-defined",
"settings": {
"Boolean": True,
"IpAddress": "10.20.30.0",
"multipleIpAddress": ["10.20.30.0", "10.20.30.1"]
}
}
data = {}
def post():
settings = post_data['settings']
for key, value in settings.iteritems():
if isinstance(value, list):
data[key] = ' '.join(value)
else:
data[key] = str(value)
def get():
config = ConfigParser.ConfigParser()
config.read('config.ini')
post()
result = {}
for key, value in data.iteritems():
data_type = ConfigSectionMap(config, key)['type']
if data_type == 'integer':
value = int(value)
elif data_type == 'number':
value = float(value)
elif data_type == 'boolean':
value = value == 'True'
elif data_type == 'array':
value = value.split(' ')
result[key] = value
return result
class test(unittest.TestCase):
def test_post_result(self):
post()
self.assertEqual(data, {'Boolean': 'True',
'IpAddress': '10.20.30.0',
'multipleIpAddress': '10.20.30.0 10.20.30.1'})
def test_get_result(self):
self.assertEqual(get(), post_data['settings'])
import unittest
json_schema = {
"title": "Schema for Profile Item",
"description": "Schema for Profile Item",
"type": "object",
"properties": {
"title": {
"title": "name",
"type": "string",
"minLength": 1,
"maxLength": 255
},
"type": {
"title": "profile type",
"type": "string",
"enum": [
"factory-defined",
"user-defined"
],
"default": "user-defined"
},
"settings": {
"title": "Settings for the profile",
"type": "object",
"properties": {
"Boolean": {
"type": "boolean",
"default": False
},
"Integer": {
"type": "integer"
},
"Real": {
"type": "number"
},
"String": {
"type": "string",
"minLength": 1,
"maxLength": 255
},
"Hostname": {
"type": "string",
"pattern": "^[a-z0-9].eng.velocix.com$"
},
"IpAddress": {
"type": "string",
"pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
},
"multipleIpAddress": {
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
}
},
"URL": {
"type": "string",
"pattern": "^(.*:)//([A-Za-z0-9-.]+)(:[0-9]+)?(.*)$"
}
},
"additionalProperties": False
}
},
"required": [
"title",
"settings"
],
"additionalProperties": False
}
post_data = {
"title": "Base",
"type": "factory-defined",
"settings": {
"Boolean": True,
"IpAddress": "10.20.30.0",
"multipleIpAddress": ["10.20.30.0", "10.20.30.1"]
}
}
data = {}
def post():
settings = post_data['settings']
for key, value in settings.iteritems():
if isinstance(value, list):
data[key] = ' '.join(value)
else:
data[key] = str(value)
def get():
post()
result = {}
SETTINGS = json_schema['properties']['settings']['properties']
for key, value in data.iteritems():
data_type = SETTINGS[key]['type']
if data_type == 'integer':
value = int(value)
elif data_type == 'number':
value = float(value)
elif data_type == 'boolean':
value = value == 'True'
elif data_type == 'array':
value = value.split(' ')
result[key] = value
return result
class test(unittest.TestCase):
def test_post_result(self):
post()
self.assertEqual(data, {'Boolean': 'True',
'IpAddress': '10.20.30.0',
'multipleIpAddress': '10.20.30.0 10.20.30.1'})
def test_get_result(self):
self.assertEqual(get(), post_data['settings'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment