root/umitGUI/OptionBuilder.py @ 489

Revision 489, 6.9 kB (checked in by boltrix, 7 years ago)

Removed installers scripts, and created a installer script generator

Line 
1#!/usr/bin/env python
2# Copyright (C) 2005 Insecure.Com LLC.
3#
4# Author: Adriano Monteiro Marques <py.adriano@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20import gtk
21
22from xml.dom import minidom
23
24from higwidgets.higboxes import HIGHBox
25from higwidgets.higlabels import HIGEntryLabel
26from higwidgets.higbuttons import HIGButton
27
28from umitGUI.FileChoosers import AllFilesFileChooserDialog
29
30from umitCore.NmapOptions import NmapOptions
31from umitCore.I18N import _
32from umitCore.Paths import Path
33
34options = Path.options
35
36
37class OptionBuilder(object):
38    def __init__(self, xml_file):
39        xml_desc = open(xml_file)
40        self.xml = minidom.parse(xml_desc)
41
42        # Closing file to avoid problems with file descriptors
43        xml_desc.close()
44       
45        self.root_tag = "interface"
46        self.actions = {'option_list':self.__parse_option_list,\
47                        'option_check':self.__parse_option_check}
48       
49        self.xml = self.xml.getElementsByTagName(self.root_tag)[0]
50        self.options = NmapOptions(options)
51       
52        self.groups = self.__parse_groups()
53        self.section_names = self.__parse_section_names()
54        self.tabs = self.__parse_tabs()
55   
56    def __parse_section_names(self):
57        dic = {}
58        for group in self.groups:
59            grp = self.xml.getElementsByTagName(group)[0]
60            dic[group] = grp.getAttribute(u'label')
61        return dic
62   
63    def __parse_groups(self):
64        return [g_name.getAttribute(u'name') for g_name in \
65                  self.xml.getElementsByTagName(u'groups')[0].\
66                  getElementsByTagName(u'group')]
67
68    def __parse_tabs(self):
69        dic = {}
70        for tab_name in self.groups:
71            tab = self.xml.getElementsByTagName(tab_name)[0]
72            widgets_list = []
73            # Cannot use list comprehhension because text nodes raise exception
74            # when tagName is called
75            for option in tab.childNodes:
76                try:option.tagName
77                except:pass
78                else:
79                    if option.tagName in self.actions.keys():
80                        widgets_list.append\
81                            (self.actions[option.tagName](option))
82           
83            dic[tab_name] = widgets_list
84       
85        return dic
86   
87    def __parse_option_list(self, option_list):
88        options = option_list.getElementsByTagName(u'option')
89       
90        label = HIGEntryLabel(option_list.getAttribute(u'label'))
91        opt_list = OptionList()
92       
93        for opt in options:
94            opt_list.append(self.options.get_option\
95                            (opt.getAttribute(u'name')))
96       
97        return label, opt_list
98   
99    def __parse_option_check(self, option_check):
100        arg_type = option_check.getAttribute(u'arg_type')
101        option = option_check.getAttribute(u'option')
102        label = option_check.getAttribute(u'label')
103       
104        check = OptionCheck(label, self.options.get_option\
105                            (option_check.getAttribute(u'option')))
106        additional = None
107       
108        if arg_type == "str":
109            additional = OptionEntry()
110        elif arg_type == "int":
111            additional = OptionIntSpin()
112        elif arg_type == "float":
113            additional = OptionFloatSpin()
114        elif arg_type == "level":
115            additional = OptionLevelSpin()
116        elif arg_type == "path":
117            additional = OptionFile()
118        elif arg_type == "interface":
119            additional = OptionInterface()
120       
121        return check, additional
122
123class OptionWidget:
124    def enable_widget(self):
125        self.set_sensitive(True)
126   
127    def disable_widget(self):
128        self.set_sensitive(False)
129
130class OptionInterface(gtk.ComboBoxEntry, OptionWidget):
131    def __init__(self):
132        self.list = gtk.ListStore(str)
133        gtk.ComboBoxEntry.__init__(self, self.list)
134       
135        cell = gtk.CellRendererText()
136        self.pack_start(cell, True)
137        self.add_attribute(cell, 'text', 0)
138
139class OptionList(gtk.ComboBox, OptionWidget):
140    def __init__(self):
141        self.list = gtk.ListStore(str)
142        gtk.ComboBox.__init__(self, self.list)
143       
144        cell = gtk.CellRendererText()
145        self.pack_start(cell, True)
146        self.add_attribute(cell, 'text', 0)
147       
148        self.options = []
149   
150    def append(self, option):
151        self.list.append([option[u'name']])
152        self.options.append(option)
153
154class OptionCheck(gtk.CheckButton, OptionWidget):
155    def __init__(self, label=None, option=None):
156        gtk.CheckButton.__init__(self, label)
157       
158        self.option = option
159   
160    def get_option(self):
161        return self.option
162
163
164class OptionEntry(gtk.Entry, OptionWidget):
165    def __init__(self):
166        gtk.Entry.__init__(self)
167
168class OptionLevelSpin(gtk.SpinButton, OptionWidget):
169    def __init__(self, initial=0):
170        gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10,1),0.0,0)
171
172class OptionIntSpin(gtk.SpinButton, OptionWidget):
173    def __init__(self, initial=1):
174        gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10**100,1),0.0,0)
175
176class OptionFloatSpin(gtk.SpinButton, OptionWidget):
177    def __init__(self, initial=1):
178        gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10**100,1),0.1,2)
179
180class OptionFile(HIGHBox, OptionWidget, object):
181    def __init__(self):
182        HIGHBox.__init__(self)
183       
184        self.entry = OptionEntry()
185        self.button = HIGButton(stock=gtk.STOCK_OPEN)
186       
187        self._pack_expand_fill(self.entry)
188        self._pack_noexpand_nofill(self.button)
189       
190        self.button.connect('clicked', self.open_dialog_cb)
191   
192    def open_dialog_cb(self, widget):
193        dialog = AllFilesFileChooserDialog(_("Choose file"))
194        if dialog.run() == gtk.RESPONSE_OK:
195            self.entry.set_text(dialog.get_filename())
196        dialog.destroy()
197
198    def get_filename(self):
199        return "\ ".join(self.entry.get_text().split(" "))
200
201    def set_filename(self, filename):
202        self.entry.set_text(" ".join(filename.split("\ ")))
203
204    filename = property(get_filename, set_filename)
205
206if __name__ == '__main__':
207    o = OptionBuilder('profile_editor.xml')
208   
209    ol = OptionFile()
210    w = gtk.Window()
211    w.add(ol)
212    w.show_all()
213    w.connect('delete-event', lambda x,y,z=None: gtk.main_quit())
214    gtk.main()
Note: See TracBrowser for help on using the browser.