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] yaml_checker.check(source, y) assistant = yaml_assistant.YamlAssistant(name, y, source, superassistant, fully_loaded=fully_loaded, role=role) return assistant
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
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 = loaded_ass 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
def _create_snippet(cls, name, path, parsed_yaml): yaml_checker.check(path, parsed_yaml) snip = snippet.Snippet(name, parsed_yaml, path) return snip
def _create_snippet(cls, name, path, parsed_yaml): yaml_checker.check(path, parsed_yaml) snip = snippet.Snippet(name, parsed_yaml, path) cls._snippets[name] = snip return snip
def get_snippet_by_name(cls, name): found = cls._find_snippet(name) if found != None: return found loaded = yaml_loader.YamlLoader.load_yaml_by_relpath(cls.snippets_dirs, name + '.yaml') if loaded: path, parsed_yaml = loaded yaml_checker.check(path, parsed_yaml) snip = snippet.Snippet(name, parsed_yaml, path) cls._snippets[snip.path] = snip return snip raise exceptions.SnippetNotFoundException('no such snippet: {name}'.format(name=name))
def get_snippet_by_name(cls, name): found = cls._find_snippet(name) if found != None: return found loaded = yaml_loader.YamlLoader.load_yaml_by_relpath( cls.snippets_dirs, name + '.yaml') if loaded: path, parsed_yaml = loaded yaml_checker.check(path, parsed_yaml) snip = snippet.Snippet(name, parsed_yaml, path) cls._snippets[snip.path] = snip return snip raise exceptions.SnippetNotFoundException( 'no such snippet: {name}'.format(name=name))
def check_yamls(cls, dap): '''Check that all assistants and snippets are valid. Return list of DapProblems.''' problems = list() for yaml in dap.assistants_and_snippets: path = yaml + '.yaml' parsed_yaml = YamlLoader.load_yaml_by_path(dap._get_file(path, prepend=True)) if parsed_yaml: try: yaml_checker.check(path, parsed_yaml) except YamlError as e: problems.append(DapProblem(exc_as_decoded_string(e), level=logging.ERROR)) else: problems.append(DapProblem('Empty YAML ' + path, level=logging.WARNING)) return problems
def check_yamls(cls, dap): '''Check that all assistants and snippets are valid. Return list of DapProblems.''' problems = list() for yaml in dap.assistants_and_snippets: path = yaml + '.yaml' parsed_yaml = YamlLoader.load_yaml_by_path( dap._get_file(path, prepend=True)) if parsed_yaml: try: yaml_checker.check(path, parsed_yaml) except YamlError as e: problems.append( DapProblem(exc_as_decoded_string(e), level=logging.ERROR)) else: problems.append( DapProblem('Empty YAML ' + path, level=logging.WARNING)) return problems