|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright 2009 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +"""Samples for the Documents List API v3.""" |
| 18 | + |
| 19 | +__author__ = 'afshar@google.com (Ali Afshar)' |
| 20 | + |
| 21 | +import os.path |
| 22 | +import gdata.data |
| 23 | +import gdata.acl.data |
| 24 | +import gdata.docs.client |
| 25 | +import gdata.docs.data |
| 26 | +import gdata.sample_util |
| 27 | + |
| 28 | + |
| 29 | +class SampleConfig(object): |
| 30 | + APP_NAME = 'GDataDocumentsListAPISample-v1.0' |
| 31 | + DEBUG = False |
| 32 | + |
| 33 | + |
| 34 | +def CreateClient(): |
| 35 | + """Create a Documents List Client.""" |
| 36 | + client = gdata.docs.client.DocsClient(source=SampleConfig.APP_NAME) |
| 37 | + client.http_client.debug = SampleConfig.DEBUG |
| 38 | + # Authenticate the user with CLientLogin, OAuth, or AuthSub. |
| 39 | + try: |
| 40 | + gdata.sample_util.authorize_client( |
| 41 | + client, |
| 42 | + service=client.auth_service, |
| 43 | + source=client.source, |
| 44 | + scopes=client.auth_scopes |
| 45 | + ) |
| 46 | + except gdata.client.BadAuthentication: |
| 47 | + exit('Invalid user credentials given.') |
| 48 | + except gdata.client.Error: |
| 49 | + exit('Login Error') |
| 50 | + return client |
| 51 | + |
| 52 | + |
| 53 | +def PrintResource(resource): |
| 54 | + """Display a resource to Standard Out.""" |
| 55 | + print resource.resource_id.text, resource.GetResourceType() |
| 56 | + |
| 57 | + |
| 58 | +def PrintFeed(feed): |
| 59 | + """Display a feed to Standard Out.""" |
| 60 | + for entry in feed.entry: |
| 61 | + PrintResource(entry) |
| 62 | + |
| 63 | + |
| 64 | +def _GetDataFilePath(name): |
| 65 | + return os.path.join( |
| 66 | + os.path.dirname( |
| 67 | + os.path.dirname( |
| 68 | + os.path.dirname(__file__))), |
| 69 | + 'tests', 'gdata_tests', 'docs', 'data', name) |
| 70 | + |
| 71 | + |
| 72 | +def GetResourcesSample(): |
| 73 | + """Get and display first page of resources.""" |
| 74 | + client = CreateClient() |
| 75 | + # Get a feed and print it |
| 76 | + feed = client.GetResources() |
| 77 | + PrintFeed(feed) |
| 78 | + |
| 79 | + |
| 80 | +def GetAllResourcesSample(): |
| 81 | + """Get and display all resources, using pagination.""" |
| 82 | + client = CreateClient() |
| 83 | + # Unlike client.GetResources, this returns a list of resources |
| 84 | + for resource in client.GetAllResources(): |
| 85 | + PrintResource(resource) |
| 86 | + |
| 87 | + |
| 88 | +def GetResourceSample(): |
| 89 | + """Fetch 5 resources from a feed, then again individually.""" |
| 90 | + client = CreateClient() |
| 91 | + for e1 in client.GetResources(limit=5).entry: |
| 92 | + e2 = client.GetResource(e1) |
| 93 | + print 'Refetched: ', e2.title.text, e2.resource_id.text |
| 94 | + |
| 95 | + |
| 96 | +def GetMetadataSample(): |
| 97 | + """Get and display the Metadata for the current user.""" |
| 98 | + client = CreateClient() |
| 99 | + # Fetch the metadata entry and display bits of it |
| 100 | + metadata = client.GetMetadata() |
| 101 | + print 'Quota' |
| 102 | + print ' Total:', metadata.quota_bytes_total.text |
| 103 | + print ' Used:', metadata.quota_bytes_used.text |
| 104 | + print ' Trashed:', metadata.quota_bytes_used_in_trash.text |
| 105 | + print 'Import / Export' |
| 106 | + for input_format in metadata.import_formats: |
| 107 | + print ' Import:', input_format.source, 'to', input_format.target |
| 108 | + for export_format in metadata.export_formats: |
| 109 | + print ' Export:', export_format.source, 'to', export_format.target |
| 110 | + print 'Features' |
| 111 | + for feature in metadata.features: |
| 112 | + print ' Feature:', feature.name.text |
| 113 | + print 'Upload Sizes' |
| 114 | + for upload_size in metadata.max_upload_sizes: |
| 115 | + print ' Kind:', upload_size.kind, upload_size.text |
| 116 | + |
| 117 | + |
| 118 | +def GetChangesSample(): |
| 119 | + """Get and display the Changes for the user.""" |
| 120 | + client = CreateClient() |
| 121 | + changes = client.GetChanges() |
| 122 | + for change in changes.entry: |
| 123 | + print change.title.text, change.changestamp.value |
| 124 | + |
| 125 | + |
| 126 | +def GetResourceAclSample(): |
| 127 | + """Get and display the ACL for a resource.""" |
| 128 | + client = CreateClient() |
| 129 | + for resource in client.GetResources(limit=5).entry: |
| 130 | + acl_feed = client.GetResourceAcl(resource) |
| 131 | + for acl in acl_feed.entry: |
| 132 | + print acl.role.value, acl.scope.type, acl.scope.value |
| 133 | + |
| 134 | + |
| 135 | +def CreateEmptyResourceSample(): |
| 136 | + """Create an empty resource of type document.""" |
| 137 | + client = CreateClient() |
| 138 | + document = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 139 | + document = client.CreateResource(document) |
| 140 | + print 'Created:', document.title.text, document.resource_id.text |
| 141 | + |
| 142 | + |
| 143 | +def CreateCollectionSample(): |
| 144 | + """Create an empty collection.""" |
| 145 | + client = CreateClient() |
| 146 | + col = gdata.docs.data.Resource(type='folder', title='My Sample Folder') |
| 147 | + col = client.CreateResource(col) |
| 148 | + print 'Created collection:', col.title.text, col.resource_id.text |
| 149 | + |
| 150 | + |
| 151 | +def CreateResourceInCollectionSample(): |
| 152 | + """Create a collection, then create a document in it.""" |
| 153 | + client = CreateClient() |
| 154 | + col = gdata.docs.data.Resource(type='folder', title='My Sample Folder') |
| 155 | + col = client.CreateResource(col) |
| 156 | + print 'Created collection:', col.title.text, col.resource_id.text |
| 157 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 158 | + doc = client.CreateResource(doc, collection=col) |
| 159 | + print 'Created:', doc.title.text, doc.resource_id.text |
| 160 | + |
| 161 | + |
| 162 | +def UploadResourceSample(): |
| 163 | + """Upload a document, and convert to Google Docs.""" |
| 164 | + client = CreateClient() |
| 165 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 166 | + # This is a convenient MS Word doc that we know exists |
| 167 | + path = _GetDataFilePath('test.0.doc') |
| 168 | + print 'Selected file at: %s' % path |
| 169 | + # Create a MediaSource, pointing to the file |
| 170 | + media = gdata.data.MediaSource() |
| 171 | + media.SetFileHandle(path, 'application/msword') |
| 172 | + # Pass the MediaSource when creating the new Resource |
| 173 | + doc = client.CreateResource(doc, media=media) |
| 174 | + print 'Created, and uploaded:', doc.title.text, doc.resource_id.text |
| 175 | + |
| 176 | + |
| 177 | +def UploadUnconvertedFileSample(): |
| 178 | + """Upload a document, unconverted.""" |
| 179 | + client = CreateClient() |
| 180 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Raw Doc') |
| 181 | + path = _GetDataFilePath('test.0.doc') |
| 182 | + media = gdata.data.MediaSource() |
| 183 | + media.SetFileHandle(path, 'application/msword') |
| 184 | + # Pass the convert=false parameter |
| 185 | + create_uri = gdata.docs.client.RESOURCE_UPLOAD_URI + '?convert=false' |
| 186 | + doc = client.CreateResource(doc, create_uri=create_uri, media=media) |
| 187 | + print 'Created, and uploaded:', doc.title.text, doc.resource_id.text |
| 188 | + |
| 189 | + |
| 190 | +def DeleteResourceSample(): |
| 191 | + """Delete a resource (after creating it).""" |
| 192 | + client = CreateClient() |
| 193 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 194 | + doc = client.CreateResource(doc) |
| 195 | + # Delete the resource we just created. |
| 196 | + client.DeleteResource(doc) |
| 197 | + |
| 198 | + |
| 199 | +def AddAclSample(): |
| 200 | + """Delete a resource (after creating it).""" |
| 201 | + client = CreateClient() |
| 202 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 203 | + doc = client.CreateResource(doc) |
| 204 | + acl_entry = gdata.docs.data.AclEntry( |
| 205 | + scope=gdata.acl.data.AclScope(value='user@example.com', type='user'), |
| 206 | + role=gdata.acl.data.AclRole(value='reader'), |
| 207 | + ) |
| 208 | + client.AddAclEntry(doc, acl_entry, send_notification=False) |
| 209 | + |
| 210 | + |
| 211 | +def AddAclBatchSample(): |
| 212 | + """Add a list of ACLs as a batch.""" |
| 213 | + client = CreateClient() |
| 214 | + doc = gdata.docs.data.Resource(type='document', title='My Sample Doc') |
| 215 | + doc = client.CreateResource(doc) |
| 216 | + acl1 = gdata.docs.data.AclEntry( |
| 217 | + scope=gdata.acl.data.AclScope(value='user1@example.com', type='user'), |
| 218 | + role=gdata.acl.data.AclRole(value='reader'), |
| 219 | + batch_operation=gdata.data.BatchOperation(type='insert'), |
| 220 | + ) |
| 221 | + acl2 = gdata.docs.data.AclEntry( |
| 222 | + scope=gdata.acl.data.AclScope(value='user2@example.com', type='user'), |
| 223 | + role=gdata.acl.data.AclRole(value='reader'), |
| 224 | + batch_operation=gdata.data.BatchOperation(type='insert'), |
| 225 | + ) |
| 226 | + # Create a list of operations to perform together. |
| 227 | + acl_operations = [acl1, acl2] |
| 228 | + # Perform the operations. |
| 229 | + client.BatchProcessAclEntries(doc, acl_operations) |
| 230 | + |
| 231 | + |
| 232 | +def GetRevisionsSample(): |
| 233 | + """Get the revision history for resources.""" |
| 234 | + client = CreateClient() |
| 235 | + for entry in client.GetResources(limit=55).entry: |
| 236 | + revisions = client.GetRevisions(entry) |
| 237 | + for revision in revisions.entry: |
| 238 | + print revision.publish, revision.GetPublishLink() |
| 239 | + |
| 240 | + |
| 241 | +if __name__ == '__main__': |
| 242 | + import samplerunner |
| 243 | + samplerunner.Run(__file__) |
0 commit comments