Selaa lähdekoodia

HUE-8737 [core] Futurize desktop/libs/dashboard for Python 3.5

Ying Chen 6 vuotta sitten
vanhempi
commit
d1fa02d17a

+ 5 - 4
desktop/libs/dashboard/src/dashboard/api.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import filter
 import hashlib
 import json
 import logging
@@ -67,7 +68,7 @@ def search(request):
         response = get_engine(request.user, collection, facet, cluster=cluster).fetch_result(collection, query, facet)
       else:
         response = get_engine(request.user, collection, facet, cluster=cluster).query(collection, query, facet)
-    except RestException, e:
+    except RestException as e:
       response.update(extract_solr_exception_message(e))
     except Exception as e:
       raise PopupException(e, title=_('Error while accessing Solr'))
@@ -118,11 +119,11 @@ def index_fields_dynamic(request):
     result['message'] = ''
     result['fields'] = [
         Collection2._make_field(name, properties)
-        for name, properties in dynamic_fields['fields'].iteritems() if 'dynamicBase' in properties
+        for name, properties in dynamic_fields['fields'].items() if 'dynamicBase' in properties
     ]
     result['gridlayout_header_fields'] = [
         Collection2._make_gridlayout_header_field({'name': name, 'type': properties.get('type')}, True)
-        for name, properties in dynamic_fields['fields'].iteritems() if 'dynamicBase' in properties
+        for name, properties in dynamic_fields['fields'].items() if 'dynamicBase' in properties
     ]
     result['status'] = 0
   except Exception as e:
@@ -346,7 +347,7 @@ def get_timeline(request):
           fq['filter'] = [{'value': qdata, 'exclude': False}]
 
     # Remove other facets from collection for speed
-    collection['facets'] = filter(lambda f: f['widgetType'] == 'histogram-widget', collection['facets'])
+    collection['facets'] = [f for f in collection['facets'] if f['widgetType'] == 'histogram-widget']
 
     response = SolrApi(SOLR_URL.get(), request.user).query(collection, query)
     response = augment_solr_response(response, collection, query)

+ 6 - 4
desktop/libs/dashboard/src/dashboard/controller.py

@@ -16,6 +16,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import str
+from builtins import object
 import logging
 
 from django.db.models import Q
@@ -82,9 +84,9 @@ class DashboardController(object):
           doc.delete()
           doc2.delete()
       result['status'] = 0
-    except Exception, e:
+    except Exception as e:
       LOG.warn('Error deleting collection: %s' % e)
-      result['message'] = unicode(str(e), "utf8")
+      result['message'] = str(e)
 
     return result
 
@@ -107,9 +109,9 @@ class DashboardController(object):
           doc2.update_data({'collection': collection.data['collection']})
           doc2.save()
       result['status'] = 0
-    except Exception, e:
+    except Exception as e:
       LOG.exception('Error copying collection')
-      result['message'] = unicode(str(e), "utf8")
+      result['message'] = str(e)
 
     return result
 

+ 1 - 0
desktop/libs/dashboard/src/dashboard/dashboard_api.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import object
 import logging
 
 

+ 1 - 0
desktop/libs/dashboard/src/dashboard/data_export.py

@@ -17,6 +17,7 @@
 #
 # Handling of data export
 
+from past.builtins import basestring, long
 import logging
 
 from django.utils.encoding import smart_str

+ 19 - 12
desktop/libs/dashboard/src/dashboard/facet_builder.py

@@ -16,9 +16,16 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import division
+from __future__ import print_function
+from future import standard_library
+standard_library.install_aliases()
+from builtins import str
+from builtins import range
+from past.utils import old_div
 import logging
 import numbers
-import urllib
+import urllib.request, urllib.parse, urllib.error
 import re
 
 from datetime import datetime, timedelta
@@ -91,7 +98,7 @@ for interval in TIME_INTERVALS:
   interval['ms'] = TIME_INTERVALS_MS[interval['unit']] * interval['coeff']
 
 def utf_quoter(what):
-  return urllib.quote(unicode(what).encode('utf-8'), safe='~@#$&()*!+=:;,.?/\'')
+  return urllib.parse.quote(str(what).encode('utf-8'), safe='~@#$&()*!+=:;,.?/\'')
 
 
 def _guess_range_facet(widget_type, solr_api, collection, facet_field, properties, start=None, end=None, gap=None, window_size=None, slot = 0):
@@ -100,20 +107,20 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
     stat_facet = stats_json['stats']['stats_fields'][facet_field]
 
     _compute_range_facet(widget_type, stat_facet, properties, start, end, gap, window_size = window_size, SLOTS = slot)
-  except Exception, e:
-    print e
+  except Exception as e:
+    print(e)
     LOG.info('Stats not supported on all the fields, like text: %s' % e)
 
 
 def _get_interval(domain_ms, SLOTS):
   biggest_interval = TIME_INTERVALS[len(TIME_INTERVALS) - 1]
