我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用urllib.request.url2pathname()。
def normalize_cdmi_url(self, path): """Normalize URL path relative to current path and return. :arg path: path relative to current path :returns: absolute CDMI URL """ # Turn URL path into OS path for manipulation mypath = url2pathname(path) if not os.path.isabs(mypath): mypath = os.path.join(url2pathname(self.pwd()), mypath) # normalize path mypath = os.path.normpath(mypath) if path.endswith(os.path.sep) and not mypath.endswith(os.path.sep): mypath += os.path.sep # if isinstance(mypath, str): # mypath = mypath.encode('utf8') url = self.cdmi_url + pathname2url(mypath) return url
def get_partial_names_for_template(template=None, get_all=True, requested_partials=None): template = template or settings.DJANGOCMS_SPA_DEFAULT_TEMPLATE if requested_partials: # Transform the requested partials into a list requested_partials = url2pathname(requested_partials).split(',') else: requested_partials = [] try: partials = settings.DJANGOCMS_SPA_TEMPLATES[template]['partials'] except KeyError: try: default_template_path = settings.DJANGOCMS_SPA_DEFAULT_TEMPLATE partials = settings.DJANGOCMS_SPA_TEMPLATES[default_template_path]['partials'] except KeyError: partials = [] if get_all: return partials else: return [partial for partial in partials if partial in requested_partials]
def url2path(url): return url2pathname(urlparse(url).path)
def url2path(url): return urllib.url2pathname(urlparse(url).path)
def uri_to_filename(self, uri: str) -> str: return url2pathname(urlparse(uri).path)
def test_basic(self): # Make sure simple tests pass expected_path = os.path.join("parts", "of", "a", "path") expected_url = "parts/of/a/path" result = urllib_request.pathname2url(expected_path) self.assertEqual(expected_url, result, "pathname2url() failed; %s != %s" % (result, expected_url)) result = urllib_request.url2pathname(expected_url) self.assertEqual(expected_path, result, "url2pathame() failed; %s != %s" % (result, expected_path))
def test_quoting(self): # Test automatic quoting and unquoting works for pathnam2url() and # url2pathname() respectively given = os.path.join("needs", "quot=ing", "here") expect = "needs/%s/here" % urllib_parse.quote("quot=ing") result = urllib_request.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) expect = given result = urllib_request.url2pathname(result) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result)) given = os.path.join("make sure", "using_quote") expect = "%s/using_quote" % urllib_parse.quote("make sure") result = urllib_request.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) given = "make+sure/using_unquote" expect = os.path.join("make+sure", "using_unquote") result = urllib_request.url2pathname(given) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result))
def test_ntpath(self): given = ('/C:/', '///C:/', '/C|//') expect = 'C:\\' for url in given: result = urllib_request.url2pathname(url) self.assertEqual(expect, result, 'urllib_request..url2pathname() failed; %s != %s' % (expect, result)) given = '///C|/path' expect = 'C:\\path' result = urllib_request.url2pathname(given) self.assertEqual(expect, result, 'urllib_request.url2pathname() failed; %s != %s' % (expect, result))
def test_converting_drive_letter(self): self.assertEqual(url2pathname("///C|"), 'C:') self.assertEqual(url2pathname("///C:"), 'C:') self.assertEqual(url2pathname("///C|/"), 'C:\\')
def test_converting_when_no_drive_letter(self): # cannot end a raw string in \ self.assertEqual(url2pathname("///C/test/"), r'\\\C\test' '\\') self.assertEqual(url2pathname("////C/test/"), r'\\C\test' '\\')
def test_non_ascii_drive_letter(self): self.assertRaises(IOError, url2pathname, "///\u00e8|/")
def test_roundtrip_url2pathname(self): list_of_paths = ['C:', r'\\\C\test\\', r'C:\foo\bar\spam.foo' ] for path in list_of_paths: self.assertEqual(url2pathname(pathname2url(path)), path)
def test_roundtrip_pathname2url(self): list_of_paths = ['///C:', '/////folder/test/', '///C:/foo/bar/spam.foo'] for path in list_of_paths: self.assertEqual(pathname2url(url2pathname(path)), path)
def normalize_admin_url(self, path): """Normalize URL path. :arg path: path relative to current path :returns: absolute Admin URL """ # Turn URL path into OS path for manipulation mypath = url2pathname(path) if not os.path.isabs(mypath): mypath = '/' + mypath url = self.admin_url + mypath return url
def uri_to_filename(uri: str) -> str: return url2pathname(urlparse(uri).path)
def get_path(self): encoded_path = self._uri.path if get_platform() == OSPlatform.WINDOWS: # prepend "netloc" (the drive letter - e.g. "c:") encoded_path = self._uri.netloc + encoded_path # decode all special characters like "%20" and replace "/" with "\\" (Windows) return url2pathname(encoded_path)
def nativejoin(base, path): """ Joins two paths - returning a native file path. Given a base path and a relative location, (in posix format) return a file path in a (relatively) OS native way. """ return url2pathname(pathjoin(base, path))
def _get_file_path_from_dnd_dropped_uri(self, uri): """ helper to get a useful path from a drop uri""" path = url2pathname(uri) # escape special chars path = path.strip('\r\n\x00') # remove \r\n and NULL # get the path to file if path.startswith('file:\\\\\\'): # windows path = path[8:] # 8 is len('file:///') elif path.startswith('file://'): # nautilus, rox path = path[7:] # 7 is len('file://') elif path.startswith('file:'): # xffm path = path[5:] # 5 is len('file:') return path