Client.send_file now properly closes the file-object.
This commit is contained in:
		@@ -408,6 +408,24 @@ class Client(object):
 | 
				
			|||||||
                return m.group(1)
 | 
					                return m.group(1)
 | 
				
			||||||
        return None
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _resolve_destination(self, destination):
 | 
				
			||||||
 | 
					        if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
 | 
				
			||||||
 | 
					            return (destination.id, destination.is_private)
 | 
				
			||||||
 | 
					        elif isinstance(destination, User):
 | 
				
			||||||
 | 
					            found = utils.find(lambda pm: pm.user == destination, self.private_channels)
 | 
				
			||||||
 | 
					            if found is None:
 | 
				
			||||||
 | 
					                # Couldn't find the user, so start a PM with them first.
 | 
				
			||||||
 | 
					                self.start_private_message(destination)
 | 
				
			||||||
 | 
					                channel_id = self.private_channels[-1].id
 | 
				
			||||||
 | 
					                return (channel_id, True)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                return (found.id, True)
 | 
				
			||||||
 | 
					        elif isinstance(destination, str):
 | 
				
			||||||
 | 
					            channel_id = destination
 | 
				
			||||||
 | 
					            return (destination, True)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            raise InvalidDestination('Destination must be Channel, PrivateChannel, User, or str')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def on_error(self, event_method, *args, **kwargs):
 | 
					    def on_error(self, event_method, *args, **kwargs):
 | 
				
			||||||
        msg = 'Caught exception in {} with args (*{}, **{})'
 | 
					        msg = 'Caught exception in {} with args (*{}, **{})'
 | 
				
			||||||
        log.exception(msg.format(event_method, args, kwargs))
 | 
					        log.exception(msg.format(event_method, args, kwargs))
 | 
				
			||||||
@@ -514,23 +532,7 @@ class Client(object):
 | 
				
			|||||||
        :return: The :class:`Message` sent or None if error occurred.
 | 
					        :return: The :class:`Message` sent or None if error occurred.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        channel_id = ''
 | 
					        channel_id, is_private_message = self._resolve_destination(destination)
 | 
				
			||||||
        is_private_message = True
 | 
					 | 
				
			||||||
        if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
 | 
					 | 
				
			||||||
            channel_id = destination.id
 | 
					 | 
				
			||||||
            is_private_message = destination.is_private
 | 
					 | 
				
			||||||
        elif isinstance(destination, User):
 | 
					 | 
				
			||||||
            found = utils.find(lambda pm: pm.user == destination, self.private_channels)
 | 
					 | 
				
			||||||
            if found is None:
 | 
					 | 
				
			||||||
                # Couldn't find the user, so start a PM with them first.
 | 
					 | 
				
			||||||
                self.start_private_message(destination)
 | 
					 | 
				
			||||||
                channel_id = self.private_channels[-1].id
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                channel_id = found.id
 | 
					 | 
				
			||||||
        elif isinstance(destination, str):
 | 
					 | 
				
			||||||
            channel_id = destination
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            raise InvalidDestination('Destination must be Channel, PrivateChannel, User, or str')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        content = str(content)
 | 
					        content = str(content)
 | 
				
			||||||
        mentions = self._resolve_mentions(content, mentions)
 | 
					        mentions = self._resolve_mentions(content, mentions)
 | 
				
			||||||
@@ -559,34 +561,26 @@ class Client(object):
 | 
				
			|||||||
    def send_file(self, destination, filename):
 | 
					    def send_file(self, destination, filename):
 | 
				
			||||||
        """Sends a message to the destination given with the file given.
 | 
					        """Sends a message to the destination given with the file given.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        The destination could be a :class:`Channel` or a :class:`PrivateChannel`. For convenience
 | 
					        The destination parameter follows the same rules as :meth:`send_message`.
 | 
				
			||||||
        it could also be a :class:`User`. If it's a :class:`User` or :class:`PrivateChannel` then it
 | 
					
 | 
				
			||||||
        sends the message via private message, otherwise it sends the message to the channel.
 | 
					        Note that this requires proper permissions in order to work.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        :param destination: The location to send the message.
 | 
					        :param destination: The location to send the message.
 | 
				
			||||||
        :param filename: The relative file path to the file to be sent. 
 | 
					        :param filename: The file to send.
 | 
				
			||||||
 | 
					        :return: The :class:`Message` sent or None if an error occurred.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        channel_id = ''
 | 
					        channel_id, is_private_message = self._resolve_destination(destination)
 | 
				
			||||||
        is_private_message = True
 | 
					
 | 
				
			||||||
        if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
 | 
					        url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
 | 
				
			||||||
            channel_id = destination.id
 | 
					        response = None
 | 
				
			||||||
            is_private_message = destination.is_private
 | 
					
 | 
				
			||||||
        elif isinstance(destination, User):
 | 
					        with open(filename, 'rb') as f:
 | 
				
			||||||
            found = utils.find(lambda pm: pm.user == destination, self.private_channels)
 | 
					            files = {
 | 
				
			||||||
            if found is None:
 | 
					                'file': (filename, f)
 | 
				
			||||||
                # Couldn't find the user, so start a PM with them first.
 | 
					            }
 | 
				
			||||||
                self.start_private_message(destination)
 | 
					            response = requests.post(url, files=files, headers=self.headers)
 | 
				
			||||||
                channel_id = self.private_channels[-1].id
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                channel_id = found.id
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            raise InvalidDestination('Destination must be Channel, PrivateChannel, or User')
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        response = requests.post(url, files={'file' : (filename, open(filename, 'rb'))}, headers=self.headers)
 | 
					 | 
				
			||||||
            
 | 
					 | 
				
			||||||
        if is_response_successful(response):
 | 
					        if is_response_successful(response):
 | 
				
			||||||
            data = response.json()
 | 
					            data = response.json()
 | 
				
			||||||
            log.debug(request_success_log.format(name='send_file', response=response, json=response.text, data=filename))
 | 
					            log.debug(request_success_log.format(name='send_file', response=response, json=response.text, data=filename))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user