Esempio n. 1
0
	def updateAliceConfiguration(self, key: str, value: typing.Any):
		if key not in self._aliceConfigurations:
			self.logWarning(f'Was asked to update {key} but key doesn\'t exist')
			raise ConfigurationUpdateFailed()

		try:
			# Remove skill configurations
			if key == 'skills':
				value = {k: v for k, v in value.items() if k not in self._aliceSkillConfigurationKeys}
		except AttributeError:
			raise ConfigurationUpdateFailed()

		self._aliceConfigurations[key] = value
		self.writeToAliceConfigurationFile(self.aliceConfigurations)
Esempio n. 2
0
    def writeToAliceConfigurationFile(self, confs: dict):
        """
		Saves the given configuration into config.py
		:param confs: the dict to save
		"""
        sort = dict(sorted(confs.items()))

        # Only store "active", "version", "author", "conditions" value for module config
        misterProper = ['active', 'version', 'author', 'conditions']

        # pop modules key so it gets added in the back
        modules = sort.pop('modules')

        sort['modules'] = dict()
        for moduleName, setting in modules.items():
            moduleCleaned = {
                key: value
                for key, value in setting.items() if key in misterProper
            }
            sort['modules'][moduleName] = moduleCleaned

        self._aliceConfigurations = sort

        try:
            s = json.dumps(sort,
                           indent=4).replace('false',
                                             'False').replace('true', 'True')
            Path('config.py').write_text(f'settings = {s}')
            importlib.reload(config)
        except Exception:
            raise ConfigurationUpdateFailed()
Esempio n. 3
0
    def updateAliceConfiguration(self,
                                 key: str,
                                 value: typing.Any,
                                 dump: bool = True,
                                 doPreAndPostProcessing: bool = True):
        """
		Updating a core config is sensitive, if the request comes from a skill.
		First check if the request came from a skill at anytime and if so ask permission
		to the user
		:param doPreAndPostProcessing: If set to false, all pre and post processings won't be called
		:param key: str
		:param value: str
		:param dump: bool If set to False, the configs won't be dumped to the json file
		:return: None
		"""

        # TODO reimplement UI side
        # rootSkills = [name.lower() for name in self.SkillManager.NEEDED_SKILLS]
        # callers = [inspect.getmodulename(frame[1]).lower() for frame in inspect.stack()]
        # if 'aliceskill' in callers:
        # 	skillName = callers[callers.index("aliceskill") + 1]
        # 	if skillName not in rootSkills:
        # 		self._pendingAliceConfUpdates[key] = value
        # 		self.logWarning(f'Skill **{skillName}** is trying to modify a core configuration')
        #
        # 		self.ThreadManager.doLater(
        # 			interval=2,
        # 			func=self.MqttManager.publish,
        # 			kwargs={
        # 				'topic': constants.TOPIC_SKILL_UPDATE_CORE_CONFIG_WARNING,
        # 				'payload': {
        # 					'skill': skillName,
        # 					'key'  : key,
        # 					'value': value
        # 				}
        # 			}
        # 		)
        # 		return

        if key not in self._aliceConfigurations:
            self.logWarning(
                f'Was asked to update **{key}** but key doesn\'t exist')
            raise ConfigurationUpdateFailed()

        pre = self.getAliceConfUpdatePreProcessing(key)
        if doPreAndPostProcessing and pre and not self.ConfigManager.doConfigUpdatePreProcessing(
                pre, value):
            return

        if key == 'deviceName':
            self.updateMainDeviceName(value=value)

        self._aliceConfigurations[key] = value

        if dump:
            self.writeToAliceConfigurationFile()

        pp = self.ConfigManager.getAliceConfUpdatePostProcessing(key)
        if doPreAndPostProcessing and pp:
            self.ConfigManager.doConfigUpdatePostProcessing(pp)
    def updateAliceConfiguration(self, key: str, value: typing.Any):
        if key not in self._aliceConfigurations:
            self.logWarning(
                f'Was asked to update **{key}** but key doesn\'t exist')
            raise ConfigurationUpdateFailed()

        self._aliceConfigurations[key] = value
        self.writeToAliceConfigurationFile(self.aliceConfigurations)
    def updateAliceConfiguration(self,
                                 key: str,
                                 value: Any,
                                 dump: bool = True,
                                 doPreAndPostProcessing: bool = True):
        """
		Updating a core config is sensitive, if the request comes from a skill.
		First check if the request came from a skill at anytime and if so ask permission
		to the user
		:param doPreAndPostProcessing: If set to false, all pre- and post-processing won't be called
		:param key: str
		:param value: str
		:param dump: bool If set to False, the configs won't be dumped to the json file
		:return: None
		"""

        rootSkills = [name.lower() for name in self.SkillManager.NEEDED_SKILLS]
        callers = [
            inspect.getmodulename(frame[1]).lower()
            for frame in inspect.stack()
        ]
        if 'aliceskill' in callers:
            skillName = callers[callers.index('aliceskill') + 1]
            if skillName not in rootSkills:
                self._pendingAliceConfUpdates[key] = value
                self.logWarning(
                    f'Skill **{skillName}** is trying to modify a core configuration'
                )

                self.WebUINotificationManager.newNotification(
                    typ=UINotificationType.ALERT,
                    notification='coreConfigUpdateWarning',
                    replaceBody=[skillName, key, value])
                return

        if key not in self._aliceConfigurations:
            self.logWarning(
                f"Was asked to update **{key}** but key doesn't exist")
            raise ConfigurationUpdateFailed()

        pre = self.getAliceConfUpdatePreProcessing(key)
        if doPreAndPostProcessing and pre and not self.ConfigManager.doConfigUpdatePreProcessing(
                pre, value):
            return

        if key == 'deviceName':
            self.updateMainDeviceName(value=value)

        self._aliceConfigurations[key] = value

        if dump:
            self.writeToAliceConfigurationFile()

        pp = self.ConfigManager.getAliceConfUpdatePostProcessing(key)
        if doPreAndPostProcessing and pp:
            self.ConfigManager.doConfigUpdatePostProcessing(pp)
Esempio n. 6
0
    def writeToAliceConfigurationFile(self, confs: dict = None):
        """
		Saves the given configuration into config.json
		:param confs: the dict to save
		"""
        confs = confs if confs else self._aliceConfigurations

        sort = dict(sorted(confs.items()))
        self._aliceConfigurations = sort

        try:
            self.CONFIG_FILE.write_text(
                json.dumps(sort, indent=4, sort_keys=True))
        except Exception:
            raise ConfigurationUpdateFailed()
    def writeToAliceConfigurationFile(self, confs: dict):
        """
		Saves the given configuration into config.py
		:param confs: the dict to save
		"""
        sort = dict(sorted(confs.items()))
        self._aliceConfigurations = sort

        try:
            confString = json.dumps(sort, indent=4).replace('false',
                                                            'False').replace(
                                                                'true', 'True')
            Path(self.CONFIG_FILE).write_text(f'settings = {confString}')
            importlib.reload(config)
        except Exception:
            raise ConfigurationUpdateFailed()
Esempio n. 8
0
    def updateAliceConfiguration(self, key: str, value: typing.Any):
        try:
            if key not in self._aliceConfigurations:
                self._logger.warning(
                    '[{}] Was asked to update {} but key doesn\'t exist'.
                    format(self.name, key))
                raise Exception

            #Remove module configurations
            if key == 'modules':
                value = dict((k, v) for k, v in value.items()
                             if k not in self._aliceModuleConfigurationKeys)

            self._aliceConfigurations[key] = value
            self._writeToAliceConfigurationFile(self.aliceConfigurations)
        except Exception as e:
            raise ConfigurationUpdateFailed(e)
Esempio n. 9
0
    def updateAliceConfiguration(self, key: str, value: typing.Any):
        """
		Updating a core config is sensitive, if the request comes from a skill.
		First check if the request came from a skill at anytime and if so ask permission
		to the user
		:param key: str
		:param value: str
		:return: None
		"""

        rootSkills = [name.lower() for name in self.SkillManager.NEEDED_SKILLS]
        callers = [
            inspect.getmodulename(frame[1]).lower()
            for frame in inspect.stack()
        ]
        if 'aliceskill' in callers:
            skillName = callers[callers.index("aliceskill") + 1]
            if skillName not in rootSkills:
                self._pendingAliceConfUpdates[key] = value
                self.logWarning(
                    f'Skill **{skillName}** is trying to modify a core configuration'
                )

                self.ThreadManager.doLater(
                    interval=2,
                    func=self.MqttManager.publish,
                    kwargs={
                        'topic':
                        constants.TOPIC_SKILL_UPDATE_CORE_CONFIG_WARNING,
                        'payload': {
                            'skill': skillName,
                            'key': key,
                            'value': value
                        }
                    })
                return

        if key not in self._aliceConfigurations:
            self.logWarning(
                f'Was asked to update **{key}** but key doesn\'t exist')
            raise ConfigurationUpdateFailed()

        self._aliceConfigurations[key] = value
        self.writeToAliceConfigurationFile()
    def updateAliceConfiguration(self,
                                 key: str,
                                 value: typing.Any,
                                 doPreAndPostProcessing: bool = True):
        if key not in self._aliceConfigurations:
            self.logWarning(
                f'Was asked to update {key} but key doesn\'t exist')
            raise ConfigurationUpdateFailed()

        if doPreAndPostProcessing:
            pre = self.getAliceConfUpdatePreProcessing(confName=key)

            if pre and not self.ConfigManager.doConfigUpdatePreProcessing(
                    pre, value):
                return

        self._aliceConfigurations[key] = value
        self.writeToAliceConfigurationFile(self.aliceConfigurations)

        if doPreAndPostProcessing:
            post = self.getAliceConfUpdatePostProcessing(confName=key)
            if post:
                self.doConfigUpdatePostProcessing(functions={post})