Skip to content

Commit d996880

Browse files
author
api.jfisher
committed
Adding in support to upload presentations to the Documents List API.
1 parent 1514426 commit d996880

3 files changed

Lines changed: 120 additions & 80 deletions

File tree

samples/docs/docs_example.py

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,61 @@
2727

2828

2929
class DocsSample(object):
30+
"""A DocsSample object demonstrates the Document List feed."""
3031

3132
def __init__(self, email, password):
32-
"""Takes an email and password corresponding to a gmail account to
33-
demonstrate the functionality of the Document List feed."""
34-
33+
"""Constructor for the DocsSample object.
34+
35+
Takes an email and password corresponding to a gmail account to
36+
demonstrate the functionality of the Document List feed.
37+
38+
Args:
39+
email: [string] The e-mail address of the account to use for the sample.
40+
password: [string] The password corresponding to the account specified by
41+
the email parameter.
42+
43+
Returns:
44+
A DocsSample object used to run the sample demonstrating the
45+
functionality of the Document List feed.
46+
"""
3547
self.gd_client = gdata.docs.service.DocsService()
3648
self.gd_client.email = email
3749
self.gd_client.password = password
3850
self.gd_client.source = 'Document List Python Sample'
3951
self.gd_client.ProgrammaticLogin()
4052

4153
def _PrintFeed(self, feed):
42-
"""Prints out the contents of a feed to the console."""
43-
54+
"""Prints out the contents of a feed to the console.
55+
56+
Args:
57+
feed: A gdata.docs.DocumentListFeed instance.
58+
"""
4459
print '\n'
45-
if not len(feed.entry):
60+
if not feed.entry:
4661
print 'No entries in feed.\n'
4762
for i, entry in enumerate(feed.entry):
4863
print '%s %s\n' % (i+1, entry.title.text.encode('UTF-8'))
4964

5065
def _GetFileExtension(self, file_name):
51-
"""Returns the three letter file extension for a file in upper case
52-
characters."""
53-
54-
match = re.search('.*\.([a-zA-Z]{3}$)',file_name)
66+
"""Returns the uppercase file extension for a file.
67+
68+
Args:
69+
file_name: [string] The basename of a filename.
70+
71+
Returns:
72+
A string containing the file extension of the file.
73+
"""
74+
match = re.search('.*\.([a-zA-Z]{3}$)', file_name)
5575
if match:
5676
return match.group(1).upper()
57-
else:
58-
return False
77+
return False
5978

6079
def _UploadMenu(self):
6180
"""Prompts that enable a user to upload a file to the Document List feed."""
62-
6381
file_path = ''
6482
file_path = raw_input('Enter path to file: ')
6583

66-
if file_path == '':
84+
if not file_path:
6785
return
6886
elif not os.path.isfile(file_path):
6987
print 'Not a valid file.'
@@ -79,18 +97,21 @@ def _UploadMenu(self):
7997
content_type = gdata.docs.service.SUPPORTED_FILETYPES[ext]
8098

8199
title = ''
82-
while title == '':
100+
while not title:
83101
title = raw_input('Enter name for document: ')
84102

85103
try:
86-
ms = gdata.MediaSource(file_path = file_path, content_type = content_type)
104+
ms = gdata.MediaSource(file_path=file_path, content_type=content_type)
87105
except IOError:
88106
print 'Problems reading file. Check permissions.'
89107
return
90108

91-
if ext in ['CSV','ODS','XLS']:
109+
if ext in ['CSV', 'ODS', 'XLS']:
92110
print 'Uploading spreadsheet...'
93111
entry = self.gd_client.UploadSpreadsheet(ms, title)
112+
elif ext in ['PPT', 'PPS']:
113+
print 'Uploading presentation...'
114+
entry = self.gd_client.UploadPresentation(ms, title)
94115
else:
95116
print 'Uploading word processor document...'
96117
entry = self.gd_client.UploadDocument(ms, title)
@@ -103,50 +124,60 @@ def _UploadMenu(self):
103124

104125
def _ListAllDocuments(self):
105126
"""Retrieves a list of all of a user's documents and displays them."""
106-
107127
feed = self.gd_client.GetDocumentListFeed()
108128
self._PrintFeed(feed)
109129

110130
def _ListAllSpreadsheets(self):
111131
"""Retrieves a list of a user's spreadsheets and displays them."""
112-
113-
q = gdata.docs.service.DocumentQuery(categories=['spreadsheet'])
114-
feed = self.gd_client.Query(q.ToUri())
132+
query = gdata.docs.service.DocumentQuery(categories=['spreadsheet'])
133+
feed = self.gd_client.Query(query.ToUri())
115134
self._PrintFeed(feed)
116135

117136
def _ListAllWPDocuments(self):
118-
"""Retrieves a list of a user's word processor documents and displays
119-
them."""
137+
"""Retrieves a list of a user's WP documents and displays them."""
138+
query = gdata.docs.service.DocumentQuery(categories=['document'])
139+
feed = self.gd_client.Query(query.ToUri())
140+
self._PrintFeed(feed)
120141

