Commits

roland.gude  committed 4639975

use poster to support unicode again

  • Participants
  • Parent commits bd65259

Comments (0)

Files changed (1)

File mercurial_reviewboard/reviewboard.py

 
 import cookielib
 import getpass
-import mimetools
+import mimetools, mimetypes
 import os
 import urllib2
 import mercurial.ui
 import datetime
+import itertools
 from urlparse import urljoin, urlparse
+from cStringIO import StringIO
+from io import BytesIO
+from poster.encode import MultipartParam, multipart_encode
+from poster.streaminghttp import register_openers
 
 try:
     import simplejson
         self.cookie_file = os.path.join(homepath, ".post-review-cookies.txt")
         self._cj = cookielib.MozillaCookieJar(self.cookie_file)
         self._password_mgr = ReviewBoardHTTPPasswordMgr(self.url)
-        self._opener = opener = urllib2.build_opener(
-                        urllib2.ProxyHandler(proxy),
-                        urllib2.UnknownHandler(),
-                        urllib2.HTTPHandler(),
-                        HttpErrorHandler(),
-                        urllib2.HTTPErrorProcessor(),
-                        urllib2.HTTPCookieProcessor(self._cj),
-                        urllib2.HTTPBasicAuthHandler(self._password_mgr),
-                        urllib2.HTTPDigestAuthHandler(self._password_mgr)
-                        )
+        #self._opener = opener = urllib2.build_opener(
+        #                urllib2.ProxyHandler(proxy),
+        #                urllib2.UnknownHandler(),
+        #                urllib2.HTTPHandler(debuglevel=1),
+        #                HttpErrorHandler(),
+        #                urllib2.HTTPErrorProcessor(),
+        #                urllib2.HTTPCookieProcessor(self._cj),
+        #                urllib2.HTTPBasicAuthHandler(self._password_mgr),
+        #                urllib2.HTTPDigestAuthHandler(self._password_mgr)
+        #                )
+	opener = register_openers();
+	opener.add_handler(urllib2.ProxyHandler(proxy))
+	opener.add_handler(urllib2.UnknownHandler())
+	opener.add_handler(HttpErrorHandler())
+	opener.add_handler(urllib2.HTTPErrorProcessor())
+	opener.add_handler(urllib2.HTTPCookieProcessor(self._cj))
+	opener.add_handler(urllib2.HTTPBasicAuthHandler(self._password_mgr))
+	opener.add_handler(urllib2.HTTPDigestAuthHandler(self._password_mgr))
+	self._opener = opener
         urllib2.install_opener(self._opener)
 
     def set_credentials(self, username, password):
         body = None
         headers = {}
         if fields or files:
-            content_type, body = self._encode_multipart_formdata(fields, files)
-            headers = {
-                'Content-Type': content_type,
-                'Content-Length': str(len(body))
-                }
+            #content_type, body = self._encode_multipart_formdata(fields, files)
+            #headers = {
+            #    'Content-Type': content_type,
+            #    'Content-Length': str(len(body))
+            #    }
+	    body, headers = self._encode_multipart_formdata(fields, files)
         try:
+            print body, headers
             r = ApiRequest(method, url, body, headers)
             data = urllib2.urlopen(r).read()
             try:
         """
         Encodes data for use in an HTTP POST.
         """
-        BOUNDARY = mimetools.choose_boundary()
-        content = ""
-
         fields = fields or {}
         files = files or {}
+        fields_params = [MultipartParam(name=k, value=v) for k,v in fields.items()]
+        files_params = [MultipartParam(name=k, value=v['content'], filename=v['filename'])for k, v in files.items()]
+        params = list(itertools.chain(fields_params, files_params))
+        datagen, headers = multipart_encode(params)
+	return datagen, headers
+        #for key in fields:
+        #    content.write("--" + BOUNDARY + "\r\n")
+        #    content.write("Content-Disposition: form-data; name=\"%s\"\r\n" % key)
+        #    content.write("\r\n")
+        #    content.write(fields[key] + "\r\n")
 
-        for key in fields:
-            content += "--" + BOUNDARY + "\r\n"
-            content += "Content-Disposition: form-data; name=\"%s\"\r\n" % key
-            content += "\r\n"
-            content += fields[key] + "\r\n"
+        #for key in files:
+        #    filename = files[key]['filename']
+        #    value = files[key]['content']
+	#    file_content_type = mimetypes.guess_type(filename)[0] or 'application/octet-stream' #"application/x-hgbundle"#mimetypes.guess_type(filename)[0]
+        #    content.write("--" + BOUNDARY + "\r\n")
+        #    content.write("Content-Disposition: form-data; name=\"%s\"; " % key)
+        #    content.write("filename=\"%s\"\r\n" % filename)
+	#    content.write("Content-Type: %s\r\n" %file_content_type)
+        #    content.write("\r\n")
+        #    content.write(value + "\r\n")
 
-        for key in files:
-            filename = files[key]['filename']
-            value = files[key]['content']
-            content += "--" + BOUNDARY + "\r\n"
-            content += "Content-Disposition: form-data; name=\"%s\"; " % key
-            content += "filename=\"%s\"\r\n" % filename
-            content += "\r\n"
-            content += value + "\r\n"
+        #content.write("--" + BOUNDARY + "--\r\n")
+        #content.write("\r\n")
 
-        content += "--" + BOUNDARY + "--\r\n"
-        content += "\r\n"
-
-        content_type = "multipart/form-data; boundary=%s" % BOUNDARY
-
-        return content_type, content
+        #content_type = "multipart/form-data; boundary=%s" % BOUNDARY
+        #return content_type, content.getvalue()
 
 
 class ApiClient: