Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit ce7127e

Browse files
author
Lars Kellogg-Stedman
committed
documentation updates
1 parent 0e31876 commit ce7127e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

drive.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,33 @@ def __init__(self,
4444
self.credentials = credentials
4545

4646
def authenticate(self):
47+
'''Establish Google credentials. This will load stored credentials
48+
and validate them, and it will call self.login() if stored
49+
credentials are unavailable or fail to validate.'''
50+
4751
self.load_credentials()
4852

4953
if self.token is None:
5054
self.login()
5155
else:
5256
try:
57+
# Always refresh the token. This is a dirty hack to avoid
58+
# doing anything more complicated.
5359
self.refresh()
5460
self.validate()
5561
except ValueError:
5662
self.login()
5763

64+
# Add an Authorization header to all requests made through
65+
# our requests.Session object.
5866
self.session.headers.update({
5967
'Authorization': 'Bearer %(access_token)s' % self.token
6068
})
6169

6270
def refresh(self):
71+
'''Use a refresh_token to refresh the access_token. See
72+
https://developers.google.com/drive/about-auth'''
73+
6374
if not 'refresh_token' in self.token:
6475
raise ValueError('no refresh token')
6576

@@ -76,6 +87,8 @@ def refresh(self):
7687
self.store_credentials()
7788

7889
def login(self):
90+
'''Perform OAuth authentication.'''
91+
7992
params = {
8093
'client_id': self.client_id,
8194
'scope': ' '.join(OAUTH_SCOPES),
@@ -108,35 +121,48 @@ def login(self):
108121
self.store_credentials()
109122

110123
def store_credentials(self):
124+
'''Write credentials to file.'''
111125
with open(self.credentials, 'w') as fd:
112126
fd.write(yaml.safe_dump(self.token, encoding='utf-8',
113127
default_flow_style=False))
114128

115129
def load_credentials(self):
130+
'''Read credentials from file.'''
116131
try:
117132
with open(self.credentials) as fd:
118133
self.token = yaml.load(fd)
119134
except IOError:
120135
pass
121136

122137
def validate(self):
138+
'''Validate token.'''
139+
123140
r = requests.get('%s?access_token=%s' % (
124141
VALIDATE_URI, self.token['access_token']
125142
))
126143

144+
self._validate_response = r
145+
127146
if not r:
128147
raise ValueError('failed to validate')
129148

130149
def files(self):
150+
'''Return an iterator over the files in Google Drive.'''
151+
131152
r = self.session.get('%s/files' % DRIVE_URI).json
132153

133154
for fspec in r['items']:
134155
yield fspec
135156

136157
def get_file_metadata(self, fid):
158+
'''Return the file metadata for a file identified by its ID.'''
159+
137160
return self.session.get('%s/files/%s' % (DRIVE_URI, fid)).json
138161

139162
def revisions(self, fid):
163+
'''Return an iterator over the revisions of a file
164+
identified by its ID.'''
165+
140166
r = self.session.get('%s/files/%s/revisions' % (
141167
DRIVE_URI, fid)).json
142168

gitdriver.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def parse_args():
1616
p.add_argument('--html', '-H', action='store_const', const='text/html',
1717
dest='mime-type')
1818
p.add_argument('--mime-type', default='text/html')
19+
p.add_argument('--raw', '-R', action='store_true',
20+
help='Download original document if possible.')
1921
p.add_argument('docid')
2022

2123
return p.parse_args()
@@ -29,19 +31,36 @@ def main():
2931
scopes=[DRIVE_RW_SCOPE],
3032
)
3133

34+
# Establish our credentials.
3235
gd.authenticate()
3336

37+
# Get information about the specified file. This will throw
38+
# an exception if the file does not exist.
3439
md = gd.get_file_metadata(opts.docid)
3540

41+
# Initialize the git repository.
3642
print 'Create repository "%(title)s"' % md
3743
subprocess.call(['git','init',md['title']])
3844
os.chdir(md['title'])
3945

46+
# Iterate over the revisions (from oldest to newest).
4047
for rev in gd.revisions(opts.docid):
4148
with open('content', 'w') as fd:
42-
r = gd.session.get(rev['exportLinks'][opts.mime_type])
49+
if 'exportLinks' in rev and not opts.raw:
50+
# If the file provides an 'exportLinks' dictionary,
51+
# download the requested MIME type.
52+
r = gd.session.get(rev['exportLinks'][opts.mime_type])
53+
elif 'downloadUrl' in rev:
54+
# Otherwise, if there is a downloadUrl, use that.
55+
r = gd.session.get(rev['downloadUrl'])
56+
else:
57+
raise KeyError('unable to download revision')
58+
59+
# Write file content into local file.
4360
for chunk in r.iter_content():
4461
fd.write(chunk)
62+
63+
# Commit changes to repository.
4564
subprocess.call(['git', 'add', 'content'])
4665
subprocess.call(['git', 'commit', '-m',
4766
'revision from %s' % rev['modifiedDate']])

0 commit comments

Comments
 (0)