lib/imageio/core/request.py
def _parse_uri(self, uri):
        """ Try to figure our what we were given
        """
        py3k = sys.version_info[0] == 3
        is_read_request = self.mode[0] == 'r'
        is_write_request = self.mode[0] == 'w'
        
        if isinstance(uri, string_types):
            # Explicit
            if uri.startswith('http://') or uri.startswith('https://'):
                self._uri_type = URI_HTTP
                self._filename = uri
            elif uri.startswith('ftp://') or uri.startswith('ftps://'):
                self._uri_type = URI_FTP
                self._filename = uri
            elif uri.startswith('file://'):
                self._uri_type = URI_FILENAME
                self._filename = uri[7:]
            elif uri.startswith('<video') and is_read_request:
                self._uri_type = URI_BYTES
                self._filename = uri
            elif uri == RETURN_BYTES and is_write_request:
                self._uri_type = URI_BYTES
                self._filename = '<bytes>'
            # Less explicit (particularly on py 2.x)
            elif py3k:
                self._uri_type = URI_FILENAME
                self._filename = uri
            else:  # pragma: no cover - our ref for coverage is py3k
                try:
                    isfile = os.path.isfile(uri)
                except Exception:
                    isfile = False  # If checking does not even work ...
                if isfile:
                    self._uri_type = URI_FILENAME
                    self._filename = uri
                elif len(uri) < 256:  # Can go wrong with veeery tiny images
                    self._uri_type = URI_FILENAME
                    self._filename = uri
                elif isinstance(uri, binary_type) and is_read_request:
                    self._uri_type = URI_BYTES
                    self._filename = '<bytes>'
                    self._bytes = uri
                else:
                    self._uri_type = URI_FILENAME
                    self._filename = uri
        elif py3k and isinstance(uri, binary_type) and is_read_request:
            self._uri_type = URI_BYTES
            self._filename = '<bytes>'
            self._bytes = uri
        # Files
        elif is_read_request:
            if hasattr(uri, 'read') and hasattr(uri, 'close'):
                self._uri_type = URI_FILE
                self._filename = '<file>'
                self._file = uri
        elif is_write_request:
            if hasattr(uri, 'write') and hasattr(uri, 'close'):
                self._uri_type = URI_FILE
                self._filename = '<file>'
                self._file = uri
        
        # Expand user dir
        if self._uri_type == URI_FILENAME and self._filename.startswith('~'):
            self._filename = os.path.expanduser(self._filename)
        
        # Check if a zipfile
        if self._uri_type == URI_FILENAME:
            # Search for zip extension followed by a path separater
            for needle in ['.zip/', '.zip\\']:
                zip_i = self._filename.lower().find(needle)
                if zip_i > 0:                    
                    zip_i += 4
                    self._uri_type = URI_ZIPPED
                    self._filename_zip = (self._filename[:zip_i], 
                                          self._filename[zip_i:].lstrip('/\\'))
                    break
        
        # Check if we could read it
        if self._uri_type is None:
            uri_r = repr(uri)
            if len(uri_r) > 60:
                uri_r = uri_r[:57] + '...'
            raise IOError("Cannot understand given URI: %s." % uri_r)
        
        # Check if this is supported
        noWriting = [URI_HTTP, URI_FTP]
        if is_write_request and self._uri_type in noWriting:
            raise IOError('imageio does not support writing to http/ftp.')
        
        # Check if an example image
        if is_read_request and self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            fn = self._filename
            if self._filename_zip:
                fn = self._filename_zip[0]
            if (not os.path.exists(fn)) and (fn in EXAMPLE_IMAGES):
                fn = get_remote_file('images/' + fn)
                self._filename = fn
                if self._filename_zip:
                    self._filename_zip = fn, self._filename_zip[1]
                    self._filename = fn + '/' + self._filename_zip[1]
        
        # Make filename absolute 
        if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            if self._filename_zip:
                self._filename_zip = (os.path.abspath(self._filename_zip[0]),
                                      self._filename_zip[1])
            else:
                self._filename = os.path.abspath(self._filename)
        
        # Check wether file name is valid
        if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            fn = self._filename
            if self._filename_zip:
                fn = self._filename_zip[0]
            if is_read_request:
                # Reading: check that the file exists (but is allowed a dir)
                if not os.path.exists(fn):
                    raise IOError("No such file: '%s'" % fn)
            else:
                # Writing: check that the directory to write to does exist
                dn = os.path.dirname(fn)
                if not os.path.exists(dn):
                    raise IOError("The directory %r does not exist" % dn)
 Try to figure our what we were given
        
