示例#1
0
    def loop(self):
        logger.set_file_logger(self.config_name)
        logger.info(f'Start scheduler loop: {self.config_name}')
        is_first = True
        failure_record = {}

        while 1:
            if self.stop_event is not None:
                if self.stop_event.is_set():
                    logger.info("Update event detected")
                    logger.info(f"Alas [{self.config_name}] exited.")
                    break
            task = self.get_next_task()

            # Skip first restart
            if is_first and task == 'Restart':
                logger.info('Skip task `Restart` at scheduler start')
                self.config.task_delay(server_update=True)
                del self.__dict__['config']
                continue

            # Run
            logger.info(f'Scheduler: Start task `{task}`')
            self.device.stuck_record_clear()
            self.device.click_record_clear()
            self.device.screenshot()
            logger.hr(task, level=0)
            success = self.run(inflection.underscore(task))
            logger.info(f'Scheduler: End task `{task}`')
            is_first = False

            # Check failures
            failed = deep_get(failure_record, keys=task, default=0)
            failed = 0 if success else failed + 1
            deep_set(failure_record, keys=task, value=failed)
            if failed >= 3:
                logger.critical(f"Task `{task}` failed 3 or more times.")
                logger.critical(
                    "Possible reason #1: You haven't used it correctly. "
                    "Please read the help text of the options.")
                logger.critical(
                    "Possible reason #2: There is a problem with this task. "
                    "Please contact developers or try to fix it yourself.")
                logger.critical('Request human takeover')
                exit(1)

            if success:
                del self.__dict__['config']
                continue
            elif self.config.Error_HandleError:
                # self.config.task_delay(success=False)
                del self.__dict__['config']
                continue
            else:
                break
示例#2
0
 def _alas_thread_update_config(self) -> None:
     modified = {}
     valid = []
     invalid = []
     while self.alive:
         try:
             d = self.modified_config_queue.get(timeout=10)
             config_name = self.alas_name
         except queue.Empty:
             continue
         modified[self.idx_to_path[d["name"]]] = parse_pin_value(d["value"])
         while True:
             try:
                 d = self.modified_config_queue.get(timeout=1)
                 modified[self.idx_to_path[d["name"]]] = parse_pin_value(
                     d["value"])
             except queue.Empty:
                 config = read_file(filepath_config(config_name))
                 for k, v in modified.copy().items():
                     validate = deep_get(self.ALAS_ARGS, k + ".validate")
                     if not len(str(v)):
                         default = deep_get(self.ALAS_ARGS, k + ".value")
                         deep_set(config, k, default)
                         valid.append(self.path_to_idx[k])
                         modified[k] = default
                     elif not validate or re_fullmatch(validate, v):
                         deep_set(config, k, v)
                         valid.append(self.path_to_idx[k])
                     else:
                         modified.pop(k)
                         invalid.append(self.path_to_idx[k])
                         logger.warning(
                             f"Invalid value {v} for key {k}, skip saving.")
                         # toast(t("Gui.Toast.InvalidConfigValue").format(
                         #       t('.'.join(k.split('.')[1:] + ['name']))),
                         #       duration=0, position='right', color='warn')
                 self.pin_remove_invalid_mark(valid)
                 self.pin_set_invalid_mark(invalid)
                 if modified:
                     toast(
                         t("Gui.Toast.ConfigSaved"),
                         duration=1,
                         position="right",
                         color="success",
                     )
                     logger.info(
                         f"Save config {filepath_config(config_name)}, {dict_to_kv(modified)}"
                     )
                     write_file(filepath_config(config_name), config)
                 modified.clear()
                 valid.clear()
                 invalid.clear()
                 break
示例#3
0
    def loop():
        while True:
            data = input_group(inputs=get_inputs())
            if data is None:
                save()
                break

            if data['action'] == 'Next':

                modified[V.lang][f'{V.group}.{V.arg}.{V.key}'] = data[
                    V.lang].replace("\\n", "\n")
                deep_set(dict_lang[V.lang], f'{V.group}.{V.arg}.{V.key}',
                         data[V.lang].replace("\\n", "\n"))
                next_key()
            if data['action'] == 'Skip':
                next_key()
            elif data['action'] == 'Submit':

                modified[V.lang][f'{V.group}.{V.arg}.{V.key}'] = data[
                    V.lang].replace("\\n", "\n")
                deep_set(dict_lang[V.lang], f'{V.group}.{V.arg}.{V.key}',
                         data[V.lang].replace("\\n", "\n"))
                continue
示例#4
0
    def _save_config(self, modified: Dict[str, str], config_name: str) -> None:
        try:
            valid = []
            invalid = []
            config = State.config_updater.read_file(config_name)
            for k, v in modified.copy().items():
                valuetype = deep_get(self.ALAS_ARGS, k + ".valuetype")
                v = parse_pin_value(v, valuetype)
                validate = deep_get(self.ALAS_ARGS, k + ".validate")
                if not len(str(v)):
                    default = deep_get(self.ALAS_ARGS, k + ".value")
                    deep_set(config, k, default)
                    valid.append(self.path_to_idx[k])
                    modified[k] = default
                elif not validate or re_fullmatch(validate, v):
                    deep_set(config, k, v)
                    valid.append(self.path_to_idx[k])

                    # update Emotion Record if Emotion Value is changed
                    if "Emotion" in k and "Value" in k:
                        k = k.split(".")
                        k[-1] = k[-1].replace("Value", "Record")
                        k = ".".join(k)
                        v = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                        modified[k] = v
                        deep_set(config, k, v)
                        valid.append(self.path_to_idx[k])
                        pin[self.path_to_idx[k]] = v
                else:
                    modified.pop(k)
                    invalid.append(self.path_to_idx[k])
                    logger.warning(f"Invalid value {v} for key {k}, skip saving.")
            self.pin_remove_invalid_mark(valid)
            self.pin_set_invalid_mark(invalid)
            if modified:
                toast(
                    t("Gui.Toast.ConfigSaved"),
                    duration=1,
                    position="right",
                    color="success",
                )
                logger.info(
                    f"Save config {filepath_config(config_name)}, {dict_to_kv(modified)}"
                )
                State.config_updater.write_file(config_name, config)
        except Exception as e:
            logger.exception(e)
示例#5
0
 def save():
     for LANG in LANGUAGES:
         d = read_file(filepath_i18n(LANG))
         for k in modified[LANG].keys():
             deep_set(d, k, modified[LANG][k])
         write_file(filepath_i18n(LANG), d)