From 41febe1e36e0f7d93e6818e21259ef63dfb663af Mon Sep 17 00:00:00 2001 From: Yang Yousek Date: Sun, 31 Dec 2017 03:44:08 +0900 Subject: [PATCH] Change finding logic for Dropxbox folder paths Change Dropbox metadata file to info.json because newer Dropbox find Dropbox folder paths using info.json instead of host.db. Also, there are chances to have more than one Dropbox accounts, so support to choose right one. --- mackup/utils.py | 49 +++++++++++++++++++++++++++---- tests/fixtures/.dropbox/host.db | 2 -- tests/fixtures/.dropbox/info.json | 1 + 3 files changed, 45 insertions(+), 7 deletions(-) delete mode 100644 tests/fixtures/.dropbox/host.db create mode 100644 tests/fixtures/.dropbox/info.json diff --git a/mackup/utils.py b/mackup/utils.py index dad9828fb..d3df46b7a 100644 --- a/mackup/utils.py +++ b/mackup/utils.py @@ -1,5 +1,5 @@ """System static utilities being used by the modules.""" -import base64 +import json import os import platform import shutil @@ -46,6 +46,40 @@ def confirm(question): return confirmed +def choose(question, candidates): + """ + Ask the user which one to be chosen + + Args: + question(str): What can happen + candidates(list): Candidates to be chosen + + Returns: + (str) chosen candidate + """ + + length = len(candidates) + while True: + print('{} (1-{})'.format(question, length)) + + for i, candidate in enumerate(candidates): + print('{}: {}'.format(i + 1, candidate)) + + if sys.version_info[0] < 3: + answer = raw_input() + else: + answer = input() + + if not answer.isdigit(): + continue + + answer = int(answer) + if answer in range(1, length + 1): + break + + return candidates[answer - 1] + + def delete(filepath): """ Delete the given file, directory or link. @@ -200,13 +234,18 @@ def get_dropbox_folder_location(): Returns: (str) Full path to the current Dropbox folder """ - host_db_path = os.path.join(os.environ['HOME'], '.dropbox/host.db') + info_json_path = os.path.join(os.environ['HOME'], '.dropbox/info.json') try: - with open(host_db_path, 'r') as f_hostdb: - data = f_hostdb.read().split() + with open(info_json_path, 'r') as f_info_json: + data = json.loads(f_info_json.read()) + if len(data) == 1: + dropbox_home = list(data.values())[0]['path'] + else: + account = choose("Choose Dropbox account", data.keys()) + dropbox_home = data[account]['path'] + except IOError: error("Unable to find your Dropbox install =(") - dropbox_home = base64.b64decode(data[1]) return dropbox_home diff --git a/tests/fixtures/.dropbox/host.db b/tests/fixtures/.dropbox/host.db deleted file mode 100644 index 683154f68..000000000 --- a/tests/fixtures/.dropbox/host.db +++ /dev/null @@ -1,2 +0,0 @@ -0000000000000000000000000000000000000000 -L2hvbWUvc29tZV91c2VyL0Ryb3Bib3g= diff --git a/tests/fixtures/.dropbox/info.json b/tests/fixtures/.dropbox/info.json new file mode 100644 index 000000000..3a9769beb --- /dev/null +++ b/tests/fixtures/.dropbox/info.json @@ -0,0 +1 @@ +{"personal": {"path": "/home/some_user/Dropbox", "host": 32050288048, "is_team": false, "subscription_type": "Basic"}}