Exemple #1
0
class PackageData():
    """ Tool to insert necessary data in database
    """

    def __init__(self, json_path):
        """ Init tool
            @param plugin_name : plugin name
        """

        self._db = DbHelper()
        try:
            self.pkg = PackageJson(path = json_path).json
        except PackageException as exp:
            print("Error in json file:")
            print( exp.value )
            exit()
        except:
            print(str(traceback.format_exc()))
            return
        print("Json file OK")

        # check type == plugin
        if self.pkg["identity"]["type"] not in ["plugin", "external"]:
            print("Error : this package type is not recognized")
            exit()
        # check if json version is at least 2
        if self.pkg['json_version'] < 2:
            print("Error : this package is to old for this version of domogik")
            exit()

    def insert(self):
        """ Insert data for plugin
        """
        ### Plugin
        print("plugin %s" % self.pkg["identity"]["id"])
        if self._db.get_plugin(self.pkg["identity"]["id"]) == None:
            # add if not exists
            print("add...")
            self._db.add_plugin(self.pkg["identity"]["id"],
                                  self.pkg["identity"]["description"],
                                  self.pkg["identity"]["version"])
        else:
            # update if exists
            print("update...")
            self._db.update_plugin(self.pkg["identity"]["id"],
                                  self.pkg["identity"]["description"],
                                  self.pkg["identity"]["version"])
 
        ### Device types
        for device_type in self.pkg["device_types"].keys():
            print("Device type %s" % device_type)
            device_type = self.pkg["device_types"][device_type]
            if self._db.get_device_type_by_id(device_type["id"]) == None:
                # add if not exists
                print("add...")
                self._db.add_device_type(device_type["id"],
                                         device_type["name"],
                                         self.pkg["identity"]["id"],
                                         device_type["description"])
            else:
                # update if exists
                print("update...")
                self._db.update_device_type(device_type["id"],
                                         device_type["name"],
                                         self.pkg["identity"]["id"],
                                         device_type["description"])