Version 1.2 of Pythonista includes the official Dropbox Python SDK as a built-in module. Because of the way OAuth works, using it can be a little daunting at first, so I've created a minimal example that uses the webbrowser module for logging in and the keychain module for storing the credentials.
The script then just shows some information about the logged-in account, but once you're there, downloading and uploading files is relatively easy.
If you make sure that the script's name contains no spaces, you can also import it from other scripts and use the get_client function from there, so that you don't have to duplicate all that login boilerplate code everywhere you want to use the dropbox module.
Your script 'dropboxlogin' has to be installed to get the authentication. It's intended to work with an app folder as the first step it takes if if doesn't know anything is to download everything! And then upload everything. After the first run it stores the local state in a hidden file named '.dropbox_state' and should only download/upload/delete when something changes
If something breaks the best thing to do is start from scratch by removing the persistent state file: > os.remove('.dropbox_state')
I think the more likely use case is to prevent downloading from Dropbox.
For example: you could have a Pythonista folder in your Dropbox, managed on your desktop using git, and have it (almost) automatically synced to your devices via Dropbox and this script. But there's no need to download the hidden .git files/folders, as they'd serve no purpose within Pythonista... so it would be nice to be able to set parameters in the script telling it to ignore them and not download them.
@wrenoud Any chance you will update your script to also support the entire folder? I can't figure out what lines to change to not download my entire Dropbox first.
Any chance of an update to your sync script to include an optional subfolder variable, so the sync doesn't happen with the root of your Dropbox but with a specified path?
I've been trying to work out how to alter it myself, but I'm struggling! :-S
Update: Er, for anyone else wondering the same thing, I thought I'd come back here to say that it's actually very easy to get this script syncing with a single folder. Register your app as a specific folder rather than the full access, and it's automatic! (The sync folder will end up inside the 'Apps' folder...)
It's also pretty straightforward to have two Dropbox apps setup, one for syncing everything in Pythonista with a specific folder, and one with full dropbox access (which I wanted for other purposes).
I've added editor.reload_files() to the script because when it creates files in pythonista my list qas not being updated unless I've ordered again or closed and opened the app.
I'm trying to run the script dropboxlogin - after creating the Dropbox app, getting the key and secret, setting these in the script and the access type to 'dropbox' - but when I run the script I get the error "Invalid key (.......). Check your app's configuration to make sure everything. Is correct". In this message the key string is correct. I am sure I am missing something but help would be appreciated.
@johnbenallen Make sure that the app you've configured on the Dropbox developer page is also set to "full access" (and not app folder), that's the only thing I can think of right now.
On the App Info page it says that access type is "Full Dropbox'", so should be ok.
Another thought: when I created the app I took the option "core API". Should I have selected "sync API"? This choice is not visible in the App Info page.
Thanks for the script, it works fine. The only annoying thing was that it syncs everything, including .git/ folders and the like.
This is my solution to exclude a folder from syncing: 1. move the .git folder out of the Dropbox sync folder 2. create an empty .git folder in the old location 3. uncheck this empty folder in the selective sync list in the Dropbox preferences. It will be deleted locally (that's why we moved it in step 1) 4. move back the folder to it's previous location. Dropbox will now ignore it.