root/trunk/main.py

Revision 21, 5.4 KB (checked in by lasarux, 3 years ago)
  • main.py:
  • misc/utils.py:
  • plugins/info/info/init.py:
  • plugins/info/info/info.glade:
  • plugins/zfs/zfs/init.py:
  • misc/utils.py (added): Treeview class now in utils.py Info tab uses a treeview to output data
  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import os
4import sys
5import pkg_resources
6sys.modules['simplepanels'] = __import__(__name__)
7
8ENTRYPOINT = 'simplepanels.plugins'
9PLUGIN_DIR = os.path.join(os.path.dirname(__file__), 'plugins')
10import gtk
11
12from threading import Thread
13from misc.SimpleGladeApp import SimpleGladeApp
14from misc.SimpleGladeApp import bindtextdomain
15from misc.pexpect import pexpect, pxssh
16from misc.utils import Treeview
17import config
18
19try:
20    import gtkmozembed #A mozilla embeded!
21except:
22    gtkmozembed = None
23
24gtk.gdk.threads_init()
25
26app_name = "simplepanels"
27app_version = "0.0.1"
28
29class MainWnd(SimpleGladeApp):
30    def new(self):
31        self.config = config
32        self.settings = {}
33        self.attach_plugins()
34        self.show_hosts()
35        #self.init_treeview()
36
37    def attach_plugins(self):
38        # threading plugins
39        for plugin in get_plugins_by_capability('thread'):
40            plugin.start()
41           
42        # attach plugins
43        for plugin in get_plugins_by_capability('attach'):
44            print '%s: %s' % (plugin, plugin.attach(self))
45       
46        #self.plugin = {}
47        #for item in config.PLUGINS:
48        #    self.plugin[item] = __import__("plugins/%s" % item)
49        #    if self.plugin[item].attach(self.info_vbox, self):
50        #        print "Plugin \"%s\" loaded" % item
51        #    else:
52        #        print "Plugin \"%s\" disabled" % item
53
54    def show_hosts(self):
55        """Show host from the config file"""
56        data = []
57        columns = [('visible', 'str', 'Name'),
58                   ('visible', 'str', 'OS'),
59                   ('visible', 'str', 'IP Address'),
60                   ('novisible', 'str', 'Port')]
61        for i in config.HOSTS:
62            j = config.HOSTS[i]
63            try:
64                row = (i, j["os"], j["ip"], j["port"])
65            except:
66                row = (i, j["os"], j["ip"], "22")
67            data.append(row)
68        self.t = Treeview("list", columns, data, self.treeview)
69
70    def show_users(self):
71        """Show host from the config file"""
72        data = []
73        columns = [('visible', 'str', 'Nick'),
74                   ('visible', 'str', 'Name'),
75                   ('visible', 'str', 'Status')]
76        for i in config.USERS:
77            j = config.USERS[i]
78            row = (i, j["name"], j["status"])
79            data.append(row)
80        self.t = Treeview("list", columns, data, self.treeview)
81
82    def on_connect_btn_clicked(self, button):
83        self.s = pxssh.pxssh()
84        user = "root" #self.settings["user"]
85        ip = self.settings["ip"]
86        port = self.settings["port"]
87        os = self.settings["os"]
88        if self.s.login(ip, user, port=port):
89            print "login!"
90        else:
91            print "no login!"
92
93    def on_treeview_button_release_event(self, widget, event):
94        #Get the path at the specific mouse position
95        model, iter = widget.get_selection().get_selected()
96        self.settings["ip"] = model.get_value(iter, 2)
97        self.settings["port"] = model.get_value(iter, 3)
98        self.settings["os"] = model.get_value(iter, 1)
99
100    def on_table_combo_changed(self, widget):
101        """Prepare data to show in treeview"""
102        data = []
103        notebook_wnd = None
104        table = widget.get_active_text().lower()
105        if table == "users":
106            self.show_users()
107        elif table == "hosts":
108            self.show_hosts()
109
110    def get_settings(self, key):
111        return self.settings[key]
112
113    def set_username(self, username):
114        self.username = username
115
116    def on_quit_activate(self, object):
117        gtk.main_quit()
118
119    def on_about_activate(self, object):
120        about_wnd = AboutWnd("%s/main.glade" % config.GLADEDIR, "about_wnd")
121        about_wnd.run()
122
123    def on_main_wnd_destroy(self, window):
124        gtk.main_quit()
125
126
127class LoginWnd(SimpleGladeApp):
128    def on_ok_btn_clicked(self, button):
129        username = self.username_entry.get_text()
130        self.main_widget.destroy()
131        main_wnd = MainWnd("%s/main.glade" % config.GLADEDIR, "main_wnd")
132        main_wnd.set_username(username)
133        main_wnd.run()
134
135    def on_login_wnd_destroy(self, window):
136        gtk.main_quit()
137
138
139class AboutWnd(SimpleGladeApp):
140    def on_about_wnd_close(self, widget):
141        gtk.main_quit()
142
143    def on_about_wnd_destroy(self, window):
144        gtk.main_quit()
145
146# plugins base from http://lucumr.pocoo.org/blogarchive/setuptools-plugins
147def init_plugins():
148    pkg_resources.working_set.add_entry(PLUGIN_DIR)
149    pkg_env = pkg_resources.Environment([PLUGIN_DIR])
150    plugins = {}
151    for name in pkg_env:
152        egg = pkg_env[name][0]
153        egg.activate()
154        modules = []
155        for name in egg.get_entry_map(ENTRYPOINT):
156            entry_point = egg.get_entry_info(ENTRYPOINT, name)
157            cls = entry_point.load()
158            if not hasattr(cls, 'capabilities'):
159                cls.capabilities = []
160            instance = cls()
161            for c in cls.capabilities:
162                plugins.setdefault(c, []).append(instance)
163    return plugins
164
165plugins = init_plugins() #load plugins
166
167def get_plugins_by_capability(capability):
168    return plugins.get(capability, [])
169
170def get_all_plugins():
171    result = set()
172    for p in plugins.itervalues():
173        for plugin in p:
174            result.add(plugin)
175    return list(result)
176
177
178def main():
179    """Main loop"""
180    aplicacion = LoginWnd("%s/main.glade" % config.GLADEDIR, "login_wnd")
181    aplicacion.run()
182   
183if __name__ == "__main__":
184    main()
Note: See TracBrowser for help on using the browser.