Beispiel #1
0
def uri_to_path(uri):
    """Takes a file URI and returns the path.

    Args:
        uri: Either bytes or text URI
    Raises:
        ValueError: in case the URI isn't a valid file URI
    Returns:
        fsnative
    """

    if not isinstance(uri, bytes):
        uri = uri.encode("ascii")

    parsed = urlparse(uri)
    scheme = parsed[0]
    netloc = parsed[1]
    path = parsed[2]

    if scheme != b"file" or netloc:
        raise ValueError("Not a file URI")
    else:
        if os.name == "nt":
            return url2pathname(path).decode("utf-8")
        else:
            return url2pathname(path)
Beispiel #2
0
def show_uri(label, uri):
    """Shows a uri. The uri can be anything handled by GIO or a quodlibet
    specific one.

    Currently handled quodlibet uris:
        - quodlibet:///prefs/plugins/<plugin id>

    Args:
        label (str)
        uri (str) the uri to show
    Returns:
        True on success, False on error
    """

    parsed = urlparse(uri)
    if parsed.scheme == "quodlibet":
        if parsed.netloc != "":
            print_w("Unknown QuodLibet URL format (%s)" % uri)
            return False
        else:
            return __show_quodlibet_uri(parsed)
    else:
        # Gtk.show_uri_on_window exists since 3.22
        if hasattr(Gtk, "show_uri_on_window"):
            from quodlibet.qltk import get_top_parent
            return Gtk.show_uri_on_window(get_top_parent(label), uri, 0)
        else:
            return Gtk.show_uri(None, uri, 0)
Beispiel #3
0
    def __new__(klass, value, escaped=True):
        """Create a new URI object. By default, the URI is assumed to be
        escaped already. Pass escaped=False if you need the URI escaped
        (this is imperfect now).

        The URI returned will be equivalent, but not necessarily
        equal, to the value passed in."""

        values = list(urlparse(value))
        if not escaped:
            # FIXME: Handle netloc
            # FIXME: Handle query args
            values[2] = quote_plus(values[2], safe="/~")

        value = _urlunparse(values)
        obj = str.__new__(klass, value)

        # len() > 1 to not interpret windows paths as URIs
        # there are no schemes
        if not obj.scheme or not len(obj.scheme) > 1:
            raise ValueError("URIs must have a scheme, such as 'http://'")
        elif not (obj.netloc or obj.path):
            raise ValueError("URIs must have a network location or path")
        else:
            return obj
Beispiel #4
0
 def __handle_incoming_uri(self, obj, uri):
     if not PROCESS_QL_URLS:
         print_w("Processing of quodlibet:// URLs is disabled. (%s)" % uri)
         return
     uri = urlparse(uri)
     if (uri.scheme == 'quodlibet' and uri.netloc == 'callbacks' and
             uri.path == '/soundcloud'):
         try:
             code = parse_qs(uri.query)["code"][0]
         except IndexError:
             print_w("Malformed response in callback URI: %s" % uri)
             return
         print_d("Processing Soundcloud callback (%s)" % (uri,))
         self.api_client.get_token(code)
     else:
         print_w("Unknown URL format (%s)" % (uri,))
Beispiel #5
0
def uri_is_valid(uri):
    """Returns True if the passed in text is a valid URI (file, http, etc.)

    Returns:
        bool
    """

    if not isinstance(uri, bytes):
        uri = uri.encode("ascii")

    parsed = urlparse(uri)
    if not parsed.scheme or not len(parsed.scheme) > 1:
        return False
    elif not (parsed.netloc or parsed.path):
        return False
    else:
        return True
