wheezy.core / src / wheezy / core / url.py

""" ``url`` module
"""

from wheezy.core.comp import urlunsplit


def urlparts(parts=None, scheme=None, netloc=None, path=None,
             query=None, fragment=None):
    """ Factory function for :py:class:`~wheezy.core.url.UrlParts` that
        create an instance :py:class:`~wheezy.core.url.UrlParts` with
        partial content.

        ``parts`` must be a 5-tuple:
        (scheme, netloc, path, query, fragment)

        >>> from wheezy.core.comp import urlsplit
        >>> parts = urlsplit('http://www.python.org/dev/peps/pep-3333')
        >>> urlparts(parts)
        urlparts('http', 'www.python.org', '/dev/peps/pep-3333', '', '')
        >>> urlparts(scheme='https', path='/test')
        urlparts('https', None, '/test', None, None)

        Otherwise raise assertion error

        >>> urlparts(('https', )) # doctest: +ELLIPSIS
        Traceback (most recent call last):
            ...
        AssertionError: ...
    """
    if not parts:
        parts = (scheme, netloc, path, query, fragment)
    return UrlParts(parts)


class UrlParts(tuple):
    """ Concrete class for :func:`urlparse.urlsplit` results.
    """

    def __init__(self, parts):
        assert len(parts) == 5, '`parts` must be a tupple of length 6'
        super(UrlParts, self).__init__()

    def __repr__(self):
        return 'urlparts' + super(UrlParts, self).__repr__()

    def geturl(self):
        """ Return the re-combined version of the original URL as a string.

            >>> from wheezy.core.comp import urlsplit
            >>> parts = urlsplit('http://www.python.org/dev/peps/pep-3333')
            >>> parts = urlparts(parts)
            >>> parts.geturl()
            'http://www.python.org/dev/peps/pep-3333'
        """
        return urlunsplit(self)

    def join(self, other):
        """ Joins with another ``UrlParts`` instance by taking
            none-empty values from ``other``. Returns new ``UrlParts``
            instance.

            Query and Fragment parts are taken unconditionally from ``other``.

            >>> from wheezy.core.comp import urlsplit
            >>> parts = urlsplit('http://www.python.org/dev/peps/pep-3333')
            >>> parts = urlparts(parts)
            >>> parts = parts.join(urlparts(scheme='https', path='/test'))
            >>> parts.geturl()
            'https://www.python.org/test'
        """
        parts = (
            other[0] or self[0],
            other[1] or self[1],
            other[2] or self[2],
            other[3],
            other[4])
        return UrlParts(parts)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.