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

Commit 6a4a1e8

Browse files
committed
PEP8 compliance and testing
1 parent e63dd60 commit 6a4a1e8

5 files changed

Lines changed: 223 additions & 130 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ python:
55
- '2.7'
66
install:
77
- pip install -r requirements.txt --use-mirrors
8-
script: python test.py
8+
script:
9+
- pep8 documentcloud
10+
- python test.py
911
env:
1012
global:
1113
# Encrypted DOCUMENTCLOUD_TEST_USERNAME

documentcloud/MultipartPostHandler.py

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
####
44
# 02/2006 Will Holcomb <wholcomb@gmail.com>
5-
#
5+
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
88
# License as published by the Free Software Foundation; either
99
# version 2.1 of the License, or (at your option) any later version.
10-
#
10+
1111
# This library is distributed in the hope that it will be useful,
1212
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1414
# Lesser General Public License for more details.
1515
#
16-
# 7/26/07 Slightly modified by Brian Schneider
16+
# 7/26/07 Slightly modified by Brian Schneider
1717
# in order to support unicode files ( multipart_encode function )
1818
"""
1919
Usage:
@@ -39,52 +39,64 @@
3939
The main function of this file is a sample which downloads a page and
4040
then uploads it to the W3C validator.
4141
"""
42-
42+
import sys
43+
import tempfile
4344
import urllib
4445
import urllib2
45-
import mimetools, mimetypes
46-
import os, stat
46+
import mimetools
47+
import mimetypes
48+
import os
49+
import stat
50+
from os import SEEK_END
4751
from cStringIO import StringIO
4852

53+
4954
class Callable:
5055
def __init__(self, anycallable):
5156
self.__call__ = anycallable
5257

53-
# Controls how sequences are uncoded. If true, elements may be given multiple values by
54-
# assigning a sequence.
58+
# Controls how sequences are uncoded. If true, elements
59+
# may be given multiple values byassigning a sequence.
5560
doseq = 1
5661

62+
5763
class MultipartPostHandler(urllib2.BaseHandler):
58-
handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first
64+
# needs to run first
65+
handler_order = urllib2.HTTPHandler.handler_order - 10
5966

6067
def http_request(self, request):
6168
data = request.get_data()
6269
if data is not None and type(data) != str:
6370
v_files = []
6471
v_vars = []
6572
try:
66-
for(key, value) in data.items():
67-
if hasattr(value, 'read'):
68-
v_files.append((key, value))
69-
else:
70-
v_vars.append((key, value))
73+
for(key, value) in data.items():
74+
if hasattr(value, 'read'):
75+
v_files.append((key, value))
76+
else:
77+
v_vars.append((key, value))
7178
except TypeError:
72-
systype, value, traceback = sys.exc_info()
73-
raise TypeError, "not a valid non-string sequence or mapping object", traceback
79+
raise TypeError
7480
if len(v_files) == 0:
7581
data = urllib.urlencode(v_vars, doseq)
7682
else:
7783
boundary, data = self.multipart_encode(v_vars, v_files)
7884
contenttype = 'multipart/form-data; boundary=%s' % boundary
79-
if(request.has_header('Content-Type')
80-
and request.get_header('Content-Type').find('multipart/form-data') != 0):
81-
print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data')
85+
if (
86+
request.has_header('Content-Type') and
87+
request.get_header('Content-Type').find(
88+
'multipart/form-data') != 0
89+
):
90+
print "Replacing %s with %s" % (
91+
request.get_header('content-type'),
92+
'multipart/form-data'
93+
)
8294
request.add_unredirected_header('Content-Type', contenttype)
8395
request.add_data(data)
84-
96+
8597
return request
8698

87-
def multipart_encode(vars, files, boundary = None, buf = None):
99+
def multipart_encode(vars, files, boundary=None, buf=None):
88100
if boundary is None:
89101
boundary = mimetools.choose_boundary()
90102
if buf is None:
@@ -99,11 +111,14 @@ def multipart_encode(vars, files, boundary = None, buf = None):
99111
filename = fd.name.split('/')[-1]
100112
except AttributeError:
101113
# Spoof a file name if the object doesn't have one.
102-
# This is designed to catch when the user submits a StringIO object
114+
# This is designed to catch when the user submits
115+
# a StringIO object
103116
filename = 'temp.pdf'
104-
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
117+
contenttype = mimetypes.guess_type(filename)[0] or \
118+
'application/octet-stream'
105119
buf.write('--%s\r\n' % boundary)
106-
buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
120+
buf.write('Content-Disposition: form-data; \
121+
name="%s"; filename="%s"\r\n' % (key, filename))
107122
buf.write('Content-Type: %s\r\n' % contenttype)
108123
# buffer += 'Content-Length: %s\r\n' % file_size
109124
fd.seek(0)
@@ -119,28 +134,26 @@ def getsize(o_file):
119134
"""
120135
get the size, either by seeeking to the end.
121136
"""
122-
from os import SEEK_END
123-
startpos=o_file.tell()
137+
startpos = o_file.tell()
124138
o_file.seek(0)
125-
o_file.seek(0,SEEK_END)
126-
size=o_file.tell()
139+
o_file.seek(0, SEEK_END)
140+
size = o_file.tell()
127141
o_file.seek(startpos)
128142
return size
129143

130144

131145
def main():
132-
import tempfile, sys
133-
134146
validatorURL = "http://validator.w3.org/check"
135147
opener = urllib2.build_opener(MultipartPostHandler)
136-
148+
137149
def validateFile(url):
138150
temp = tempfile.mkstemp(suffix=".html")
139151
os.write(temp[0], opener.open(url).read())
140-
params = { "ss" : "0", # show source
141-
"doctype" : "Inline",
142-
"uploaded_file" : open(temp[1], "rb") }
143-
print opener.open(validatorURL, params).read()
152+
params = {
153+
"ss": "0", # show source
154+
"doctype": "Inline",
155+
"uploaded_file": open(temp[1], "rb")
156+
}
144157
os.remove(temp[1])
145158

146159
if len(sys.argv[1:]) > 0:
@@ -149,7 +162,5 @@ def validateFile(url):
149162
else:
150163
validateFile("http://www.google.com")
151164

152-
if __name__=="__main__":
165+
if __name__ == "__main__":
153166
main()
154-
155-

0 commit comments

Comments
 (0)