conduit r1462 - in trunk: . conduit/modules/GoogleModule test/python-tests



Author: jstowers
Date: Sun May 11 13:23:48 2008
New Revision: 1462
URL: http://svn.gnome.org/viewvc/conduit?rev=1462&view=rev

Log:
2008-05-12  John Stowers  <john stowers gmail com>

	* conduit/modules/GoogleModule/GoogleModule.py:
	* conduit/modules/GoogleModule/documents-config.glade:
	* test/python-tests/TestDataProviderGoogleDocuments.py: Small fixes to
	google docs.



Added:
   trunk/conduit/modules/GoogleModule/documents-config.glade
Modified:
   trunk/ChangeLog
   trunk/conduit/modules/GoogleModule/GoogleModule.py
   trunk/test/python-tests/TestDataProviderGoogleDocuments.py

Modified: trunk/conduit/modules/GoogleModule/GoogleModule.py
==============================================================================
--- trunk/conduit/modules/GoogleModule/GoogleModule.py	(original)
+++ trunk/conduit/modules/GoogleModule/GoogleModule.py	Sun May 11 13:23:48 2008
@@ -124,8 +124,7 @@
         return '/calendar/feeds/' + self.get_uri() + '/private/full'
 
 def convert_madness_to_datetime(inputDate):
-    log.debug('Attempting to parse the following: %s'%inputDate)
-    logging.debug('Attempting to parse the following: %s'%inputDate)
+    log.debug('Attempting to parse: %s' % inputDate)
     dateStr = None
     dateDate = None
     dateDateTime = None
@@ -151,7 +150,7 @@
         
     if dateDateTime is not None:
         if dateDateTime.tzinfo is not None:
-            logging.warn("returning: %s",dateDateTime)
+            log.warn("returning: %s",dateDateTime)
             ts = dateDateTime.timetuple()
             dateDateTime = dateDateTime.fromtimestamp(time.mktime(ts))
             return dateDateTime
@@ -988,6 +987,34 @@
             self._set_username(username.get_text())
             self._set_password(password.get_text())
         dlg.destroy()    
+        
+class _GoogleDocument:
+    def __init__(self, doc):
+        self.id = doc.id.text
+        #raw text version link
+        self.raw = doc.content.src
+        #edit link
+        self.link = doc.GetAlternateLink().href
+        self.title = doc.title.text.encode('UTF-8')
+        self.authorName = doc.author[0].name.text
+        self.authorEmail = doc.author[0].email.text
+        self.type = doc.category[0].label
+        
+        self.updated = convert_madness_to_datetime(doc.updated.text)
+        self.docid = self.get_document_id(self.link)
+        
+    # Parses the document id out of the alternate link url, the atom feed
+    # doesn't actually provide the document id
+    @staticmethod
+    def get_document_id(LUID):
+        from urlparse import *
+        parsed_url = urlparse(LUID)
+        url_params = parsed_url[4]
+        document_id = url_params.split('=')[1]
+        return document_id
+        
+    def __str__(self):
+        return "%s:%s by %s (modified:%s) (id:%s)" % (self.type,self.title,self.authorName,self.updated,self.docid)
 
 class DocumentsSink(GoogleBase,  DataProvider.DataSink):
     """
@@ -1003,7 +1030,7 @@
     _icon_ = "applications-office"
     
     SUPPORTED_DOCUMENTS = ('DOC','ODT','SWX','TXT','RTF','HTM','HTML')
-    SUPPORTED_SHEETS = ('ODS','XLS','CSV','TSV')
+    SUPPORTED_SPREADSHEETS = ('ODS','XLS','CSV','TSV')
     SUPPORTED_PRESENTATIONS = ('PPT','PPS')
     
     TYPE_DOCUMENT = 'document'
@@ -1015,6 +1042,12 @@
         DataProvider.DataSink.__init__(self)
         self.service = gdata.docs.service.DocsService()
         
+        self.documentFormat = 'ODT'
+        self.spreadsheetFormat = 'ODS'
+        self.presentationFormat = 'PPT'
+        
+        self._docs = {}
+        
     def _do_login(self):
         self.service.ClientLogin(self.username, self.password)
         
@@ -1029,7 +1062,7 @@
         #upload using the appropriate service
         if ext in self.SUPPORTED_DOCUMENTS:
             entry = self.service.UploadDocument(ms,name)
-        elif ext in self.SUPPORTED_SHEETS:
+        elif ext in self.SUPPORTED_SPREADSHEETS:
             entry = self.service.UploadSpreadsheet(ms,name)
         elif ext in self.SUPPORTED_PRESENTATIONS:
             entry = self.service.UploadPresentation(ms,name)
@@ -1040,48 +1073,67 @@
         return entry.id.text
         
     def _get_all_documents(self):
+        docs = {}
         feed = self.service.GetDocumentListFeed()
-        if not feed.entry:
-            return []
-        return [str(doc.id.text) for doc in feed.entry]
+        if feed.entry:
+            for xmldoc in feed.entry:
+                docs[xmldoc.id.text] = _GoogleDocument(xmldoc)
+        return docs
         
     def _get_document(self, LUID):
         if not LUID:
             return None
 
+        #try cached doc first
+        if LUID in self._docs:
+            return self._docs[LUID]
+
         #get the gdata contact from google
         try:
-            gd = self.service.GetDocumentListEntry(LUID)
+            xmldoc = self.service.GetDocumentListEntry(LUID)
         except gdata.service.RequestError:
             return None
 
-        return gd
-        
-    # Parses the document id out of the alternate link url, the atom feed
-    # doesn't actually provide the document id      
-    def _get_document_id(self, LUID):
-        from urlparse import *
-        parsed_url = urlparse(LUID)
-        url_params = parsed_url[4]
-        document_id = url_params.split('=')[1]
-        return document_id
+        return _GoogleDocument(xmldoc)
         
-    def _download_doc(self, LUID):
-        docid = self._get_document_id(LUID)
+    def _download_doc(self, googleDoc):
+        docid = googleDoc.docid
     
-        #self.service.debug = True
-        #https://docs.google.com/MiscCommands?command=saveasdoc&exportformat=%s&docID=%s
-        resp = atom.service.HttpRequest(
-                                service=self.service,
-                                operation='GET',
-                                data=None,
-                                uri='/MiscCommands',
-                                extra_headers={'Authorization':self.service._GetAuthToken()},
-                                url_params={'command':'saveasdoc','exportformat':'doc','docID':docid},
-                                escape_params=True,
-                                content_type='application/atom+xml')
+        #print self.service.server
+        #return 
+        self.service.debug = True
+        
+        if googleDoc.type in ("document","presentation"):
+            format = "pdf"
+            #https://docs.google.com/MiscCommands?command=saveasdoc&exportformat=%s&docID=%s
+            resp = atom.service.HttpRequest(
+                                    service=self.service,
+                                    operation='GET',
+                                    data=None,
+                                    uri='/MiscCommands',
+                                    extra_headers={'Authorization':self.service._GetAuthToken()},
+                                    url_params={'command':'saveasdoc','exportformat':format,'docID':docid},
+                                    escape_params=True,
+                                    content_type='application/atom+xml')
+        elif False:#NOT WORKING googleDoc.type == "spreadsheet":
+            format = "xls"
+            #https://spreadsheets.google.com/ccc?output=%s&key=%s
+            #http://spreadsheets.google.com/fm?key=%s&fmcmd=4&hl=en
+            #self.service.server = "spreadsheets.google.com"
+            resp = atom.service.HttpRequest(
+                                    service=self.service,
+                                    operation='GET',
+                                    data=None,
+                                    uri='/ccc',
+                                    extra_headers={'Authorization':self.service._GetAuthToken()},
+                                    url_params={'output':format,'key':docid},
+                                    escape_params=True,
+                                    content_type='application/atom+xml')
+        else:
+            log.warn("Unknown format")
+            return None
 
-        path = "/home/john/Desktop/%s.doc" % docid
+        path = "/home/john/Desktop/%s.%s" % (docid, format)
         file_handle = open(path, 'wb')
         file_handle.write(resp.read())
         file_handle.close()
@@ -1093,6 +1145,13 @@
         self._login()
         if not self.loggedIn:
             raise Exceptions.RefreshError("Could not log in")
+            
+    def get_all(self):
+        self._docs = self._get_all_documents()
+        return self._docs.keys()
+        
+    def get(self, LUID):
+        pass
 
 #    def put(self, doc, overwrite, LUID=None):            
 #        #Check if we have already uploaded the document
@@ -1136,10 +1195,27 @@
         """
         Configures the PicasaTwoWay
         """
+        import gtk
+
+        def make_combo(widget, docType, val, values):
+            cb = widget.get_widget("%sCombo" % docType)
+            store = gtk.ListStore(str)
+            cell = gtk.CellRendererText()
+            
+            cb.set_model(store)
+            cb.pack_start(cell, True)
+            cb.add_attribute(cell, 'text', 0)
+            
+            for name in values:
+                rowref = store.append( (name,) )
+                if name == val:
+                    cb.set_active_iter(rowref)
+            
+
         widget = Utils.dataprovider_glade_get_widget(
                         __file__, 
-                        "contacts-config.glade", 
-                        "GoogleContactsConfigDialog")
+                        "documents-config.glade", 
+                        "GoogleDocumentsConfigDialog")
                         
         #get a whole bunch of widgets
         username = widget.get_widget("username")
@@ -1148,8 +1224,12 @@
         #preload the widgets        
         username.set_text(self.username)
         password.set_text(self.password)
+
+        #preload the combos
+        for i in (("document", self.documentFormat,self.SUPPORTED_DOCUMENTS),("spreadsheet", self.spreadsheetFormat,self.SUPPORTED_SPREADSHEETS),("presentation",self.presentationFormat,self.SUPPORTED_PRESENTATIONS)):
+            make_combo(widget, *i)
         
-        dlg = widget.get_widget("GoogleContactsConfigDialog")
+        dlg = widget.get_widget("GoogleDocumentsConfigDialog")
         response = Utils.run_dialog (dlg, window)
         if response == True:
             self._set_username(username.get_text())

Added: trunk/conduit/modules/GoogleModule/documents-config.glade
==============================================================================
--- (empty file)
+++ trunk/conduit/modules/GoogleModule/documents-config.glade	Sun May 11 13:23:48 2008
@@ -0,0 +1,216 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
+<!--*- mode: xml -*-->
+<glade-interface>
+  <widget class="GtkDialog" id="GoogleDocumentsConfigDialog">
+    <property name="visible">True</property>
+    <property name="title" translatable="yes">Google Documents</property>
+    <property name="resizable">False</property>
+    <property name="default_width">250</property>
+    <property name="default_height">350</property>
+    <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
+    <child internal-child="vbox">
+      <widget class="GtkVBox" id="vbox30">
+        <property name="visible">True</property>
+        <property name="spacing">5</property>
+        <child>
+          <widget class="GtkLabel" id="label74">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">&lt;b&gt;Account Details&lt;/b&gt;</property>
+            <property name="use_markup">True</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkLabel" id="label75">
+            <property name="visible">True</property>
+            <property name="xalign">0</property>
+            <property name="label" translatable="yes">Email:</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">3</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkEntry" id="username">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">4</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkLabel" id="label76">
+            <property name="visible">True</property>
+            <property name="xalign">0</property>
+            <property name="label" translatable="yes">Password:</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">5</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkEntry" id="password">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="visibility">False</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">6</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkHButtonBox" id="hbuttonbox13">
+            <property name="visible">True</property>
+            <property name="layout_style">GTK_BUTTONBOX_END</property>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">7</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkLabel" id="label1">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">&lt;b&gt;Document Format&lt;/b&gt;</property>
+            <property name="use_markup">True</property>
+          </widget>
+          <packing>
+            <property name="position">8</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkHBox" id="hbox1">
+            <property name="visible">True</property>
+            <child>
+              <widget class="GtkLabel" id="label2">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Documents</property>
+                <property name="width_chars">13</property>
+              </widget>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
+            <child>
+              <widget class="GtkComboBox" id="documentCombo">
+                <property name="visible">True</property>
+              </widget>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="position">9</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkHBox" id="hbox2">
+            <property name="visible">True</property>
+            <child>
+              <widget class="GtkLabel" id="label3">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Spreadsheets</property>
+                <property name="width_chars">13</property>
+              </widget>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
+            <child>
+              <widget class="GtkComboBox" id="spreadsheetCombo">
+                <property name="visible">True</property>
+              </widget>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="position">10</property>
+          </packing>
+        </child>
+        <child>
+          <widget class="GtkHBox" id="hbox3">
+            <property name="visible">True</property>
+            <child>
+              <widget class="GtkLabel" id="label4">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Presentations</property>
+                <property name="width_chars">13</property>
+              </widget>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
+            <child>
+              <widget class="GtkComboBox" id="presentationCombo">
+                <property name="visible">True</property>
+              </widget>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="position">11</property>
+          </packing>
+        </child>
+        <child internal-child="action_area">
+          <widget class="GtkHButtonBox" id="hbuttonbox12">
+            <property name="visible">True</property>
+            <property name="layout_style">GTK_BUTTONBOX_END</property>
+            <child>
+              <widget class="GtkButton" id="button32">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="can_default">True</property>
+                <property name="label">gtk-cancel</property>
+                <property name="use_stock">True</property>
+                <property name="response_id">-6</property>
+              </widget>
+            </child>
+            <child>
+              <widget class="GtkButton" id="okBtn">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="can_default">True</property>
+                <property name="label">gtk-ok</property>
+                <property name="use_stock">True</property>
+                <property name="response_id">-5</property>
+              </widget>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">GTK_PACK_END</property>
+          </packing>
+        </child>
+      </widget>
+    </child>
+  </widget>
+</glade-interface>

Modified: trunk/test/python-tests/TestDataProviderGoogleDocuments.py
==============================================================================
--- trunk/test/python-tests/TestDataProviderGoogleDocuments.py	(original)
+++ trunk/test/python-tests/TestDataProviderGoogleDocuments.py	Sun May 11 13:23:48 2008
@@ -17,30 +17,29 @@
 except Exception, err:
     ok("Logged in (%s)" % err, False) 
 
-docs = google._get_all_documents()
-for d in docs:
-    print "DOC: %s" % d
-
-doc = google._get_document(d)
-#print doc info
-info = {"raw txt link":doc.content.src,
-        "link":doc.GetAlternateLink().href,
-        "title": doc.title.text.encode('UTF-8'),
-        "updated":doc.updated.text,
-        "author_name":doc.author[0].name.text,
-        "author_email":doc.author[0].email.text,
-        "type":doc.category[0].label}
-for k,v in info.items():
-    print "\tINFO %s=%s" % (k,v)
-#for c in doc.category:
-#    print "CAT: %s" % c.label
+docs = google.get_all()
+num = len(docs)
+ok("Got %s documents" % num, num > 0)
 
-path = google._download_doc(info['link'])
-print "DL: %s" % path
+doc = google._get_document(docs[-1])
+ok("Got safe document", doc != None)
 
-f = File.File(URI="/home/john/Desktop/test.odt")
+
+
+#finished()
+
+
+
+#path = google._download_doc(info['link'])
+#print "DL: %s" % path
+
+f = File.File(URI="/home/john/Desktop/test.ppt")
 LUID = google._upload_document(f)
-print "UL: %s" % LUID
+doc = google._get_document(LUID)
+
+ok("Upload document: %s" % doc, doc != None)
 
+path = google._download_doc(doc)
+print path
 
 finished()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]