Try to figure our what we were given
Try to figure our what we were given
def _parse_uri(self, uri):
        py3k = sys.version_info[0] == 3
        is_read_request = self.mode[0] == 'r'
        is_write_request = self.mode[0] == 'w'
        if isinstance(uri, string_types):
            if uri.startswith('http://') or uri.startswith('https://'):
                self._uri_type = URI_HTTP
                self._filename = uri
            elif uri.startswith('ftp://') or uri.startswith('ftps://'):
                self._uri_type = URI_FTP
                self._filename = uri
            elif uri.startswith('file://'):
                self._uri_type = URI_FILENAME
                self._filename = uri[7:]
            elif uri.startswith('<video') and is_read_request:
                self._uri_type = URI_BYTES
                self._filename = uri
            elif uri == RETURN_BYTES and is_write_request:
                self._uri_type = URI_BYTES
                self._filename = '<bytes>'
            elif py3k:
                self._uri_type = URI_FILENAME
                self._filename = uri
            else:  
                try:
                    isfile = os.path.isfile(uri)
                except Exception:
                    isfile = False  
                if isfile:
                    self._uri_type = URI_FILENAME
                    self._filename = uri
                elif len(uri) < 256:  
                    self._uri_type = URI_FILENAME
                    self._filename = uri
                elif isinstance(uri, binary_type) and is_read_request:
                    self._uri_type = URI_BYTES
                    self._filename = '<bytes>'
                    self._bytes = uri
                else:
                    self._uri_type = URI_FILENAME
                    self._filename = uri
        elif py3k and isinstance(uri, binary_type) and is_read_request:
            self._uri_type = URI_BYTES
            self._filename = '<bytes>'
            self._bytes = uri
        elif is_read_request:
            if hasattr(uri, 'read') and hasattr(uri, 'close'):
                self._uri_type = URI_FILE
                self._filename = '<file>'
                self._file = uri
        elif is_write_request:
            if hasattr(uri, 'write') and hasattr(uri, 'close'):
                self._uri_type = URI_FILE
                self._filename = '<file>'
                self._file = uri
        if self._uri_type == URI_FILENAME and self._filename.startswith('~'):
            self._filename = os.path.expanduser(self._filename)
        if self._uri_type == URI_FILENAME:
            for needle in ['.zip/', '.zip\\']:
                zip_i = self._filename.lower().find(needle)
                if zip_i > 0:                    
                    zip_i += 4
                    self._uri_type = URI_ZIPPED
                    self._filename_zip = (self._filename[:zip_i], 
                                          self._filename[zip_i:].lstrip('/\\'))
                    break
        if self._uri_type is None:
            uri_r = repr(uri)
            if len(uri_r) > 60:
                uri_r = uri_r[:57] + '...'
            raise IOError("Cannot understand given URI: %s." % uri_r)
        noWriting = [URI_HTTP, URI_FTP]
        if is_write_request and self._uri_type in noWriting:
            raise IOError('imageio does not support writing to http/ftp.')
        if is_read_request and self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            fn = self._filename
            if self._filename_zip:
                fn = self._filename_zip[0]
            if (not os.path.exists(fn)) and (fn in EXAMPLE_IMAGES):
                fn = get_remote_file('images/' + fn)
                self._filename = fn
                if self._filename_zip:
                    self._filename_zip = fn, self._filename_zip[1]
                    self._filename = fn + '/' + self._filename_zip[1]
        if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            if self._filename_zip:
                self._filename_zip = (os.path.abspath(self._filename_zip[0]),
                                      self._filename_zip[1])
            else:
                self._filename = os.path.abspath(self._filename)
        if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
            fn = self._filename
            if self._filename_zip:
                fn = self._filename_zip[0]
            if is_read_request:
                if not os.path.exists(fn):
                    raise IOError("No such file: '%s'" % fn)
            else:
                dn = os.path.dirname(fn)
                if not os.path.exists(dn):
                    raise IOError("The directory %r does not exist" % dn)
def _parse_uri ( self , uri ) : py3k = sys . version_info [ 0 ] == 3 is_read_request = self . mode [ 0 ] == 'r' is_write_request = self . mode [ 0 ] == 'w' if isinstance ( uri , string_types ) : if uri . startswith ( 'http://' ) or uri . startswith ( 'https://' ) : self . _uri_type = URI_HTTP self . _filename = uri elif uri . startswith ( 'ftp://' ) or uri . startswith ( 'ftps://' ) : self . _uri_type = URI_FTP self . _filename = uri elif uri . startswith ( 'file://' ) : self . _uri_type = URI_FILENAME self . _filename = uri [ 7 : ] elif uri . startswith ( '<video' ) and is_read_request : self . _uri_type = URI_BYTES self . _filename = uri elif uri == RETURN_BYTES and is_write_request : self . _uri_type = URI_BYTES self . _filename = '<bytes>' elif py3k : self . _uri_type = URI_FILENAME self . _filename = uri else : try : isfile = os . path . isfile ( uri ) except Exception : isfile = False if isfile : self . _uri_type = URI_FILENAME self . _filename = uri elif len ( uri ) < 256 : self . _uri_type = URI_FILENAME self . _filename = uri elif isinstance ( uri , binary_type ) and is_read_request : self . _uri_type = URI_BYTES self . _filename = '<bytes>' self . _bytes = uri else : self . _uri_type = URI_FILENAME self . _filename = uri elif py3k and isinstance ( uri , binary_type ) and is_read_request : self . _uri_type = URI_BYTES self . _filename = '<bytes>' self . _bytes = uri elif is_read_request : if hasattr ( uri , 'read' ) and hasattr ( uri , 'close' ) : self . _uri_type = URI_FILE self . _filename = '<file>' self . _file = uri elif is_write_request : if hasattr ( uri , 'write' ) and hasattr ( uri , 'close' ) : self . _uri_type = URI_FILE self . _filename = '<file>' self . _file = uri if self . _uri_type == URI_FILENAME and self . _filename . startswith ( '~' ) : self . _filename = os . path . expanduser ( self . _filename ) if self . _uri_type == URI_FILENAME : for needle in [ '.zip/' , '.zip\\' ] : zip_i = self . _filename . lower ( ) . find ( needle ) if zip_i > 0 : zip_i += 4 self . _uri_type = URI_ZIPPED self . _filename_zip = ( self . _filename [ : zip_i ] , self . _filename [ zip_i : ] . lstrip ( '/\\' ) ) break if self . _uri_type is None : uri_r = repr ( uri ) if len ( uri_r ) > 60 : uri_r = uri_r [ : 57 ] + '...' raise IOError ( "Cannot understand given URI: %s." % uri_r ) noWriting = [ URI_HTTP , URI_FTP ] if is_write_request and self . _uri_type in noWriting : raise IOError ( 'imageio does not support writing to http/ftp.' ) if is_read_request and self . _uri_type in [ URI_FILENAME , URI_ZIPPED ] : fn = self . _filename if self . _filename_zip : fn = self . _filename_zip [ 0 ] if ( not os . path . exists ( fn ) ) and ( fn in EXAMPLE_IMAGES ) : fn = get_remote_file ( 'images/' + fn ) self . _filename = fn if self . _filename_zip : self . _filename_zip = fn , self . _filename_zip [ 1 ] self . _filename = fn + '/' + self . _filename_zip [ 1 ] if self . _uri_type in [ URI_FILENAME , URI_ZIPPED ] : if self . _filename_zip : self . _filename_zip = ( os . path . abspath ( self . _filename_zip [ 0 ] ) , self . _filename_zip [ 1 ] ) else : self . _filename = os . path . abspath ( self . _filename ) if self . _uri_type in [ URI_FILENAME , URI_ZIPPED ] : fn = self . _filename if self . _filename_zip : fn = self . _filename_zip [ 0 ] if is_read_request : if not os . path . exists ( fn ) : raise IOError ( "No such file: '%s'" % fn ) else : dn = os . path . dirname ( fn ) if not os . path . exists ( dn ) : raise IOError ( "The directory %r does not exist" % dn )
Try to figure our what we were given
Try to figure our what we were given
""" Try to figure our what we were given
        """ # Explicit # Less explicit (particularly on py 2.x) # pragma: no cover - our ref for coverage is py3k # If checking does not even work ... # Can go wrong with veeery tiny images # Files # Expand user dir # Check if a zipfile # Search for zip extension followed by a path separater # Check if we could read it # Check if this is supported # Check if an example image # Make filename absolute  # Check wether file name is valid # Reading: check that the file exists (but is allowed a dir) # Writing: check that the directory to write to does exist
self
uri