Esempio n. 1
0
    def load(cls, value: Dict[str, Any], path: str,
             config: ImagesConfig) -> 'Image':
        filename = os.path.basename(path)
        name = mut.withdraw(value, 'name', str)
        if not name:
            msg = 'No "name" field found in {}'.format(path)
            raise ImagesInputError(filename, '<unknown>', msg)

        image = cls(name, filename, config)
        image.alt = mut.withdraw(value, 'alt', str)

        raw_outputs = mut.withdraw(value, 'output', mut.list_str_any_dict)

        try:
            web_output = [
                int(raw['width']) for raw in raw_outputs
                if raw['type'] == 'web'
            ]
            image.width = web_output[0]
        except (ValueError, IndexError) as err:
            raise ImagesInputError(filename, name, 'No "web" output') from err

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            config.root_config.warn(ImagesInputError(path, name, msg))

        return image
Esempio n. 2
0
    def load(cls, value: Any, path: str, config: ExtractConfig) -> 'Extract':
        ref = mut.withdraw(value, 'ref', str)
        if not ref:
            raise ExtractsInputError(path, '<unknown>', 'Extract with no ref')

        extract = cls(ref, path, config)  # type: Extract
        inherit = mut.withdraw(value, 'inherit', mut.str_dict,
                               default={})  # type: Dict[str, str]
        if not inherit:
            inherit = mut.withdraw(value, 'source', mut.str_dict, default={})

        if inherit:
            extract._inherit = (inherit['file'], inherit['ref'])

        replacements = mut.withdraw(value,
                                    'replacement',
                                    mut.str_dict,
                                    default={})  # type: Dict[str, str]
        for src, dest in replacements.items():
            extract.state.replacements[src] = dest

        extract.state.title = mut.withdraw(value, 'title', str)
        extract.state.post = mut.withdraw(value, 'post', str)
        extract.state.style = mut.withdraw(value, 'style', str)
        extract.state.only = mut.withdraw(value, 'only', str)
        extract.state.content = mut.withdraw(value, 'content', str)
        append = mut.withdraw(value, 'append', mut.string_list)
        if append:
            extract.state.append = append

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            raise ExtractsInputError(path, ref, msg)

        return extract
Esempio n. 3
0
def load_exercises(value: Dict[str, Any], path: str,
                   config: ExerciseConfig) -> List['Exercise']:
    ref = mut.withdraw(value, 'ref', str)
    if not ref:
        raise ExerciseInputError(path, ref, 'Missing "ref"')

    languages = mut.withdraw(value, 'languages', str_dict_dict)
    return [
        Exercise.load('{}-{}'.format(ref, language), language, value, path,
                      config) for (language, value) in languages.items()
    ]
Esempio n. 4
0
    def load(cls, ref: str, language: str, value: Dict[str, Any], path: str,
             config: ExerciseConfig) -> 'Exercise':
        exercise = cls(ref, path, config)  # type: Exercise

        exercise.language = language
        src_path = mut.withdraw(value, 'src', str)
        exercise.test_command = mut.withdraw(value, 'test-command', str)

        if not src_path:
            raise ExerciseInputError(path, ref, 'Missing "src"')

        exercise.src_path = config.root_config.get_root_path(src_path)

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            raise ExerciseInputError(path, ref, msg)

        return exercise
Esempio n. 5
0
    def load(cls, value: Any, path: str, config: OptionsConfig) -> 'Option':
        program = mut.withdraw(value, 'program', str)
        if not program:
            raise OptionInputError(path, '<unknown>', 'Missing field "name"')

        name = mut.withdraw(value, 'name', str)
        if not name:
            raise OptionInputError(path, program, 'Missing field "name"')

        try:
            option = cls(program, name, path, config)  # type: Option
        except ValueError as err:
            raise OptionInputError(path, program, str(err)) from err

        try:
            option._populate(value)
        except ValueError as err:
            raise OptionInputError(path, option.state.ref, str(err)) from err

        return option
Esempio n. 6
0
    def load(cls, value: Dict[str, Any], path: str,
             config: ReleaseConfig) -> 'ReleaseEntry':
        ref = mut.withdraw(value, 'ref', str)
        state = ReleaseEntryState(ref)
        entry = cls(ref, state, path, config)  # type: ReleaseEntry

        entry.state.pre = mut.withdraw(value, 'pre', str)
        entry.state.language = mut.withdraw(value, 'language', str)
        entry.state.code = mut.withdraw(value, 'code', str)
        entry.state.content = mut.withdraw(value, 'content', str)
        entry.state.post = mut.withdraw(value, 'post', str)

        if 'source' in value:
            entry._inherit = mut.withdraw(value, 'source', mut.str_dict)['ref']

        replacements = mut.withdraw(value, 'replacement', mut.str_dict)
        if replacements:
            for src, dest in replacements.items():
                entry.state.replacements[src] = dest

        return entry
Esempio n. 7
0
    def load(cls, value: Any, path: str, config: TocConfig) -> 'TocEntry':
        entry_target = mut.withdraw(value, 'file', str)
        if entry_target is None:
            entry_target = mut.withdraw(value, 'ref', str)
        if entry_target is None:
            try:
                entry_target = value['source']['ref']
            except KeyError as err:
                raise TocInputError(path, entry_target, '') from err

        entry = cls(entry_target, path, config)  # type: TocEntry
        entry.state.name = mut.withdraw(value, 'name', str)
        entry.state.description = mut.withdraw(value, 'description', str)
        entry.state.level = mut.withdraw(value, 'level', int)
        entry.state.text_only = mut.withdraw(value, 'text_only', bool)

        raw_inherit = mut.withdraw(value, 'source', mut.str_dict)
        try:
            if raw_inherit:
                entry._inherit = (raw_inherit['file'], raw_inherit['ref'])
        except KeyError as err:
            raise TocInputError(path, entry_target, '') from err

        return entry
