File: calendar_resource_example.py

package info (click to toggle)
python-gdata 2.0.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 8,460 kB
  • ctags: 17,143
  • sloc: python: 70,779; ansic: 150; makefile: 27; sh: 3
file content (184 lines) | stat: -rw-r--r-- 6,235 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sample app for Google Apps Calendar Resource features.

  CalendarResourceSample: Demonstrates the use of the Calendar Resource API
"""


__author__ = 'pti@google.com (Prashant Tiwari)'


import getpass
from gdata.calendar_resource.client import CalendarResourceClient


class CalendarResourceSample(object):
  def __init__(self, domain, email, password):
    """Constructor for the CalendarResourceSample object.

    Construct a CalendarResourceSample with the given args.

    Args:
      domain: The domain name ("domain.com")
      email: The email account of the user or the admin ("john@domain.com")
      password: The domain admin's password
    """
    self.client = CalendarResourceClient(domain=domain)
    self.client.ClientLogin(email=email, password=password,
                            source='googlecode-calendarresourcesample-v1')


  def create(self, resource_properties):
    """Creates a calendar resource with the given resource_properties
    
    Args:
      resource_properties: A dictionary of calendar resource properties
    """
    print 'Creating a new calendar resource with id %s...' % (
        resource_properties['resource_id'])
    print self.client.CreateResource(
        resource_id=resource_properties['resource_id'],
        resource_common_name=resource_properties['resource_name'],
        resource_description=resource_properties['resource_description'],
        resource_type=resource_properties['resource_type'])

  def get(self, resource_id=None):
    """Retrieves the calendar resource with the given resource_id
    
    Args:
      resource_id: The optional calendar resource identifier
    """
    if resource_id:
      print 'Retrieving the calendar resource with id %s...' % (resource_id)
      print self.client.GetResource(resource_id=resource_id)
    else:
      print 'Retrieving all calendar resources...'
      print self.client.GetResourceFeed()

  def update(self, resource_properties):
    """Updates the calendar resource with the given resource_properties
    
    Args:
      resource_properties: A dictionary of calendar resource properties
    """
    print 'Updating the calendar resource with id %s...' % (
        resource_properties['resource_id'])
    print self.client.UpdateResource(
        resource_id=resource_properties['resource_id'],
        resource_common_name=resource_properties['resource_name'],
        resource_description=resource_properties['resource_description'],
        resource_type=resource_properties['resource_type'])

  def delete(self, resource_id):
    """Deletes the calendar resource with the given resource_id
    
    Args:
      resource_id: The unique calendar resource identifier
    """
    print 'Deleting the calendar resource with id %s...' % (resource_id)
    self.client.DeleteResource(resource_id)
    print 'Calendar resource successfully deleted.'


def main():
  """Demonstrates the Calendar Resource API using CalendarResourceSample."""
  domain = None
  admin_email = None
  admin_password = None
  do_continue = 'y'
  print("Google Apps Calendar Resource API Sample\n\n")
  while not domain:
    domain = raw_input('Google Apps domain: ')
  while not admin_email:
    admin_email = '%s@%s' % (raw_input('Administrator username: '), domain)
  while not admin_password:
    admin_password = getpass.getpass('Administrator password: ')

  sample = CalendarResourceSample(domain=domain, email=admin_email,
                                  password=admin_password)

  while do_continue.lower() != 'n':
    do_continue = call_service(sample)


def call_service(sample):
  """Calls the service methods on the user input"""
  operation = None

  while operation not in ['c', 'C', 'g', 'G', 'u', 'U', 'd', 'D', 'q', 'Q']:
    operation = raw_input('Do [c=create|g=get|u=update|d=delete|q=quit]: ')

  operation = operation.lower()

  if operation == 'q':
    return 'n'

  resource_properties = get_input(operation)

  if operation == 'c':
    sample.create(resource_properties)
  elif operation == 'g':
    sample.get(resource_properties['resource_id'])
  elif operation == 'u':
    sample.update(resource_properties)
  elif operation == 'd':
    sample.delete(resource_properties['resource_id'])

  do_continue = None
  while do_continue not in ['', 'y', 'Y', 'n', 'N']:
    do_continue = raw_input('Want to continue (Y/n): ')

  if do_continue == '':
    do_continue = 'y'

  return do_continue.lower()


def get_input(operation):
  """Gets user input from console"""
  resource_id = None
  resource_name = None
  resource_description = None
  resource_type = None

  if operation == 'g':
    resource_id = raw_input('Resource id (leave blank to get all resources): ')
  else:
    while not resource_id:
      resource_id = raw_input('Resource id: ')

  if operation == 'c':
    resource_name = raw_input('Resource common name (recommended): ')
    resource_description = raw_input('Resource description (recommended): ')
    resource_type = raw_input('Resource type (recommended): ')
  elif operation == 'u':
    resource_name = raw_input(
        'New resource common name (leave blank if no change): ')
    resource_description = raw_input(
        'New resource description (leave blank if no change): ')
    resource_type = raw_input('New resource type (leave blank if no change): ')

  resource_properties = {'resource_id': resource_id,
                         'resource_name': resource_name,
                         'resource_description': resource_description,
                         'resource_type': resource_type}
  return resource_properties


if __name__ == '__main__':
  main()