From 600e47d4aa71de2eada4e260a41968f5d7f9cc2d Mon Sep 17 00:00:00 2001 From: hochikong <1097225749@qq.com> Date: Mon, 5 Sep 2016 14:55:11 +0800 Subject: Fix some logic error in commit() Fix the readme add a new exception rewrite the error handle in router.py --- vymgmt/mgmt_common.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'vymgmt/mgmt_common.py') diff --git a/vymgmt/mgmt_common.py b/vymgmt/mgmt_common.py index 1ccb96a..1153c54 100644 --- a/vymgmt/mgmt_common.py +++ b/vymgmt/mgmt_common.py @@ -1,5 +1,7 @@ # Copyright (c) 2016 Hochikong +CODEC = 'utf8' + def messenger(obj, config): """This method used for sending configuration to VyOS @@ -11,8 +13,9 @@ def messenger(obj, config): try: obj.sendline(config) obj.prompt() - if len(obj.before) > obj.before.index('\r\n') + 2: - return obj.before + result = decodetool(obj.before, CODEC) + if len(result) > result.index('\r\n') + 2: + return result else: return "Result : Configured successfully" except Exception as e: @@ -26,12 +29,34 @@ def committer(obj, config): :param config: A configuration string :return: A message or an error """ + exception_string = "enhanced syslogd: rsyslogd" try: obj.sendline(config) obj.prompt() - if len(obj.before) > obj.before.index('\r\n') + 2: - return obj.before + result = decodetool(obj.before, CODEC) + if len(result) > result.index('\r\n') + 2: + if exception_string in result: + return "Result : Commit successfully" + else: + return result else: return "Result : Commit successfully" except Exception as e: return e + + +def decodetool(target, codec): + """This method is used for decoding obj.before to string when run this + library under python3 + + :param target: The obj.before + :param codec: The codec use to decode + :return: + """ + try: + if type(target) == str: + return target + if type(target) == bytes: + return target.decode(codec) + except Exception as e: + return e -- cgit v1.2.3