root/branch/k0p/umitInterfaceEditor/ProfileCore.py @ 1407

Revision 1407, 16.0 kB (checked in by kop-labs, 6 years ago)

WizardEditor?

Line 
1# Copyright (C) 2007 Insecure.Com LLC.
2#
3# Author: Luis A. Bastiao Silva <luis.kop@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19from types import StringTypes
20from xml.dom import minidom   
21#try:
22    #import xml.dom.ext
23#except:
24    #pass
25# Testing at devel
26from os.path import split, join
27
28from umitCore.Paths import Path
29#Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
30#END DEV TEST
31options = Path.options
32#from umitCore.NmapCommand import CommandConstructor
33from umitCore.NmapOptions import NmapOptions
34#from umitGUI.OptionBuilder import OptionBuilder
35
36
37'''
38ProfileCore manage all of the profile.xml
39- Supports the reading of options
40- Supports the writing ordenabled options and groups
41- Add/Remove a group
42- Add options for each group
43
44## Special info:
45- If delete a group all options on this group should be
46  deleted too.
47- A feauture that should be have it's delete a option
48  if it's deleted by options.xml
49- Solve all problems with options dependentions
50'''
51
52
53class ProfileOption(object):
54    '''
55    This class contains a option on Profile and Wizard
56    It is a bridge between ProfileCore and Profile (Editable Box)
57    '''
58    def __init__(self, type, label, option=None, arg_type=None , option_list=None ):
59        self._type = type
60        self._label = label
61        self._option = option
62        self._arg_type = arg_type
63        self._option_list = option_list
64        self._section = None
65        self._next_opt = None
66    def set_option_list(self, optionlist):
67        self._option_list = optionlist
68    def set_label(self, name):
69        self._label= name
70    def set_section(self, section):
71        self._section = section
72    def set_type(self, type):
73        self._type = type
74    def set_next_opt(self, opt):
75        self._next_opt= opt
76    def get_next_opt(self):
77        return self._next_opt
78    def get_type(self):
79        return self._type
80    def get_label(self):
81        return self._label
82    def get_option(self):
83        return self._option
84    def get_arg_type(self):
85        return self._arg_type
86    def get_option_list(self):
87        return self._option_list
88    def get_hint(self):
89        pass
90    def get_section(self):
91        return self._section
92   
93
94def option_to_profileoption(option):
95    '''
96    This is a simples method to create a new ProfileOption from
97    a new Option.
98    It's a very primitive and useful when you create a new option at
99    profile based on a option
100    WHEN USE: example when do drag-n-drop, and received a Option
101    as we working with Profile, it's need a ProfileOption
102    '''
103    from umitInterfaceEditor.OptionsCore import Option
104    o = option
105    result = ProfileOption('option_check', o.get_name(), o.get_name(),
106                           o.get_arg_type())
107    return result
108   
109
110
111
112
113class ProfileCore(object):
114    def __init__(self, xml_file):
115        '''
116        Open XML file
117        It should be inheritance of OptionBuilder But I don't need
118        a function that update command.
119        So I implement a new way of read elements like OptionBuilder
120        with support of write in XML
121       
122       
123        @param xml_file: String with a path of file
124        @type xml_file: str
125        '''
126       
127        xml_desc = open(xml_file)
128        self.xml_parse = minidom.parse(xml_desc)
129        xml_desc.close()
130        self.root_tag = "interface"
131        self.xml = self.xml_parse.getElementsByTagName(self.root_tag)[0]
132        self.options = NmapOptions(options)
133       
134        self.groups = self.__parse_groups()
135        self.section_names = self.__parse_section_names()
136        self.tabs = self.__parse_tabs()
137       
138    # Private API
139   
140    def __parse_section_names(self):
141        dic = {}
142        for group in self.groups:
143            grp = self.xml.getElementsByTagName(group)[0]
144            dic[group] = grp.getAttribute(u'label')
145        return dic
146   
147    def __parse_groups(self):
148        return [g_name.getAttribute(u'name') for g_name in \
149                  self.xml.getElementsByTagName(u'groups')[0].\
150                  getElementsByTagName(u'group')]
151
152    def __parse_tabs(self):
153        '''
154       
155        This part should be interact with BoxEditable of Items
156        '''
157       
158        #XXX fix OptionTab - implement new way
159        dic = {}
160        for tab_name in self.groups:
161            #dic[tab_name] = OptionTab(self.xml.getElementsByTagName(tab_name)[0],
162            #                          self.options, self.constructor, self.update_func)
163            dic[tab_name] = self.xml.getElementsByTagName(tab_name)[0]
164        return dic
165   
166       
167
168    #Public API
169    def get_group(self):
170       
171        for root_tab in self.tabs:
172            print root_tab
173            for option_element in self.tabs[root_tab].childNodes:
174                try:option_element.tagName
175                except:pass
176                else:
177                    print option_element.tagName
178                    #self.widgets_list.append(actions[option_element.tagName](option_element, options_used))
179                #print ".."
180
181    def create_element_optlist(self, name):
182        element = self.xml_parse.createElementNS(None, 'option')
183        element.setAttribute('name', name)
184        return element
185   
186    def update_option_list(self, section, name, newoptionlist):
187        '''
188        to update the list of option_list
189        update all childs inside option_list
190       
191       
192        steps of implementation :
193        - get element
194        - remove childs
195        - create childs
196        - append childs
197        '''
198       
199        element = self.search_option(section,name)
200        for i in element.childNodes:
201            try: i.tagName
202            except:pass
203            else:
204                element.removeChild(i)
205        for i in newoptionlist:
206            new = self.create_element_optlist(i)
207            element.appendChild(new)
208           
209           
210   
211    def rename_option(self, section, name, newname):
212        '''
213        rename label option inside a section
214        '''
215       
216        element = self.search_option(section, name)
217        element.setAttribute('label', newname)
218   
219    def rename_section(self, name, newname):
220        '''
221        rename section name, should be renamed at groups
222        and the <Name...>
223        '''
224        node = self.get_section_node(name)
225        node.setAttribute('name', newname)
226       
227        element = self.xml_parse.createElement(newname)
228        for option in self.tabs[name].childNodes:
229            try: option.tagName
230            except:pass
231            else:
232                element.appendChild(option)
233       
234        self.xml.removeChild(self.tabs[name])
235        self.xml.appendChild(element)
236       
237
238   
239    def get_section_node(self, section):
240        '''
241        get section node
242        '''
243               
244        element = self.xml.getElementsByTagName('groups')[0]
245        element_group = element.getElementsByTagName(u'group')
246        result = None 
247        for i in element_group:
248            result = i
249            if i.getAttribute('name') == section:
250                break
251           
252           
253        return result
254    def get_section(self, section):
255        '''
256        @param section: a section name
257        @type section: str
258        @return: a list of options
259        @rtype: a list of ProfileOption
260       
261        '''
262        result = []
263        for option in self.tabs[section].childNodes:
264            try: option.tagName
265            except:pass
266            else:
267                tagName = option.tagName
268                if tagName == 'option_check':
269                    tmp_po = ProfileOption(tagName, option.getAttribute(u'label'),
270                                           option.getAttribute(u'option'),
271                                           option.getAttribute(u'arg_type'),
272                                           None)
273                elif tagName == 'option_list':
274                   
275                    option_list = []
276                    for k in option.getElementsByTagName('option'):
277                        option_list.append(k.getAttribute(u'name'))
278                    tmp_po = ProfileOption(tagName, option.getAttribute(u'label'),
279                                           None, None, option_list)
280                                           
281                result.append(tmp_po)
282        return result
283
284       
285    def add_section(self, name):
286        '''
287        Add section inside groups and create a new tagName
288        '''
289       
290        self.add_section_before(name, None)
291        #Create a void tag of section:
292        element = self.xml_parse.createElementNS(None, name)
293        element.setAttribute('label',name)
294        self.xml.appendChild(element)
295        self.groups = self.__parse_groups()
296        self.section_names = self.__parse_section_names()
297        self.tabs = self.__parse_tabs()
298
299    def remove_opt(self, section, name):       
300        if section == None or name == None:
301            return False
302        elem = self.search_option(section, name)
303        root = self.xml_parse.getElementsByTagName(self.root_tag)[0]
304        sec = root.getElementsByTagName(section)
305       
306        sec[0].removeChild(elem)
307    def remove_section(self, section_name):
308        '''
309        Removes section of the DOM
310        @param section_name: name of the section that will be removed
311        @type section_name: str
312        '''
313       
314       
315        root = self.xml_parse.getElementsByTagName(self.root_tag)[0]
316       
317        elem = self.search_in_groups(name)
318        root.removeChild(elem)
319       
320       
321       
322    def search_in_groups(self, name):
323        '''
324        Search a element of group
325        @param name: name of group to search
326        @type name: str
327       
328        @return : a Child None of DOM if found or None if not found
329        @rtype: ChildNode or None
330        '''
331       
332        element = self.xml.getElementsByTagName('groups')[0]
333        element_group = element.getElementsByTagName(u'group')
334        result = None 
335        for i in element_group:
336            if i.getAttribute('name') == name:
337                result = i
338        return result
339    def add_section_after(self, section, section_after):
340        pass
341   
342    def add_section_before(self, section, section_before):
343        '''
344        This function is to insert a new section at groups
345        with a new interface it is need insert and move sections
346       
347       
348        '''
349        #Fill element
350        element = self.xml_parse.createElementNS(None, "group")
351        element.setAttributeNS(None, 'name',section) 
352        #search Node to insert before it
353        beforeChild = self.search_in_groups(section_before)
354        #Add to xml doc
355        self.xml.getElementsByTagName('groups')[0].insertBefore(element,
356                                                                beforeChild )   
357   
358    def search_option(self, section_name, option_name):
359        '''
360        Search a option with a name and return this Node
361        @param section_name: A section name where is to looking for
362        @type section_name: str
363        @param option_name: A name of option for searching
364        @type option_name: str
365       
366        @return: A node with a element found or None if not found
367        @rtype: ChildNode element or None
368        '''
369       
370        grp = self.xml.getElementsByTagName(section_name)[0]
371        result = None 
372        for elem in grp.childNodes:
373            try: elem.getAttribute(u'label')
374            except: pass
375            else: 
376                if elem.getAttribute(u'label') == option_name:
377                    result = elem
378                    break 
379           
380        return result
381       
382    def add_option_before(self, section_name, option, before):
383
384        result = self.xml.getElementsByTagName(section_name)[0].insertBefore(option, before)       
385    def add_from_profileoption(self, profileoption):
386        '''
387        use add_option
388        '''
389        po = profileoption
390        section = po.get_section()
391        tagname = po.get_type()
392        label = po.get_label()
393        option = po.get_option()
394        arg_type = po.get_arg_type()
395        option_list = po.get_option_list()
396        option_name_next = po.get_next_opt()
397       
398       
399        self.add_option(section, tagname, label, option, arg_type, 
400                        option_list, option_name_next)
401       
402   
403    def add_option(self, section_name, tagname, label, option=None, 
404                   arg_type=None, options_list=None, option_name_before=None):
405        '''
406        Add a option in a section
407        - If is a optionlist it have a option_list and if the option
408        should be not inserted at the end of section have a option_name_before
409        @param section_name: A section name where options should be added
410        @type section_name: str
411        @param tagname: option_check, option_list or others used at
412        Profile Editor or Wizard
413        @type tagname: str
414        @param label: Name of this option
415        @type label: str
416        @param option: The option is used to link with file options.xml
417        @type option:  str
418        @param arg_type: type of arg (#XXX I think that it's obsulete right now)
419        @type arg_type:str
420        @param option_list: A list with a name options to option_list
421        @type option_list: list
422        @param option_name_before: name of option after where is inserted
423        @type option_name_before: str
424        '''
425        element = self.xml_parse.createElementNS(None, tagname)
426        if arg_type != None: 
427            element.setAttribute('arg_type', arg_type)
428        element.setAttribute('label',label)
429        if option != None:
430            element.setAttribute('option', option)
431        elements = [] 
432        if options_list != None: 
433           
434            for i in options_list:
435                element_in = self.xml_parse.createElementNS(None, 'option')
436                element_in.setAttributeNS(None, 'name', i)
437                elements.append(element_in)
438        after = None 
439        if option_name_before != None:
440            after = self.search_option(section_name, option_name_before)
441       
442        el = self.xml.getElementsByTagName(section_name)[0]
443        result = el.insertBefore(element, after)           
444        if elements != [] :
445            for i in elements:
446                result.appendChild(i)
447   
448    def _determine_prev(self,section, list):
449        result = None 
450        for i in list:
451            if i.getAttribute('name') == section:
452                break
453           
454            result = i
455        return result
456   
457    def get_prev(self, section): 
458        '''
459        get prev section
460        '''
461               
462        element = self.get_group_element()
463        element_group = element.getElementsByTagName(u'group')
464        result = self._determine_prev(section, element_group)
465        return result
466    def get_next(self, section): 
467        '''
468        get next section
469        '''
470               
471        element = self.get_group_element()
472        element_group = element.getElementsByTagName(u'group')
473        element_group.reverse()
474        result = self._determine_prev(section, element_group)
475        return result
476
477
478    def get_group_element(self):
479        element = self.xml.getElementsByTagName('groups')[0]
480        return element
481    def move_section_left(self, section):
482        node_prev = self.get_prev(section)
483        node = self.get_section_node(section)
484        element = self.get_group_element()
485        element.removeChild(node)
486        element.insertBefore(node, node_prev)
487
488       
489    def move_section_right(self, section):
490        node_next = self.get_next(section)
491        node = self.get_section_node(section)
492        element = self.get_group_element()
493        element.removeChild(node_next)
494        element.insertBefore(node_next, node)
495       
496
497    def get_list_opt(self, section, label):
498        '''
499        Based on label of OptionList
500        return a List with a Options in.
501        '''
502        elem = self.search_option(section, label)
503        list = elem.getElementsByTagName(u'option')
504        result = [] 
505        for i in list:
506            name = i.getAttribute(u'name')
507            result.append(name)
508        return result
509    def get_prev_opt(section, option):
510        #XXX <- May be lack in other side
511        elem = self.search_option(section, option)
512        pass
513
514    def move_option_up(self, section,option, option_up):
515        option_prev =  self.search_option(section, option_up)
516        if option_up == None : 
517            pass
518        else: 
519            elem = self.search_option(section, option)
520            self.remove_opt(section, option)
521            self.add_option_before(section, elem, option_prev)
522
523           
524       
525    def move_option_down(self, section, option, option_down):
526        option_next =  self.search_option(section, option_down)
527        if option_down == None : 
528            pass
529        else: 
530            self.remove_opt(section, option_down)
531            elem = self.search_option(section, option)
532            self.add_option_before(section, option_next, elem)
533           
534
535   
536   
537    def print_screen(self):
538        xml.dom.ext.PrettyPrint(self.xml)
539    def write_file(self,filename):
540        '''
541        Write Option List to XML file
542        @param filename: File name of xml
543        @type filename: String
544        '''
545        #self.print_screen()
546        #self.xml.unlink()
547        file_object = open(filename, "w")
548        #xml.dom.ext.PrettyPrint(self.xml, file_object)
549        #doc_str = self.xml.toprettyxml(indent='', newl='')
550        #doc_str = self.xml.toxml()
551        #print doc_str
552        #doc_str = doc_str.replace('\n','')
553        #doc_str = doc_str.replace('\t','')
554        #print doc_str
555        #self.xml = minidom.parseString(doc_str)
556        self.xml.writexml(file_object, indent='', newl='')
557        file_object.close()
558
559       
560       
561# This is only for test a implementation of ProfileCore
562
563def main():
564    s = ProfileCore("/home/kop/.umit/profile_editor.xml")
565    #for group in s.groups:
566        #print group
567    #for sections in s.section_names:
568        #print sections
569    #for tab in s.tabs:
570        #print tab
571    #s.get_group()
572    s.get_section('Scan')
573    #s.add_section_before('New label','Ping')
574    #s.add_option('Scan', 'option_list', 'dasdas', 'dasda',
575    #            'dasdas', ['sdas','dasda'], 'Timing: ')
576    s.remove_opt('Scan', 'TCP scan: ')
577    s.print_screen()
578   
579   
580
581if __name__ == "__main__":
582    main() 
583   
Note: See TracBrowser for help on using the browser.