Browse Source

Merge branch 'feat' into 'dev'

Feat

See merge request sun/pyfbx!13
7rc0
Laurent 3 years ago
parent
commit
3e7ad3ba6f
  1. 80
      pyfbx/__main__.py
  2. 3
      pyfbx/client.py
  3. 4
      requirements-dev.txt
  4. 2
      setup.py
  5. 5
      tests/conn_test.py

80
pyfbx/__main__.py

@ -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__":

3
pyfbx/client.py

@ -86,6 +86,7 @@ class Fbx():
def __init__(self, url=None, nomdns=False, session=None):
self.log = logging.getLogger("pyfbx.fbx")
self._trn = FbxTransport(url, nomdns=nomdns, session=session)
self.app_id = None
# Create on the fly attributes to classes
_globals = globals()
@ -116,6 +117,8 @@ class Fbx():
self.token = token
if app_id:
self.app_id = app_id
elif not self.app_id:
raise Exception("Missing app_id")
login = self._trn.api_exec("GET", "login/")
if not login['logged_in']:
data = {

4
requirements-dev.txt

@ -1,5 +1,5 @@
mock
pytest
pytest-cov
pytest>=4.4.1
pytest-cov>=2.6.1
twine
wheel

2
setup.py

@ -3,7 +3,7 @@ from setuptools import setup, find_packages
setup(
name="pyfbx",
version="0.0.5",
version="0.0.6rc0",
description="Freebox thin client",
long_description=(pathlib.Path(__file__).parent / "README.md").read_text(),
long_description_content_type='text/markdown',

5
tests/conn_test.py

@ -14,6 +14,8 @@ from pyfbx.mdns import FbxMDNS
def test_fbx_register():
f = Fbx("http://192.168.1.254/api/v6")
with pytest.raises(Exception):
f.mksession()
with patch('pyfbx.client.FbxTransport.api_exec',
side_effect=iter([
{"track_id": "id", "app_token": "tok"},
@ -27,8 +29,7 @@ def test_fbx_session_local():
tokname = os.path.join(os.path.dirname(__file__), 'token.txt')
with open(tokname) as tokfile:
token = tokfile.readline().rstrip()
app_id = tokfile.readline().rstrip()
token, app_id = tokfile.read().splitlines()
print("token:{}, app_id:{}".format(token, app_id))
f = Fbx()
f.mksession(app_id=app_id, token=token)

Loading…
Cancel
Save