Exemple #1
0
    def get_item_href(self, item: "Item_Type", parent_dir: str) -> str:
        parsed_parent_dir = safe_urlparse(parent_dir)
        join_type = JoinType.from_parsed_uri(parsed_parent_dir)

        item_root = join_path_or_url(join_type, parent_dir,
                                     "{}".format(item.id))

        return join_path_or_url(join_type, item_root,
                                "{}.json".format(item.id))
Exemple #2
0
    def get_collection_href(self, col: "Collection_Type", parent_dir: str,
                            is_root: bool) -> str:
        parsed_parent_dir = safe_urlparse(parent_dir)
        join_type = JoinType.from_parsed_uri(parsed_parent_dir)

        if is_root:
            col_root = parent_dir
        else:
            col_root = join_path_or_url(join_type, parent_dir,
                                        "{}".format(col.id))

        return join_path_or_url(join_type, col_root, col.DEFAULT_FILE_NAME)
Exemple #3
0
    def get_catalog_href(self, cat: "Catalog_Type", parent_dir: str,
                         is_root: bool) -> str:
        parsed_parent_dir = safe_urlparse(parent_dir)
        join_type = JoinType.from_parsed_uri(parsed_parent_dir)

        if is_root:
            cat_root = parent_dir
        else:
            cat_root = join_path_or_url(join_type, parent_dir,
                                        "{}".format(cat.id))

        return join_path_or_url(join_type, cat_root, cat.DEFAULT_FILE_NAME)
Exemple #4
0
    def get_item_href(self, item: "Item_Type", parent_dir: str) -> str:
        parsed_parent_dir = safe_urlparse(parent_dir)
        join_type = JoinType.from_parsed_uri(parsed_parent_dir)

        if self.item_template is None:
            return self.fallback_strategy.get_item_href(item, parent_dir)
        else:
            template_path = self.item_template.substitute(item)
            if not template_path.endswith(".json"):
                template_path = join_path_or_url(join_type, template_path,
                                                 "{}.json".format(item.id))

            return join_path_or_url(join_type, parent_dir, template_path)
Exemple #5
0
    def get_collection_href(self, col: "Collection_Type", parent_dir: str,
                            is_root: bool) -> str:
        parsed_parent_dir = safe_urlparse(parent_dir)
        join_type = JoinType.from_parsed_uri(parsed_parent_dir)

        if is_root or self.collection_template is None:
            return self.fallback_strategy.get_collection_href(
                col, parent_dir, is_root)
        else:
            template_path = self.collection_template.substitute(col)
            if not template_path.endswith(".json"):
                template_path = join_path_or_url(join_type, template_path,
                                                 col.DEFAULT_FILE_NAME)

            return join_path_or_url(join_type, parent_dir, template_path)
Exemple #6
0
    def read_text_from_href(self, href: str) -> str:
        """Reads file as a UTF-8 string.

        If ``href`` has a "scheme" (e.g. if it starts with "https://") then this will
        use :func:`urllib.request.urlopen` to open the file and read the contents;
        otherwise, :func:`open` will be used to open a local file.

        Args:

            href : The URI of the file to open.
        """
        parsed = safe_urlparse(href)
        href_contents: str
        if parsed.scheme != "":
            try:
                with urlopen(href) as f:
                    href_contents = f.read().decode("utf-8")
            except HTTPError as e:
                raise Exception("Could not read uri {}".format(href)) from e
        else:
            with open(href, encoding="utf-8") as f:
                href_contents = f.read()
        return href_contents