3
3
This is an example app for API v2.
4
4
"""
5
5
6
+ from __future__ import print_function
7
+
6
8
import sys
9
+
7
10
import dropbox
8
- from dropbox .files import WriteMode
9
11
from dropbox .exceptions import ApiError , AuthError
12
+ from dropbox .files import WriteMode
10
13
11
14
# Add OAuth2 access token here.
12
15
# 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/>
14
18
TOKEN = ''
15
19
16
20
LOCALFILE = 'my-file.txt'
17
21
BACKUPPATH = '/my-file-backup.txt'
18
22
19
23
# Uploads contents of LOCALFILE to Dropbox
24
+
25
+
20
26
def backup ():
21
27
with open (LOCALFILE , 'rb' ) as f :
22
28
# We use WriteMode=overwrite to make sure that the settings in the file
23
29
# 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
+ "..." )
25
36
try :
26
37
dbx .files_upload (f .read (), BACKUPPATH , mode = WriteMode ('overwrite' ))
27
38
except ApiError as err :
@@ -37,44 +48,62 @@ def backup():
37
48
print (err )
38
49
sys .exit ()
39
50
40
- # Change the text string in LOCALFILE to be new_content
41
- # @param new_content is a string
42
51
def 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
+ """
43
57
print ("Changing contents of " + LOCALFILE + " on local machine..." )
44
58
with open (LOCALFILE , 'wb' ) as f :
45
59
f .write (new_content )
46
60
47
- # Restore the local and Dropbox files to a certain revision
48
61
def 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
+ """
50
67
print ("Restoring " + BACKUPPATH + " to revision " + rev + " on Dropbox..." )
51
68
dbx .files_restore (BACKUPPATH , rev )
52
69
53
70
# 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
+ "..." )
55
77
dbx .files_download_to_file (LOCALFILE , BACKUPPATH , rev )
56
78
57
- # Look at all of the available revisions on Dropbox, and return the oldest one
58
79
def 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
+ """
60
86
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
62
89
revisions = sorted (entries , key = lambda entry : entry .server_modified )
63
90
64
91
for revision in revisions :
65
92
print (revision .rev , revision .server_modified )
66
93
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)
68
96
return revisions [0 ].rev
69
97
70
98
if __name__ == '__main__' :
71
99
# Check for an access token
72
- if ( len (TOKEN ) == 0 ) :
100
+ if len (TOKEN ) == 0 :
73
101
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." )
76
104
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.
78
107
print ("Creating a Dropbox object..." )
79
108
dbx = dropbox .Dropbox (TOKEN )
80
109
@@ -83,7 +112,7 @@ def select_revision():
83
112
dbx .users_get_current_account ()
84
113
except AuthError as err :
85
114
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." )
87
116
88
117
# Create a backup of the current settings file
89
118
backup ()
0 commit comments