| 1 | |
|---|
| 2 | |
|---|
| 3 | '''Module for encoding data as form-data/multipart''' |
|---|
| 4 | |
|---|
| 5 | import os |
|---|
| 6 | import base64 |
|---|
| 7 | |
|---|
| 8 | class Part(object): |
|---|
| 9 | '''A single part of the multipart data. |
|---|
| 10 | |
|---|
| 11 | >>> Part({'name': 'headline'}, 'Nice Photo') |
|---|
| 12 | ... # doctest: +ELLIPSIS |
|---|
| 13 | <flickrapi.multipart.Part object at 0x...> |
|---|
| 14 | |
|---|
| 15 | >>> image = file('tests/photo.jpg') |
|---|
| 16 | >>> Part({'name': 'photo', 'filename': image}, image.read(), 'image/jpeg') |
|---|
| 17 | ... # doctest: +ELLIPSIS |
|---|
| 18 | <flickrapi.multipart.Part object at 0x...> |
|---|
| 19 | ''' |
|---|
| 20 | |
|---|
| 21 | def __init__(self, parameters, payload, content_type=None): |
|---|
| 22 | self.content_type = content_type |
|---|
| 23 | self.parameters = parameters |
|---|
| 24 | self.payload = payload |
|---|
| 25 | |
|---|
| 26 | def render(self): |
|---|
| 27 | '''Renders this part -> List of Strings''' |
|---|
| 28 | |
|---|
| 29 | parameters = ['%s="%s"' % (k, v) |
|---|
| 30 | for k, v in self.parameters.iteritems()] |
|---|
| 31 | |
|---|
| 32 | lines = ['Content-Disposition: form-data; %s' % '; '.join(parameters)] |
|---|
| 33 | |
|---|
| 34 | if self.content_type: |
|---|
| 35 | lines.append("Content-Type: %s" % self.content_type) |
|---|
| 36 | |
|---|
| 37 | lines.append('') |
|---|
| 38 | |
|---|
| 39 | if isinstance(self.payload, unicode): |
|---|
| 40 | lines.append(self.payload.encode('utf-8')) |
|---|
| 41 | else: |
|---|
| 42 | lines.append(self.payload) |
|---|
| 43 | |
|---|
| 44 | return lines |
|---|
| 45 | |
|---|
| 46 | class FilePart(Part): |
|---|
| 47 | '''A single part with a file as the payload |
|---|
| 48 | |
|---|
| 49 | This example has the same semantics as the second Part example: |
|---|
| 50 | |
|---|
| 51 | >>> FilePart({'name': 'photo'}, 'tests/photo.jpg', 'image/jpeg') |
|---|
| 52 | ... #doctest: +ELLIPSIS |
|---|
| 53 | <flickrapi.multipart.FilePart object at 0x...> |
|---|
| 54 | ''' |
|---|
| 55 | |
|---|
| 56 | def __init__(self, parameters, filename, content_type): |
|---|
| 57 | parameters['filename'] = filename |
|---|
| 58 | |
|---|
| 59 | imagefile = open(filename, 'rb') |
|---|
| 60 | payload = imagefile.read() |
|---|
| 61 | imagefile.close() |
|---|
| 62 | |
|---|
| 63 | Part.__init__(self, parameters, payload, content_type) |
|---|
| 64 | |
|---|
| 65 | def boundary(): |
|---|
| 66 | """Generate a random boundary, a bit like Python 2.5's uuid module.""" |
|---|
| 67 | |
|---|
| 68 | bytes = os.urandom(16) |
|---|
| 69 | return base64.b64encode(bytes, 'ab').strip('=') |
|---|
| 70 | |
|---|
| 71 | class Multipart(object): |
|---|
| 72 | '''Container for multipart data''' |
|---|
| 73 | |
|---|
| 74 | def __init__(self): |
|---|
| 75 | '''Creates a new Multipart.''' |
|---|
| 76 | |
|---|
| 77 | self.parts = [] |
|---|
| 78 | self.content_type = 'form-data/multipart' |
|---|
| 79 | self.boundary = boundary() |
|---|
| 80 | |
|---|
| 81 | def attach(self, part): |
|---|
| 82 | '''Attaches a part''' |
|---|
| 83 | |
|---|
| 84 | self.parts.append(part) |
|---|
| 85 | |
|---|
| 86 | def __str__(self): |
|---|
| 87 | '''Renders the Multipart''' |
|---|
| 88 | |
|---|
| 89 | lines = [] |
|---|
| 90 | for part in self.parts: |
|---|
| 91 | lines += ['--' + self.boundary] |
|---|
| 92 | lines += part.render() |
|---|
| 93 | lines += ['--' + self.boundary + "--"] |
|---|
| 94 | |
|---|
| 95 | return '\r\n'.join(lines) |
|---|
| 96 | |
|---|
| 97 | def header(self): |
|---|
| 98 | '''Returns the top-level HTTP header of this multipart''' |
|---|
| 99 | |
|---|
| 100 | return ("Content-Type", |
|---|
| 101 | "multipart/form-data; boundary=%s" % self.boundary) |
|---|