Skip to content

Commit

Permalink
Handling UTF-8 unicode charaters.
Browse files Browse the repository at this point in the history
Code changes done to handle utf-8 unicode characters.

Signed-off-by: Archana Singh <[email protected]>
  • Loading branch information
Archana Singh authored and danielhb committed Mar 16, 2016
1 parent 64631a1 commit aebe3ae
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 87 deletions.
30 changes: 19 additions & 11 deletions model/cioignore.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# Project Ginger S390x
#
Expand Down Expand Up @@ -64,8 +65,8 @@ def remove(self, name, devices):
raise InvalidParameter('GS390XINVINPUT', {'reason': 'input must '
'be of type'
' list'})

wok_log.info('Removing devices %s from ignore list' % devices)
wok_log.info('Create task for removing devices \"% s\" from ignore'
'list' % devices)
taskid = add_task('/plugins/gingers390x/cioignore/remove',
_remove_devices, self.objstore, devices)
return self.task.lookup(taskid)
Expand All @@ -88,8 +89,10 @@ def _remove_devices(cb, devices):
wok_log.info('Removing devices %s from ignore list' % devices)
failed_devices = {}
for device in devices:
if isinstance(device, unicode):
device = device.encode('utf-8')
device = str(device)
if not device.isspace():
if device and not device.isspace():
if '-' in device:
# if range, remove space if any before or after '-'
device = device.replace(' ', '')
Expand All @@ -107,16 +110,21 @@ def _remove_devices(cb, devices):
' device id is empty')

if failed_devices:
wok_log.error('failed to remove devices %s from'
' ignore list', failed_devices)
# to handle unicode charater
str_failed_devices = ', '.join('%s: %s' % (device, err)
for (device, err)
in failed_devices.items())
wok_log.error('failed to remove devices \"%s\" from'
' ignore list', str_failed_devices)
raise OperationFailed('GS390XIOIG002E',
{'failed_devices': failed_devices})
wok_log.info('Successfully removed devices %s from'
' ignore list' % devices)
cb('Successfully removed devices %s from ignore'
' list' % devices, True)
{'failed_devices': str_failed_devices})
str_devices = ', '.join(devices)
wok_log.info('Successfully removed devices \"%s\" from'
' ignore list' % str_devices)
cb('Successfully removed devices \"%s\" from ignore'
' list' % str_devices, True)
except Exception as e:
cb(e.__str__(), False)
cb(e.message, False)


def _parse_ignore_output(cmd_out):
Expand Down
2 changes: 1 addition & 1 deletion model/fc_luns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# Project Ginger S390x
#
Expand All @@ -17,7 +18,6 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


import threading
import utils

Expand Down
4 changes: 3 additions & 1 deletion model/model_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# Project Ginger S390x
#
Expand All @@ -16,7 +17,8 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#


import glob
import re

Expand Down
40 changes: 30 additions & 10 deletions model/nwdevices.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# Project Ginger S390x
#
Expand Down Expand Up @@ -136,6 +137,10 @@ def configure(self, interface):
:return: returns task json
"""
wok_log.info('Configuring network device %s' % interface)
if isinstance(interface, unicode):
# as str() cannot be done on non ascii unicode
# it needs encoded value
interface = interface.encode('utf-8')
device = str(interface).strip()
if ENCCW in device:
# filtering out device id from interface
Expand All @@ -153,6 +158,10 @@ def unconfigure(self, interface):
:return: returns task json
"""
wok_log.info('Un-configuring network device %s' % interface)
if isinstance(interface, unicode):
# as str() cannot be done on non ascii unicode
# it needs encoded value
interface = interface.encode('utf-8')
device = str(interface).strip()
if ENCCW in device:
# filtering out device id from interface
Expand Down Expand Up @@ -310,7 +319,7 @@ def _configure_interface(cb, interface):
rollback.commitAll()
cb('Successfully configured network device %s' % interface, True)
except Exception as e:
cb(e.__str__(), False)
cb(e.message, False)


def _unconfigure_interface(cb, interface):
Expand All @@ -331,7 +340,7 @@ def _unconfigure_interface(cb, interface):
rollback.commitAll()
cb('Successfully un-configured network device %s' % interface, True)
except Exception as e:
cb(e.__str__(), False)
cb(e.message, False)


