Пример #1
0
    def put(self, event):
        """Store a given configuration"""

        self.log("Configuration put request ", event.user)

        try:
            component = model_factory(Schema).find_one({"uuid": event.data["uuid"]})

            component.update(event.data)
            component.save()

            response = {
                "component": "isomer.ui.configurator",
                "action": "put",
                "data": True,
            }
            self.log("Updated component configuration:", component.name)

            self.fireEvent(reload_configuration(component.name))
        except (KeyError, ValueError, ValidationError, PermissionError) as e:
            response = {
                "component": "isomer.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
Пример #2
0
    def setUp(self):
        """Set up the test scaffolding"""
        self.schema = {
            "name": "Country",
            "id": "#Country",
            "properties": {
                "name": {"type": "string"},
                "abbreviation": {"type": "string"},
                "languages": {"type": "array", "items": {"type": "string"}},
            },
            "additionalProperties": False,
        }

        # Connect to formal_test - hopefully it doesn't exist
        formal.connect("formal_test")
        self.Country = formal.model_factory(self.schema)

        # Drop all the data in it
        self.Country.collection().delete_many({})

        # 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()
Пример #3
0
def _build_model_factories(store):
    """Generate factories to construct objects from schemata"""

    result = {}

    for schemaname in store:

        schema = None

        try:
            schema = store[schemaname]["schema"]
        except KeyError:
            db_log("No schema found for ", schemaname, lvl=critical, exc=True)

        try:
            result[schemaname] = formal.model_factory(schema, IsomerBaseModel)
        except Exception as e:
            db_log(
                "Could not create factory for schema ",
                schemaname,
                schema,
                lvl=critical,
                exc=True,
            )

    return result
Пример #4
0
def clean_test_components():
    """Removes test-generated component data"""

    print("Removing test components...")
    for item in model_factory(ComponentConfigSchemaTemplate).find(
        {'componentclass': 'TestComponent'}):
        item.delete()
Пример #5
0
def config(ctx):
    """[GROUP] Configuration management operations"""

    from isomer import database

    database.initialize(ctx.obj["dbhost"], ctx.obj["dbname"])

    from isomer.schemata.component import ComponentConfigSchemaTemplate

    ctx.obj["col"] = model_factory(ComponentConfigSchemaTemplate)
Пример #6
0
    def testValidateInteger(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "integer"}},
        }

        model = formal.model_factory(schema)

        self.assertRaises(ValidationError, model, {"field": "hi"})
Пример #7
0
    def testValidateString(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "string"}},
        }

        model = formal.model_factory(schema)

        m = model({"field": "asdf"})

        self.assertEqual("asdf", m.field)
        self.assertRaises(ValidationError, model, {"field": 5})
Пример #8
0
    def testValidateBool(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "boolean"}},
        }

        model = formal.model_factory(schema)

        m = model({"field": False})

        self.assertEqual(False, m.field)
        self.assertRaises(ValidationError, model, {"field": "hi"})
Пример #9
0
    def testValidateArray(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "array", "items": {"type": "string"}}},
        }

        model = formal.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"})
Пример #10
0
def test_schemata():
    """Validates all registered schemata"""

    objects = {}

    for schemaname in schemastore.keys():
        objects[schemaname] = formal.model_factory(
            schemastore[schemaname]["schema"])
        try:
            testobject = objects[schemaname]()
            testobject.validate()
        except Exception as e:
            schemata_log("Blank schema did not validate:",
                         schemaname,
                         exc=True)
Пример #11
0
    def testCastWithArray(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "array", "items": {"type": "integer"}}},
        }
        model = formal.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])
Пример #12
0
    def testValidateNumber(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {"field": {"type": "number"}},
        }

        model = formal.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)
Пример #13
0
    def testBasicCast(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {
                "field": {"type": "integer"},
                "other_field": {"type": "string"},
            },
        }
        model = formal.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"])
Пример #14
0
    def testValidateObjectId(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {
                "_id": {
                    "type": "string",
                    "pattern": r"^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)",
                    "additionalProperties": False,
                }
            },
        }

        model = formal.model_factory(schema)

        m = model({"_id": str(ObjectId("45cbc4a0e4123f6920000002"))})

        self.assertEqual(ObjectId("45cbc4a0e4123f6920000002"), ObjectId(m._id))
        self.assertRaises(ValidationError, model, {"_id": "hi"})
Пример #15
0
    def testCastWithObject(self):
        schema = {
            "name": "Model",
            "id": "#Model",
            "properties": {
                "field": {
                    "type": "object",
                    "properties": {"subfield": {"type": "integer"}},
                }
            },
        }
        model = formal.model_factory(schema)

        m = model()

        old_fields = {"field": {"subfield": 5.2}}

        fields = m.cast(old_fields)

        self.assertEqual(5, fields["field"]["subfield"])
Пример #16
0
    def get(self, event):
        """Get a stored configuration"""

        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": "isomer.ui.configurator",
            "action": "get",
            "data": component.serializablefields(),
        }
        self.fireEvent(send(event.client.uuid, response))
Пример #17
0
    def setUp(self):
        """Set up the test scaffolding"""
        self.schema = {
            "name": "Country",
            "sql": True,
            "id": "#Country",
            "properties": {
                "name": {
                    "type": "string"
                },
                "abbreviation": {
                    "type": "string",
                    "primary": True
                },
                "dialcode": {
                    "type": "integer"
                },
            },
            "additionalProperties": False,
        }

        formal.connect_sql(":memory:", "sqlite", "", "", "", 0)

        self.Country = formal.model_factory(self.schema)

        # Drop all the data in it
        # self.Country.clear()

        # Create some defaults
        self.Country({
            "name": "Sweden",
            "abbreviation": "SE",
            "dialcode": 46
        }).save()
        self.Country({
            "name": "United States of America",
            "abbreviation": "US",
            "dialcode": 1
        }).save()
Пример #18
0
    def getlist(self, event):
        """Processes configuration list requests

        :param event:
        """

        try:

            componentlist = model_factory(Schema).find({})
            data = []
            for comp in componentlist:
                try:
                    data.append(
                        {
                            "name": comp.name,
                            "uuid": comp.uuid,
                            "class": comp.componentclass,
                            "active": comp.active,
                            "present": comp.name in self.names
                        }
                    )
                except AttributeError:
                    self.log(
                        "Bad component without component class encountered:", lvl=warn
                    )
                    self.log(comp.serializablefields(), pretty=True, lvl=warn)

            data = sorted(data, key=lambda x: x["name"])

            response = {
                "component": "isomer.ui.configurator",
                "action": "getlist",
                "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)
Пример #19
0
def profile(schemaname="sensordata", profiletype="pjs"):
    """Profiles object model handling with a very simple benchmarking test"""

    db_log("Profiling ", schemaname)

    schema = schemastore[schemaname]["schema"]

    db_log("Schema: ", schema, lvl=debug)

    testclass = None

    if profiletype == "formal":
        db_log("Running formal benchmark")
        testclass = formal.model_factory(schema)
    elif profiletype == "pjs":
        db_log("Running PJS benchmark")
        try:
            import python_jsonschema_objects as pjs
        except ImportError:
            db_log("PJS benchmark selected but not available. Install "
                   "python_jsonschema_objects (PJS)")
            return

        db_log()
        builder = pjs.ObjectBuilder(schema)
        ns = builder.build_classes()
        pprint(ns)
        testclass = ns[schemaname]
        db_log("ns: ", ns, lvl=warn)

    if testclass is not None:
        db_log("Instantiating elements...")
        for i in range(100):
            testclass()
    else:
        db_log("No Profiletype available!")

    db_log("Profiling done")
Пример #20
0
    def __init__(self):
        super(GIFMaster, self).__init__("GIFMASTER")

        self.players = {}
        self.player_model = formal.model_factory(
            self.configprops['channels']['items'])
Пример #21
0
    def __init__(self, uniquename, no_db=False, *args, **kwargs):
        """Check for configuration issues and instantiate a component"""

        LoggingMeta.__init__(self, uniquename, *args, **kwargs)

        if no_db is True:
            self.no_db = True
            self.log("Not using database!")
            return
        else:
            self.no_db = False

        self.configschema = deepcopy(ComponentBaseConfigSchema)

        self.configschema["schema"]["properties"].update(self.configprops)
        if len(self.configform) > 0:
            self.configschema["form"] += self.configform
        else:
            self.configschema["form"] = ["*"]

        # 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)

        environment_identifier = 'ISOMER_COMPONENT_' + self.uniquename

        overrides = [
            key for key, item in os.environ.items()
            if key.startswith(environment_identifier)
        ]

        if len(overrides) > 0:
            self.log('Environment overrides found:', overrides)
            for item in overrides:
                path = item.lstrip(environment_identifier).lower().split("_")
                nested_map_update(self.config._fields, os.environ[item], path)

            self.config.save()

        if self.config.active is False:
            self.log("Component disabled.", lvl=warn)