PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/boto-2.5.2/boto/ec2/tag.py

#
Python | 87 lines | 40 code | 14 blank | 33 comment | 3 complexity | e8bfbfd0ad4d094a8e4683cee9c8dde8 MD5 | raw file
 1# Copyright (c) 2010 Mitch Garnaat http://garnaat.org/
 2# Copyright (c) 2010, Eucalyptus Systems, Inc.
 3#
 4# Permission is hereby granted, free of charge, to any person obtaining a
 5# copy of this software and associated documentation files (the
 6# "Software"), to deal in the Software without restriction, including
 7# without limitation the rights to use, copy, modify, merge, publish, dis-
 8# tribute, sublicense, and/or sell copies of the Software, and to permit
 9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23class TagSet(dict):
24    """
25    A TagSet is used to collect the tags associated with a particular
26    EC2 resource.  Not all resources can be tagged but for those that
27    can, this dict object will be used to collect those values.  See
28    :class:`boto.ec2.ec2object.TaggedEC2Object` for more details.
29    """
30    
31    def __init__(self, connection=None):
32        self.connection = connection
33        self._current_key = None
34        self._current_value = None
35
36    def startElement(self, name, attrs, connection):
37        if name == 'item':
38            self._current_key = None
39            self._current_value = None
40        return None
41
42    def endElement(self, name, value, connection):
43        if name == 'key':
44            self._current_key = value
45        elif name == 'value':
46            self._current_value = value
47        elif name == 'item':
48            self[self._current_key] = self._current_value
49
50
51class Tag(object):
52    """
53    A Tag is used when creating or listing all tags related to
54    an AWS account.  It records not only the key and value but
55    also the ID of the resource to which the tag is attached
56    as well as the type of the resource.
57    """
58    
59    def __init__(self, connection=None, res_id=None, res_type=None,
60                 name=None, value=None):
61        self.connection = connection
62        self.res_id = res_id
63        self.res_type = res_type
64        self.name = name
65        self.value = value
66
67    def __repr__(self):
68        return 'Tag:%s' % self.name
69
70    def startElement(self, name, attrs, connection):
71        return None
72
73    def endElement(self, name, value, connection):
74        if name == 'resourceId':
75            self.res_id = value
76        elif name == 'resourceType':
77            self.res_type = value
78        elif name == 'key':
79            self.name = value
80        elif name == 'value':
81            self.value = value
82        else:
83            setattr(self, name, value)
84
85
86            
87