Python 3 support in zanata_userinfo.py

* UTF-8 characters need to be handled differently with Python 2 and 3.
  In Python 2 unicode strings must be encoded into ASCII strings.
  Python 3 is naturely unicode-aware and there is no need to encode
  unicode strings before passing the CSV writer.
* CSV writer in Python3 expects a file is open as
  non-binary mode. In Python 2, binary mode is expected.
* Move the encoding logic to CSV writer. It is a bit tricky to have
  encoded strings (i.e., byte string) in regular data structure.

Co-Authored-By: Akihiro Motoki <amotoki@gmail.com>
Change-Id: I9d678d86b540d8b188ec0bf3240628a288f7ad3c
Implements: blueprint python35-support
This commit is contained in:
Ian Y. Choi 2017-06-08 00:53:31 +09:00
parent 7c4826a0d9
commit 8689d0b79a

View File

@ -21,6 +21,7 @@ import os
import sys
from oslo_log import log as logging
import six
import yaml
from ZanataUtils import IniConfig
from ZanataUtils import ZanataRestService
@ -129,7 +130,7 @@ def get_zanata_userdata(zc, verify, role, language_teams):
user_data = accounts.get_account_data(user_id)
if user_data:
user['name'] = user_data['name'].encode('utf-8')
user['name'] = user_data['name']
user['email'] = user_data['email']
return users
@ -144,13 +145,18 @@ def write_userdata_to_file(users, output_file):
def _write_userdata_to_csvfile(userdata, output_file):
with open(output_file, 'wb') as csvfile:
mode = 'wb' if six.PY2 else 'w'
with open(output_file, mode) as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['user_id', 'lang_code', 'lang',
'name', 'email'])
for data in userdata:
writer.writerow([data['user_id'], data['lang_code'],
data['lang'], data['name'], data['email']])
d = [data['user_id'], data['lang_code'],
data['lang'], data['name'], data['email']]
if six.PY2:
# In Python2 we need to encode unicode strings into strings.
d = [s.encode('utf-8') for s in d]
writer.writerow(d)
def _comma_separated_list(s):