mwclient/client.py
def upload(self, file=None, filename=None, description='', ignore=False, file_size=None,
               url=None, filekey=None, comment=None):
        """
        Uploads a file to the site. Returns JSON result from the API.
        Can raise `errors.InsufficientPermission` and `requests.exceptions.HTTPError`.

        : Parameters :
          - file         : File object or stream to upload.
          - filename     : Destination filename, don't include namespace
                           prefix like 'File:'
          - description  : Wikitext for the file description page.
          - ignore       : True to upload despite any warnings.
          - file_size    : Deprecated in mwclient 0.7
          - url          : URL to fetch the file from.
          - filekey      : Key that identifies a previous upload that was
                           stashed temporarily.
          - comment      : Upload comment. Also used as the initial page text
                           for new files if `description` is not specified.

        Note that one of `file`, `filekey` and `url` must be specified, but not more
        than one. For normal uploads, you specify `file`.

        Example:

        >>> client.upload(open('somefile', 'rb'), filename='somefile.jpg',
                          description='Some description')
        """

        if file_size is not None:
            # Note that DeprecationWarning is hidden by default since Python 2.7
            warnings.warn(
                'file_size is deprecated since mwclient 0.7',
                DeprecationWarning
            )
            file_size = None

        if filename is None:
            raise TypeError('filename must be specified')

        if len([x for x in [file, filekey, url] if x is not None]) != 1:
            raise TypeError("exactly one of 'file', 'filekey' and 'url' must be specified")

        image = self.Images[filename]
        if not image.can('upload'):
            raise errors.InsufficientPermission(filename)

        predata = {}

        if comment is None:
            predata['comment'] = description
        else:
            predata['comment'] = comment
            predata['text'] = description

        if ignore:
            predata['ignorewarnings'] = 'true'
        predata['token'] = image.get_token('edit')
        predata['action'] = 'upload'
        predata['format'] = 'json'
        predata['filename'] = filename
        if url:
            predata['url'] = url

        # Renamed from sessionkey to filekey
        # https://git.wikimedia.org/commit/mediawiki%2Fcore.git/5f13517e
        if self.version[:2] < (1, 18):
            predata['sessionkey'] = filekey
        else:
            predata['filekey'] = filekey

        postdata = predata
        files = None
        if file is not None:

            # Workaround for https://github.com/mwclient/mwclient/issues/65
            # ----------------------------------------------------------------
            # Since the filename in Content-Disposition is not interpreted,
            # we can send some ascii-only dummy name rather than the real
            # filename, which might contain non-ascii.
            file = ('fake-filename', file)
            # End of workaround
            # ----------------------------------------------------------------

            files = {'file': file}

        wait_token = self.wait_token()
        while True:
            try:
                data = self.raw_call('api', postdata, files)
                info = json.loads(data)
                if not info:
                    info = {}
                if self.handle_api_result(info, kwargs=predata):
                    return info.get('upload', {})
            except requests.exceptions.HTTPError as e:
                if e.args[0] == 503 and e.args[1].getheader('X-Database-Lag'):
                    self.wait(wait_token, int(e.args[1].getheader('Retry-After')))
                elif e.args[0] < 500 or e.args[0] > 599:
                    raise
                else:
                    self.wait(wait_token)
            except requests.exceptions.ConnectionError:
                self.wait(wait_token)

        Uploads a file to the site. Returns JSON result from the API.
        Can raise `errors.InsufficientPermission` and `requests.exceptions.HTTPError`.

        : Parameters :
          - file         : File object or stream to upload.
          - filename     : Destination filename, don't include namespace
                           prefix like 'File:'
          - description  : Wikitext for the file description page.
          - ignore       : True to upload despite any warnings.
          - file_size    : Deprecated in mwclient 0.7
          - url          : URL to fetch the file from.
          - filekey      : Key that identifies a previous upload that was
                           stashed temporarily.
          - comment      : Upload comment. Also used as the initial page text
                           for new files if `description` is not specified.

        Note that one of `file`, `filekey` and `url` must be specified, but not more
        than one. For normal uploads, you specify `file`.

        Example:

        >>> client.upload(open('somefile', 'rb'), filename='somefile.jpg',
                          description='Some description')
        
Uploads a file to the site. Returns JSON result from the API.
Uploads a file to the site . Returns JSON result from the API .
def upload(self, file=None, filename=None, description='', ignore=False, file_size=None,
               url=None, filekey=None, comment=None):
        if file_size is not None:
            warnings.warn(
                'file_size is deprecated since mwclient 0.7',
                DeprecationWarning
            )
            file_size = None
        if filename is None:
            raise TypeError('filename must be specified')
        if len([x for x in [file, filekey, url] if x is not None]) != 1:
            raise TypeError("exactly one of 'file', 'filekey' and 'url' must be specified")
        image = self.Images[filename]
        if not image.can('upload'):
            raise errors.InsufficientPermission(filename)
        predata = {}
        if comment is None:
            predata['comment'] = description
        else:
            predata['comment'] = comment
            predata['text'] = description
        if ignore:
            predata['ignorewarnings'] = 'true'
        predata['token'] = image.get_token('edit')
        predata['action'] = 'upload'
        predata['format'] = 'json'
        predata['filename'] = filename
        if url:
            predata['url'] = url
        if self.version[:2] < (1, 18):
            predata['sessionkey'] = filekey
        else:
            predata['filekey'] = filekey
        postdata = predata
        files = None
        if file is not None:
            file = ('fake-filename', file)
            files = {'file': file}
        wait_token = self.wait_token()
        while True:
            try:
                data = self.raw_call('api', postdata, files)
                info = json.loads(data)
                if not info:
                    info = {}
                if self.handle_api_result(info, kwargs=predata):
                    return info.get('upload', {})
            except requests.exceptions.HTTPError as e:
                if e.args[0] == 503 and e.args[1].getheader('X-Database-Lag'):
                    self.wait(wait_token, int(e.args[1].getheader('Retry-After')))
                elif e.args[0] < 500 or e.args[0] > 599:
                    raise
                else:
                    self.wait(wait_token)
            except requests.exceptions.ConnectionError:
                self.wait(wait_token)
