Beispiel #1
0
 def _get_skill_id(self):
     """Get the id of the skill in the request"""
     skill_global_id = (self.display_data.get('skill_gid')
                        or self.display_data.get('identifier'))
     skill_repo = SkillRepository(self.db)
     skill_id = skill_repo.ensure_skill_exists(skill_global_id)
     self.skill = Skill(skill_global_id, skill_id)
Beispiel #2
0
def add_skill(db, skill_global_id, settings_fields=None):
    display_data = _build_display_data(skill_global_id, settings_fields)
    skill_repo = SkillRepository(db)
    skill_id = skill_repo.ensure_skill_exists(skill_global_id)
    skill = Skill(skill_global_id, skill_id)
    settings_display = SettingsDisplay(skill_id, display_data)
    settings_display_repo = SettingsDisplayRepository(db)
    settings_display.id = settings_display_repo.add(settings_display)

    return skill, settings_display
class SkillManifestReconciler(object):
    def __init__(self, db, device_manifest, db_manifest):
        self.db = db
        self.skill_manifest_repo = DeviceSkillRepository(db)
        self.skill_repo = SkillRepository(self.db)
        self.device_manifest = {sm.skill_gid: sm for sm in device_manifest}
        self.db_manifest = {ds.skill_gid: ds for ds in db_manifest}
        self.device_manifest_global_ids = {
            gid
            for gid in self.device_manifest.keys()
        }
        self.db_manifest_global_ids = {gid for gid in self.db_manifest}

    def reconcile(self):
        """Compare the manifest sent by the device to that on the database."""
        self._update_skills()
        self._remove_skills()
        self._add_skills()

    def _update_skills(self):
        common_global_ids = self.device_manifest_global_ids.intersection(
            self.db_manifest_global_ids)
        for gid in common_global_ids:
            if self.device_manifest[gid] == self.db_manifest[gid]:
                self.skill_manifest_repo.update_manifest_skill(
                    self.device_manifest[gid])

    def _remove_skills(self):
        skills_to_remove = self.db_manifest_global_ids.difference(
            self.device_manifest_global_ids)
        for gid in skills_to_remove:
            manifest_skill = self.db_manifest[gid]
            self.skill_manifest_repo.remove_manifest_skill(manifest_skill)
            if manifest_skill.device_id in gid:
                self.skill_repo.remove_by_gid(gid)

    def _add_skills(self):
        skills_to_add = self.device_manifest_global_ids.difference(
            self.db_manifest_global_ids)

        for gid in skills_to_add:
            skill_id = self.skill_repo.ensure_skill_exists(gid)
            self.device_manifest[gid].skill_id = skill_id
            self.skill_manifest_repo.add_manifest_skill(
                self.device_manifest[gid])
    def _update_skill_display_table(self):
        skill_count = 0
        skill_repository = SkillRepository(self.db)
        display_repository = SkillDisplayRepository(self.db)
        for skill_name, skill_metadata in self.skill_display_data.items():
            skill_count += 1
            skill_id = skill_repository.ensure_skill_exists(
                skill_metadata['skill_gid']
            )

            # add the skill display row
            display_data = SkillDisplay(
                skill_id=skill_id,
                core_version=self.args.core_version,
                display_data=json.dumps(skill_metadata)
            )
            display_repository.upsert(display_data)

        self.log.info("updated {} skills".format(skill_count))