Example #1
0
    def _update_imports(self, history=[]):
        for impname, imp in (self.imports or {}).items():
            if impname not in SKIP_LIBS:
                source = imp.get("source")
                if source is not None and source.endswith(".robot"):
                    try:
                        source = find_file(source)
                    except Exception:
                        pass

                self._load_libdoc(impname, source=source)

        for match in re.findall(RE_IMPORT, "\n".join(history), flags=re.I | re.M):
            self._load_libdoc(match[2])
 def visit_ResourceImport(self, node):  # noqa
     """
     Check all imports in scanned file. If one of our scanned file is imported somewhere else
     it means this file is resource type
      """
     path_normalized = normalize_robot_path(node.name, Path(self.source).parent, self.exec_dir)
     try:
         path_normalized = find_file(path_normalized, self.source.parent, file_type='Resource')
     except DataError:
         pass
     else:
         path = Path(path_normalized)
         if path in self.files:
             self.files[path] = FileType.RESOURCE
Example #3
0
    def _process_dependency(self, dependency):
        """
        Processes a test suite dependency (could be a Resource or Library). This is read off the disk and added to the
        dependencies dict. This will then get transferred over to the remote side with the test suite. A dependency's
        location will be different on the remote host. This function also updates the reference to the dependency so
        that it resolves correctly on the remote side

        :param dependency: dependency to process
        :type dependency: robot.model.imports.Import
        """
        # RobotFramework will be installed on the remote machine so we don't need to bundle these
        if dependency.name not in STDLIBS:
            # Locate the dependency file
            dep_filepath = find_file(dependency.name, dependency.directory, dependency.type)
            dep_filename = os.path.basename(dep_filepath)
            logger.debug('Resolved dependency to: `{}`'.format(dependency.name))

            # Change the path to the reference so that it will resolve on the remote side. The directory containing all
            # of the dependencies will be added to the PYTHONPATH, so just the filename is sufficient
            dependency.name = dep_filename

            if dep_filename not in self._dependencies:
                if dependency.type == 'Resource':
                    # Import the Resource in order to parse its dependencies
                    res = ResourceFile(dep_filepath).populate()

                    # A Resource may have a dependency on other Resource and Library files, also collect these
                    for imp in res.imports:
                        logger.debug('Processing dependency: `{}` for: `{}`'.format(imp.name, dependency.name))
                        self._process_dependency(imp)

                    # Get the resource file data with the new reference paths
                    string_io = StringIO()
                    res.save(output=string_io)
                    logger.debug('Patched Resource file: `{}`'.format(dependency.name))
                    self._dependencies[dep_filename] = string_io.getvalue()
                elif dependency.type == 'Library':
                    # A Library is just a python file, so read this straight from disk
                    logger.debug('Reading Python library from disk: `{}`'.format(dep_filepath))
                    self._dependencies[dep_filename] = read_file_from_disk(dep_filepath)
            else:
                logger.debug('Dependency is already in the cache, skipping')
Example #4
0
    def do_inspect(self, code: str, cursor_pos: int, detail_level: int,
                   history: List[str]) -> dict:
        """ Handle inspect payload and return a message spec with some docs
            set of completions
        """
        doc = None

        line, line_cursor, offset = find_line(code, cursor_pos)
        tokens = DataRow(RobotReader.split_row(line)).data

        self.docs(history + [code])

        if len(tokens) > 1:
            if tokens[0].lower() in ["library"]:
                doc = self._lib_html(tokens[1], history)
            elif tokens[0].lower() in ["resource"]:
                source = None
                try:
                    source = find_file(tokens[1])
                except Exception:
                    pass

                if source is not None:
                    doc = self._lib_html(source, history)

        if not doc:
            doc = self._keyword_html(tokens, code, line, line_cursor, offset,
                                     history)

        if doc is not None:
            return {
                "status": "ok",
                "data": {
                    "text/html": doc
                },
                "metadata": {},
                "found": True,
            }
        else:
            return {"status": "ok", "data": {}, "metadata": {}, "found": False}
Example #5
0
    def _load_libdoc(self, name, source=None, use_cache=True):
        """ Try to load a libdoc, either by name, or named source. Tries to use
            the cache.
        """
        strategies = [name]

        if source is not None and source.endswith(".robot"):
            try:
                strategies += [find_file(source)]
            except Exception:
                pass

        for strategy in strategies:
            if use_cache and strategy in self._doc_cache:
                return self._doc_cache[strategy]

            try:
                libdoc = LibraryDocumentation(strategy)
                self._doc_cache[strategy] = self._doc_cache[name] = libdoc
                return libdoc
            except Exception as err:
                pass
                self.log.debug("Could not load libdoc for %s: %s", strategy, err)