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)
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)
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
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,))
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
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
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
def path(self): """URI path (e.g. '/~user')""" return urlparse(self)[2]
def uri_is_valid(uri): return bool(urlparse(uri)[0])
def scheme(self): """URI scheme (e.g. 'http')""" return urlparse(self)[0]
def netloc(self): """URI network location (e.g. 'example.com:21')""" return urlparse(self)[1]
def params(self): """URI parameters""" return urlparse(self)[3]
def fragment(self): """URI fragment ('foo' in '#foo')""" return urlparse(self)[5]
def query(self): """URI query string (e.g. 'foo=bar&a=b')""" return urlparse(self)[4]
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)