From 9f171da570d233488db25d522d9d7b186ed0212c Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 10 May 2016 10:11:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcli=20=E7=AB=AF=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=E5=88=97=E8=A1=A8=E6=98=BE=E7=A4=BA=20(#226)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modify(jperm) 授权列表模糊搜索 修改授权规则搜索为模糊搜索 * fix(cli nav align) Max Hostname length 30, else will be truncate. --- connect.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/connect.py b/connect.py index bb7af929c..ac8010c32 100755 --- a/connect.py +++ b/connect.py @@ -508,16 +508,30 @@ class Nav(object): # 如果没有输入就展现所有 self.search_result = self.perm_assets + @staticmethod + def truncate_str(str_, length=30): + str_ = str_.decode('utf-8') + if len(str_) > length: + return str_[:14] + '..' + str_[-14:] + else: + return str_ + + @staticmethod + def get_max_asset_property_length(assets, property_='hostname'): + return max([len(getattr(asset, property_)) for asset in assets]) + def print_search_result(self): - color_print('[%-3s] %-12s %-15s %-5s %-10s %s' % ('ID', '主机名', 'IP', '端口', '系统用户', '备注'), 'title') + hostname_max_length = self.get_max_asset_property_length(self.search_result) + line = '[%-3s] %-16s %-5s %-' + str(hostname_max_length) + 's %-10s %s' + color_print(line % ('ID', 'IP', 'Port', 'Hostname', 'SysUser', 'Comment'), 'title') if hasattr(self.search_result, '__iter__'): for index, asset in enumerate(self.search_result): # 获取该资产信息 asset_info = get_asset_info(asset) # 获取该资产包含的角色 role = [str(role.name) for role in self.user_perm.get('asset').get(asset).get('role')] - print '[%-3s] %-15s %-15s %-5s %-10s %s' % (index, asset.hostname, asset.ip, asset_info.get('port'), - role, asset.comment) + print line % (index, asset.ip, asset_info.get('port'), + self.truncate_str(asset.hostname), str(role).replace("'", ''), asset.comment) print def try_connect(self):