-  biggest_interval_is_too_small = domain_ms / biggest_interval['ms'] > SLOTS
+  biggest_interval_is_too_small = old_div(domain_ms, biggest_interval['ms']) > SLOTS
   if biggest_interval_is_too_small:
-    coeff = min(ceil(domain_ms / SLOTS), 100) # If we go over 100 years, something has gone wrong.
+    coeff = min(ceil(old_div(domain_ms, SLOTS)), 100) # If we go over 100 years, something has gone wrong.
     return {'ms': YEAR_MS * coeff, 'coeff': coeff, 'unit': 'YEARS'}
 
   for i in range(len(TIME_INTERVALS) - 2, 0, -1):
-    slots = domain_ms / TIME_INTERVALS[i]['ms']
+    slots = old_div(domain_ms, TIME_INTERVALS[i]['ms'])
     if slots > SLOTS:
       return TIME_INTERVALS[i + 1]
 
@@ -168,7 +175,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
         SLOTS = 5
       elif widget_type == 'facet-widget' or widget_type == 'text-facet-widget' or widget_type == 'histogram-widget' or widget_type == 'bar-widget' or widget_type == 'bucket-widget' or widget_type == 'timeline-widget':
         if window_size:
-          SLOTS = int(window_size) / 75 # Value is determined as the thinnest space required to display a timestamp on x axis
+          SLOTS = old_div(int(window_size), 75) # Value is determined as the thinnest space required to display a timestamp on x axis
         else:
           SLOTS = 10
       else:
@@ -195,7 +202,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
         end = int(end)
 
       if gap is None:
-        gap = int((end - start) / SLOTS)
+        gap = int(old_div((end - start), SLOTS))
       if gap < 1:
         gap = 1
 
@@ -212,7 +219,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
       try:
         start_ts = datetime.strptime(start, '%Y-%m-%dT%H:%M:%SZ')
         start_ts.strftime('%Y-%m-%dT%H:%M:%SZ') # Check for dates before 1900
