Esempio n. 1
0
    def read_config(self, fields: List[OptionInstance]) -> None:
        pure = True

        # get a scanner to read the yaml
        with open(self.__filename, "r+t") as file:
            options = load(file, Loader=Loader)

            if len(fields) > 0:
                for field in fields:
                    if options is not None and field.name in options:
                        # get the type
                        field.return_value(options[field.name])
                    else:
                        field.return_default()
                        pure = False

        # check if there were any missing members
        if not pure:
            # write back to yaml
            with open(self.__filename, "w+t") as file:
                dumper = Dumper(stream=file)

                try:
                    dumper.open()

                    mapping = {field.name: field.value for field in fields}
                    dumper.serialize(dumper.represent_mapping(None, mapping))

                    dumper.close()
                finally:
                    dumper.dispose()

            raise OptionsError(
                'Config is missing some members! Fill them out and try again.')