~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/client/gui/gtk.py

Merged 424018-add-keypair-support [r=jkakar] [f=424018]

This change implements three keypair methods in the EC2 client:
 1. describe_keypairs
 2. create_keypair
 3. delete_keypair

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from txaws.service import AWSServiceRegion
15
15
 
16
16
 
17
 
__all__ = ['main']
 
17
__all__ = ["main"]
18
18
 
19
19
 
20
20
class AWSStatusIcon(gtk.StatusIcon):
25
25
        self.set_from_stock(gtk.STOCK_NETWORK)
26
26
        self.set_visible(True)
27
27
        self.reactor = reactor
28
 
        self.connect('activate', self.on_activate)
 
28
        self.connect("activate", self.on_activate)
29
29
        self.probing = False
30
 
        # Nested import because otherwise we get 'reactor already installed'.
 
30
        # Nested import because otherwise we get "reactor already installed".
31
31
        self.password_dialog = None
32
32
        try:
33
33
            creds = AWSCredentials()
35
35
            creds = self.from_gnomekeyring()
36
36
        self.region = AWSServiceRegion(creds)
37
37
        self.create_client(creds)
38
 
        menu = '''
 
38
        menu = """
39
39
            <ui>
40
40
             <menubar name="Menubar">
41
41
              <menu action="Menu">
43
43
              </menu>
44
44
             </menubar>
45
45
            </ui>
46
 
        '''
 
46
        """
47
47
        actions = [
48
 
            ('Menu',  None, 'Menu'),
49
 
            ('Stop instances', gtk.STOCK_STOP, '_Stop instances...', None,
50
 
                'Stop instances', self.on_stop_instances),
 
48
            ("Menu",  None, "Menu"),
 
49
            ("Stop instances", gtk.STOCK_STOP, "_Stop instances...", None,
 
50
                "Stop instances", self.on_stop_instances),
51
51
            ]
52
 
        ag = gtk.ActionGroup('Actions')
 
52
        ag = gtk.ActionGroup("Actions")
53
53
        ag.add_actions(actions)
54
54
        self.manager = gtk.UIManager()
55
55
        self.manager.insert_action_group(ag, 0)
56
56
        self.manager.add_ui_from_string(menu)
57
57
        self.menu = self.manager.get_widget(
58
 
            '/Menubar/Menu/Stop instances').props.parent
59
 
        self.connect('popup-menu', self.on_popup_menu)
 
58
            "/Menubar/Menu/Stop instances").props.parent
 
59
        self.connect("popup-menu", self.on_popup_menu)
60
60
 
61
61
    def create_client(self, creds):
62
62
        if creds is not None:
73
73
            items = gnomekeyring.find_items_sync(
74
74
                gnomekeyring.ITEM_GENERIC_SECRET,
75
75
                {
76
 
                    'aws-host': 'aws.amazon.com',
 
76
                    "aws-host": "aws.amazon.com",
77
77
                })
78
78
        except (gnomekeyring.NoMatchError,
79
79
            gnomekeyring.DeniedError):
80
80
            self.show_a_password_dialog()
81
81
            return None
82
82
        else:
83
 
            key_id, secret_key = items[0].secret.split(':')
 
83
            key_id, secret_key = items[0].secret.split(":")
84
84
            return AWSCredentials(access_key=key_id, secret_key=secret_key)
85
85
 
86
86
    def show_a_password_dialog(self):
106
106
        add_entry("AWS _Secret Key")
107
107
 
108
108
        self.password_dialog.show()
109
 
        self.password_dialog.connect('response', self.save_key)
 
109
        self.password_dialog.connect("response", self.save_key)
110
110
        self.password_dialog.run()
111
111
 
112
112
    def on_activate(self, data):
141
141
            gnomekeyring.item_create_sync(
142
142
                None,
143
143
                gnomekeyring.ITEM_GENERIC_SECRET,
144
 
                'AWS access credentials',
145
 
                    {'aws-host': 'aws.amazon.com'},
 
144
                "AWS access credentials",
 
145
                    {"aws-host": "aws.amazon.com"},
146
146
                    "%s:%s" % (key_id, secret_key), True)
147
147
        finally:
148
148
            self.password_dialog.hide()
152
152
    def showhide(self, reservation):
153
153
        active = 0
154
154
        for instance in reservation:
155
 
            if instance.instanceState == 'running':
 
155
            if instance.instanceState == "running":
156
156
                active += 1
157
 
        self.set_tooltip('AWS Status - %d instances' % active)
 
157
        self.set_tooltip("AWS Status - %d instances" % active)
158
158
        self.set_visible(active != 0)
159
159
        self.queue_check()
160
160
 
192
192
    Typical use:
193
193
    >>> sys.exit(main(sys.argv))
194
194
 
195
 
    :param argv: The arguments to run it with, e.g. sys.argv.
196
 
    :param reactor: The reactor to use. Must be compatible with gtk as this
197
 
        module uses gtk API's.
198
 
    :return exitcode: The exit code it returned, as per sys.exit.
 
195
    @param argv: The arguments to run it with, e.g. sys.argv.
 
196
    @param reactor: The reactor to use. Must be compatible with gtk as this
 
197
        module uses gtk API"s.
 
198
    @return exitcode: The exit code it returned, as per sys.exit.
199
199
    """
200
200
    if reactor is None:
201
201
        from twisted.internet import gtk2reactor
202
202
        gtk2reactor.install()
203
203
        from twisted.internet import reactor
204
204
    status = AWSStatusIcon(reactor)
205
 
    gobject.set_application_name('aws-status')
 
205
    gobject.set_application_name("aws-status")
206
206
    reactor.run()