/gdata/spreadsheet/service.py

http://radioappz.googlecode.com/ · Python · 484 lines · 432 code · 13 blank · 39 comment · 6 complexity · d6f792f2200714cf96eff187ad200c1d MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """SpreadsheetsService extends the GDataService to streamline Google
  17. Spreadsheets operations.
  18. SpreadsheetService: Provides methods to query feeds and manipulate items.
  19. Extends GDataService.
  20. DictionaryToParamList: Function which converts a dictionary into a list of
  21. URL arguments (represented as strings). This is a
  22. utility function used in CRUD operations.
  23. """
  24. __author__ = 'api.laurabeth@gmail.com (Laura Beth Lincoln)'
  25. import gdata
  26. import atom.service
  27. import gdata.service
  28. import gdata.spreadsheet
  29. import atom
  30. class Error(Exception):
  31. """Base class for exceptions in this module."""
  32. pass
  33. class RequestError(Error):
  34. pass
  35. class SpreadsheetsService(gdata.service.GDataService):
  36. """Client for the Google Spreadsheets service."""
  37. def __init__(self, email=None, password=None, source=None,
  38. server='spreadsheets.google.com', additional_headers=None,
  39. **kwargs):
  40. """Creates a client for the Google Spreadsheets service.
  41. Args:
  42. email: string (optional) The user's email address, used for
  43. authentication.
  44. password: string (optional) The user's password.
  45. source: string (optional) The name of the user's application.
  46. server: string (optional) The name of the server to which a connection
  47. will be opened. Default value: 'spreadsheets.google.com'.
  48. **kwargs: The other parameters to pass to gdata.service.GDataService
  49. constructor.
  50. """
  51. gdata.service.GDataService.__init__(
  52. self, email=email, password=password, service='wise', source=source,
  53. server=server, additional_headers=additional_headers, **kwargs)
  54. def GetSpreadsheetsFeed(self, key=None, query=None, visibility='private',
  55. projection='full'):
  56. """Gets a spreadsheets feed or a specific entry if a key is defined
  57. Args:
  58. key: string (optional) The spreadsheet key defined in /ccc?key=
  59. query: DocumentQuery (optional) Query parameters
  60. Returns:
  61. If there is no key, then a SpreadsheetsSpreadsheetsFeed.
  62. If there is a key, then a SpreadsheetsSpreadsheet.
  63. """
  64. uri = ('http://%s/feeds/spreadsheets/%s/%s'
  65. % (self.server, visibility, projection))
  66. if key is not None:
  67. uri = '%s/%s' % (uri, key)
  68. if query != None:
  69. query.feed = uri
  70. uri = query.ToUri()
  71. if key:
  72. return self.Get(uri,
  73. converter=gdata.spreadsheet.SpreadsheetsSpreadsheetFromString)
  74. else:
  75. return self.Get(uri,
  76. converter=gdata.spreadsheet.SpreadsheetsSpreadsheetsFeedFromString)
  77. def GetWorksheetsFeed(self, key, wksht_id=None, query=None,
  78. visibility='private', projection='full'):
  79. """Gets a worksheets feed or a specific entry if a wksht is defined
  80. Args:
  81. key: string The spreadsheet key defined in /ccc?key=
  82. wksht_id: string (optional) The id for a specific worksheet entry
  83. query: DocumentQuery (optional) Query parameters
  84. Returns:
  85. If there is no wksht_id, then a SpreadsheetsWorksheetsFeed.
  86. If there is a wksht_id, then a SpreadsheetsWorksheet.
  87. """
  88. uri = ('http://%s/feeds/worksheets/%s/%s/%s'
  89. % (self.server, key, visibility, projection))
  90. if wksht_id != None:
  91. uri = '%s/%s' % (uri, wksht_id)
  92. if query != None:
  93. query.feed = uri
  94. uri = query.ToUri()
  95. if wksht_id:
  96. return self.Get(uri,
  97. converter=gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  98. else:
  99. return self.Get(uri,
  100. converter=gdata.spreadsheet.SpreadsheetsWorksheetsFeedFromString)
  101. def AddWorksheet(self, title, row_count, col_count, key):
  102. """Creates a new worksheet in the desired spreadsheet.
  103. The new worksheet is appended to the end of the list of worksheets. The
  104. new worksheet will only have the available number of columns and cells
  105. specified.
  106. Args:
  107. title: str The title which will be displayed in the list of worksheets.
  108. row_count: int or str The number of rows in the new worksheet.
  109. col_count: int or str The number of columns in the new worksheet.
  110. key: str The spreadsheet key to the spreadsheet to which the new
  111. worksheet should be added.
  112. Returns:
  113. A SpreadsheetsWorksheet if the new worksheet was created succesfully.
  114. """
  115. new_worksheet = gdata.spreadsheet.SpreadsheetsWorksheet(
  116. title=atom.Title(text=title),
  117. row_count=gdata.spreadsheet.RowCount(text=str(row_count)),
  118. col_count=gdata.spreadsheet.ColCount(text=str(col_count)))
  119. return self.Post(new_worksheet,
  120. 'http://%s/feeds/worksheets/%s/private/full' % (self.server, key),
  121. converter=gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  122. def UpdateWorksheet(self, worksheet_entry, url=None):
  123. """Changes the size and/or title of the desired worksheet.
  124. Args:
  125. worksheet_entry: SpreadsheetWorksheet The new contents of the
  126. worksheet.
  127. url: str (optional) The URL to which the edited worksheet entry should
  128. be sent. If the url is None, the edit URL from the worksheet will
  129. be used.
  130. Returns:
  131. A SpreadsheetsWorksheet with the new information about the worksheet.
  132. """
  133. target_url = url or worksheet_entry.GetEditLink().href
  134. return self.Put(worksheet_entry, target_url,
  135. converter=gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  136. def DeleteWorksheet(self, worksheet_entry=None, url=None):
  137. """Removes the desired worksheet from the spreadsheet
  138. Args:
  139. worksheet_entry: SpreadsheetWorksheet (optional) The worksheet to
  140. be deleted. If this is none, then the DELETE reqest is sent to
  141. the url specified in the url parameter.
  142. url: str (optaional) The URL to which the DELETE request should be
  143. sent. If left as None, the worksheet's edit URL is used.
  144. Returns:
  145. True if the worksheet was deleted successfully.
  146. """
  147. if url:
  148. target_url = url
  149. else:
  150. target_url = worksheet_entry.GetEditLink().href
  151. return self.Delete(target_url)
  152. def GetCellsFeed(self, key, wksht_id='default', cell=None, query=None,
  153. visibility='private', projection='full'):
  154. """Gets a cells feed or a specific entry if a cell is defined
  155. Args:
  156. key: string The spreadsheet key defined in /ccc?key=
  157. wksht_id: string The id for a specific worksheet entry
  158. cell: string (optional) The R1C1 address of the cell
  159. query: DocumentQuery (optional) Query parameters
  160. Returns:
  161. If there is no cell, then a SpreadsheetsCellsFeed.
  162. If there is a cell, then a SpreadsheetsCell.
  163. """
  164. uri = ('http://%s/feeds/cells/%s/%s/%s/%s'
  165. % (self.server, key, wksht_id, visibility, projection))
  166. if cell != None:
  167. uri = '%s/%s' % (uri, cell)
  168. if query != None:
  169. query.feed = uri
  170. uri = query.ToUri()
  171. if cell:
  172. return self.Get(uri,
  173. converter=gdata.spreadsheet.SpreadsheetsCellFromString)
  174. else:
  175. return self.Get(uri,
  176. converter=gdata.spreadsheet.SpreadsheetsCellsFeedFromString)
  177. def GetListFeed(self, key, wksht_id='default', row_id=None, query=None,
  178. visibility='private', projection='full'):
  179. """Gets a list feed or a specific entry if a row_id is defined
  180. Args:
  181. key: string The spreadsheet key defined in /ccc?key=
  182. wksht_id: string The id for a specific worksheet entry
  183. row_id: string (optional) The row_id of a row in the list
  184. query: DocumentQuery (optional) Query parameters
  185. Returns:
  186. If there is no row_id, then a SpreadsheetsListFeed.
  187. If there is a row_id, then a SpreadsheetsList.
  188. """
  189. uri = ('http://%s/feeds/list/%s/%s/%s/%s'
  190. % (self.server, key, wksht_id, visibility, projection))
  191. if row_id is not None:
  192. uri = '%s/%s' % (uri, row_id)
  193. if query is not None:
  194. query.feed = uri
  195. uri = query.ToUri()
  196. if row_id:
  197. return self.Get(uri,
  198. converter=gdata.spreadsheet.SpreadsheetsListFromString)
  199. else:
  200. return self.Get(uri,
  201. converter=gdata.spreadsheet.SpreadsheetsListFeedFromString)
  202. def UpdateCell(self, row, col, inputValue, key, wksht_id='default'):
  203. """Updates an existing cell.
  204. Args:
  205. row: int The row the cell to be editted is in
  206. col: int The column the cell to be editted is in
  207. inputValue: str the new value of the cell
  208. key: str The key of the spreadsheet in which this cell resides.
  209. wksht_id: str The ID of the worksheet which holds this cell.
  210. Returns:
  211. The updated cell entry
  212. """
  213. row = str(row)
  214. col = str(col)
  215. # make the new cell
  216. new_cell = gdata.spreadsheet.Cell(row=row, col=col, inputValue=inputValue)
  217. # get the edit uri and PUT
  218. cell = 'R%sC%s' % (row, col)
  219. entry = self.GetCellsFeed(key, wksht_id, cell)
  220. for a_link in entry.link:
  221. if a_link.rel == 'edit':
  222. entry.cell = new_cell
  223. return self.Put(entry, a_link.href,
  224. converter=gdata.spreadsheet.SpreadsheetsCellFromString)
  225. def _GenerateCellsBatchUrl(self, spreadsheet_key, worksheet_id):
  226. return ('http://spreadsheets.google.com/feeds/cells/%s/%s/'
  227. 'private/full/batch' % (spreadsheet_key, worksheet_id))
  228. def ExecuteBatch(self, batch_feed, url=None, spreadsheet_key=None,
  229. worksheet_id=None,
  230. converter=gdata.spreadsheet.SpreadsheetsCellsFeedFromString):
  231. """Sends a batch request feed to the server.
  232. The batch request needs to be sent to the batch URL for a particular
  233. worksheet. You can specify the worksheet by providing the spreadsheet_key
  234. and worksheet_id, or by sending the URL from the cells feed's batch link.
  235. Args:
  236. batch_feed: gdata.spreadsheet.SpreadsheetsCellFeed A feed containing
  237. BatchEntry elements which contain the desired CRUD operation and
  238. any necessary data to modify a cell.
  239. url: str (optional) The batch URL for the cells feed to which these
  240. changes should be applied. This can be found by calling
  241. cells_feed.GetBatchLink().href.
  242. spreadsheet_key: str (optional) Used to generate the batch request URL
  243. if the url argument is None. If using the spreadsheet key to
  244. generate the URL, the worksheet id is also required.
  245. worksheet_id: str (optional) Used if the url is not provided, it is
  246. oart of the batch feed target URL. This is used with the spreadsheet
  247. key.
  248. converter: Function (optional) Function to be executed on the server's
  249. response. This function should take one string as a parameter. The
  250. default value is SpreadsheetsCellsFeedFromString which will turn the result
  251. into a gdata.spreadsheet.SpreadsheetsCellsFeed object.
  252. Returns:
  253. A gdata.BatchFeed containing the results.
  254. """
  255. if url is None:
  256. url = self._GenerateCellsBatchUrl(spreadsheet_key, worksheet_id)
  257. return self.Post(batch_feed, url, converter=converter)
  258. def InsertRow(self, row_data, key, wksht_id='default'):
  259. """Inserts a new row with the provided data
  260. Args:
  261. uri: string The post uri of the list feed
  262. row_data: dict A dictionary of column header to row data
  263. Returns:
  264. The inserted row
  265. """
  266. new_entry = gdata.spreadsheet.SpreadsheetsList()
  267. for k, v in row_data.iteritems():
  268. new_custom = gdata.spreadsheet.Custom()
  269. new_custom.column = k
  270. new_custom.text = v
  271. new_entry.custom[new_custom.column] = new_custom
  272. # Generate the post URL for the worksheet which will receive the new entry.
  273. post_url = 'http://spreadsheets.google.com/feeds/list/%s/%s/private/full'%(
  274. key, wksht_id)
  275. return self.Post(new_entry, post_url,
  276. converter=gdata.spreadsheet.SpreadsheetsListFromString)
  277. def UpdateRow(self, entry, new_row_data):
  278. """Updates a row with the provided data
  279. If you want to add additional information to a row, it is often
  280. easier to change the values in entry.custom, then use the Put
  281. method instead of UpdateRow. This UpdateRow method will replace
  282. the contents of the row with new_row_data - it will change all columns
  283. not just the columns specified in the new_row_data dict.
  284. Args:
  285. entry: gdata.spreadsheet.SpreadsheetsList The entry to be updated
  286. new_row_data: dict A dictionary of column header to row data
  287. Returns:
  288. The updated row
  289. """
  290. entry.custom = {}
  291. for k, v in new_row_data.iteritems():
  292. new_custom = gdata.spreadsheet.Custom()
  293. new_custom.column = k
  294. new_custom.text = v
  295. entry.custom[k] = new_custom
  296. for a_link in entry.link:
  297. if a_link.rel == 'edit':
  298. return self.Put(entry, a_link.href,
  299. converter=gdata.spreadsheet.SpreadsheetsListFromString)
  300. def DeleteRow(self, entry):
  301. """Deletes a row, the provided entry
  302. Args:
  303. entry: gdata.spreadsheet.SpreadsheetsList The row to be deleted
  304. Returns:
  305. The delete response
  306. """
  307. for a_link in entry.link:
  308. if a_link.rel == 'edit':
  309. return self.Delete(a_link.href)
  310. class DocumentQuery(gdata.service.Query):
  311. def _GetTitleQuery(self):
  312. return self['title']
  313. def _SetTitleQuery(self, document_query):
  314. self['title'] = document_query
  315. title = property(_GetTitleQuery, _SetTitleQuery,
  316. doc="""The title query parameter""")
  317. def _GetTitleExactQuery(self):
  318. return self['title-exact']
  319. def _SetTitleExactQuery(self, document_query):
  320. self['title-exact'] = document_query
  321. title_exact = property(_GetTitleExactQuery, _SetTitleExactQuery,
  322. doc="""The title-exact query parameter""")
  323. class CellQuery(gdata.service.Query):
  324. def _GetMinRowQuery(self):
  325. return self['min-row']
  326. def _SetMinRowQuery(self, cell_query):
  327. self['min-row'] = cell_query
  328. min_row = property(_GetMinRowQuery, _SetMinRowQuery,
  329. doc="""The min-row query parameter""")
  330. def _GetMaxRowQuery(self):
  331. return self['max-row']
  332. def _SetMaxRowQuery(self, cell_query):
  333. self['max-row'] = cell_query
  334. max_row = property(_GetMaxRowQuery, _SetMaxRowQuery,
  335. doc="""The max-row query parameter""")
  336. def _GetMinColQuery(self):
  337. return self['min-col']
  338. def _SetMinColQuery(self, cell_query):
  339. self['min-col'] = cell_query
  340. min_col = property(_GetMinColQuery, _SetMinColQuery,
  341. doc="""The min-col query parameter""")
  342. def _GetMaxColQuery(self):
  343. return self['max-col']
  344. def _SetMaxColQuery(self, cell_query):
  345. self['max-col'] = cell_query
  346. max_col = property(_GetMaxColQuery, _SetMaxColQuery,
  347. doc="""The max-col query parameter""")
  348. def _GetRangeQuery(self):
  349. return self['range']
  350. def _SetRangeQuery(self, cell_query):
  351. self['range'] = cell_query
  352. range = property(_GetRangeQuery, _SetRangeQuery,
  353. doc="""The range query parameter""")
  354. def _GetReturnEmptyQuery(self):
  355. return self['return-empty']
  356. def _SetReturnEmptyQuery(self, cell_query):
  357. self['return-empty'] = cell_query
  358. return_empty = property(_GetReturnEmptyQuery, _SetReturnEmptyQuery,
  359. doc="""The return-empty query parameter""")
  360. class ListQuery(gdata.service.Query):
  361. def _GetSpreadsheetQuery(self):
  362. return self['sq']
  363. def _SetSpreadsheetQuery(self, list_query):
  364. self['sq'] = list_query
  365. sq = property(_GetSpreadsheetQuery, _SetSpreadsheetQuery,
  366. doc="""The sq query parameter""")
  367. def _GetOrderByQuery(self):
  368. return self['orderby']
  369. def _SetOrderByQuery(self, list_query):
  370. self['orderby'] = list_query
  371. orderby = property(_GetOrderByQuery, _SetOrderByQuery,
  372. doc="""The orderby query parameter""")
  373. def _GetReverseQuery(self):
  374. return self['reverse']
  375. def _SetReverseQuery(self, list_query):
  376. self['reverse'] = list_query
  377. reverse = property(_GetReverseQuery, _SetReverseQuery,
  378. doc="""The reverse query parameter""")