121-
q = gdata.docs.service.DocumentQuery(categories=['document'])
122-
feed = self.gd_client.Query(q.ToUri())
142+
def _ListAllPresentations(self):
143+
"""Retrieves a list of a user's presentations and displays them."""
144+
query = gdata.docs.service.DocumentQuery(categories=['presentation'])
145+
feed = self.gd_client.Query(query.ToUri())
123146
self._PrintFeed(feed)
124147

125148
def _FullTextSearch(self):
126-
"""Provides prompts to search a user's documents and displays the results
149+
"""Searches a user's documents for a text string.
150+
151+
Provides prompts to search a user's documents and displays the results
127152
of such a search. The text_query parameter of the DocumentListQuery object
128153
corresponds to the contents of the q parameter in the feed. Note that this
129-
parameter searches the content of documents, not just their titles."""
130-
154+
parameter searches the content of documents, not just their titles.
155+
"""
131156
input = raw_input('Enter search term: ')
132-
q = gdata.docs.service.DocumentQuery(text_query=input)
133-
feed = self.gd_client.Query(q.ToUri())
157+
query = gdata.docs.service.DocumentQuery(text_query=input)
158+
feed = self.gd_client.Query(query.ToUri())
134159
self._PrintFeed(feed)
135160

136161
def _PrintMenu(self):
137162
"""Displays a menu of options for the user to choose from."""
138-
139163
print ('\nDocument List Sample\n'
140164
'1) List all of your documents.\n'
141165
'2) List all of your spreadsheets.\n'
142166
'3) List all of your word processor documents.\n'
143-
'4) Search your documents.\n'
144-
'5) Upload a document.\n'
145-
'6) Exit.\n')
167+
'4) List all of your presentations.\n'
168+
'5) Search your documents.\n'
169+
'6) Upload a document.\n'
170+
'7) Exit.\n')
146171

147172
def _GetMenuChoice(self, max):
148-
"""Retrieves the menu selection from the user."""
149-
173+
"""Retrieves the menu selection from the user.
174+
175+
Args:
176+
max: [int] The maximum number of allowed choices (inclusive)
177+
178+
Returns:
179+
The integer of the menu item chosen by the user.
180+
"""
150181
while True:
151182
input = raw_input('> ')
152183

@@ -162,15 +193,13 @@ def _GetMenuChoice(self, max):
162193
return num
163194

164195
def Run(self):
165-
"""Allows the user to pick from various actions which demonstrate uses of
166-
the Document List feed."""
167-
196+
"""Prompts the user to choose funtionality to be demonstrated."""
168197
try:
169198
while True:
170199

171200
self._PrintMenu()
172201

173-
choice = self._GetMenuChoice(6)
202+
choice = self._GetMenuChoice(7)
174203

175204
if choice == 1:
176205
self._ListAllDocuments()
@@ -179,10 +208,12 @@ def Run(self):
179208
elif choice == 3:
180209
self._ListAllWPDocuments()
181210
elif choice == 4:
182-
self._FullTextSearch()
211+
self._ListAllPresentations()
183212
elif choice == 5:
184-
self._UploadMenu()
213+
self._FullTextSearch()
185214
elif choice == 6:
215+
self._UploadMenu()
216+
elif choice == 7:
186217
return
187218

188219
except KeyboardInterrupt:
@@ -191,12 +222,10 @@ def Run(self):
191222

192223

193224
def main():
194-
"""Demonstrates use of the Docs client library extension using the
195-
DocsSample object."""
196-
225+
"""Demonstrates use of the Docs extension using the DocsSample object."""
197226
# Parse command line options
198227
try:
199-
opts, args = getopt.getopt(sys.argv[1:], "", ["user=", "pw="])
228+
opts, args = getopt.getopt(sys.argv[1:], '', ['user=', 'pw='])
200229
except getopt.error, msg:
201230
print 'python docsExample.py --user [username] --pw [password] '
202231
sys.exit(2)
@@ -206,17 +235,17 @@ def main():
206235
key = ''
207236
# Process options
208237
for option, arg in opts:
209-
if option == "--user":
238+
if option == '--user':
210239
user = arg
211-
elif option == "--pw":
240+
elif option == '--pw':
212241
pw = arg
213242

214-
if user == '':
243+
while not user:
215244
print 'NOTE: Please run these tests only with a test account.'
216245
user = raw_input('Please enter your username: ')
217-
while pw == '':
246+
while not pw:
218247
pw = getpass.getpass()
219-
if pw == '':
248+
if not pw:
220249
print 'Password cannot be blank.'
221250

222251
try:

src/gdata/docs/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818

1919
__author__ = 'api.jfisher (Jeff Fisher)'
2020

21-
try:
22-
from xml.etree import cElementTree as ElementTree
23-
except ImportError:
24-
try:
25-
import cElementTree as ElementTree
26-
except ImportError:
27-
from elementtree import ElementTree
2821
import atom
2922
import gdata
3023

@@ -71,5 +64,3 @@ def DocumentListFeedFromString(xml_string):
7164
A DocumentListFeed object corresponding to the given XML.
7265
"""
7366
return atom.CreateClassFromXMLString(DocumentListFeed, xml_string)
74-
75-

0 commit comments

Comments
 (0)