Пример #1
0
    def initial_registry(self, paths):
        """
        Build initial report registry from given paths.

        To fit to validator behaviors, if a path is an existing local file path
        will be resolved to its absolute path. If it does not exists or an URL
        it is left unchanged.

        Arguments:
            paths (list): List of page path(s) which have been required for
                checking.

        Returns:
            list: Registry of required path with initial value ``None``,
            except for unexisting file paths which will contain a critical
            error log.
        """
        registry = []

        for path in paths:
            path_key = path

            if is_local_ressource(path):
                if os.path.exists(path):
                    path_key = os.path.abspath(path)

            registry.append((path_key, None))

        return registry
Пример #2
0
    def get_urls(self, path):
        """
        Parse given sitemap to get urls.

        Arguments:
            path (string): Sitemap path.

        Returns:
            list: List of urls.
        """
        if is_local_ressource(path):
            ressource = self.get_file_ressource(path)
        else:
            ressource = self.get_url_ressource(path)

        contenttype = self.contenttype(path)

        if contenttype == "json":
            return self.parse_sitemap_json(ressource)
        elif contenttype == "xml":
            return self.parse_sitemap_xml(ressource)
        # Should never occurs
        else:
            msg = ("Unable to parse ressource from given path, unknowed "
                   "content type: {}".format(contenttype))
            raise SitemapInvalidError(msg)
Пример #3
0
def validate_sitemap_path(logger, path):
    """
    Validate sitemap file path.

    Invalid sitemap file path is a critical error which should stop program
    execution.

    Arguments:
        logger (logging.logger): Logging object to output error messages.
        paths (list): List of path to validate, only filepaths are checked.

    Returns:
        bool: ``True`` if given filepath is a valid local path or an url, else
        it returns ``False``.
    """
    if is_local_ressource(path):
        if not os.path.exists(path):
            msg = "Given sitemap path does not exists: {}".format(path)
            logger.critical(msg)
            return False
        elif os.path.isdir(path):
            msg = "Directory sitemap path are not supported: {}".format(path)
            logger.critical(msg)
            return False

    return True
Пример #4
0
    def check_local_filepath(self, path):
        """
        Check local file path exist and is not a directory.

        Arguments:
            logger (logging.logger): Logging object to output error messages.
            paths (list): List of path to validate, only filepaths are checked.
        """
        if is_local_ressource(path):
            if not os.path.exists(path):
                msg = "Given path does not exists: {}".format(path)
                return msg
            elif os.path.isdir(path):
                msg = "Directory path are not supported: {}".format(path)
                return msg

        return False
Пример #5
0
def test_is_local_ressource(path, expected):
    """
    Should detect if given path is a filepath.
    """
    assert expected == is_local_ressource(path)