From a1a31471eec5c3c51e8224bf0f0f2647a4d3857f Mon Sep 17 00:00:00 2001 From: HappyZ Date: Fri, 14 Dec 2018 11:41:52 -0600 Subject: [PATCH] try to find default client key file paths --- dpt-tools.py | 2 -- python_api/libDPT.py | 63 +++++++++++++++++++++++++++++++++++- python_api/libInteractive.py | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/dpt-tools.py b/dpt-tools.py index 63067e6..6a479d4 100644 --- a/dpt-tools.py +++ b/dpt-tools.py @@ -107,8 +107,6 @@ def main(): "Make sure your id, key, and ip addresses are correct." ) exit(1) - dpt.client_id_fp = args.get('dpt_id', "") - dpt.key_fp = args.get('dpt_key', "") interactive(dpt, diagnosis=args.get('diagnosis', False)) diff --git a/python_api/libDPT.py b/python_api/libDPT.py index 05db8ab..331e94d 100644 --- a/python_api/libDPT.py +++ b/python_api/libDPT.py @@ -545,12 +545,73 @@ class DPT(): return self._get_api( "/system/controls/pastlog", cookies=self.cookies, isfile=True) - def authenticate(self, client_id_fp, key_fp, testmode=False): + def get_client_key_fps(self): + ''' + return the stored client key file paths + ''' + return self.client_id_fp, self.key_fp + + def set_client_key_fps(self, client_id_fp, key_fp): + ''' + store the client key file paths + ''' + self.client_id_fp = client_id_fp + self.key_fp = key_fp + + def auto_find_client_key_fps(self): + ''' + automatically find the client key file paths + inspired from https://github.com/janten/dpt-rp1-py/pull/52 + ''' + default_client_fp, default_key_fp = self.get_client_key_fps() + if os.path.isfile(default_client_fp) and os.path.isfile(default_key_fp): + return default_client_fp, default_key_fp + dpa_path = "." + # MacOS + try: + home_path = os.path.expanduser("~") + except BaseException: + return default_client_fp, default_key_fp + tmp_dpa_path = "{}/Library/Application Support/".format(home_path) + tmp_dpa_path += "Sony Corporation/Digital Paper App" + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + # windows + tmp_dpa_path = "{}/AppData/Roaming/".format(home_path) + tmp_dpa_path += "Sony Corporation/Digital Paper App" + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + # Linux + tmp_dpa_path = "{}/.dpapp".format(home_path) + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + tmp_client_fp = "{}/deviceid.dat".format(dpa_path) + tmp_key_fp = "{}/privatekey.dat".format(dpa_path) + if os.path.isfile(tmp_client_fp) and os.path.isfile(tmp_key_fp): + default_client_fp = tmp_client_fp + default_key_fp = tmp_key_fp + return default_client_fp, default_key_fp + + def reauthenticate(self): + ''' + reauthentication (must done after reboot) + ''' + return self.authenticate() + + + def authenticate(self, client_id_fp="", key_fp="", testmode=False): + ''' + authenticate is necessary to send url request + ''' + # find client_id_fp and key_fp optional + if not client_id_fp or not key_fp: + client_id_fp, key_fp = auto_find_client_key_fps() if not os.path.isfile(client_id_fp) or not os.path.isfile(key_fp): print( "! Err: did not find {0} or {1}" .format(client_id_fp, key_fp)) return False + self.set_client_key_fps(client_id_fp, key_fp) with open(client_id_fp) as f: client_id = f.read().strip() diff --git a/python_api/libInteractive.py b/python_api/libInteractive.py index 8ffb7de..d6fbfbd 100644 --- a/python_api/libInteractive.py +++ b/python_api/libInteractive.py @@ -125,7 +125,7 @@ def obtain_diagnosis_access(dpt): except BaseException as e: dpt.err_print(str(e)) return False - if not dpt.authenticate(dpt.client_id_fp, dpt.key_fp): + if not dpt.reauthenticate(): dpt.err_print("Cannot reauthenticate after reboot") dpt.err_print("Client id filepath: {}".format(dpt.client_id_fp)) dpt.err_print("Client key filepath: {}".format(dpt.key_fp))