Browse Source

Exception handling

dev
Laurent Kislaire 3 years ago
parent
commit
104c5ea0d0
  1. 44
      pyfbx/__main__.py

44
pyfbx/__main__.py

@ -9,6 +9,7 @@ import argparse
import pprint
import requests
from pyfbx import Fbx
from pyfbx.client import (ResponseError, RequestError)
log_level = (logging.WARNING, logging.INFO, logging.DEBUG)
@ -27,7 +28,7 @@ def console(log, level):
def output(fbx, command, json_output, send_url):
try:
res = eval("fbx.{}".format(command))
except Exception as exc:
except RequestError as exc:
if "403 Client Error" in str(exc):
fbx.log.info("Got 403, refreshing token")
fbx.mksession()
@ -75,24 +76,31 @@ def main():
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)
try:
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)
while True:
log.debug("\n")
for command in args.command:
output(myfb, command, args.json, args.send)
if not args.delay:
break
time.sleep(args.delay)
while True:
log.debug("\n")
for command in args.command:
output(myfb, command, args.json, args.send)
if not args.delay:
break
time.sleep(args.delay)
except RequestError as exc:
print("Got Http Error {}".format(exc))
return 2
except ResponseError as exc:
print("Got Response Error {}".format(exc))
return 1
return 0

Loading…
Cancel
Save