55and/or modify under the same terms as Python.
66"""
77
8- import urlparse
8+ from __future__ import print_function
9+
10+ try :
11+ from urllib .parse import urlparse
12+ except ImportError :
13+ from urlparse import urlparse
914import os
1015import json
1116import pprint
2025from roundup .exceptions import *
2126from roundup .cgi .exceptions import *
2227
28+ # Py3 compatible basestring
29+ try :
30+ basestring
31+ except NameError :
32+ basestring = str
33+ unicode = str
2334
2435def _data_decorator (func ):
2536 """Wrap the returned data into an object."""
2637 def format_object (self , * args , ** kwargs ):
2738 # get the data / error from function
2839 try :
2940 code , data = func (self , * args , ** kwargs )
30- except NotFound , msg :
41+ except NotFound as msg :
3142 code = 404
3243 data = msg
33- except IndexError , msg :
44+ except IndexError as msg :
3445 code = 404
3546 data = msg
36- except Unauthorised , msg :
47+ except Unauthorised as msg :
3748 code = 403
3849 data = msg
39- except UsageError , msg :
50+ except UsageError as msg :
4051 code = 400
4152 data = msg
42- except (AttributeError , Reject ), msg :
53+ except (AttributeError , Reject ) as msg :
4354 code = 405
4455 data = msg
45- except ValueError , msg :
56+ except ValueError as msg :
4657 code = 409
4758 data = msg
4859 except NotImplementedError :
@@ -52,13 +63,13 @@ def format_object(self, *args, **kwargs):
5263 exc , val , tb = sys .exc_info ()
5364 code = 400
5465 ts = time .ctime ()
55- if self .client .request . DEBUG_MODE :
66+ if getattr ( self .client .request , ' DEBUG_MODE' , None ) :
5667 data = val
5768 else :
5869 data = '%s: An error occurred. Please check the server log' \
5970 ' for more information.' % ts
6071 # out to the logfile
61- print 'EXCEPTION AT' , ts
72+ print ( 'EXCEPTION AT' , ts )
6273 traceback .print_exc ()
6374
6475 # decorate it
@@ -284,19 +295,15 @@ def prop_from_arg(self, cl, key, value, itemid=None):
284295 prop = None
285296 if isinstance (key , unicode ):
286297 try :
287- key = key .encode ('ascii' )
298+ x = key .encode ('ascii' )
288299 except UnicodeEncodeError :
289300 raise UsageError (
290301 'argument %r is no valid ascii keyword' % key
291302 )
292- if isinstance (value , unicode ):
293- value = value .encode ('utf-8' )
294303 if value :
295304 try :
296- prop = hyperdb .rawToHyperdb (
297- self .db , cl , itemid , key , value
298- )
299- except hyperdb .HyperdbValueError , msg :
305+ prop = hyperdb .rawToHyperdb (self .db , cl , itemid , key , value )
306+ except hyperdb .HyperdbValueError as msg :
300307 raise UsageError (msg )
301308
302309 return prop
@@ -478,9 +485,7 @@ def get_element(self, class_name, item_id, input):
478485 props = value .split ("," )
479486
480487 if props is None :
481- props = class_obj .properties .keys ()
482-
483- props .sort () # sort properties
488+ props = list (sorted (class_obj .properties .keys ()))
484489
485490 try :
486491 result = [
@@ -490,7 +495,7 @@ def get_element(self, class_name, item_id, input):
490495 'View' , self .db .getuid (), class_name , prop_name ,
491496 )
492497 ]
493- except KeyError , msg :
498+ except KeyError as msg :
494499 raise UsageError ("%s field not valid" % msg )
495500 result = {
496501 'id' : item_id ,
@@ -592,9 +597,9 @@ def post_collection(self, class_name, input):
592597 try :
593598 item_id = class_obj .create (** props )
594599 self .db .commit ()
595- except (TypeError , IndexError , ValueError ), message :
600+ except (TypeError , IndexError , ValueError ) as message :
596601 raise ValueError (message )
597- except KeyError , msg :
602+ except KeyError as msg :
598603 raise UsageError ("Must provide the %s property." % msg )
599604
600605 # set the header Location
@@ -634,7 +639,7 @@ def put_element(self, class_name, item_id, input):
634639 class_obj = self .db .getclass (class_name )
635640
636641 props = self .props_from_args (class_obj , input .value , item_id )
637- for p in props . iterkeys () :
642+ for p in props :
638643 if not self .db .security .hasPermission (
639644 'Edit' , self .db .getuid (), class_name , p , item_id
640645 ):
@@ -645,7 +650,7 @@ def put_element(self, class_name, item_id, input):
645650 try :
646651 result = class_obj .set (item_id , ** props )
647652 self .db .commit ()
648- except (TypeError , IndexError , ValueError ), message :
653+ except (TypeError , IndexError , ValueError ) as message :
649654 raise ValueError (message )
650655
651656 result = {
@@ -696,7 +701,7 @@ def put_attribute(self, class_name, item_id, attr_name, input):
696701 try :
697702 result = class_obj .set (item_id , ** props )
698703 self .db .commit ()
699- except (TypeError , IndexError , ValueError ), message :
704+ except (TypeError , IndexError , ValueError ) as message :
700705 raise ValueError (message )
701706
702707 result = {
@@ -820,7 +825,7 @@ def delete_attribute(self, class_name, item_id, attr_name, input):
820825 try :
821826 class_obj .set (item_id , ** props )
822827 self .db .commit ()
823- except (TypeError , IndexError , ValueError ), message :
828+ except (TypeError , IndexError , ValueError ) as message :
824829 raise ValueError (message )
825830
826831 result = {
@@ -893,7 +898,7 @@ def patch_element(self, class_name, item_id, input):
893898 # else patch operation is processing data
894899 props = self .props_from_args (class_obj , input .value , item_id )
895900
896- for prop , value in props . iteritems () :
901+ for prop in props :
897902 if not self .db .security .hasPermission (
898903 'Edit' , self .db .getuid (), class_name , prop , item_id
899904 ):
@@ -909,7 +914,7 @@ def patch_element(self, class_name, item_id, input):
909914 try :
910915 result = class_obj .set (item_id , ** props )
911916 self .db .commit ()
912- except (TypeError , IndexError , ValueError ), message :
917+ except (TypeError , IndexError , ValueError ) as message :
913918 raise ValueError (message )
914919
915920 result = {
@@ -975,7 +980,7 @@ def patch_attribute(self, class_name, item_id, attr_name, input):
975980 try :
976981 result = class_obj .set (item_id , ** props )
977982 self .db .commit ()
978- except (TypeError , IndexError , ValueError ), message :
983+ except (TypeError , IndexError , ValueError ) as message :
979984 raise ValueError (message )
980985
981986 result = {
@@ -1113,7 +1118,7 @@ def dispatch(self, method, uri, input):
11131118 # priority : extension from uri (/rest/issue.json),
11141119 # header (Accept: application/json, application/xml)
11151120 # default (application/json)
1116- ext_type = os .path .splitext (urlparse . urlparse (uri ).path )[1 ][1 :]
1121+ ext_type = os .path .splitext (urlparse (uri ).path )[1 ][1 :]
11171122 data_type = ext_type or accept_type or self .__default_accept_type
11181123
11191124 # check for pretty print
@@ -1140,9 +1145,9 @@ def dispatch(self, method, uri, input):
11401145 # Call the appropriate method
11411146 try :
11421147 output = Routing .execute (self , uri , method , input )
1143- except NotFound , msg :
1148+ except NotFound as msg :
11441149 output = self .error_obj (404 , msg )
1145- except Reject , msg :
1150+ except Reject as msg :
11461151 output = self .error_obj (405 , msg )
11471152
11481153 # Format the content type
0 commit comments