11"""
2- Python bindings for the Box.net API
2+ Python bindings for the Box.com API
33
44Copyright (c) 2007 Thomas Van Machelen <thomas dot vanmachelen at gmail dot com>
55Copyright (c) 2007 John Stowers <john dot stowers at gmail dot com>
1818WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1919See the License for the specific language governing permissions and
2020limitations under the License.
21-
2221"""
2322
24- import urllib
25- import urllib2
26- import mimetools
27- import mimetypes
28- import os
29- import sys
30-
31- from xml .dom .minidom import parseString
23+ import sys , os , urllib , urllib2 , mimetools , mimetypes , webbrowser
3224import xml .dom
25+ from xml .dom .minidom import parseString
26+
27+
28+ __version__ = '0.2.1'
29+
3330
3431def get_content_type (filename ):
35- return mimetypes .guess_type (filename )[0 ] or 'application/octet-stream'
32+ return mimetypes .guess_type (filename )[0 ] or 'application/octet-stream'
3633
3734########################################################################
3835# XML functionality
@@ -130,9 +127,9 @@ class BoxDotNetError(Exception):
130127 pass
131128
132129class BoxDotNet (object ):
133- END_POINT = 'http://www.box.net /api/1.0/rest?'
130+ END_POINT = 'http://www.box.com /api/1.0/rest?'
134131
135- #The box.net return status codes are all over the show
132+ #The box.com return status codes are all over the show
136133 # method_name : return_value_that_is_ok
137134 RETURN_CODES = {
138135 'get_ticket' : 'get_ticket_ok' ,
@@ -144,21 +141,19 @@ class BoxDotNet(object):
144141 'delete' : 's_delete_node'
145142 }
146143
147- def __init__ (self , browser = "firefox" ):
148- self .browser = browser
149-
144+ def __init__ (self ):
150145 self .__handlerCache = {}
151146
152147 #encodes the args and handles params[] supplied in a list
153148 @classmethod
154149 def __url_encode_params (cls , params = {}):
155- if not isinstance (params , dict ):
156- raise Exception ("You must pass a dictionary!" )
157- params_list = []
158- for k ,v in params .items ():
159- if isinstance (v , list ): params_list .extend ([(k + '[]' ,x ) for x in v ])
160- else : params_list .append ((k , v ))
161- return urllib .urlencode (params_list )
150+ if not isinstance (params , dict ):
151+ raise Exception ("You must pass a dictionary!" )
152+ params_list = []
153+ for k ,v in params .items ():
154+ if isinstance (v , list ): params_list .extend ([(k + '[]' ,x ) for x in v ])
155+ else : params_list .append ((k , v ))
156+ return urllib .urlencode (params_list )
162157
163158 @classmethod
164159 def check_errors (cls , method , xml ):
@@ -167,15 +162,18 @@ def check_errors(cls, method, xml):
167162 if status == cls .RETURN_CODES [method ]:
168163 return
169164
170- raise BoxDotNetError ("Box.net returned [%s] for action [%s]" % (status , method ))
165+ raise BoxDotNetError ("Box.com returned [%s] for action [%s]" % (status , method ))
171166
172167 def login (self , api_key ):
173168 # get ticket
174169 rsp = self .get_ticket (api_key = api_key )
175170 ticket = rsp .ticket [0 ].elementText
171+
176172 # open url
177- url = "http://www.box.net/api/1.0/auth/%s" % ticket
178- os .system ('%s "%s"' % (self .browser , url ))
173+ url = "http://www.box.com/api/1.0/auth/%s" % ticket
174+
175+ #os.system('%s "%s"' % ("firefox", url))
176+ webbrowser .open_new_tab (url )
179177
180178 # get token
181179 rsp = self .get_auth_token (api_key = api_key , ticket = ticket )
@@ -184,7 +182,7 @@ def login(self, api_key):
184182
185183 def __getattr__ (self , method , ** arg ):
186184 """
187- Handle all box.net calls
185+ Handle all box.com calls
188186 """
189187 if not self .__handlerCache .has_key (method ):
190188 def handler (_self = self , _method = method , ** arg ):
@@ -209,22 +207,21 @@ def handler(_self = self, _method = method, **arg):
209207
210208 return self .__handlerCache [method ]
211209
212- #-------------------------------------------------------------------
213- #-------------------------------------------------------------------
210+
214211 def upload (self , filename , ** arg ):
215212 """
216- Upload a file to box.net.
213+ Upload a file to box.com
217214 """
218215
219216 if filename == None :
220217 raise UploadException ("filename OR jpegData must be specified" )
221218
222219 # verify key names
223- for a in arg . keys () :
224- if a != "api_key" and a != "auth_token" and a != "folder_id" and a != 'share' :
225- sys .stderr .write ("Box.net api: warning: unknown parameter \" %s\" sent to Box.net .upload\n " % (a ))
220+ for a in arg :
221+ if a not in ( "api_key" , "auth_token" , "folder_id" , 'share' ) :
222+ sys .stderr .write ("Box.com api: warning: unknown parameter \" %s\" sent to Box.com .upload\n " % (a ))
226223
227- url = 'http://upload.box.net /api/1.0/upload/%s/%s' % (arg ['auth_token' ], arg ['folder_id' ])
224+ url = 'http://upload.box.com /api/1.0/upload/%s/%s' % (arg ['auth_token' ], arg ['folder_id' ])
228225
229226 # construct POST data
230227 boundary = mimetools .choose_boundary ()
@@ -246,12 +243,12 @@ def upload(self, filename, **arg):
246243 data = fp .read ()
247244 fp .close ()
248245
249- postData = body .encode ("utf_8" ) + data + \
250- ("\r \n --%s--" % (boundary )).encode ("utf_8" )
246+ postData = body .encode ("utf_8" ) + data + \
247+ ("\r \n --%s--" % (boundary )).encode ("utf_8" )
251248
252249 request = urllib2 .Request (url )
253250 request .add_data (postData )
254- request .add_header ("Content-Type" , \
251+ request .add_header ("Content-Type" ,\
255252 "multipart/form-data; boundary=%s" % boundary )
256253 response = urllib2 .urlopen (request )
257254 rspXML = response .read ()
0 commit comments