Browse Source

HUE-8737 [hbase] Futurize apps/hbase for Python 3.5

Ying Chen 6 years ago
parent
commit
b930a22977

File diff suppressed because it is too large
+ 114 - 111
apps/hbase/gen-py/hbased/Hbase.py


+ 2 - 1
apps/hbase/gen-py/hbased/constants.py

@@ -1,3 +1,4 @@
+from __future__ import absolute_import
 #
 # Autogenerated by Thrift Compiler (0.9.3)
 #
@@ -7,5 +8,5 @@
 #
 
 from thrift.Thrift import TType, TMessageType, TException, TApplicationException
-from ttypes import *
+from .ttypes import *
 

+ 17 - 15
apps/hbase/gen-py/hbased/ttypes.py

@@ -6,6 +6,8 @@
 #  options string: py:new_style
 #
 
+from builtins import range
+from builtins import object
 from thrift.Thrift import TType, TMessageType, TException, TApplicationException
 
 from thrift.transport import TTransport
@@ -91,7 +93,7 @@ class TCell(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -264,7 +266,7 @@ class ColumnDescriptor(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -409,7 +411,7 @@ class TRegionInfo(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -515,7 +517,7 @@ class Mutation(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -561,7 +563,7 @@ class BatchMutation(object):
         if ftype == TType.LIST:
           self.mutations = []
           (_etype3, _size0) = iprot.readListBegin()
-          for _i4 in xrange(_size0):
+          for _i4 in range(_size0):
             _elem5 = Mutation()
             _elem5.read(iprot)
             self.mutations.append(_elem5)
@@ -604,7 +606,7 @@ class BatchMutation(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -711,7 +713,7 @@ class TIncrement(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -757,7 +759,7 @@ class TRowResult(object):
         if ftype == TType.MAP:
           self.columns = {}
           (_ktype8, _vtype9, _size7 ) = iprot.readMapBegin()
-          for _i11 in xrange(_size7):
+          for _i11 in range(_size7):
             _key12 = iprot.readString()
             _val13 = TCell()
             _val13.read(iprot)
@@ -782,7 +784,7 @@ class TRowResult(object):
     if self.columns is not None:
       oprot.writeFieldBegin('columns', TType.MAP, 2)
       oprot.writeMapBegin(TType.STRING, TType.STRUCT, len(self.columns))
-      for kiter14,viter15 in self.columns.items():
+      for kiter14,viter15 in list(self.columns.items()):
         oprot.writeString(kiter14)
         viter15.write(oprot)
       oprot.writeMapEnd()
@@ -802,7 +804,7 @@ class TRowResult(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -873,7 +875,7 @@ class TScan(object):
         if ftype == TType.LIST:
           self.columns = []
           (_etype19, _size16) = iprot.readListBegin()
-          for _i20 in xrange(_size16):
+          for _i20 in range(_size16):
             _elem21 = iprot.readString()
             self.columns.append(_elem21)
           iprot.readListEnd()
@@ -955,7 +957,7 @@ class TScan(object):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -1027,7 +1029,7 @@ class IOError(TException):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -1098,7 +1100,7 @@ class IllegalArgument(TException):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):
@@ -1169,7 +1171,7 @@ class AlreadyExists(TException):
 
   def __repr__(self):
     L = ['%s=%r' % (key, value)
-      for key, value in self.__dict__.iteritems()]
+      for key, value in self.__dict__.items()]
     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
 
   def __eq__(self, other):

+ 6 - 4
apps/hbase/src/hbase/api.py

@@ -15,6 +15,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import range
+from builtins import object
 import json
 import logging
 import re
@@ -46,7 +48,7 @@ class HbaseApi(object):
         return getattr(self, action)(*args)
       cluster = args[0]
       return self.queryCluster(action, cluster, *args[1:])
-    except Exception, e:
+    except Exception as e:
       if 'Could not connect to' in e.message:
         raise PopupException(_("HBase Thrift 1 server cannot be contacted: %s") % e.message)
       else:
@@ -158,7 +160,7 @@ class HbaseApi(object):
       scan = get_thrift_type('TScan')(startRow=query, stopRow=None, timestamp=None, columns=[], caching=None, filterString="PrefixFilter('" + query + "') AND ColumnPaginationFilter(1,0)", batchSize=None)
       scanner = client.scannerOpenWithScan(tableName, scan, None, doas=self.user.username)
       return [result.row for result in client.scannerGetList(scanner, numRows, doas=self.user.username)]
-    except Exception, e:
+    except Exception as e:
       LOG.error('Autocomplete error: %s' % smart_str(e))
       return []
 
@@ -201,7 +203,7 @@ class HbaseApi(object):
     client = self.connectCluster(cluster)
     mutations = []
     Mutation = get_thrift_type('Mutation')
-    for column in data.keys():
+    for column in list(data.keys()):
       value = smart_str(data[column]) if data[column] is not None else None
       mutations.append(Mutation(column=smart_str(column), value=value)) # must use str for API, does thrift coerce by itself?
     return client.mutateRow(tableName, smart_str(row), mutations, None, doas=self.user.username)
@@ -243,7 +245,7 @@ class HbaseApi(object):
     data = data.read()
     dialect = csv.Sniffer().sniff(data)
     reader = csv.reader(data.splitlines(), delimiter=dialect.delimiter)
-    columns = reader.next()
+    columns = next(reader)
     batches = []
     for row in reader:
       row_key = row[0]

+ 3 - 2
apps/hbase/src/hbase/conf.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import print_function
 import logging
 import os
 import sys
@@ -87,8 +88,8 @@ def config_validator(user):
       # Check connectivity
       api.connectCluster(cluster_name)
       api.getTableList(cluster_name)
-  except Exception, e:
-    print e
+  except Exception as e:
+    print(e)
     if 'Could not connect' in str(e):
       msg = "The application won't work without a running HBase Thrift Server v1."
     else:

+ 1 - 1
apps/hbase/src/hbase/hbase_site.py

@@ -95,7 +95,7 @@ def _parse_site():
   SITE_PATH = os.path.join(HBASE_CONF_DIR.get(), 'hbase-site.xml')
   try:
     data = file(SITE_PATH, 'r').read()
-  except IOError, err:
+  except IOError as err:
     if err.errno != errno.ENOENT:
       LOG.error('Cannot read from "%s": %s' % (SITE_PATH, err))
       return

+ 5 - 4
apps/hbase/src/hbase/tests.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import object
 import json
 import os
 import shutil
@@ -144,18 +145,18 @@ def test_impersonation():
 
 
 
-class MockHttpClient():
+class MockHttpClient(object):
   def __init__(self):
     self.headers = {}
 
   def setCustomHeaders(self, headers):
     self.headers = headers
 
-class MockTransport():
+class MockTransport(object):
   def __init__(self):
     self._TBufferedTransport__trans = MockHttpClient()
 
-class MockProtocol():
+class MockProtocol(object):
   def __init__(self):
     self.trans = MockTransport()
 
@@ -167,7 +168,7 @@ class MockProtocol():
 
 
 
-class TestIntegrationWithHBase:
+class TestIntegrationWithHBase(object):
   integration = True
 
   @classmethod

+ 12 - 5
apps/hbase/src/hbase/views.py

@@ -15,12 +15,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from future import standard_library
+standard_library.install_aliases()
 import base64
 import json
 import logging
 import re
-import StringIO
-import urllib
+import sys
+import urllib.request, urllib.parse, urllib.error
 
 from avro import datafile, io
 
@@ -37,6 +39,11 @@ from hbase.server.hbase_lib import get_thrift_type
 
 from desktop.auth.backend import is_admin
 
+if sys.version_info[0] > 2:
+  from io import StringIO as string_io
+else:
+  from cStringIO import StringIO as string_io
+
 LOG = logging.getLogger(__name__)
 
 
@@ -70,7 +77,7 @@ def api_router(request, url): # On split, deserialize anything
         data[i] = deserialize(item) # Sets local binding, needs to set in data
     return data
 
-  decoded_url_params = [urllib.unquote(arg) for arg in re.split(r'(?<!\\)/', url.strip('/'))]
+  decoded_url_params = [urllib.parse.unquote(arg) for arg in re.split(r'(?<!\\)/', url.strip('/'))]
   url_params = [safe_json_load((arg, request.POST.get(arg[0:16], arg))[arg[0:15] == 'hbase-post-key-'])
                 for arg in decoded_url_params] # Deserialize later
 
@@ -95,7 +102,7 @@ def api_dump(response):
         #detect if avro file
         if(data[:3] == '\x4F\x62\x6A'):
           #write data to file in memory
-          output = StringIO.StringIO()
+          output = io.StringIO()
           output.write(data)
 
           #read and parse avro
@@ -139,7 +146,7 @@ def install_examples(request):
     try:
       hbase_setup.Command().handle(user=request.user)
       result['status'] = 0
-    except Exception, e:
+    except Exception as e:
       LOG.exception(e)
       result['message'] = str(e)
 

Some files were not shown because too many files changed in this diff