def assistant_from_yaml(cls, source, y, superassistant, fully_loaded=True,
                            role=settings.DEFAULT_ASSISTANT_ROLE):
        """Constructs instance of YamlAssistant loaded from given structure y, loaded
        from source file source.

        Args:
            source: path to assistant source file
            y: loaded yaml structure
            superassistant: superassistant of this assistant
        Returns:
            YamlAssistant instance constructed from y with source file source
        Raises:
            YamlError: if the assistant is malformed
        """
        # In pre-0.9.0, we required assistant to be a mapping of {name: assistant_attributes}
        # now we allow that, but we also allow omitting the assistant name and putting
        # the attributes to top_level, too.
        name = os.path.splitext(os.path.basename(source))[0]
        attrs = utils.get_assistant_attrs_from_dict(y, source)
        yaml_checker.check(source, attrs)
        assistant = yaml_assistant.YamlAssistant(name,
                                                 attrs,
                                                 source,
                                                 superassistant,
                                                 fully_loaded=fully_loaded,
                                                 role=role)

        return assistant
Esempio n. 2
0
 def inner(self, *args, **kwargs):
     if not self.fully_loaded:
         loaded_yaml = yaml_loader.YamlLoader.load_yaml_by_path(self.path)
         self.parsed_yaml = utils.get_assistant_attrs_from_dict(
             loaded_yaml, self.path)
         self.fully_loaded = True
     return method(self, *args, **kwargs)
    def assistant_from_yaml(cls,
                            source,
                            y,
                            superassistant,
                            fully_loaded=True,
                            role=settings.DEFAULT_ASSISTANT_ROLE):
        """Constructs instance of YamlAssistant loaded from given structure y, loaded
        from source file source.

        Args:
            source: path to assistant source file
            y: loaded yaml structure
            superassistant: superassistant of this assistant
        Returns:
            YamlAssistant instance constructed from y with source file source
        Raises:
            YamlError: if the assistant is malformed
        """
        # In pre-0.9.0, we required assistant to be a mapping of {name: assistant_attributes}
        # now we allow that, but we also allow omitting the assistant name and putting
        # the attributes to top_level, too.
        name = os.path.splitext(os.path.basename(source))[0]
        attrs = utils.get_assistant_attrs_from_dict(y, source)
        yaml_checker.check(source, attrs)
        assistant = yaml_assistant.YamlAssistant(name,
                                                 attrs,
                                                 source,
                                                 superassistant,
                                                 fully_loaded=fully_loaded,
                                                 role=role)

        return assistant
Esempio n. 4
0
    def _ass_refresh_attrs(self, cached_ass, file_ass):
        """Completely refreshes cached assistant from file.

        Args:
            cached_ass: an assistant from cache hierarchy
                        (for format see Cache class docstring)
            file_ass: the respective assistant from filesystem hierarchy
                      (for format see what refresh_role accepts)
        """
        # we need to process assistant in custom way to see unexpanded args, etc.
        loaded_ass = yaml_loader.YamlLoader.load_yaml_by_path(file_ass['source'], log_debug=True)
        attrs = utils.get_assistant_attrs_from_dict(loaded_ass, file_ass['source'])
        yaml_checker.check(file_ass['source'], attrs)
        cached_ass['source'] = file_ass['source']
        cached_ass['ctime'] = os.path.getctime(file_ass['source'])
        cached_ass['attrs'] = {}
        cached_ass['snippets'] = {}
        # only cache these attributes if they're actually found in assistant
        # we do this to specify the default values for them just in one place
        # which is currently YamlAssistant.parsed_yaml property setter
        for a in ['fullname', 'description', 'icon_path']:
            if a in attrs:
                cached_ass['attrs'][a] = attrs.get(a)
        # args have different processing, we can't just take them from assistant
        if 'args' in attrs:
            cached_ass['attrs']['args'] = {}
        for argname, argparams in attrs.get('args', {}).items():
            if 'use' in argparams or 'snippet' in argparams:
                snippet_name = argparams.pop('use', None) or argparams.pop('snippet')
                snippet = yaml_snippet_loader.YamlSnippetLoader.get_snippet_by_name(snippet_name)
                cached_ass['attrs']['args'][argname] = snippet.get_arg_by_name(argname)
                cached_ass['attrs']['args'][argname].update(argparams)
                cached_ass['snippets'][snippet.name] = self._get_snippet_ctime(snippet.name)
            else:
                cached_ass['attrs']['args'][argname] = argparams
Esempio n. 5
0
    def _ass_refresh_attrs(self, cached_ass, file_ass):
        """Completely refreshes cached assistant from file.

        Args:
            cached_ass: an assistant from cache hierarchy
                        (for format see Cache class docstring)
            file_ass: the respective assistant from filesystem hierarchy
                      (for format see what refresh_role accepts)
        """
        # we need to process assistant in custom way to see unexpanded args, etc.
        loaded_ass = yaml_loader.YamlLoader.load_yaml_by_path(file_ass['source'])
        attrs = utils.get_assistant_attrs_from_dict(loaded_ass, file_ass['source'])
        yaml_checker.check(file_ass['source'], attrs)
        cached_ass['source'] = file_ass['source']
        cached_ass['ctime'] = os.path.getctime(file_ass['source'])
        cached_ass['attrs'] = {}
        cached_ass['snippets'] = {}
        # only cache these attributes if they're actually found in assistant
        # we do this to specify the default values for them just in one place
        # which is currently YamlAssistant.parsed_yaml property setter
        for a in ['fullname', 'description', 'icon_path']:
            if a in attrs:
                cached_ass['attrs'][a] = attrs.get(a)
        # args have different processing, we can't just take them from assistant
        if 'args' in attrs:
            cached_ass['attrs']['args'] = {}
        for argname, argparams in attrs.get('args', {}).items():
            if 'use' in argparams or 'snippet' in argparams:
                snippet_name = argparams.pop('use', None) or argparams.pop('snippet')
                snippet = yaml_snippet_loader.YamlSnippetLoader.get_snippet_by_name(snippet_name)
                cached_ass['attrs']['args'][argname] = snippet.get_arg_by_name(argname)
                cached_ass['attrs']['args'][argname].update(argparams)
                cached_ass['snippets'][snippet.name] = self._get_snippet_ctime(snippet.name)
            else:
                cached_ass['attrs']['args'][argname] = argparams
Esempio n. 6
0
 def inner(self, *args, **kwargs):
     if not self.fully_loaded:
         loaded_yaml = yaml_loader.YamlLoader.load_yaml_by_path(self.path)
         self.parsed_yaml = utils.get_assistant_attrs_from_dict(loaded_yaml, self.path)
         self.fully_loaded = True
     return method(self, *args, **kwargs)