我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用urllib.url2pathname()。
def __init__(self, iso_url, **kwargs): Version.__init__(self, **kwargs) if re.match(r'/', iso_url): self.m_iso_url = "file://" + iso_url self.m_iso_path = iso_url else: self.m_iso_url = iso_url self.m_iso_path = None # We can't determine the final ISO file name yet because the work # directory is not known at this point, but we can precalculate the # basename of it. self.m_iso_basename = os.path.basename( urllib.url2pathname(urlparse.urlparse(iso_url)[2])) m = re.match(r"(.*)cd.*iso|NetBSD-[0-9\._A-Z]+-(.*).iso", self.m_iso_basename) if m is None: raise RuntimeError("cannot guess architecture from ISO name '%s'" % self.m_iso_basename) if m.group(1) is not None: self.m_arch = m.group(1) if m.group(2) is not None: self.m_arch = m.group(2) check_arch_supported(self.m_arch, 'iso')
def open_file(self, url): path = urllib.url2pathname(urllib.unquote(url)) if os.path.isdir(path): if path[-1] != os.sep: url = url + '/' indexpath = os.path.join(path, "index.html") if os.path.exists(indexpath): return self.open_file(url + "index.html") try: names = os.listdir(path) except os.error, msg: raise IOError, msg, sys.exc_traceback names.sort() s = MyStringIO("file:"+url, {'content-type': 'text/html'}) s.write('<BASE HREF="file:%s">\n' % urllib.quote(os.path.join(path, ""))) for name in names: q = urllib.quote(name) s.write('<A HREF="%s">%s</A>\n' % (q, q)) s.seek(0) return s return urllib.FancyURLopener.open_file(self, url)
def __init__(self, base, config, layered = False, **kwargs): addrspace.BaseAddressSpace.__init__(self, base, config, **kwargs) self.as_assert(base == None or layered, 'Must be first Address Space') self.as_assert(config.LOCATION.startswith("file://"), 'Location is not of file scheme') path = urllib.url2pathname(config.LOCATION[7:]) self.as_assert(os.path.exists(path), 'Filename must be specified and exist') self.name = os.path.abspath(path) self.fname = self.name self.mode = 'rb' if config.WRITE: self.mode += '+' self.fhandle = open(self.fname, self.mode) self.fhandle.seek(0, 2) self.fsize = self.fhandle.tell() self.offset = 0 # Abstract Classes cannot register options, and since this checks config.WRITE in __init__, we define the option here
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 __init__(self, base, config, layered = False, **kwargs): addrspace.BaseAddressSpace.__init__(self, base, config, **kwargs) self.as_assert(base == None or layered, 'Must be first Address Space') self.as_assert(config.LOCATION.startswith("file://"), 'Location is not of file scheme') path = urllib.url2pathname(config.LOCATION[7:]) self.as_assert(os.path.exists(path), 'Filename must be specified and exist') self.name = os.path.abspath(path) self.fname = self.name self.mode = 'rb' if config.WRITE: self.mode += '+' self.fhandle = open(self.fname, self.mode) self.fhandle.seek(0, 2) self.fsize = self.fhandle.tell() # Abstract Classes cannot register options, and since this checks config.WRITE in __init__, we define the option here
def initialize(self, core, config): self.core = core self.config = config # Store path of currently playing album track = self.core.playback.get_current_track().get() if track: track_path = urllib.url2pathname(urlparse.urlparse(track.uri.split('file://')[1]).path) self.album_path = os.path.dirname(track_path) else: return self.redirect('/auto') # Read all section config to an array. Ready for dynamic amount of sections self.sections = [] section = 0 exists = "s{}_folder".format(section) in self.config['auto'] while exists: self.sections.append({ "start": self.config['auto']["s{}_start".format(section)], "folder": self.config['auto']["s{}_folder".format(section)], "max_volume": self.config['auto']["s{}_max_volume".format(section)] }) section += 1 exists = "s{}_folder".format(section) in self.config['auto']
def post(self): to = self.get_body_argument("to", False) if to: destination_uri = os.path.join(self.config['auto']['base_path'], to) destination_path = urllib.url2pathname(urlparse.urlparse(destination_uri.split('file://')[1]).path) logger.info("Moving '{}' to '{}'".format(self.album_path, destination_path)) self.core.tracklist.clear() try: shutil.move(self.album_path, destination_path) logger.info("Moved album '{}' to '{}'".format(self.album_path, destination_path)) except shutil.Error: logger.error("Could not move album '{}' to '{}'".format(self.album_path, destination_path), exc_info=1) else: logger.error('Destination not supplied, move canceled') return self.redirect('/auto')
def open_local_file(self, req): import email.utils import mimetypes host = req.get_host() filename = req.get_selector() localfile = url2pathname(filename) try: stats = os.stat(localfile) size = stats.st_size modified = email.utils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(filename)[0] headers = mimetools.Message(StringIO( 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) if not host or \ (not port and _safe_gethostbyname(host) in self.get_names()): if host: origurl = 'file://' + host + filename else: origurl = 'file://' + filename return addinfourl(open(localfile, 'rb'), headers, origurl) except OSError, msg: # urllib2 users shouldn't expect OSErrors coming from urlopen() raise URLError(msg) raise URLError('file not on local host')
def url2path(url): return url2pathname(urlparse(url).path)
def url2path(url): return urllib.url2pathname(urlparse(url).path)
def downloadPackageOnly(self, output=None): """Download a single package, if needed. An MD5 signature is used to determine whether download is needed, and to test that we actually downloaded what we expected. If output is given it is a file-like object that will receive a log of what happens. If anything unforeseen happened the method returns an error message string. """ scheme, loc, path, query, frag = urlparse.urlsplit(self._dict['Download-URL']) path = urllib.url2pathname(path) filename = os.path.split(path)[1] self.archiveFilename = os.path.join(self._db.preferences.downloadDir, filename) if not self._archiveOK(): if scheme == 'manual': return "Please download package manually and save as %s" % self.archiveFilename downloader = PimpUrllibDownloader(None, self._db.preferences.downloadDir, watcher=self._db.preferences.watcher) if not downloader.download(self._dict['Download-URL'], self.archiveFilename, output): return "download command failed" if not os.path.exists(self.archiveFilename) and not NO_EXECUTE: return "archive not found after download" if not self._archiveOK(): return "archive does not have correct MD5 checksum"
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.pathname2url(expected_path) self.assertEqual(expected_url, result, "pathname2url() failed; %s != %s" % (result, expected_url)) result = urllib.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.quote("quot=ing") result = urllib.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) expect = given result = urllib.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.quote("make sure") result = urllib.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.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.url2pathname(url) self.assertEqual(expect, result, 'nturl2path.url2pathname() failed; %s != %s' % (expect, result)) given = '///C|/path' expect = 'C:\\path' result = urllib.url2pathname(given) self.assertEqual(expect, result, 'nturl2path.url2pathname() failed; %s != %s' % (expect, result))
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 do_HEAD(self): self.path = urllib.url2pathname(self.path) self.do_WORK(onlyhead=1)
def do_GET(self): self.path = urllib.url2pathname(self.path) pos = self.path.find('?') if pos >= 0: self.query = cgi.parse_qs(self.path[pos+1:]) self.path = self.path[0:pos] else: self.query = {} self.do_WORK()
def do_POST(self): self.path = urllib.url2pathname(self.path) x = self.rfile.read(int(self.headers.getheader('content-length'))) # The following throws away two extra bytes sent by IE, if we would # not do this, POSTs from IE would not work: [see bug #427345] try: while select.select([self.rfile], [], [], 0)[0]: if not self.rfile.read(1): break except: Utils.Error("Select hack raised exception.",prefix="Warning") self.query = cgi.parse_qs(x) self.do_WORK()
def open_local_file(self, req): try: import email.utils as emailutils except ImportError: # python 2.4 import email.Utils as emailutils import mimetypes host = req.get_host() file = req.get_selector() localfile = url2pathname(file) try: stats = os.stat(localfile) size = stats.st_size modified = emailutils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(file)[0] headers = mimetools.Message(StringIO( 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) if not host or \ (not port and socket.gethostbyname(host) in self.get_names()): return addinfourl(open(localfile, 'rb'), headers, 'file:'+file) except OSError, msg: # urllib2 users shouldn't expect OSErrors coming from urlopen() raise URLError(msg) raise URLError('file not on local host')
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 file_path(self, url): """ Returns the path to the media file on disk for the given URL. The passed URL is assumed to begin with ADMIN_MEDIA_PREFIX. If the resultant file path is outside the media directory, then a ValueError is raised. """ # Remove ADMIN_MEDIA_PREFIX. relative_url = url[len(self.media_url):] relative_path = urllib.url2pathname(relative_url) return safe_join(self.media_dir, relative_path)
def reload_file(self, path): def assign_used_file(): self.currentPath = path self.fname = self.used_files[path]['fname'] self.name = self.used_files[path]['fname'] self.mode = self.used_files[path]['mode'] self.fhandle = self.used_files[path]['fhandle'] self.fhandle.seek(0, 2) self.fsize = self.used_files[path]['fsize'] if path in self.used_files: if self.currentPath == path: return assign_used_file() else: debug.debug('read from file: ' + path) path_name = urllib.url2pathname(path) if not os.path.exists(path_name): debug.warning('File not exist: ' + path + ' Returning zero bytes..') currentPath = 'ZERO' return # assert os.path.exists(path_name), 'Filename must be specified and exist' self.used_files[path]['fname'] = os.path.abspath(path_name) self.used_files[path]['mode'] = 'rb' if self._config.WRITE: self.used_files[path]['mode'] += '+' self.used_files[path]['fhandle'] = open(self.used_files[path]['fname'], self.used_files[path]['mode']) self.used_files[path]['fsize'] = self.fhandle.tell() assign_used_file()