Esempio n. 8
0
    def load(cls, value: Any, path: str,
             config: ApiargsConfig) -> 'ApiargEntry':
        entry_name = mut.withdraw(value, 'name', str)
        if entry_name is None:
            try:
                entry_name = value['source']['ref']
            except KeyError as err:
                msg = 'No "name" field found in {}'.format(path)
                raise ApiargsInputError(path, entry_name, msg) from err

        entry = cls(entry_name, path, config)  # type: ApiargEntry
        entry.state.arg_name = mut.withdraw(value, 'arg_name', str)
        entry.state.description = mut.withdraw(value, 'description', str)
        entry.state.interface = mut.withdraw(value, 'interface', str)
        entry.state.operation = mut.withdraw(value, 'operation', str)
        entry.state.optional = mut.withdraw(value, 'optional', bool)
        entry.state.position = mut.withdraw(value, 'position', int)
        entry.state.type = mut.withdraw(value, 'type', mut.str_or_list)
        entry.state.pre = mut.withdraw(value, 'pre', str)

        raw_parent = mut.withdraw(value, 'source', mut.str_dict)
        if raw_parent is not None:
            parent_path = mut.withdraw(raw_parent, 'file', str)
            parent_ref = mut.withdraw(raw_parent, 'ref', str)
            entry._inherit = parent_path + '#' + parent_ref

        replacements = mut.withdraw(value, 'replacement', mut.str_dict)
        if replacements:
            for src, dest in replacements.items():
                entry.state.replacements[src] = dest

        ref = mut.withdraw(value, 'ref', str)
        if ref is not None:
            msg = 'Deprecated field: "ref"'
            config.root_config.warn(ApiargsInputError(path, entry_name, msg))

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            raise ApiargsInputError(path, entry_name, msg)

        return entry
Esempio n. 9
0
    def _populate(self, value: Any) -> None:
        self.state.aliases = mut.withdraw(value, 'aliases', str)
        self.state.args = mut.withdraw(value, 'args', str)
        self.state.default = mut.withdraw(value, 'default', str)
        self.state.description = mut.withdraw(value, 'description', str)

        directive = mut.withdraw(value, 'directive', str)
        try:
            self.state.directive = directive
        except ValueError:
            msg = 'Illegal key "{}". Must be in set {}'.format(
                directive, OptionState.DIRECTIVES)
            raise ValueError(msg)

        self.state.optional = mut.withdraw(value, 'optional', bool)
        self.state.type = mut.withdraw(value, 'type', str)

        self.state.pre = mut.withdraw(value, 'pre', str)
        self.state.post = mut.withdraw(value, 'post', str)

        replacements = mut.withdraw(value, 'replacement', mut.str_dict)
        if replacements:
            for src, dest in replacements.items():
                self.state.replacements[src] = dest

        raw_inherit = mut.withdraw(value, 'inherit', mut.str_dict,
                                   default={})  # type: Dict[str, str]
        if raw_inherit:
            self._inherit = (raw_inherit['file'],
                             '{}-{}'.format(raw_inherit['program'],
                                            raw_inherit['name']))

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            raise ValueError(msg)
Esempio n. 10
0
    def load(cls, value: Any, path: str, config: StepsConfig) -> 'Step':
        inherit = mut.withdraw(value, 'inherit', mut.str_dict) or \
                  mut.withdraw(value, 'source', mut.str_dict)

        ref = mut.withdraw(value, 'ref', str)
        if ref is None:
            ref = inherit.get('ref') if inherit else None
        if ref is None:
            raise KeyError('ref: {}'.format(path))

        step = cls(ref, path, config)  # type: Step
        step.state.pre = mut.withdraw(value, 'pre', str)
        step.state.post = mut.withdraw(value, 'post', str)

        title = mut.withdraw(value, 'title', str_or_str_dict)
        if isinstance(title, str):
            step.state.title = title
        elif title:
            step.state.title = title['text']
            if 'character' in title:
                step.state.level = LEVEL_CHARACTERS[title['character']]

        step.state.level = mut.withdraw(value, 'level', int, default=step.state.level)
        step.state.content = mut.withdraw(value, 'content', str)
        raw_actions = mut.withdraw(value, 'action', str_or_dict_to_list)

        if raw_actions:
            try:
                step.state.actions = [Action.load(raw_action) for raw_action in raw_actions]
            except ValueError as err:
                raise StepsInputError(path, ref, str(err)) from err

        if raw_actions and step.state.content:
            msg = '"action" will replace "content"'
            config.root_config.warn(StepsInputError(path, ref, msg))

        replacements = mut.withdraw(value, 'replacement', mut.str_dict)
        if replacements:
            for src, dest in replacements.items():
                step.state.replacements[src] = dest

        if inherit:
            step._inherit = (inherit['file'], inherit['ref'])

        if mut.withdraw(value, 'stepnum', int):
            msg = 'Deprecated field: "stepnum"'
            config.root_config.warn(StepsInputError(path, ref, msg))

        if value:
            msg = 'Unknown fields "{}"'.format(', '.join(value.keys()))
            config.root_config.warn(StepsInputError(path, ref, msg))

        return step