Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

updated for unicode support, handle file-like objects#71

Closed
shaneshifflett wants to merge 5 commits into
datadesk:masterfrom
BayCitizen:master
Closed

updated for unicode support, handle file-like objects#71
shaneshifflett wants to merge 5 commits into
datadesk:masterfrom
BayCitizen:master

Conversation

@shaneshifflett

Copy link
Copy Markdown

I'm working on a django app for document cloud documents and your python wrapper for the doccloud api was a great starting point. We host many of our PDF's and documents on Amazon S3 via django-storages. First thing I noticed was using file paths wouldn't work. I worked with palewire to provide the option to pass in file objects or file paths. As I kept working, MultipartPostHandler started to throw some encoding exceptions. The encode function's buffer was a plain ascii string that worked as long as the file was opened using the python's open function (default encoding=ascii). Upon retrieval, our files are stored in memory as a StringIO object (wrapped in an S3BotoStorageFile). StringIO objects are unicode and thus the the encode function blew up as it tried to append the contents of a unicode file to an ascii header. I changed the encode to method to use a StringIO object as the buffer and things worked fine. I don't think it will matter if the file-like objects use some other class to store the contents of a file because the issue had everything to do with the encoding. I also updated some other checks to look for the read attr instead of testing to see if the file object inherited from python's File class. And, I added a function to calculate the size of a file without using fstat.

Let me know if you see any issues!

…stance). Upload also uses an object instead of a file path
…oking for file attributes instead of descending from File and also using StringIO as a buffer instead of plain string (no unicode ascii issues no mo)
@palewire

Copy link
Copy Markdown
Contributor

Great. Giving it a look now. I might not handle it via Github's pull interface, but I'm on it.

@palewire palewire closed this in d7889e3 Jun 15, 2011
@shaneshifflett

Copy link
Copy Markdown
Author

Hey looks like you pulled in several changes. I was wondering, did you
submit those to pip?

On Wed, Jun 15, 2011 at 3:35 PM, palewire <
reply@reply.github.com>wrote:

Great. Giving it a look now. I might now handle it via Github's pull
interface, but I'm on it.

Reply to this email directly or view it on GitHub:

#71 (comment)

Shane Shifflett
312-379-9064

@palewire

Copy link
Copy Markdown
Contributor

Not on the right computer to check everything again for you, but I'm pretty sure the file object and unicode fixes should be out on pip now with version 0.151. I tweaked a couple small things, but it's mostly your work. Tried to get you props in the changelog in the docs. If I screwed something up, let me know.

@shaneshifflett

Copy link
Copy Markdown
Author

Thanks so much I really appreciate your help! I think your changes look
great and we're now using the python-documentcloud on our staging server.
To integrate more closely with doccloud, we're about to put this app in to
production: https://github.com/BayCitizen/django-doccloud

Still having an occasional issue with the pdf file object in the upload
function. Seems that sometimes we're falling in to the file path condition
even when a file-like object is passed into the function. Will look at it
later today or over the weekend and let you know if I find any other issues.

Required parameters

    if hasattr(pdf, 'read'):
        params = {'file': pdf}
    else:

        params = {'file': open(pdf, 'rb')}

On Fri, Jun 17, 2011 at 9:21 AM, palewire <
reply@reply.github.com>wrote:

Not on the right computer to check everything again for you, but I'm pretty
sure the file object and unicode fixes should be out on pip now with version
0.151. I tweaked a couple small things, but it's mostly your work. Tried to
get you props in the changelog in the docs. If I screwed something up, let
me know.

Reply to this email directly or view it on GitHub:

#71 (comment)

Shane Shifflett
312-379-9064

@palewire

Copy link
Copy Markdown
Contributor

Okay, maybe that check works better an isinstance instead of hasattr, though then we might have the subclassing problems you talked about when we get something descending from "file" or "stringio." If we figure out an example that crashes, I bet we can solve it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants