its-rigs / Trolly

A Python wrapper around the Trello API. Provides a group of Python classes to represent Trello Objects. All classes come with basic Trello API method calls and are easily extensible to suit your needs. See the README for more details.
MIT License
100 stars 29 forks source link

How to attach a .png? #1

Closed borax00 closed 11 years ago

borax00 commented 11 years ago

Hi,

I'm trying to attach files to cards.

If I use:

myCard.addAttachment( myFilename, open(myFilename).read() )

then this works for attaching text files. But if the file is a png, I get:

Traceback (most recent call last): File "test/mytest.py", line 52, in myCard.addAttachment( myFilename, open(myFilename).read() ) File "/home/borax00/git/trolly/trello/card.py", line 123, in addAttachment headers = { 'Content-Type': content_type }, File "/home/borax00/git/trolly/trello/trelloobject.py", line 30, in fetchJson headers = headers, File "/home/borax00/git/trolly/trello/client.py", line 98, in fetchJson headers = headers File "/usr/lib/python2.7/dist-packages/httplib2/init.py", line 1543, in request (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "/usr/lib/python2.7/dist-packages/httplib2/init.py", line 1293, in _request (response, content) = self._conn_request(conn, request_uri, method, body, headers) File "/usr/lib/python2.7/dist-packages/httplib2/init.py", line 1230, in _conn_request conn.request(method, request_uri, body, headers) File "/usr/lib/python2.7/httplib.py", line 958, in request self._send_request(method, url, body, headers) File "/usr/lib/python2.7/httplib.py", line 992, in _send_request self.endheaders(body) File "/usr/lib/python2.7/httplib.py", line 954, in endheaders self._send_output(message_body) File "/usr/lib/python2.7/httplib.py", line 812, in _send_output msg += message_body UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 390: ordinal not in range(128)

How would I go about attaching a png?

Thanks very much,

Alex

its-rigs commented 11 years ago

Hi Alex,

I tried recreating your error but I couldn't. When I uploaded a png it worked fine. I have uploaded a fix that should hopeful solve the problem. Please let me know if this solves the problem.

Reference: http://stackoverflow.com/questions/11310220/why-am-i-getting-the-error-not-a-jpeg-file-starts-with-0x89-0x50

Luke

borax00 commented 11 years ago

Hi Luke,

Thanks for looking at it. It's definitely not a png-vs-jpeg problem. I hoped to upload my test image but not sure how to do it on this board. So I used the cat image to the left of this post (filename is "gravatar-user-420.png"). Similar result:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 390: ordinal not in range(128)

Here's my python version, maybe yours is different?

$ python --version Python 2.7.3rc2

I did a "git pull" before re-trying (by the way I had to make one change after pulling to get it to work:

$ git diff diff --git a/trolly/client.py b/trolly/client.py index 498ccd7..cef2029 100644 --- a/trolly/client.py +++ b/trolly/client.py @@ -15,7 +15,7 @@ from card import Card from checklist import Checklist from member import Member

-from trello import Unauthorised, ResourceUnavailable +from trolly import Unauthorised, ResourceUnavailable

class Client( object ):

its-rigs commented 11 years ago

Hi Alex,

The link was more of a reference in terms of the fix rather than being about jpeg vs png :) Thanks for the import bug, should be fixed now.

My python version is the same as yours and I have re-run the test with the new avatar png and the it uploaded without a hitch.

Are you running the code from another application or is it just being run from a terminal?

borax00 commented 11 years ago

I was running from another application, but I tried running from a terminal too and got the same result. What else could be different ??

its-rigs commented 11 years ago

Ok lets see: Windows or Linux? What was the application? What is the file name? Does it have any special characters?

Anything else you can think of...

borax00 commented 11 years ago

OK, I managed to get it to work after playing around some more. In the following snippet...

listId = "50ab5da1a93c17460b004a5c" list = trolly.list.List( client, listId )

if False: newCard = list.addCard( { "name" : "New Card (entered autonomously)" } ) print "newCard.id: "+str(newCard.id) else: cardId = "50acc371a77d0df20e00708d"; newCard = trolly.card.Card( client, cardId )

filename = "test/flags.png"; fileValues = open(filename,'rb').read() newCard.addAttachment( filename, fileValues )

with "if False" (ie creating a new card) it fails with the unicode error. but if I change the "if False" to "if True" (ie attaching something to an existing card) it works fine. No idea why...

[to answer your questions:

borax00 commented 11 years ago

But this creation method snippet gets the unicode error (on the addAttachment call):

tempCard = list.addCard( { "name" : "New Card (entered autonomously)" } ) newCard = trolly.card.Card( client, tempCard.id )

its-rigs commented 11 years ago

Ok think i've got it this time!
I wasn't encoding the id from the JSON returned from Trello so in the form data this was causing an issue.

Hopefully that fixes the problem... Let me know

borax00 commented 11 years ago

That works! Thanks very much for all the help, it's hugely appreciated.

its-rigs commented 11 years ago

No Problem glad it worked! :)

borax00 commented 11 years ago

Hey by the way: the first place I looked for API wrappers was here: https://trello.com/docs/ Maybe you want to write to Fog Creek and get them to add Trolly to the wrapper list? It seems to be more feature-complete than the other python wrappers on that list.

its-rigs commented 11 years ago

Cheers! I'll take a look :)