Преглед изворни кода

[indexer] Improve parsing of binary or non unicode data

Adding tests.
Romain Rigaux пре 10 година
родитељ
комит
df2c2e6

+ 39 - 0
desktop/libs/indexer/src/indexer/test_utils.py

@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import StringIO
+
+from nose.tools import assert_equal
+
+from indexer.utils import field_values_from_separated_file
+
+
+def test_get_ensemble():
+  # Non ascii
+  data = StringIO.StringIO('fieldA\nrel=""nofollow"">Twitter for Péché')
+  result = list(field_values_from_separated_file(data, delimiter='\t', quote_character='"'))
+  assert_equal(u'rel=""nofollow"">Twitter for Péché', result[0]['fieldA'])
+
+  data = StringIO.StringIO('fieldA\nrel=""nofollow"">Twitter for BlackBerry®')
+  result = list(field_values_from_separated_file(data, delimiter='\t', quote_character='"'))
+  assert_equal(u'rel=""nofollow"">Twitter for BlackBerry®', result[0]['fieldA'])
+
+  # Bad binary
+  data = StringIO.StringIO('fieldA\naaa\x80\x02\x03')
+  result = list(field_values_from_separated_file(data, delimiter='\t', quote_character='"'))
+  assert_equal(u'aaa\x02\x03', result[0]['fieldA'])

+ 3 - 6
desktop/libs/indexer/src/indexer/utils.py

@@ -197,11 +197,6 @@ def get_type_from_morphline_type(morphline_type):
     return 'string'
 
 
-def utf_8_encoder(unicode_csv_data):
-  for line in unicode_csv_data:
-    yield force_unicode(line, errors='ignore') # Even 'replace' seems to break the DictReader
-
-
 def field_values_from_separated_file(fh, delimiter, quote_character, fields=None):
   if fields is None:
     field_names = None
@@ -257,10 +252,12 @@ def field_values_from_separated_file(fh, delimiter, quote_character, fields=None
       headers = [name.strip() for name in headers]
 
     # User dict reader
-    reader = csv.DictReader(utf_8_encoder(csvfile), fieldnames=headers, delimiter=smart_str(delimiter), quotechar=smart_str(quote_character))
+    reader = csv.DictReader(csvfile, fieldnames=headers, delimiter=smart_str(delimiter), quotechar=smart_str(quote_character))
 
     remove_keys = None
     for row in reader:
+      row = dict([(force_unicode(k), force_unicode(v, errors='ignore')) for k, v in row.iteritems()]) # Get rid of invalid binary chars and convert to unicode from DictReader
+
       # Remove keys that aren't in collection
       if remove_keys is None:
         if field_names is None: