33This is an example app for API v2.
44"""
55
6+ from __future__ import print_function
7+
68import sys
9+
710import dropbox
8- from dropbox .files import WriteMode
911from dropbox .exceptions import ApiError , AuthError
12+ from dropbox .files import WriteMode
1013
1114# Add OAuth2 access token here.
1215# You can generate one for yourself in the App Console.
13- # See <https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/>
16+ # See
17+ # <https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/>
1418TOKEN = ''
1519
1620LOCALFILE = 'my-file.txt'
1721BACKUPPATH = '/my-file-backup.txt'
1822
1923# Uploads contents of LOCALFILE to Dropbox
24+
25+
2026def backup ():
2127 with open (LOCALFILE , 'rb' ) as f :
2228 # We use WriteMode=overwrite to make sure that the settings in the file
2329 # are changed on upload
24- print ("Uploading " + LOCALFILE + " to Dropbox as " + BACKUPPATH + "..." )
30+ print (
31+ "Uploading " +
32+ LOCALFILE +
33+ " to Dropbox as " +
34+ BACKUPPATH +
35+ "..." )
2536 try :
2637 dbx .files_upload (f .read (), BACKUPPATH , mode = WriteMode ('overwrite' ))
2738 except ApiError as err :
@@ -37,44 +48,62 @@ def backup():
3748 print (err )
3849 sys .exit ()
3950
40- # Change the text string in LOCALFILE to be new_content
41- # @param new_content is a string
4251def change_local_file (new_content ):
52+ """
53+ Change the text string in LOCALFILE to be new_content
54+ Params: new_content:string
55+ Return: None
56+ """
4357 print ("Changing contents of " + LOCALFILE + " on local machine..." )
4458 with open (LOCALFILE , 'wb' ) as f :
4559 f .write (new_content )
4660
47- # Restore the local and Dropbox files to a certain revision
4861def restore (rev = None ):
49- # Restore the file on Dropbox to a certain revision
62+ """
63+ Restore the file on Dropbox to a certain revision
64+ Params: rev:string - revision number
65+ Return: None
66+ """
5067 print ("Restoring " + BACKUPPATH + " to revision " + rev + " on Dropbox..." )
5168 dbx .files_restore (BACKUPPATH , rev )
5269
5370 # Download the specific revision of the file at BACKUPPATH to LOCALFILE
54- print ("Downloading current " + BACKUPPATH + " from Dropbox, overwriting " + LOCALFILE + "..." )
71+ print (
72+ "Downloading current " +
73+ BACKUPPATH +
74+ " from Dropbox, overwriting " +
75+ LOCALFILE +
76+ "..." )
5577 dbx .files_download_to_file (LOCALFILE , BACKUPPATH , rev )
5678
57- # Look at all of the available revisions on Dropbox, and return the oldest one
5879def select_revision ():
59- # Get the revisions for a file (and sort by the datetime object, "server_modified")
80+ """
81+ Get revisions for a file (sort by datetime object "server_modified")
82+
83+ Params: None
84+ Return: Revisions in descending order by date modified
85+ """
6086 print ("Finding available revisions on Dropbox..." )
61- entries = dbx .files_list_revisions (BACKUPPATH , limit = 30 ).entries # pylint: disable=no-member
87+ entries = dbx .files_list_revisions (
88+ BACKUPPATH , limit = 30 ).entries # pylint: disable=no-member
6289 revisions = sorted (entries , key = lambda entry : entry .server_modified )
6390
6491 for revision in revisions :
6592 print (revision .rev , revision .server_modified )
6693
67- # Return the oldest revision (first entry, because revisions was sorted oldest:newest)
94+ # Return the oldest revision (first entry, because revisions was sorted
95+ # oldest:newest)
6896 return revisions [0 ].rev
6997
7098if __name__ == '__main__' :
7199 # Check for an access token
72- if ( len (TOKEN ) == 0 ) :
100+ if len (TOKEN ) == 0 :
73101 sys .exit ("ERROR: Looks like you didn't add your access token. "
74- "Open up backup-and-restore-example.py in a text editor and "
75- "paste in your token in line 14." )
102+ "Open up backup-and-restore-example.py in a text editor and "
103+ "paste in your token in line 14." )
76104
77- # Create an instance of a Dropbox class, which can make requests to the API.
105+ # Create an instance of a Dropbox class, which can make requests to the
106+ # API.
78107 print ("Creating a Dropbox object..." )
79108 dbx = dropbox .Dropbox (TOKEN )
80109
@@ -83,7 +112,7 @@ def select_revision():
83112 dbx .users_get_current_account ()
84113 except AuthError as err :
85114 sys .exit ("ERROR: Invalid access token; try re-generating an "
86- "access token from the app console on the web." )
115+ "access token from the app console on the web." )
87116
88117 # Create a backup of the current settings file
89118 backup ()
0 commit comments