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

- Better command line options handling

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2801 b32b6428-25c9-4566-ad07-03861ab6144f
parent 96a2ff4b
Branches
No related tags found
No related merge requests found
......@@ -55,7 +55,14 @@ class DeviceInfo(object):
if self.attributes == None: return False
return True
def show(self):
def display(self,display='color'):
if display=='color':
self.color_display()
else:
self.normal_display()
def color_display(self):
# info & description
r = []
r.append(['Informations',''])
......@@ -84,6 +91,32 @@ class DeviceInfo(object):
r.append([k,tmp])
print(tabulate(r,tablefmt=TABLE_STYLE))
def normal_display(self):
# info & description
r = []
r.append(['Informations',''])
r.append(['============',''])
r.append(["alive", self.alive])
r.append(['dev_type',self.dev_type])
r.append(['address', self.address])
for k,v in self.description.items():
r.append([k,v])
# attributes
if len(self.attributes) > 0:
# tabulate has no minimal width so used this trick
r.append(['-'*22,'-'*46])
r.append(['Attributes',''])
r.append(['==========',''])
for k,v in self.attributes.items():
v=pprint.pformat(v,width=55).split('\n')
tmp = ''
for line in v:
tmp = tmp + line + '\n'
r.append([k,tmp])
print(tabulate(r,tablefmt=TABLE_STYLE))
class ToolboxHelper(object):
def __init__(self) -> None:
......@@ -183,7 +216,7 @@ class ToolboxHelper(object):
def engine_wait(self,timeout=2):
""" run the engine until timeout """
self.engine.add_timer(self._quit,timeout)
self.engine.add_timer(self.quit,timeout)
self.engine.run()
#####################################################
......@@ -198,9 +231,9 @@ class ToolboxHelper(object):
await asyncio.sleep(0.1)
if now() - self.last_msg_time > 0.3:
break
self._quit()
self.quit()
def _quit(self):
def quit(self):
print()
print(LINE)
print(f"Found devices: {len(self.devices)}")
......@@ -214,6 +247,25 @@ class ToolboxHelper(object):
self.parser.print_help()
exit(1)
def get_filter_address(self):
addr = None
value = self.options.filter_address
if value:
addr = tools.get_uuid(value)
if addr == None:
helper.error(f"Invalid address: {value}")
return addr
def get_filter_devtype(self):
type_ = 'any.any'
value = self.options.filter_type
if value:
if not tools.is_valid_dev_type(value):
helper.error("Invalid device type: %s" % value)
type_ = value
return type_
def colorize(color,text):
return f"{color}{text}{style.RESET}"
......@@ -228,12 +280,9 @@ def dumper():
helper = ToolboxHelper()
helper.parser.add_option("-f",dest="filter_address",help="only show given address")
(options,_) = helper.parse()
eng=helper.setup_engine()
target = None
if options.filter_address:
target = tools.get_uuid(options.filter_address)
if target == None:
helper.error(f"Invalid address: {options.filter_address}")
target = helper.get_filter_address()
async def dumper_callback(msg):
if target!=None:
......@@ -256,8 +305,7 @@ def dumper():
if options.no_color == False: print(color,end='')
msg.dump()
if options.no_color == False: print(style.RESET,end='')
eng=helper.setup_engine()
eng.subscribe(dumper_callback)
eng.run()
......@@ -266,20 +314,17 @@ def dumper():
#####################################################
def is_alive():
helper = ToolboxHelper()
helper.parser.add_option("-t",dest="filter_type",help="only show given device type")
(options,args) = helper.parse()
(eng,dev) = helper.setup_basic()
type_ = 'any.any'
if len(args) == 1:
type_ = args[0]
if not tools.is_valid_dev_type(type_):
helper.error("Invalid device type: %s" % type_)
type_ = helper.get_filter_devtype()
async def alive_callback(msg):
await asyncio.sleep(0)
if (msg.source == dev.address) or (msg.is_alive() == False):
return
# FIXME: check if dev_type is Ok.
if helper.get_device(msg.source) == None:
helper.parse_msg(msg)
if options.no_color:
......@@ -312,6 +357,7 @@ def info():
target = tools.get_uuid(args[0])
if target == None:
helper.error("Invalid address: %s" % args[0])
color = helper.options.no_color or 'color'
async def info_callback(msg,addr=target):
await asyncio.sleep(0)
......@@ -320,7 +366,7 @@ def info():
target = helper.parse_msg(msg)
# Finish ?
if target.ready():
target.show()
target.display(color)
helper.exit_event.set()
async def run():
......@@ -329,7 +375,7 @@ def info():
eng.send_get_attributes(dev,[target,])
# wait for device to complete
await helper.exit_event.wait()
helper._quit()
helper.quit()
eng.subscribe(info_callback)
eng.on_start(run)
......@@ -340,15 +386,12 @@ def info():
#####################################################
def walker():
helper = ToolboxHelper()
helper.parser.add_option("-t",dest="filter_type",help="only show given device type")
(_,args)=helper.parse()
(eng,dev) = helper.setup_basic()
type_ = helper.get_filter_devtype()
type_ = 'any.any'
if len(args) == 1:
type_ = args[0]
if not tools.is_valid_dev_type(type_):
helper.error("Invalid device type: [%s]" % type_)
color = helper.options.no_color or 'color'
def walker_callback(msg):
if msg.source == dev.address: return
......@@ -361,7 +404,7 @@ def walker():
eng.send_get_description(target,[msg.source,])
eng.send_get_attributes(target,[msg.source,])
if target.ready():
target.show()
target.display(color)
def start():
eng.send_is_alive(dev,dev_types=type_)
......@@ -378,13 +421,22 @@ def walker():
#####################################################
def log():
helper = ToolboxHelper()
helper.parser.add_option("-f",dest="filter_address",help="only show given address")
helper.parser.add_option("-t",dest="filter_type",help="only show given device type")
(options,_)=helper.parse()
eng = helper.setup_engine()
target = helper.get_filter_address()
type_ = helper.get_filter_devtype()
def log_callback(msg):
if msg.is_alive() or (msg.action in HIDE_ACTION):
return
if (target!=None):
if (target not in msg.targets + [msg.source,]):
return
# FIXME: check if dev_type is Ok.
color = Colors.DEFAULT
if msg.is_attributes_change(): color = Colors.ATTRIBUTS
elif msg.is_notify(): color=Colors.NOTIFY
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment