From 594b0d29106f49862b6fb69587c8cea76051bf8f Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 07:59:51 +0100 Subject: [PATCH] Refactors path validity check to its own function --- qutebrowser/utils/urlutils.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index e70a09f2d..54a55dd9b 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -348,6 +348,40 @@ def raise_cmdexc_if_invalid(url): if not url.isValid(): raise cmdexc.CommandError(get_errstring(url)) +def get_path_if_valid(pathstr, cwd = None, relative=False, check_exists = False): + """Check if path is a valid path + + Args: + pathstr: The path as string + + Return: + True if it is a valid path, False otherwise. + """ + log.url.debug("Checking if '{}' is a path".format(pathstr)) + + pathstr = pathstr.strip() + expanded = os.path.expanduser(pathstr) + + if os.path.isabs(expanded): + path = expanded + elif relative and cwd: + path = os.path.join(cwd, expanded) + elif relative: + try: + path = os.path.abspath(expanded) + except OSError: + path = None + else: + path = None + + if check_exists: + if path is not None and os.path.exists(path): + log.url.debug("URL is a local file") + else: + path = None + + return path + def filename_from_url(url): """Get a suitable filename from an URL.