def upload ( self , file = None , filename = None , description = '' , ignore = False , file_size = None , url = None , filekey = None , comment = None ) : if file_size is not None : warnings . warn ( 'file_size is deprecated since mwclient 0.7' , DeprecationWarning ) file_size = None if filename is None : raise TypeError ( 'filename must be specified' ) if len ( [ x for x in [ file , filekey , url ] if x is not None ] ) != 1 : raise TypeError ( "exactly one of 'file', 'filekey' and 'url' must be specified" ) image = self . Images [ filename ] if not image . can ( 'upload' ) : raise errors . InsufficientPermission ( filename ) predata = { } if comment is None : predata [ 'comment' ] = description else : predata [ 'comment' ] = comment predata [ 'text' ] = description if ignore : predata [ 'ignorewarnings' ] = 'true' predata [ 'token' ] = image . get_token ( 'edit' ) predata [ 'action' ] = 'upload' predata [ 'format' ] = 'json' predata [ 'filename' ] = filename if url : predata [ 'url' ] = url if self . version [ : 2 ] < ( 1 , 18 ) : predata [ 'sessionkey' ] = filekey else : predata [ 'filekey' ] = filekey postdata = predata files = None if file is not None : file = ( 'fake-filename' , file ) files = { 'file' : file } wait_token = self . wait_token ( ) while True : try : data = self . raw_call ( 'api' , postdata , files ) info = json . loads ( data ) if not info : info = { } if self . handle_api_result ( info , kwargs = predata ) : return info . get ( 'upload' , { } ) except requests . exceptions . HTTPError as e : if e . args [ 0 ] == 503 and e . args [ 1 ] . getheader ( 'X-Database-Lag' ) : self . wait ( wait_token , int ( e . args [ 1 ] . getheader ( 'Retry-After' ) ) ) elif e . args [ 0 ] < 500 or e . args [ 0 ] > 599 : raise else : self . wait ( wait_token ) except requests . exceptions . ConnectionError : self . wait ( wait_token )
Uploads a file to the site.
Uploads a file to the site .
"""
        Uploads a file to the site. Returns JSON result from the API.
        Can raise `errors.InsufficientPermission` and `requests.exceptions.HTTPError`.

        : Parameters :
          - file         : File object or stream to upload.
          - filename     : Destination filename, don't include namespace
                           prefix like 'File:'
          - description  : Wikitext for the file description page.
          - ignore       : True to upload despite any warnings.
          - file_size    : Deprecated in mwclient 0.7
          - url          : URL to fetch the file from.
          - filekey      : Key that identifies a previous upload that was
                           stashed temporarily.
          - comment      : Upload comment. Also used as the initial page text
                           for new files if `description` is not specified.

        Note that one of `file`, `filekey` and `url` must be specified, but not more
        than one. For normal uploads, you specify `file`.

        Example:

        >>> client.upload(open('somefile', 'rb'), filename='somefile.jpg',
                          description='Some description')
        """ # Note that DeprecationWarning is hidden by default since Python 2.7 # Renamed from sessionkey to filekey # https://git.wikimedia.org/commit/mediawiki%2Fcore.git/5f13517e # Workaround for https://github.com/mwclient/mwclient/issues/65 # ---------------------------------------------------------------- # Since the filename in Content-Disposition is not interpreted, # we can send some ascii-only dummy name rather than the real # filename, which might contain non-ascii. # End of workaround # ----------------------------------------------------------------
self
file
filename
description
ignore
file_size
url
filekey
comment
Parameters
file         : File object or stream to upload.
filename     : Destination filename, don't include namespace
prefix like 'File:'
description  : Wikitext for the file description page.
ignore       : True to upload despite any warnings.
file_size    : Deprecated in mwclient 0.7
url          : URL to fetch the file from.
filekey      : Key that identifies a previous upload that was
stashed temporarily.
comment      : Upload comment. Also used as the initial page text
for new files if `description` is not specified.

Note that one of `file`, `filekey` and `url` must be specified, but not more
than one. For normal uploads, you specify `file`.



>>> client.upload(open('somefile', 'rb'), filename='somefile.jpg',
description='Some description')
file : File object or stream to upload . filename : Destination filename don ' t include namespace prefix like ' File : ' description : Wikitext for the file description page . ignore : True to upload despite any warnings . file_size : Deprecated in mwclient 0 . 7 url : URL to fetch the file from . filekey : Key that identifies a previous upload that was stashed temporarily . comment : Upload comment . Also used as the initial page text for new files if ` description ` is not specified . Note that one of ` file ` ` filekey ` and ` url ` must be specified but not more than one . For normal uploads you specify ` file ` . >>> client . upload ( open ( ' somefile ' ' rb ' ) filename = ' somefile . jpg ' description = ' Some description ' )