Skip to content

Commit ce44272

Browse files
committed
moved to subfolder to add setup.py for submission to PyPI
changed code to be more pythonic
1 parent 51515f5 commit ce44272

2 files changed

Lines changed: 65 additions & 37 deletions

File tree

boxdotnet.py renamed to boxdotcom/__init__.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Python bindings for the Box.net API
2+
Python bindings for the Box.com API
33
44
Copyright (c) 2007 Thomas Van Machelen <thomas dot vanmachelen at gmail dot com>
55
Copyright (c) 2007 John Stowers <john dot stowers at gmail dot com>
@@ -18,21 +18,18 @@
1818
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1919
See the License for the specific language governing permissions and
2020
limitations 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
3224
import xml.dom
25+
from xml.dom.minidom import parseString
26+
27+
28+
__version__ = '0.2.1'
29+
3330

3431
def 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

132129
class 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()

‎setup.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
#from distutils.core import setup
3+
from setuptools import setup, find_packages
4+
from boxdotcom import __version__
5+
6+
setup(name="boxdotcom",
7+
version=__version__,
8+
description="Python wrapper for Box.com/.net API",
9+
long_description="""You can use the Box API to create applications and websites that integrate with Box.com/.net and can perform the following functions:
10+
* Store and retrieve files from Box
11+
* Organize files into folders
12+
* Move, rename and delete files
13+
* Share files""",
14+
license="Apache License",
15+
author="Sean Rose",
16+
author_email="sean.andrew.rose@gmail.com",
17+
url="http://github.com/dvska/python-boxdotcom",
18+
packages=find_packages(),
19+
install_requires=[],
20+
keywords= ["python", "box", "box.net", "box.com", "api", "wrapper"],
21+
classifiers = [
22+
"Programming Language :: Python",
23+
"Development Status :: 4 - Beta",
24+
"Environment :: Other Environment",
25+
"Intended Audience :: Developers",
26+
"License :: OSI Approved",
27+
"Operating System :: OS Independent",
28+
"Topic :: Software Development :: Libraries :: Python Modules",
29+
"Topic :: Software Development",
30+
],
31+
)

0 commit comments

Comments
 (0)