Browse Source

Update for a few features

* Option for json output
* Option for sending results as POST to url
* Option to cyclically execute comamnds
* Possiblity to stack multiple commands
feat
Laurent Kislaire 1 year ago
parent
commit
1804ac16f0
1 changed files with 54 additions and 26 deletions
  1. +54
    -26
      pyfbx/__main__.py

+ 54
- 26
pyfbx/__main__.py View File

@@ -2,9 +2,12 @@
Console script
"""
import os
import time
import logging
import json
import argparse
import pprint
import requests
from pyfbx import Fbx

log_level = (logging.WARNING, logging.INFO, logging.DEBUG)
@@ -18,42 +21,67 @@ def console(log, level):
log.addHandler(stream_handler)


def output(fbx, command, json_output, send_url):
res = eval("fbx.{}".format(command))
if send_url:
try:
r = requests.post(send_url, json=res)
except BaseException as exc:
print("While sending to {}, got exception {}".format(send_url, exc))
elif json_output:
print(json.dumps(res))
else:
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(res)


def main():
log = logging.getLogger("pyfbx")
parser = argparse.ArgumentParser()
parser.add_argument("app_id", type=str, help="application identifier")
parser.add_argument("-c", "--command", type=str,
help="command, defaults to System.Get_the_current_system_info()",
default="System.Get_the_current_system_info()")
parser.add_argument("-t", "--token", type=str, help="token (or f:<filename>)")
parser.add_argument("-a", "--app_id", type=str,
help="application identifier")
parser.add_argument("-t", "--token", type=str,
help="token (or f:<filename>)")
parser.add_argument("-v", "--verbose", action="count", default=0,
help="increase verbosity to INFO, use twice for DEBUG")
parser.add_argument("-n", "--http", action="store_true",
help="disable MDNS and use http known address")
parser.add_argument("-j", "--json", action="store_true", default=False,
help="json output")
parser.add_argument("-d", "--delay", type=int,
help="cylically send command (number of seconds)")
parser.add_argument("-u", "--url", type=str,
help="specific url to query",
default=None)
help="specific url to query")
parser.add_argument("-s", "--send", type=str,
help="url to send json to")
parser.add_argument("-c", "--command", action='append',
help="command, defaults to System.Get_the_current_system_info()")
args = parser.parse_args()
console(log, log_level[min(2, args.verbose)])
try:
myfb = Fbx(nomdns=args.http, url=args.url)
token = args.token
if token:
if token.startswith('f:'):
with open(args.token[2:]) as tok_file:
token = tok_file.read().strip()
else:
log.warning("Registering app {}, id {}, Press button".format(__name__, args.app_id))
token = myfb.register(app_id=args.app_id, app_name=__name__,
device=os.uname().nodename)
log.warning("Save your application token: {}".format(token))
myfb.mksession(app_id=args.app_id, token=token)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(eval("myfb.{}".format(args.command)))
return 0
except BaseException as err:
print(err)
return 2
token = args.token
app_id = args.app_id
if not args.command:
args.command = ["System.Get_the_current_system_info()"]
myfb = Fbx(nomdns=args.http, url=args.url)
if token:
if token.startswith('f:'):
with open(args.token[2:]) as tok_file:
token, app_id = tok_file.read().splitlines()
else:
log.warning("Registering app {}, id {}, Press button".format(__name__, app_id))
token = myfb.register(app_id=app_id, app_name=__name__,
device=os.uname().nodename)
log.warning("Save your application token: {}".format(token))
myfb.mksession(app_id=app_id, token=token)
pp = pprint.PrettyPrinter(indent=4)

while True:
for command in args.command:
output(myfb, command, args.json, args.send)
if not args.delay:
break
time.sleep(args.delay)
return 0


if __name__ == "__main__":


Loading…
Cancel
Save