diff --git a/README.md b/README.md index 700a91b..784045a 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ dptrp1 command [arguments] To see if you can successfully connect to the reader, try the command `dptrp1 list-documents`. If you have Sony's Digital Paper App installed, this should work without any further configuration. If this fails, register your reader with the app using `dptrp1 register`. ### Supported commands -You can get a list of the implemented commands by running `dptrp1` with no additional arguments. Supported commands include _command-help_, _copy-document_, _delete_, _delete-folder_, _download_, _list-documents_, _list-folders_, _move-document_, _new-folder_, _register_, _screenshot_, _sync_, _update-firmware_, _upload_, _wifi_, _wifi-add_, _wifi-del_, _wifi-disable_, _wifi-enable_, _wifi-list_, and _wifi-scan_. +You can get a list of the implemented commands by running `dptrp1` with no additional arguments. Supported commands include _help_, _copy-document_, _delete_, _delete-folder_, _download_, _list-documents_, _list-folders_, _move-document_, _new-folder_, _register_, _screenshot_, _sync_, _update-firmware_, _upload_, _wifi_, _wifi-add_, _wifi-del_, _wifi-disable_, _wifi-enable_, _wifi-list_, and _wifi-scan_. -For some command, you can get additional help by calling `dptrp1 command-help `, e.g. `dptrp1 command-help sync`. +For some commands, you can get additional help by calling `dptrp1 help `, e.g. `dptrp1 help sync`. Note that the root path for DPT-RP1 is `Document/`. Example command to download a document `file.pdf` from the root folder ("System Storage") of DPT-RP1: `dptrp1 download Document/file.pdf ./file.pdf`. Example command to upload a document `file.pdf` to a folder named `Articles` on DPT-RP1: `dptrp1 upload ./file.pdf Document/Articles/file.pdf` diff --git a/dptrp1/cli/dptrp1.py b/dptrp1/cli/dptrp1.py index 2965dea..68927d4 100755 --- a/dptrp1/cli/dptrp1.py +++ b/dptrp1/cli/dptrp1.py @@ -12,6 +12,9 @@ from dptrp1.dptrp1 import DigitalPaper, find_auth_files, get_default_auth_files def do_screenshot(d, filename): + """ + Take a screenshot of the device's screen and save it to the given local path. + """ pic = d.take_screenshot() with open(filename, 'wb') as f: f.write(pic) @@ -34,11 +37,18 @@ def do_copy_document(d, old_path, new_path): d.copy_file(old_path, new_path) def do_upload(d, local_path, remote_path=''): + """ + Upload a local document to the reader. + Will upload to Document/ if only the local path is specified. + """ if not remote_path: remote_path = 'Document/' + os.path.basename(local_path) d.upload_file(local_path, remote_path) def do_download(d, remote_path, local_path): + """ + Download a document from the reader to your computer. + """ data = d.download(remote_path) if os.path.isdir(local_path): @@ -49,6 +59,10 @@ def do_download(d, remote_path, local_path): f.write(data) def do_list_document_info(d, remote_path = False): + """ + Print metadata about a document on the device. + If no path is given, information is printed for every document on the device. + """ if not remote_path: infos = d.list_all() for info in infos: @@ -144,7 +158,7 @@ def do_register(d, key_file, id_file): with open(id_file, 'w') as f: f.write(device_id) -def do_help(d, command): +def do_help(command): """ Print additional information about a command, if available. """ @@ -172,7 +186,7 @@ def do_help(d, command): "register" : do_register, "update-firmware": do_update_firmware, "sync": do_sync, - "command-help": do_help + "help": do_help } def build_parser(): @@ -194,6 +208,11 @@ def build_parser(): action = 'store_true', dest = 'assume_yes', default = False) + p.add_argument('--quiet','-q', + help = "Suppress informative messages.", + action = 'store_true', + dest = 'quiet', + default = False) p.add_argument('command', help = 'Command to run', choices = sorted(commands.keys())) @@ -204,7 +223,12 @@ def build_parser(): def main(): args = build_parser().parse_args() - dp = DigitalPaper(addr=args.addr, id=args.serial, assume_yes=args.assume_yes) + if args.command in ["help", "command-help"]: + # Help is available without a device + commands[args.command](*args.command_args) + return + + dp = DigitalPaper(addr=args.addr, id=args.serial, assume_yes=args.assume_yes, quiet=args.quiet) if args.command == "register": # When registering the device, we default to storing auth files in our own configuration directory default_deviceid, default_privatekey = get_default_auth_files() diff --git a/dptrp1/dptrp1.py b/dptrp1/dptrp1.py index 51417cc..e63e22f 100755 --- a/dptrp1/dptrp1.py +++ b/dptrp1/dptrp1.py @@ -67,11 +67,12 @@ class ResolveObjectFailed(DigitalPaperException): pass class LookUpDPT: - def __init__(self): + def __init__(self, quiet=False): import threading self.addr = None self.id = None self.lock = threading.Lock() + self.quiet = quiet def add_service(self, zeroconf, type, name): info = zeroconf.get_service_info(type, name) @@ -80,18 +81,20 @@ def add_service(self, zeroconf, type, name): info = requests.get("http://{}:{}/register/information".format(addr, info.port)).json() if not self.id: self.id = info['serial_number'] - print("Found Digital Paper with serial number {}".format(self.id)) - print("To discover only this specific device, call:") - print() - print(" {} --serial {} {}".format(sys.argv[0], self.id, " ".join(sys.argv[1:]))) - print() + if not self.quiet: + print("Found Digital Paper with serial number {}".format(self.id)) + print("To discover only this specific device, call:") + print() + print(" {} --serial {} {}".format(sys.argv[0], self.id, " ".join(sys.argv[1:]))) + print() if info['serial_number'] == self.id: self.addr = str(addr) self.lock.release() def find(self, id, timeout=30): from zeroconf import ServiceBrowser, Zeroconf - print("Discovering Digital Paper for {} seconds…".format(timeout)) + if not self.quiet: + print("Discovering Digital Paper for {} seconds…".format(timeout)) sys.stdout.flush() self.id = id zc = Zeroconf() @@ -103,26 +106,26 @@ def find(self, id, timeout=30): print("Failed".format(timeout)) return None else: - print("Found digital paper at", self.addr) - print("To skip the discovery process (and this message), call:") - print() - print(" {} --addr {} {}".format(sys.argv[0], self.addr, " ".join(sys.argv[1:]))) - print() + if not self.quiet: + print("Found digital paper at", self.addr) + print("To skip the discovery process (and this message), call:") + print() + print(" {} --addr {} {}".format(sys.argv[0], self.addr, " ".join(sys.argv[1:]))) + print() return self.addr class DigitalPaper(): - def __init__(self, addr=None, id=None, assume_yes=False): + def __init__(self, addr=None, id=None, assume_yes=False, quiet=False): if addr: self.addr = addr if id: print("Ignoring serial number since address is set. Remove --serial {} from call to silence this message.".format(id)) else: - lookup = LookUpDPT() + lookup = LookUpDPT(quiet=quiet) self.addr = lookup.find(id) self.session = requests.Session() self.session.verify = False # disable ssl certificate verification - self.assume_yes = assume_yes # Whether to disable interactive prompts (currently only in sync()) @property @@ -941,9 +944,9 @@ def set_datetime(self): ### Etc def take_screenshot(self): - url = "{base_url}/system/controls/screen_shot" \ + url = "{base_url}/system/controls/screen_shot2" \ .format(base_url = self.base_url) - r = self.session.get(url) + r = self.session.get(url, params={"query": "jpeg"}) return r.content def ping(self):