Skip to content
Snippets Groups Projects
Commit e546bec0 authored by jkerdreu's avatar jkerdreu
Browse files

- Linting ... sucks but modern editor light like a christmass tree without it.


git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@3048 b32b6428-25c9-4566-ad07-03861ab6144f
parent 6dc01c02
Branches
No related tags found
No related merge requests found
......@@ -10,7 +10,8 @@ if sys.argv[0].endswith('pkgrun'):
# right now, some packages depend on gevent, so we need to import it here.
# this is only needed for the pkgrun command
try:
from gevent import monkey;monkey.patch_all(thread=False)
from gevent import monkey
monkey.patch_all(thread=False)
# print("Loaded gevent")
except ModuleNotFoundError:
pass
......@@ -33,11 +34,13 @@ from tabulate import tabulate
import pprint
import shutil # needed by the tail command
HIDE_ACTION = ['get_attributes', 'get_description', 'get_keys_values', 'get_devices', 'is_alive']
TABLE_STYLE = 'psql'
LINE = "="*78
DB_DEV_TYPE = "metadatadb.basic"
class Colors(enum.Enum):
DEFAULT = fore.WHITE
# ALIVE = fore.LIGHT_GRAY
......@@ -62,11 +65,10 @@ class Colors(enum.Enum):
INFO = fore.CYAN
DB = fore.SPRING_GREEN_1
def __str__(self):
return self.value
class DeviceInfo(object):
def __init__(self):
self.alive = False
......@@ -79,10 +81,14 @@ class DeviceInfo(object):
self.displayed = False
def ready(self):
if self.address == None: return False
if self.dev_type == None: return False
if self.description == None: return False
if self.attributes == None: return False
if self.address is None:
return False
if self.dev_type is None:
return False
if self.description is None:
return False
if self.attributes is None:
return False
return True
def display(self, color=True):
......@@ -244,7 +250,7 @@ class ToolboxHelper(object):
addr = None
if value:
addr = tools.get_uuid(value)
if addr == None:
if addr is None:
self.error(f"Invalid address: {value}")
return addr
......@@ -280,7 +286,8 @@ class ToolboxHelper(object):
def display_device(self, dev):
# show device only once
if dev.displayed: return
if dev.displayed:
return
if self.options.no_color:
dev.normal_display()
......@@ -290,7 +297,7 @@ class ToolboxHelper(object):
def is_ready(self, dev):
if self.db_server:
if dev.db == None:
if dev.db is None:
return False
return dev.ready()
......@@ -300,9 +307,11 @@ class ToolboxHelper(object):
def dump_msg(self, msg):
color = not self.options.no_color
color_value = self.color_for_msg(msg)
if color: print(color_value,end='')
if color:
print(color_value, end='')
msg.dump()
if color: print(style.RESET,end='')
if color:
print(style.RESET, end='')
def color_for_msg(self, msg):
color_value = Colors.DEFAULT
......@@ -323,12 +332,12 @@ class ToolboxHelper(object):
def parse_msg(self, msg):
""" default parser used for info/walker"""
target = self.get_device(msg.source)
if target == None:
if target is None:
target = DeviceInfo()
target.address = msg.source
target.dev_type = msg.dev_type
self.add_device(target)
if target.dev_type==None:
if target.dev_type is None:
target.dev_type = msg.dev_type
if msg.is_get_attribute_reply():
target.attributes = msg.body
......@@ -348,25 +357,26 @@ class ToolboxHelper(object):
return tmp
def request_info(self, addr):
self.engine.send_get_description(self.device,[addr,])
self.engine.send_get_attributes(self.device,[addr,])
self.engine.send_get_description(self.device, [addr])
self.engine.send_get_attributes(self.device, [addr])
def request_is_alive(self, addr=None, dev_type=None):
if addr:
self.engine.send_is_alive(self.device,[addr,])
self.engine.send_is_alive(self.device, [addr])
elif dev_type:
self.engine.send_is_alive(self.device,dev_types=[dev_type,])
self.engine.send_is_alive(self.device, dev_types=[dev_type])
else:
self.engine.send_is_alive(self.device)
def request_action(self, addr, action, body=None):
self.engine.send_request(self.device, [addr,],action,body)
self.engine.send_request(self.device, [addr], action, body)
#####################################################
# db server
#####################################################
def find_db_callback(self, msg):
if not match_dev_type(msg,DB_DEV_TYPE):return
if not match_dev_type(msg, DB_DEV_TYPE):
return
# new db server found
if msg.is_alive():
self.db_server = msg.source
......@@ -374,7 +384,7 @@ class ToolboxHelper(object):
async def find_db_server(self):
self.engine.subscribe(self.find_db_callback)
self.engine.send_is_alive(self.device,dev_types=[DB_DEV_TYPE,])
self.engine.send_is_alive(self.device, dev_types=[DB_DEV_TYPE])
await asyncio.wait([self.db_server_found.wait(), ], timeout=0.3)
self.engine.unsubscribe(self.find_db_callback)
......@@ -386,7 +396,6 @@ class ToolboxHelper(object):
if self.db_server:
self.engine.send_request(self.device, [self.db_server, ], "get_devices", {'key': key, 'value': value})
def is_db_reply(self, msg):
if match_dev_type(msg, DB_DEV_TYPE) and msg.is_reply() and self.device.address in msg.targets:
return True
......@@ -436,12 +445,15 @@ class ToolboxHelper(object):
self.parser.print_help()
exit(1)
def colorize(color, text):
return f"{color}{text}{style.RESET}"
def now():
return time.time()
def match_dev_type(msg, dev_type):
if dev_type == 'any.any':
return True
......@@ -454,6 +466,7 @@ def match_dev_type(msg,dev_type):
return True
return False
def match_address(msg, addr):
if (msg.source == addr) or (addr in msg.targets):
return True
......@@ -476,8 +489,10 @@ def dumper():
async def dumper_callback(msg):
# filter by address or dev_type
if target and not match_address(msg,target):return
if dev_type and not match_dev_type(msg, dev_type):return
if target and not match_address(msg, target):
return
if dev_type and not match_dev_type(msg, dev_type):
return
# dump message
helper.dump_msg(msg)
......@@ -498,12 +513,14 @@ def is_alive():
(eng, dev) = helper.setup_basic()
async def alive_callback(msg):
if (msg.source == dev.address) or (msg.is_alive() == False): return
if match_dev_type(msg,dev_type) == False: return
if (msg.source == dev.address) or (msg.is_alive() is False):
return
if match_dev_type(msg, dev_type) is False:
return
# idle detectiong
helper.update_idle()
if helper.get_device(msg.source) == None:
if helper.get_device(msg.source) is None:
helper.parse_msg(msg)
if color:
print(f"{colorize(Colors.ADDR,msg.source)}: {colorize(Colors.DEV_TYPE,msg.dev_type)}")
......@@ -530,7 +547,7 @@ def info():
if len(args) != 1:
helper.error("empty address")
target = tools.get_uuid(args[0])
if target == None:
if target is None:
helper.error("Invalid address: %s" % args[0])
(eng, dev) = helper.setup_basic()
......@@ -542,7 +559,8 @@ def info():
def info_callback(msg, addr=target):
# collecting description and attributes
if msg.source != addr: return
if msg.source != addr:
return
found = helper.parse_msg(msg)
ready_to_show(found)
......@@ -580,9 +598,12 @@ def walker():
helper.display_device(dev)
async def walker_callback(msg):
if msg.source == dev.address: return
if match_dev_type(msg,DB_DEV_TYPE):return
if match_dev_type(msg,dev_type) == False: return
if msg.source == dev.address:
return
if match_dev_type(msg, DB_DEV_TYPE):
return
if match_dev_type(msg, dev_type) is False:
return
found = helper.parse_msg(msg)
helper.update_idle()
......@@ -625,15 +646,22 @@ def log():
eng = helper.setup_engine()
def log_callback(msg):
if msg.is_alive() or (msg.action in HIDE_ACTION):return
if target and not match_address(msg,target):return
if dev_type and not match_dev_type(msg, dev_type):return
if msg.is_alive() or (msg.action in HIDE_ACTION):
return
if target and not match_address(msg, target):
return
if dev_type and not match_dev_type(msg, dev_type):
return
color_value = Colors.DEFAULT
if msg.is_attributes_change(): color_value = Colors.ATTRIBUTS
elif msg.is_notify(): color_value=Colors.NOTIFY
elif msg.is_request(): color_value=Colors.REQUEST
elif msg.is_reply(): color_value=Colors.REPLY
if msg.is_attributes_change():
color_value = Colors.ATTRIBUTS
elif msg.is_notify():
color_value = Colors.NOTIFY
elif msg.is_request():
color_value = Colors.REQUEST
elif msg.is_reply():
color_value = Colors.REPLY
if color:
dump = f"{Colors.DEFAULT}{time.ctime()} {Colors.ADDR}{msg.source} {Colors.DEV_TYPE}{msg.dev_type}\t{color_value}{msg.action} {msg.body}{style.RESET}"
......@@ -737,8 +765,9 @@ def clean_db():
db_devices = []
def alive_callback(msg):
if (msg.source == dev.address) or (msg.is_alive() == False): return
if helper.get_device(msg.source) == None:
if (msg.source == dev.address) or (msg.is_alive() is False):
return
if helper.get_device(msg.source) is None:
helper.parse_msg(msg)
# print(msg)
......@@ -809,7 +838,8 @@ def send():
(eng, dev) = helper.setup_basic()
def action_callback(msg):
if msg.is_alive():return
if msg.is_alive():
return
if msg.source == target:
helper.dump_msg(msg)
helper.exit_event.set()
......@@ -850,18 +880,27 @@ def tail():
eng = helper.setup_engine()
def tail_callback(msg):
if mode > 0 and msg.is_alive():return
if target and not match_address(msg,target):return
if dev_type and not match_dev_type(msg, dev_type):return
if mode > 1 and msg.action in HIDE_ACTION: return
if mode > 2 and msg.is_reply(): return
if mode > 3 and msg.is_request(): return
if mode > 0 and msg.is_alive():
return
if target and not match_address(msg, target):
return
if dev_type and not match_dev_type(msg, dev_type):
return
if mode > 1 and msg.action in HIDE_ACTION:
return
if mode > 2 and msg.is_reply():
return
if mode > 3 and msg.is_request():
return
color_value = helper.color_for_msg(msg)
schem = '*'
if msg.msg_type == MessageType.REQUEST.value: schem = '>'
elif msg.msg_type == MessageType.REPLY.value: schem = '<'
elif msg.msg_type == MessageType.NOTIFY.value: schem = '='
if msg.msg_type == MessageType.REQUEST.value:
schem = '>'
elif msg.msg_type == MessageType.REPLY.value:
schem = '<'
elif msg.msg_type == MessageType.NOTIFY.value:
schem = '='
targets = [tools.reduce_addr(addr) for addr in msg.targets]
tmp = shutil.get_terminal_size()[0] - (2 + 18 + 36 + 20 + 16 + 7)
......@@ -903,12 +942,12 @@ def pkgrun():
logger.critical("Unable to load module: %s" % xaal_mod)
continue
if hasattr(mod,'setup') == False:
if hasattr(mod, 'setup') is False:
logger.critical("Unable to find setup %s" % xaal_mod)
continue
logger.info(f"{xaal_mod} loaded")
result = mod.setup(eng)
if result != True:
if result is not True:
logger.critical("something goes wrong with package: %s" % xaal_mod)
load_pkgs()
......@@ -955,3 +994,4 @@ def shell():
print("* Ending Engine")
eng.shutdown()
print("* Bye bye")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment