source: products/quintagroup.gdocs.spreadsheet/trunk/quintagroup/gdocs/spreadsheet/adapters.py @ 2682

Last change on this file since 2682 was 2682, checked in by mylan, 9 years ago

Added GSpreadsheetDataProvider adapter

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1from zope.interface import implements
2from zope.component import adapts, queryUtility
3
4from gdata.spreadsheet.text_db import Table
5from gdata.spreadsheet.text_db import DatabaseClient
6from gdata.spreadsheet.text_db import ConvertStringsToColumnHeaders
7from gdata.spreadsheet import SpreadsheetsListFeed
8from gdata.spreadsheet import SpreadsheetsCellsFeedFromString
9from gdata.spreadsheet.service import SpreadsheetsService
10from gdata.spreadsheet.service import CellQuery as shCellQuery
11from gdata.spreadsheet.service import DocumentQuery as shDocumentQuery
12
13from quintagroup.gdocs.spreadsheet import logException, logger
14from quintagroup.gdocs.spreadsheet.interfaces import IGSpreadsheet
15from quintagroup.gdocs.spreadsheet.interfaces import IGSpreadsheetDataProvider
16
17
18class GSpreadsheetDataProvider(object):
19
20    adapts(IGSpreadsheet)
21    implements(IGSpreadsheetDataProvider)
22
23    def __init__(self, context):
24        gauth = queryUtility(IGAuthUtility)
25        self.context = context
26        self.dbcl = DatabaseClient(gauth.email, gauth.password)
27        self.shcl = SpreadsheetsService(gauth.email, gauth.password)
28        self.shcl.ProgrammaticLogin()
29
30    def getListFeed(self, startrow_idx=0, query=None):
31        """ Get SpreadsheetsListFeed
32        """
33        if query is None:
34            query = shDocumentQuery()
35        return self._safeQuery(self.shcl.GetListFeed,
36                              self.context.spreadsheet_id,
37                              wksht_id=self.context.worksheet_id,
38                              query=query)
39
40    def getWorksheetColumnsInfo(self, maxr='1', minr='1'):
41        first_row_contents = []
42        query = shCellQuery()
43        query.max_row = maxr
44        query.min_row = minr
45        feed = self._safeQuery(
46            self.dbcl._GetSpreadsheetsClient().GetCellsFeed,
47            self.context.spreadsheet_id,
48            wksht_id=self.context.worksheet_id,
49            query=query)
50        for entry in feed.entry:
51            first_row_contents.append(entry.content.text)
52        # Get the next set of cells if needed.
53        next_link = feed.GetNextLink()
54        while next_link:
55            feed = self._safeQuery(
56                self.dbcl._GetSpreadsheetsClient().Get,
57                next_link.href, converter=SpreadsheetsCellsFeedFromString)
58            for entry in feed.entry:
59                first_row_contents.append(entry.content.text)
60            next_link = feed.GetNextLink()
61        # Convert the contents of the cells to valid headers.
62        return ConvertStringsToColumnHeaders(first_row_contents)
63
64    def _safeQuery(self, meth, *margs, **mkwargs):
65        # Make safe method call with logging information about exception
66        try:
67            return meth(*margs, **mkwargs)
68        except Exception:
69            logException('%s function call: key=%s, wksht_id=%s' % (
70                meth.__name__, self.context.spreadsheet_id,
71                self.context.worksheet_id), self.context)
72        return None
Note: See TracBrowser for help on using the repository browser.