-      except Exception, e:
+      except Exception as e:
         LOG.error('Bad date: %s' % e)
         start_ts = datetime.strptime('1970-01-01T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
 
@@ -222,7 +229,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
       try:
         end_ts = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ')
         end_ts.strftime('%Y-%m-%dT%H:%M:%SZ') # Check for dates before 1900
-      except Exception, e:
+      except Exception as e:
         LOG.error('Bad date: %s' % e)
         end_ts = datetime.strptime('2050-01-01T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
       end = end_ts.strftime('%Y-%m-%dT%H:%M:%SZ')
@@ -241,7 +248,7 @@ def _compute_range_facet(widget_type, stat_facet, properties, start=None, end=No
       is_date = True
       domain_ms = _get_interval_duration(stat_facet['min'])
       interval = _get_interval(domain_ms, SLOTS)
-      nb_slot = domain_ms / interval['ms']
+      nb_slot = old_div(domain_ms, interval['ms'])
       gap = _format_interval(interval)
       end_ts = datetime.utcnow()
       end_ts_clamped = _clamp_date(interval, end_ts)

+ 17 - 13
desktop/libs/dashboard/src/dashboard/models.py

@@ -17,6 +17,10 @@
 
 from __future__ import division
 
+from builtins import next
+from builtins import str
+from builtins import zip
+from builtins import object
 import collections
 import datetime
 import dateutil
@@ -278,16 +282,16 @@ class Collection2(object):
     try:
       schema_fields = api.fields(name)
       schema_fields = schema_fields['schema']['fields']
-    except Exception, e:
+    except Exception as e:
       LOG.warn('/luke call did not succeed: %s' % e)
       try:
         fields = api.schema_fields(name)
         schema_fields = Collection2._make_luke_from_schema_fields(fields)
-      except Exception, e:
+      except Exception as e:
         LOG.error('Could not access collection: %s' % e)
         return []
 
-    return sorted([self._make_field(field, attributes) for field, attributes in schema_fields.iteritems()])
+    return sorted([self._make_field(field, attributes) for field, attributes in schema_fields.items()])
 
   def update_data(self, post_data):
     data_dict = self.data
@@ -327,7 +331,7 @@ def get_facet_field(category, field, facets):
   else:
     id_pattern = '%(field)s-%(id)s'
 
-  facets = filter(lambda facet: facet['type'] == category and id_pattern % facet == field, facets)
+  facets = [facet for facet in facets if facet['type'] == category and id_pattern % facet == field]
 
   if facets:
     return facets[0]
@@ -490,7 +494,7 @@ def augment_solr_response(response, collection, query):
         }
         normalized_facets.append(facet)
       elif category == 'query' and response['facet_counts']['facet_queries']:
-        for name, value in response['facet_counts']['facet_queries'].iteritems():
+        for name, value in response['facet_counts']['facet_queries'].items():
           collection_facet = get_facet_field(category, name, collection['facets'])
           facet = {
             'id': collection_facet['id'],
@@ -624,7 +628,7 @@ def augment_solr_response(response, collection, query):
                   _series[legend].append(row[prev_last_seen_dim_col_index])
                   _series[legend].append(cell)
 
-            for _name, val in _series.iteritems():
+            for _name, val in _series.items():
               _c = range_pair2(
                                facet['field'],
                                _name,
@@ -675,7 +679,7 @@ def augment_solr_response(response, collection, query):
           counts = _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
           actual_dimension = sum([_f['aggregate']['function'] == 'count' for _f in collection_facet['properties']['facets']])
 
-          counts = filter(lambda a: len(a['fq_fields']) == actual_dimension, counts)
+          counts = [a for a in counts if len(a['fq_fields']) == actual_dimension]
 
         num_bucket = response['facets'][name]['numBuckets'] if 'numBuckets' in response['facets'][name] else len(response['facets'][name])
         facet = {
@@ -687,7 +691,7 @@ def augment_solr_response(response, collection, query):
           'extraSeries': extraSeries,
           'dimension': dimension,
           'response': {'response': {'start': 0, 'numFound': num_bucket}}, # Todo * nested buckets + offsets
-          'docs': [dict(zip(cols, row)) for row in rows],
+          'docs': [dict(list(zip(cols, row))) for row in rows],
           'fieldsAttributes': [Collection2._make_gridlayout_header_field({'name': col, 'type': 'aggr' if '(' in col else 'string'}) for col in cols],
           'multiselect': collection_facet['properties']['facets'][0].get('multiselect', True)
         }
@@ -708,7 +712,7 @@ def augment_solr_response(response, collection, query):
 
 def _get_agg_keys(counts):
   for count in counts:
-    keys = [key for key, value in count.items() if key.lower().startswith('agg_') or key.lower().startswith('dim_')]
+    keys = [key for key, value in list(count.items()) if key.lower().startswith('agg_') or key.lower().startswith('dim_')]
     if keys:
       return keys
   return []
@@ -727,7 +731,7 @@ def augment_response(collection, query, response):
         meta = {'type': 'link', 'link': doc['link']}
         link = get_data_link(meta)
 
-      for field, value in doc.iteritems():
+      for field, value in doc.items():
         if isinstance(value, numbers.Number):
           escaped_value = value
         elif field == '_childDocuments_': # Nested documents
@@ -748,7 +752,7 @@ def augment_response(collection, query, response):
         doc['numFound'] = _doc['numFound']
         del response['moreLikeThis'][doc['hueId']]
 
-  highlighted_fields = response.get('highlighting', {}).keys()
+  highlighted_fields = list(response.get('highlighting', {}).keys())
   if highlighted_fields and not query.get('download'):
     id_field = collection.get('idField')
     if id_field:
@@ -758,7 +762,7 @@ def augment_response(collection, query, response):
 
           if highlighting:
             escaped_highlighting = {}
-            for field, hls in highlighting.iteritems():
+            for field, hls in highlighting.items():
               _hls = [escape(smart_unicode(hl, errors='replace')).replace('&lt;em&gt;', '<em>').replace('&lt;/em&gt;', '</em>') for hl in hls]
               escaped_highlighting[field] = _hls[0] if len(_hls) == 1 else _hls
 
@@ -843,7 +847,7 @@ def __augment_stats_2d(counts, label, fq_fields, fq_values, fq_filter, _selected
         # List nested fields
         _agg_keys = []
         if agg_key in bucket and bucket[agg_key]['buckets']: # Protect against empty buckets
-          for key, value in bucket[agg_key]['buckets'][0].items():
+          for key, value in list(bucket[agg_key]['buckets'][0].items()):
             if key.lower().startswith('agg_') or key.lower().startswith('dim_'):
               _agg_keys.append(key)
         _agg_keys.sort(key=lambda a: a[4:])

+ 2 - 1
desktop/libs/dashboard/src/dashboard/tests.py

@@ -16,6 +16,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from builtins import object
 import json
 
 from django.contrib.auth.models import User
@@ -44,7 +45,7 @@ def test_ranges():
   assert_equal((8000000, 9000000), _round_number_range(9045352))
 
 
-class MockResource():
+class MockResource(object):
   RESPONSE = None
 
   def __init__(self, client):

+ 1 - 1
desktop/libs/dashboard/src/dashboard/views.py

@@ -101,7 +101,7 @@ def index(request, is_mobile=False):
     else:
       collection_doc.doc.get().can_read_or_exception(request.user)
     collection = Collection2(request.user, document=collection_doc)
-  except Exception, e:
+  except Exception as e:
     raise PopupException(e, title=_("Dashboard does not exist or you don't have the permission to access it."))
 
   query = {'qs': [{'q': ''}], 'fqs': [], 'start': 0}