def _validate_device(interface):
Expand All @@ -343,6 +352,8 @@ def _validate_device(interface):
"""
wok_log.info('Validating network interface %s' % interface)
pattern_with_dot = r'^\d\.\d\.[0-9a-fA-F]{4}$'
if isinstance(interface, unicode):
interface = interface.encode('utf-8')
if interface and not str(interface).isspace():
interface = str(interface).strip()
if ENCCW in interface:
Expand Down Expand Up @@ -376,16 +387,19 @@ def _create_ifcfg_file(interface):
try:
ifcfg_file = open(ifcfg_file_path, 'w+')
ifcfg_file.close()
if isinstance(ifcfg_file_path, unicode):
# as os.system default encoding is ascii and not utf8
ifcfg_file_path.encode('utf-8')
os.system('chmod 644 ' + ifcfg_file_path)
wok_log.info('created file %s for network device %s'
% (ifcfg_file_path, interface))
except Exception as e:
wok_log.error('failed to create file %s for network device %s. '
'Error: %s' % (ifcfg_file_path, interface, e.__str__()))
'Error: %s' % (ifcfg_file_path, interface, e.message))
raise OperationFailed("GS390XIONW005E",
{'ifcfg_file_path': ifcfg_file_path,
'device': interface,
'error': e.__str__()})
'error': e.message})


def _bring_online(interface):
Expand Down Expand Up @@ -439,6 +453,8 @@ def _persist_interface(interface):
"""
wok_log.info('persisting network device %s in ifcfg file' % interface)
ifcfg_file_path = '/' + ifcfg_path.replace('<deviceid>', interface)
if isinstance(ifcfg_file_path, unicode):
ifcfg_file_path = ifcfg_file_path.encode('utf-8')
if os.path.isfile(ifcfg_file_path):
_write_ifcfg_params(interface)
else:
Expand All @@ -459,16 +475,18 @@ def _unpersist_interface(interface):
"""
wok_log.info('un persisting network device %s' % interface)
ifcfg_file_path = '/' + ifcfg_path.replace('<deviceid>', interface)
if isinstance(ifcfg_file_path, unicode):
ifcfg_file_path = ifcfg_file_path.encode('utf-8')
try:
if os.path.isfile(ifcfg_file_path):
os.remove(ifcfg_file_path)
except Exception as e:
wok_log.error('Failed to remove file %s. Error: %s'
% (ifcfg_file_path, e.__str__()))
% (ifcfg_file_path, e.message))
raise OperationFailed('GS390XIONW004E',
{'ifcfg_file_path': ifcfg_file_path,
'device': interface,
'error': e.__str__()})
'error': e.message})
wok_log.info('successfully removed ifcfg file %s to unpersist '
'network device %s' % (ifcfg_file_path, interface))

Expand Down Expand Up @@ -504,12 +522,12 @@ def _write_ifcfg_params(interface):
parser.set(path, value)
parser.save()
except Exception as e:
wok_log.error('Failedd to write device attributes to ifcfg file '
'using augeas tool. Error: %s' % e.__str__())
wok_log.error('Failed to write device attributes to ifcfg file '
'using augeas tool. Error: %s' % e.message)
raise OperationFailed('GS390XIONW002E',
{'device': interface,
'ifcfg_file_path': ifcfg_file_path,
'error': e.__str__()})
'error': e.message})
finally:
if parser:
del parser
Expand All @@ -526,11 +544,13 @@ def _is_interface_online(interface):
"""
wok_log.info('checking if the network device %s is configured' % interface)
online_file_path = '/sys/bus/ccwgroup/devices/' + interface + '/online'
if isinstance(online_file_path, unicode):
online_file_path = online_file_path.encode('utf-8')
if os.path.isfile(online_file_path):
online_file = None
try:
online_file = open(online_file_path)
value = online_file.readline()
value = online_file.read()
if value and value.strip() == '1':
wok_log.info('network device %s is '
'configured' % interface)
Expand Down
Loading

0 comments on commit aebe3ae

Please sign in to comment.