Unverified Commit 1eb34b69 authored by 老广's avatar 老广 Committed by GitHub

Py2 (#152)

* [Update] 优化日志

* [Update] py2

* [Update] py2

* [Bugfix] 修改bug

* [Update] 修改获取逻辑

* [Update] 修改msg
parent e3e031ec
......@@ -94,7 +94,7 @@ class Config(dict):
def __init__(self, root_path, defaults=None):
self.defaults = defaults or {}
self.root_path = root_path
super().__init__({})
super(Config, self).__init__({})
def from_envvar(self, variable_name, silent=False):
"""Loads a configuration from an environment variable pointing to
......@@ -272,7 +272,7 @@ class Config(dict):
def __getitem__(self, item):
try:
value = super().__getitem__(item)
value = super(Config, self).__getitem__(item)
except KeyError:
value = None
if value is not None:
......
......@@ -46,7 +46,7 @@ class ProxyNamespace(BaseNamespace):
def on_connect(self):
logger.debug("On connect event trigger")
self.get_current_user()
super().on_connect()
super(ProxyNamespace, self).on_connect()
self.new_connection()
def on_host(self, message):
......
......@@ -29,20 +29,20 @@ PROXY = 'proxy'
class InteractiveServer:
_sentinel = object()
_user_assets_cached = {}
def __init__(self, client):
self.client = client
self.closed = False
self._results = None
self.nodes = None
self.offset = 0
self.limit = 100
self.assets = []
self.finish = False
self.assets = None
self.get_user_assets_finished = False
self.page = 1
self.total_assets = 0 # 用户被授权的所有资产
self.total_asset_count = 0 # 用户被授权的所有资产数量
self.total_count = 0 # 分页展示中的资产总数量
self.node_tree = None # 授权节点树
self.load_user_assets_from_cache()
self.get_user_assets_async()
self.get_user_nodes_async()
......@@ -144,7 +144,7 @@ class InteractiveServer:
def search_and_display_assets(self, q):
assets = self.search_assets(q)
self.display_assets(assets)
self.display_assets_paging(assets)
def search_and_proxy_assets(self, opt):
assets = self.search_assets(opt)
......@@ -159,25 +159,13 @@ class InteractiveServer:
return
self.proxy(asset)
else:
self.display_assets(assets)
self.display_assets_paging(assets)
def refresh_assets_nodes(self):
self.get_user_assets_async()
self.get_user_nodes_async()
def search_assets(self, q):
if self.finish:
assets = self.search_assets_from_local(q)
else:
assets = self.search_assets_from_server(q)
return assets
def search_assets_from_server(self, q):
assets = app_service.get_search_user_granted_assets(self.client.user, q)
assets = self.filter_system_users(assets)
return assets
def search_assets_from_local(self, q):
result = []
# 所有的
......@@ -205,18 +193,17 @@ class InteractiveServer:
# Display assets
#
def display_assets(self, assets=None):
if assets is None:
while not self.assets and not self.finish:
time.sleep(0.2)
assets = self.assets
self.display_assets_paging(assets)
def display_assets(self):
while self.assets is None and not self.get_user_assets_finished:
time.sleep(0.5)
if self.assets:
self.display_assets_paging(self.assets)
def display_assets_paging(self, assets):
if len(assets) == 0:
self.client.send(wr(_("No Assets"), before=0))
return
self.total_count = self.total_assets if assets is self.assets else len(assets)
self.total_count = len(assets)
action = None
gen = self._page_generator(assets)
......@@ -237,14 +224,8 @@ class InteractiveServer:
start, page = 0, 1
while True:
_assets = assets[start:start+self.page_size]
# 等待加载
if (assets is self.assets) and (not self.finish) and (not self.need_paging):
time.sleep(1)
continue
# 最后一页
elif _assets and (page == self.total_pages) and (
assets is not self.assets
or (assets is self.assets and self.finish)):
if page == self.total_pages:
return page, _assets
# 执行动作
else:
......@@ -329,36 +310,27 @@ class InteractiveServer:
# Get assets
#
def load_user_assets_from_cache(self):
assets = self.__class__._user_assets_cached.get(
self.client.user.id
)
self.assets = assets
if assets:
self.total_asset_count = len(assets)
def set_user_assets_cache(self, assets):
self.__class__._user_assets_cached[self.client.user.id] = assets
def get_user_assets_async(self):
if self.need_paging:
thread = threading.Thread(target=self.get_user_assets_paging)
else:
thread = threading.Thread(target=self.get_user_assets_direct)
thread = threading.Thread(target=self.get_user_assets)
thread.start()
def get_user_assets_direct(self):
def get_user_assets(self):
assets = app_service.get_user_assets(self.client.user)
assets = self.filter_system_users(assets)
self.assets = assets
self.total_assets = len(assets)
self.finish = True
def get_user_assets_paging(self):
while not self.closed:
assets, total = app_service.get_user_assets_paging(
self.client.user, offset=self.offset, limit=self.limit
)
if not assets:
logger.info('Get user assets paging async finished.')
self.finish = True
break
logger.info('Get user assets paging async: {}'.format(len(assets)))
assets = self.filter_system_users(assets)
self.total_assets = total
self.assets.extend(assets)
self.offset += self.limit
self.set_user_assets_cache(assets)
self.load_user_assets_from_cache()
self.get_user_assets_finished = True
#
# Nodes
#
......@@ -413,7 +385,7 @@ class InteractiveServer:
return
assets = self.nodes[_id-1].assets_granted
self.display_assets(assets)
self.display_assets_paging(assets)
#
# System users
......
......@@ -85,7 +85,7 @@ class ProxyServer:
)
def get_server_conn(self):
logger.info("Connect to {}".format(self.asset.hostname))
logger.info("Connect to {}:{} ...".format(self.asset.hostname, self.asset.port))
self.send_connecting_message()
if not self.validate_permission():
self.client.send(warning(_('No permission')))
......
......@@ -82,7 +82,7 @@ class ReplayRecorder(object):
self.upload_replay(session_id, times-1)
else:
msg = 'Success push replay file: {}'.format(session_id)
logger.info(msg)
logger.debug(msg)
self.finish_replay(3, session_id)
os.unlink(self.file_path)
return True
......@@ -101,11 +101,11 @@ class ReplayRecorder(object):
if app_service.finish_replay(session_id):
logger.info(
"Success finish session {}'s replay ".format(session_id)
"Success finished session {}'s replay ".format(session_id)
)
return True
else:
msg = "Failed finish session {}'s replay, try {} times"
msg = "Failed finished session {}'s replay, try {} times"
logger.error(msg.format(session_id, times))
return self.finish_replay(times - 1, session_id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment