def testValidateNumber(self): schema = { "name": "Model", "properties": { "field": { "type": "number" } } } Model = warmongo.model_factory(schema) m = Model({ "field": 5.5 }) self.assertEqual(5.5, m.field) self.assertRaises(ValidationError, Model, { "field": "hi" }) # using an int should still work m = Model({ "field": 5 }) self.assertEqual(5, m.field) # using a long should still work m = Model({ "field": 10L }) self.assertEqual(10L, m.field)
def xmpp(ctx): """Hello world""" check_root() from isomer import database database.initialize(ctx.obj['dbhost'], ctx.obj['dbname']) from isomer.schemata.component import ComponentConfigSchemaTemplate factory = model_factory(ComponentConfigSchemaTemplate) bot_config = factory.find_one({'name': 'XMPPBOT'}) if bot_config is None: password = std_uuid() bot_config = factory({ 'nick': 'claptrap', 'name': 'XMPPBOT', 'componentclass': 'XMPPBot', 'jid': 'claptrap@localhost/node', 'password': password, 'uuid': std_uuid() }) bot_config.save() # log(bot_config.serializablefields(), pretty=True) ctx.obj['bot_config'] = bot_config
def setUp(self): self.schema = { 'name': 'Country', 'properties': { 'name': {'type': 'string'}, 'abbreviation': {'type': 'string'}, 'languages': { 'type': ['array', 'null'], 'items': { 'type': 'string' } } }, 'additionalProperties': False, } # Connect to warmongo_test - hopefully it doesn't exist warmongo.connect("warmongo_test") self.Country = warmongo.model_factory(self.schema) # Drop all the data in it self.Country.collection().remove({}) # Create some defaults self.Country({ "name": "Sweden", "abbreviation": "SE", "languages": ["swedish"] }) self.Country({ "name": "United States of America", "abbreviation": "US", "languages": ["english"] })
def testCastWithObject(self): schema = { "name": "Model", "properties": { "field": { "type": "object", "properties": { "subfield": {"type": "integer"} } } } } Model = warmongo.model_factory(schema) m = Model() old_fields = { "field": { "subfield": 5.2 } } fields = m.cast(old_fields) self.assertEqual(5, fields["field"]["subfield"])
def profile(schemaname='sensordata', profiletype='pjs'): hfoslog("Profiling ", schemaname, emitter='DB') schema = schemastore[schemaname]['schema'] hfoslog("Schema: ", schema, lvl=debug, emitter='DB') testclass = None if profiletype == 'warmongo': hfoslog("Running Warmongo benchmark", emitter='DB') testclass = warmongo.model_factory(schema) elif profiletype == 'pjs': hfoslog("Running PJS benchmark", emitter='DB') try: import python_jsonschema_objects as pjs except ImportError: hfoslog("PJS benchmark selected but not available. Install " "python_jsonschema_objects (PJS)", emitter="DB") hfoslog() builder = pjs.ObjectBuilder(schema) ns = builder.build_classes() pprint(ns) testclass = ns[schemaname] hfoslog("ns: ", ns, lvl=warn, emitter='DB') if testclass is not None: hfoslog("Instantiating elements...", emitter='DB') for i in range(100): testclass() else: hfoslog("No Profiletype available!", emitter="DB") hfoslog("Profiling done", emitter='DB')
def _build_model_factories(store): result = {} for schemaname in store: schema = None try: schema = store[schemaname]['schema'] except KeyError: hfoslog("No schema found for ", schemaname, lvl=critical, emitter='DB') try: result[schemaname] = warmongo.model_factory(schema) except Exception as e: hfoslog("Could not create factory for schema ", e, type(e), schemaname, schema, lvl=critical, emitter='DB') return result
def list(self, event): """Processes configuration list requests :param event: """ try: componentlist = model_factory(Schema).find({}) data = [] for comp in componentlist: data.append({ 'name': comp.name, 'uuid': comp.uuid, 'class': comp.componentclass, 'active': comp.active }) data = sorted(data, key=lambda x: x['name']) response = { 'component': 'hfos.ui.configurator', 'action': 'list', 'data': data } self.fireEvent(send(event.client.uuid, response)) return except Exception as e: self.log("List error: ", e, type(e), lvl=error, exc=True)
def get(self, event): if self._check_permission(event) is False: response = { 'component': 'hfos.ui.configurator', 'action': 'get', 'data': False } self.log('No permission to access configuration', event.user, lvl=warn) self.fireEvent(send(event.client.uuid, response)) return try: comp = event.data['uuid'] except KeyError: comp = None if not comp: self.log('Invalid get request without schema or component', lvl=error) return self.log("Config data get request for ", event.data, "from", event.user) component = model_factory(Schema).find_one({'uuid': comp}) response = { 'component': 'hfos.ui.configurator', 'action': 'get', 'data': component.serializablefields() } self.fireEvent(send(event.client.uuid, response))
def put(self, event): self.log("Configuration put request ", event.user) try: if self._check_permission(event) is False: raise PermissionError component = model_factory(Schema).find_one( {'uuid': event.data['uuid']}) component.update(event.data) component.save() response = { 'component': 'hfos.ui.configurator', 'action': 'put', 'data': True } self.log('Updated component configuration:', component.name) except (KeyError, ValueError, ValidationError, PermissionError) as e: response = { 'component': 'hfos.ui.configurator', 'action': 'put', 'data': False } self.log('Storing component configuration failed: ', type(e), e, exc=True, lvl=error) self.fireEvent(send(event.client.uuid, response)) return
def testValidateString(self): schema = {"name": "Model", "properties": {"field": {"type": "string"}}} Model = warmongo.model_factory(schema) m = Model({"field": "asdf"}) self.assertEqual("asdf", m.field) self.assertRaises(ValidationError, Model, {"field": 5})
def config(ctx, dbhost): """Configuration management operations (GROUP)""" from hfos import database database.initialize(dbhost) ctx.obj['db'] = database from hfos.schemata.component import ComponentConfigSchemaTemplate ctx.obj['col'] = model_factory(ComponentConfigSchemaTemplate)
def testValidateAny(self): schema = {"name": "Model", "properties": {"field": {"type": "any"}}} Model = warmongo.model_factory(schema) Model({"field": ["asdf", "hello"]}) Model({"field": "asdf"}) Model({"field": 5}) Model({"field": True}) Model({"field": None}) Model({"field": {"subfield": "asdf"}})
def testValidateDate(self): schema = {"name": "Model", "properties": {"field": {"type": "date"}}} Model = warmongo.model_factory(schema) m = Model({"field": datetime(2010, 5, 12)}) self.assertEqual(2010, m.field.year) self.assertEqual(5, m.field.month) self.assertEqual(12, m.field.day) self.assertRaises(ValidationError, Model, {"field": "hi"})
def __init__(self, uniquename=None, *args, **kwargs): self.uniquename = "" if uniquename: if uniquename not in self.names: self.uniquename = uniquename self.names.append(uniquename) else: hfoslog("Unique component added twice: ", uniquename, lvl=critical, emitter="CORE") else: while True: uniquename = "%s%s" % (self.name, randint(0, 32768)) if uniquename not in self.names: self.uniquename = uniquename self.names.append(uniquename) break self.configschema = deepcopy(ComponentBaseConfigSchema) self.configschema['schema']['properties'].update(self.configprops) # self.log("[UNIQUECOMPONENT] Config Schema: ", self.configschema, # lvl=critical) # pprint(self.configschema) # self.configschema['name'] = self.uniquename # self.configschema['id'] = "#" + self.uniquename # schemastore[self.uniquename] = {'schema': self.configschema, # 'form': self.configform} self.componentmodel = model_factory(self.configschema['schema']) # self.log("Component model: ", lvl=critical) # pprint(self.componentmodel._schema) self._read_config() if not self.config: self.log("Creating initial default configuration.") try: self._set_config() self._write_config() except ValidationError as e: self.log("Error during configuration reading: ", e, type(e), exc=True)
def test_schemata(): objects = {} for schemaname in schemastore.keys(): objects[schemaname] = warmongo.model_factory( schemastore[schemaname]['schema']) try: testobject = objects[schemaname]() testobject.validate() except Exception as e: hfoslog('Blank schema did not validate:', schemaname, e, type(e), lvl=verbose, emitter='DB') pprint(objects)
def testValidateObjectId(self): schema = { "name": "Model", "properties": { "field": { "type": "object_id" } } } Model = warmongo.model_factory(schema) m = Model({"field": ObjectId("123412341234123412341234")}) self.assertEqual(ObjectId("123412341234123412341234"), m.field) self.assertRaises(ValidationError, Model, {"field": "hi"})
def testValidateBool(self): schema = { "name": "Model", "properties": { "field": { "type": "boolean" } } } Model = warmongo.model_factory(schema) m = Model({"field": False}) self.assertEqual(False, m.field) self.assertRaises(ValidationError, Model, {"field": "hi"})
def testValidateInteger(self): schema = { "name": "Model", "properties": { "field": { "type": "integer" } } } Model = warmongo.model_factory(schema) # floats should not cause validation errors, but they do get truncated m = Model({"field": 7.8}) self.assertEqual(7, m.field) self.assertRaises(ValidationError, Model, {"field": "hi"})
def _buildModelFactories(): result = {} for schemaname in schemastore: schema = None try: schema = schemastore[schemaname]['schema'] except KeyError: hfoslog("[DB] No schema found for ", schemaname, lvl=critical) try: result[schemaname] = warmongo.model_factory(schema) except Exception as e: hfoslog("[DB] Could not create factory for schema ", schemaname, schema, lvl=critical) return result
def testValidateAny(self): schema = { "name": "Model", "properties": { "field": { "type": "any" } } } Model = warmongo.model_factory(schema) Model({"field": ["asdf", "hello"]}) Model({"field": "asdf"}) Model({"field": 5}) Model({"field": True}) Model({"field": None}) Model({"field": {"subfield": "asdf"}})
def testValidateNumber(self): schema = {"name": "Model", "properties": {"field": {"type": "number"}}} Model = warmongo.model_factory(schema) m = Model({"field": 5.5}) self.assertEqual(5.5, m.field) self.assertRaises(ValidationError, Model, {"field": "hi"}) # using an int should still work m = Model({"field": 5}) self.assertEqual(5, m.field) # using a long should still work m = Model({"field": 10L}) self.assertEqual(10L, m.field)
def testValidateString(self): schema = { "name": "Model", "properties": { "field": { "type": "string" } } } Model = warmongo.model_factory(schema) m = Model({ "field": "asdf" }) self.assertEqual("asdf", m.field) self.assertRaises(ValidationError, Model, { "field": 5 })
def testValidateBool(self): schema = { "name": "Model", "properties": { "field": { "type": "boolean" } } } Model = warmongo.model_factory(schema) m = Model({ "field": False }) self.assertEqual(False, m.field) self.assertRaises(ValidationError, Model, { "field": "hi" })
def testValidateObjectId(self): schema = { "name": "Model", "properties": { "field": { "type": "object_id" } } } Model = warmongo.model_factory(schema) m = Model({ "field": ObjectId("123412341234123412341234") }) self.assertEqual(ObjectId("123412341234123412341234"), m.field) self.assertRaises(ValidationError, Model, { "field": "hi" })
def testValidateArray(self): schema = { "name": "Model", "properties": { "field": { "type": "array", "items": { "type": "string" } } } } Model = warmongo.model_factory(schema) m = Model({"field": ["asdf", "hello"]}) self.assertEqual(2, len(m.field)) self.assertEqual("asdf", m.field[0]) self.assertRaises(ValidationError, Model, {"field": "hi"})
def testValidateInteger(self): schema = { "name": "Model", "properties": { "field": { "type": "integer" } } } Model = warmongo.model_factory(schema) # floats should not cause validation errors, but they do get truncated m = Model({ "field": 7.8 }) self.assertEqual(7, m.field) self.assertRaises(ValidationError, Model, { "field": "hi" })
def testBasicCast(self): schema = { "name": "Model", "properties": { "field": {"type": "integer"}, "other_field": {"type": "string"}, } } Model = warmongo.model_factory(schema) m = Model() old_fields = { "field": 5.2, "other_field": "5" } fields = m.cast(old_fields) self.assertEqual(5, fields["field"]) self.assertEqual("5", fields["other_field"])
def setUp(self): self.schema = { 'name': 'Country', 'properties': { 'name': { 'type': 'string' }, 'abbreviation': { 'type': 'string' }, 'languages': { 'type': 'array', 'items': { 'type': 'string' } } }, 'additionalProperties': False, } # Connect to warmongo_test - hopefully it doesn't exist warmongo.connect("warmongo_test") self.Country = warmongo.model_factory(self.schema) # Drop all the data in it self.Country.collection().remove({}) # Create some defaults sweden = self.Country({ "name": "Sweden", "abbreviation": "SE", "languages": ["swedish"] }) sweden.save() usa = self.Country({ "name": "United States of America", "abbreviation": "US", "languages": ["english"] }) usa.save()
def testCastWithArray(self): schema = { "name": "Model", "properties": { "field": { "type": "array", "items": { "type": "integer" } } } } Model = warmongo.model_factory(schema) m = Model() old_fields = {"field": [5.2, 7]} fields = m.cast(old_fields) self.assertEqual(5, fields["field"][0]) self.assertEqual(7, fields["field"][1])
def testBasicCast(self): schema = { "name": "Model", "properties": { "field": { "type": "integer" }, "other_field": { "type": "string" }, } } Model = warmongo.model_factory(schema) m = Model() old_fields = {"field": 5.2, "other_field": "5"} fields = m.cast(old_fields) self.assertEqual(5, fields["field"]) self.assertEqual("5", fields["other_field"])
def testValidateDate(self): schema = { "name": "Model", "properties": { "field": { "type": "date" } } } Model = warmongo.model_factory(schema) m = Model({ "field": datetime(2010, 5, 12) }) self.assertEqual(2010, m.field.year) self.assertEqual(5, m.field.month) self.assertEqual(12, m.field.day) self.assertRaises(ValidationError, Model, { "field": "hi" })
def testCastWithObject(self): schema = { "name": "Model", "properties": { "field": { "type": "object", "properties": { "subfield": { "type": "integer" } } } } } Model = warmongo.model_factory(schema) m = Model() old_fields = {"field": {"subfield": 5.2}} fields = m.cast(old_fields) self.assertEqual(5, fields["field"]["subfield"])
def profile(schemaname='sensordata', profiletype='pjs'): hfoslog("Profiling ", schemaname, emitter='DB') schema = schemastore[schemaname]['schema'] hfoslog("Schema: ", schema, lvl=debug, emitter='DB') testclass = None if profiletype == 'warmongo': hfoslog("Running Warmongo benchmark", emitter='DB') testclass = warmongo.model_factory(schema) elif profiletype == 'pjs': hfoslog("Running PJS benchmark", emitter='DB') try: import python_jsonschema_objects as pjs except ImportError: hfoslog( "PJS benchmark selected but not available. Install " "python_jsonschema_objects (PJS)", emitter="DB") hfoslog() builder = pjs.ObjectBuilder(schema) ns = builder.build_classes() pprint(ns) testclass = ns[schemaname] hfoslog("ns: ", ns, lvl=warn, emitter='DB') if testclass is not None: hfoslog("Instantiating elements...", emitter='DB') for i in range(100): testclass() else: hfoslog("No Profiletype available!", emitter="DB") hfoslog("Profiling done", emitter='DB')
def testValidateArray(self): schema = { "name": "Model", "properties": { "field": { "type": "array", "items": { "type": "string" } } } } Model = warmongo.model_factory(schema) m = Model({ "field": ["asdf", "hello"] }) self.assertEqual(2, len(m.field)) self.assertEqual("asdf", m.field[0]) self.assertRaises(ValidationError, Model, { "field": "hi" })
def testCastWithArray(self): schema = { "name": "Model", "properties": { "field": { "type": "array", "items": { "type": "integer" } } } } Model = warmongo.model_factory(schema) m = Model() old_fields = { "field": [5.2, 7] } fields = m.cast(old_fields) self.assertEqual(5, fields["field"][0]) self.assertEqual(7, fields["field"][1])
try: result[schemaname] = warmongo.model_factory(schema) except Exception as e: hfoslog("[DB] Could not create factory for schema ", schemaname, schema, lvl=critical) return result objectmodels = _buildModelFactories() # TODO: Export the following automatically from the objectmodels? userobject = warmongo.model_factory(User) profileobject = warmongo.model_factory(Profile) clientconfigobject = warmongo.model_factory(Clientconfig) mapviewobject = warmongo.model_factory(MapView) layerobject = warmongo.model_factory(Layer) layergroupobject = warmongo.model_factory(LayerGroup) controllerobject = warmongo.model_factory(Controller) controllableobject = warmongo.model_factory(Controllable) wikipageobject = warmongo.model_factory(WikiPage) vesselconfigobject = warmongo.model_factory(VesselSchema) radioconfigobject = warmongo.model_factory(RadioConfig)
def clean_test_components(): print("Removing test components...") for item in model_factory(ComponentConfigSchemaTemplate).find( {'componentclass': 'TestComponent'}): item.delete()
def model_factory(schema, base_class=twisted_model.TwistedModel): return warmongo.model_factory(schema, base_class)
except KeyError: hfoslog("[DB] No schema found for ", schemaname, lvl=critical) try: result[schemaname] = warmongo.model_factory(schema) except Exception as e: hfoslog("[DB] Could not create factory for schema ", schemaname, schema, lvl=critical) return result objectmodels = _buildModelFactories() # TODO: Export the following automatically from the objectmodels? userobject = warmongo.model_factory(User) profileobject = warmongo.model_factory(Profile) clientconfigobject = warmongo.model_factory(Clientconfig) mapviewobject = warmongo.model_factory(MapView) layerobject = warmongo.model_factory(Layer) layergroupobject = warmongo.model_factory(LayerGroup) controllerobject = warmongo.model_factory(Controller) controllableobject = warmongo.model_factory(Controllable) wikipageobject = warmongo.model_factory(WikiPage) vesselconfigobject = warmongo.model_factory(VesselSchema) radioconfigobject = warmongo.model_factory(RadioConfig)