Beispiel #6
0
def show_uri(label, uri):
    """Shows a uri. The uri can be anything handled by GIO or a quodlibet
    specific one.

    Currently handled quodlibet uris:
        - quodlibet:///prefs/plugins/<plugin id>

    Args:
        label (str)
        uri (str) the uri to show
    Returns:
        True on success, False on error
    """

    parsed = urlparse(uri)
    if parsed.scheme == "quodlibet":
        if parsed.netloc != "":
            print_w("Unknown QuodLibet URL format (%s)" % uri)
            return False
        else:
            return __show_quodlibet_uri(parsed)
    elif parsed.scheme == "file" and (is_windows() or is_osx()):
        # Gio on non-Linux can't handle file URIs for some reason,
        # fall back to our own implementation for now
        from quodlibet.qltk.showfiles import show_files

        try:
            filepath = uri2fsn(uri)
        except ValueError:
            return False
        else:
            return show_files(filepath, [])
    else:
        # Gtk.show_uri_on_window exists since 3.22
        try:
            if hasattr(Gtk, "show_uri_on_window"):
                from quodlibet.qltk import get_top_parent
                return Gtk.show_uri_on_window(get_top_parent(label), uri, 0)
            else:
                return Gtk.show_uri(None, uri, 0)
        except GLib.Error:
            return False
Beispiel #7
0
def uri_is_valid(uri):
    """Returns True if the passed in text is a valid URI (file, http, etc.)

    Args:
        uri(text or bytes)
    Returns:
        bool
    """

    try:
        if isinstance(uri, bytes):
            uri.decode("ascii")
        elif not isinstance(uri, bytes):
            uri = uri.encode("ascii")
    except ValueError:
        return False

    parsed = urlparse(uri)
    if not parsed.scheme or not len(parsed.scheme) > 1:
        return False
    elif not (parsed.netloc or parsed.path):
        return False
    else:
        return True
Beispiel #8
0
def uri_is_valid(uri):
    """Returns True if the passed in text is a valid URI (file, http, etc.)

    Args:
        uri(text or bytes)
    Returns:
        bool
    """

    try:
        if isinstance(uri, bytes):
            uri.decode("ascii")
        elif not isinstance(uri, bytes):
            uri = uri.encode("ascii")
    except ValueError:
        return False

    parsed = urlparse(uri)
    if not parsed.scheme or not len(parsed.scheme) > 1:
        return False
    elif not (parsed.netloc or parsed.path):
        return False
    else:
        return True
Beispiel #9
0
    def path(self):
        """URI path (e.g. '/~user')"""

        return urlparse(self)[2]
Beispiel #10
0
def uri_is_valid(uri):
    return bool(urlparse(uri)[0])
Beispiel #11
0
 def scheme(self):
     """URI scheme (e.g. 'http')"""
     return urlparse(self)[0]
Beispiel #12
0
 def netloc(self):
     """URI network location (e.g. 'example.com:21')"""
     return urlparse(self)[1]
Beispiel #13
0
    def path(self):
        """URI path (e.g. '/~user')"""

        return urlparse(self)[2]
Beispiel #14
0
 def params(self):
     """URI parameters"""
     return urlparse(self)[3]
Beispiel #15
0
 def fragment(self):
     """URI fragment ('foo' in '#foo')"""
     return urlparse(self)[5]
Beispiel #16
0
 def query(self):
     """URI query string (e.g. 'foo=bar&a=b')"""
     return urlparse(self)[4]
Beispiel #17
0
 def query(self):
     """URI query string (e.g. 'foo=bar&a=b')"""
     return urlparse(self)[4]
Beispiel #18
0
def uri_is_valid(uri):
    return bool(urlparse(uri)[0])
Beispiel #19
0
 def netloc(self):
     """URI network location (e.g. 'example.com:21')"""
     return urlparse(self)[1]
Beispiel #20
0
 def scheme(self):
     """URI scheme (e.g. 'http')"""
     return urlparse(self)[0]
Beispiel #21
0
 def unescaped(self):
     """an unescaped str (not URI) version of the URI"""
     values = list(urlparse(self))
     values[2] = unquote_plus(values[2])
     return _urlunparse(values)
Beispiel #22
0
 def fragment(self):
     """URI fragment ('foo' in '#foo')"""
     return urlparse(self)[5]
Beispiel #23
0
 def params(self):
     """URI parameters"""
     return urlparse(self)[3]
Beispiel #24
0
 def unescaped(self):
     """an unescaped str (not URI) version of the URI"""
     values = list(urlparse(self))
     values[2] = unquote_plus(values[2])
     return _urlunparse(values)