Project

General

Profile

Files » ansiblehostsmanager.py

Legrand François, 01/12/2018 02:39 PM

 
1
#!/usr/bin/python3
2
## @package ansiblehostmanager
3
#  AHM, Ansible Hosts Manager is a python 3 interface to manage the inventory file (hosts file) of ansible
4
#
5
#
6

    
7
from PyQt4 import QtGui, QtCore
8
import sys, re, traceback, os.path, time
9

    
10
import interface_ansible
11

    
12
## Special groups
13
ungrouped = "ungrouped"
14
alls = "all"
15
specials = [ungrouped,alls]
16

    
17
## Class derived from QListWidget
18
class MyQListWidget(QtGui.QListWidget):
19
    def __init__(self, parent=None, def_name=None):
20
        if def_name is None:
21
            (filename, line_number, function_name, text) = traceback.extract_stack()[-2]
22
            def_name = text[:text.find('=')].strip().lstrip("self.")
23
            self.defined_name = def_name
24
        super(MyQListWidget, self).__init__(parent)
25
        self.acceptfrom = []
26
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
27
        self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
28
        #self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
29
        self.setAcceptDrops(True)
30
        ### be careful #self.itemPressed.connect(self.item_Press)
31
        self.selectedItem = ""
32
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
33
        self.connect(self, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), self.onrightclick)
34

    
35
    ## Implementation of right click
36
    # Will be used to delete objects
37
    def onrightclick(self, pos):
38
        self.emit(QtCore.SIGNAL("rightclick"), pos)
39

    
40
    def dragEnterEvent(self, event):
41
        if event.mimeData().hasUrls():
42
            event.accept()
43
        else:
44
            super(MyQListWidget, self).dragEnterEvent(event)
45
            event.accept()
46

    
47
    def dragMoveEvent(self, event):
48
        if event.mimeData().hasUrls():
49
            event.setDropAction(QtCore.Qt.CopyAction)
50
            event.accept()
51
        else:
52
            super(MyQListWidget, self).dragMoveEvent(event)
53
            event.accept()
54

    
55
    def dropEvent(self, event):
56
        if (event.source().defined_name in self.acceptfrom) or (event.source().defined_name == self.defined_name):
57
            if event.mimeData().hasUrls():
58
                event.setDropAction(QtCore.Qt.MoveAction)
59
                event.accept()
60
                links = []
61
                for url in event.mimeData().urls():
62
                    links.append(str(url.toLocalFile()))
63
                self.emit(QtCore.SIGNAL("dropped"), links)
64
            else:
65

    
66
                event.setDropAction(QtCore.Qt.MoveAction)
67
                super(MyQListWidget, self).dropEvent(event)
68
                self.emit(QtCore.SIGNAL("dropped"), event.source().defined_name)
69
                event.accept()
70
                # self.emit(QtCore.SIGNAL("inserted"))
71

    
72
    ## Catch events
73
    # Raise signal "modified" when child is removed.
74
    # This ensure that all the drag and drop events are finished so the lists are up to date
75
    def event(self, QEvent):
76
        res = super(MyQListWidget, self).event(QEvent)
77
        #Raise signal modified when QEvent.type = ChildRemoved
78
        if QEvent.type() == QtCore.QEvent.ChildRemoved :
79
            self.emit(QtCore.SIGNAL("modified"))
80
        return res
81

    
82
    ## Return all the items as a list
83
    def getallitems(self):
84
        items = []
85
        for index in range(self.count()):
86
            items.append(self.item(index))
87
        return items
88

    
89

    
90
## Class to manage all the data of the Ansible hosts file
91
class AnsibleHostFile(object):
92
    ## Constructor
93
    # Structure of the datas is the following:
94
    # hosts : list of all hosts names
95
    # groups : list of all groups names
96
    # cgroups : list of all cgroups (children groups) names
97
    # attributes : list of all attributes
98
    # grouphosts : dictionary with group name as key and list of hosts in that group as value
99
    # cgroupgroups : dictionary with cgroup name as key and list of groups or cgroups in that cgroup as value
100
    # groupattributes : dictionary with group or cgroup name as key and list of attributes for that group/cgroup
101
    # hostattributes : dictionary with host name as key and list of attributes for that host
102
    def __init__(self):
103
        self.hosts = []
104
        self.groups = []
105
        self.cgroups = []
106
        self.attributes = []
107
        self.grouphosts = {}
108
        self.cgroupgroups = {}
109
        self.groupattributes = {}
110
#        self.cgroupcgroups = {}
111
        self.hostattributes = {}
112
        for gr in specials:
113
            self.addgroup(gr)
114

    
115
    def reset(self):
116
        self.groups.clear()
117
        self.hosts.clear()
118
        self.attributes.clear()
119
        self.cgroups.clear()
120
        self.grouphosts.clear()
121
        self.groupattributes.clear()
122
        self.cgroupgroups.clear()
123
        self.hostattributes.clear()
124
        for gr in specials:
125
            self.addgroup(gr)
126

    
127
    ## Function to check integrity of data
128
    # It will check that same name is not use for host, group or cgroup and that members in cgroups are really defined as group or cgroup
129
    def check_integrity(self):
130
        message = ""
131
        for host in self.hosts:
132
            message += self.check_hostname(host)
133
        for group in self.groups:
134
            message += self.check_groupname(group)
135
        #groups in cgroupgroups have to be a group or cgroup
136
        for cgroup, groups in self.cgroupgroups.items():
137
            for group in groups:
138
                if group not in self.groups and group not in self.cgroups :
139
                    message += "\nError : group " + group + " appears in Cgroup "+cgroup+" but is not defined."
140
        return "\n".join(set(message.split('\n')))
141

    
142
    ## Check that host name is not already used as group or cgroup name
143
    def check_hostname(self,host):
144
        message = ""
145
        if host in self.groups:
146
            message += "\nDuplicate name : " + host + " defined as host and group"
147
        if host in self.cgroups:
148
            message += "\nDuplicate name : " + host + " defined as host and Cgroup"
149
        return message
150

    
151
    ## Check that group name is not already used as host or cgroup name
152
    def check_groupname(self,group):
153
        message = ""
154
        if group in self.hosts:
155
            message += "\nDuplicate name : " + group + " defined as host and group"
156
        if group in self.cgroups:
157
            message += "\nDuplicate name : " + group + " defined as group and Cgroup"
158
        return message
159

    
160
    ## Check that cgroup name is not already used as host or group name
161
    def check_cgroupname(self,cgroup):
162
        message = ""
163
        if cgroup in self.hosts:
164
            message += "\nDuplicate name : " + cgroup + " defined as host and group"
165
        if cgroup in self.groups:
166
            message += "\nDuplicate name : " + cgroup + " defined as group and Cgroup"
167
        return message
168

    
169
    ## Add host to the list of hosts
170
    def addhost(self, host):
171
        if host not in self.hosts:
172
            self.hosts.append(host)
173

    
174
    ## Return the list of hosts
175
    def gethosts(self):
176
        return self.hosts
177

    
178
    ## Return hostname if in hosts list or None if not
179
    def gethost(self, hostname):
180
        if hostname in self.hosts:
181
            return hostname
182
        else:
183
            return None
184

    
185
    ## Delete host from list of hosts and from group it was in
186
    def delhost(self, host):
187
        if host in self.hosts:
188
            self.hosts.remove(host)
189
        for group in self.grouphosts:
190
            self.delhosttogroup(host, group)
191

    
192
    ## Add new group in list of groups (and in grouphosts)
193
    def addgroup(self, group):
194
        if group not in self.groups:
195
            self.groups.append(group)
196
        if group not in self.grouphosts:
197
            grouphost = self.grouphosts.setdefault(group, [])
198

    
199
    ## Delete a group from groups list and from grouphosts and cgroupgroups
200
    def delgroup(self, group):
201
        if group in specials:
202
            return
203
        if group in self.groups:
204
            self.groups.remove(group)
205
        del self.grouphosts[group]
206
        for cgroup in self.cgroupgroups:
207
            self.delgrouptocgroup(group,cgroup)
208

    
209
    ## Return group if it exists or None if not
210
    def getgroup(self, group):
211
        if group in self.groups:
212
            return group
213
        else:
214
            return None
215

    
216
    ## Return all groups
217
    def getgroups(self):
218
        return self.groups
219

    
220
    ## Add new cgroup in list of cgroups (and in cgroupgroups)
221
    def addcgroup(self, cgroup):
222
        if cgroup not in self.cgroups:
223
            self.cgroups.append(cgroup)
224
        if cgroup not in self.cgroupgroups:
225
            cgroupgroups = self.cgroupgroups.setdefault(cgroup, [])
226

    
227
    ## Delete cgroup
228
    def delcgroup(self, cgroup):
229
        if cgroup in self.cgroups:
230
            self.cgroups.remove(cgroup)
231
        del self.cgroupgroups[cgroup]
232

    
233
    ## Return cgroup if it exists or None if not
234
    def getcgroup(self, cgroup):
235
        if cgroup in self.cgroups:
236
            return cgroup
237
        else:
238
            return None
239

    
240
    ## Return all cgroups
241
    def getcgroups(self):
242
        return self.cgroups
243

    
244
    ## Add attribute to the list of attributes
245
    def addattribute(self, attribute):
246
        if attribute not in self.attributes:
247
            self.attributes.append(attribute)
248
        #        self.attributes.update({attribute.name:attribute})
249

    
250
    ## Delete attribute
251
    def delattribute(self, attribute):
252
        if attribute in self.attributes:
253
            self.attributes.remove(attribute)
254
            for group in self.groupattributes:
255
                self.delattributetogroup(attribute,group)
256
                self.delattributetocgroup(attribute,group)
257
            for host in self.hostattributes:
258
                self.delattributetohost(attribute,host)
259

    
260
    ## Return all attributes
261
    def getattributes(self):
262
        return self.attributes
263

    
264
    ## Return all attributes
265
    def gethostsattributes(self):
266
        return self.attributes
267

    
268
    ## Return attribute if it exists or None if not
269
    def getattribute(self, attribute):
270
        if attribute in self.attributes:
271
            return attribute
272
        else:
273
            return None
274

    
275
    ## Add host to group
276
    def addhosttogroup(self, host, group):
277
        #self.addgroup(group)
278
        #self.addhost(host)
279
        grouphost = self.grouphosts.setdefault(group, [])
280
        if host not in grouphost:
281
            grouphost.append(host)
282

    
283
    ## Add group to host
284
    def addgrouptohost(self, group, host):
285
        self.addhosttogroup(host, group)
286

    
287
    ## Remove host from group
288
    def delhosttogroup(self, host, group):
289
        grouphost = self.grouphosts.setdefault(group, [])
290
        if host in grouphost:
291
            grouphost.remove(host)
292

    
293
    ## Add group to cgroup
294
    def addgrouptocgroup(self, group, cgroup):
295
        #self.addgroup(group)
296
        #self.addcgroup(cgroup)
297
        cgroupgroups = self.cgroupgroups.setdefault(cgroup, [])
298
        if group not in cgroupgroups:
299
            cgroupgroups.append(group)
300

    
301
    ## Remove group from cgroup
302
    def delgrouptocgroup(self, group, cgroup):
303
        cgroupgroups = self.cgroupgroups.setdefault(cgroup, [])
304
        if group in cgroupgroups:
305
            cgroupgroups.remove(group)
306

    
307
    ## Add attribute to host
308
    def addattributetohost(self, attribute, host):
309
        #self.addattribute(attribute)
310
        #self.addhost(host)
311
        attributes = self.hostattributes.setdefault(host, [])
312
        if attribute not in attributes:
313
            attributes.append(attribute)
314
            # self.hostattributes.update({host.name:attribute.name})
315

    
316
    ## Remove attribute from host
317
    def delattributetohost(self, attribute, host):
318
        hostattributes = self.hostattributes.setdefault(host, [])
319
        if attribute in hostattributes:
320
            hostattributes.remove(attribute)
321

    
322
    ## Add attribute to group
323
    def addattributetogroup(self, attribute, group):
324
        self.addattribute(attribute)
325
        #self.addgroup(group)
326
        groupattribute = self.groupattributes.setdefault(group, [])
327
        if attribute not in groupattribute:
328
            groupattribute.append(attribute)
329
            # self.groupattributes.update({group.name:attribute.name})
330

    
331
    ## Remove attribute from group
332
    def delattributetogroup(self, attribute, group):
333
        groupattributes = self.groupattributes.setdefault(group, [])
334
        if attribute in groupattributes:
335
            groupattributes.remove(attribute)
336

    
337
    ## Add attribute to cgroup
338
    def addattributetocgroup(self, attribute, cgroup):
339
        #self.addattribute(attribute)
340
        #self.addgroup(group)
341
        cgroupattribute = self.groupattributes.setdefault(cgroup, [])
342
        if attribute not in cgroupattribute:
343
            cgroupattribute.append(attribute)
344
            # self.groupattributes.update({group.name:attribute.name})
345

    
346
    ## Remove attribute from cgroup
347
    def delattributetocgroup(self, attribute, cgroup):
348
        cgroupattributes = self.groupattributes.setdefault(cgroup, [])
349
        if attribute in cgroupattributes:
350
            cgroupattributes.remove(attribute)
351

    
352
    ## Return dictionary with group name as key and hostname as value for the required host name
353
    def getgroupsforhost(self, hostname):
354
        subdict = dict((k, v) for k, v in self.grouphosts.items() if hostname in v)
355
        return subdict
356

    
357
    ## Return dictionary with cgroup name as key and group name as value for the required cgroup name
358
    def getgroupforcgroup(self, cgroup):
359
        subdict = dict((k, v) for k, v in self.cgroupgroups.items() if cgroup == k)
360
        return subdict
361

    
362
#    def getcgroupforcgroup(self, cgroup):
363
#        subdict = dict((k, v) for k, v in self.cgroupcgroups.items() if cgroup == k)
364
#        return subdict
365

    
366
    ## Return dictionary with group name as key and attribute as value for the required attribute
367
    def getgroupforattribute(self, attribute):
368
        subdict1 = dict((k, v) for k, v in self.groupattributes.items() if attribute in v)
369
        #need to filter group (and not cgroup)
370
        subdict = dict((k, v) for k, v in subdict1.items() if k in self.groups)
371
        return subdict
372

    
373
    ## Return dictionary with cgroup name as key and attribute as value for the required attribute
374
    def getcgroupforattribute(self, attribute):
375
        subdict1 = dict((k, v) for k, v in self.groupattributes.items() if attribute in v)
376
        #need to filter cgroup (and not group)
377
        subdict = dict((k, v) for k, v in subdict1.items() if k in self.cgroups)
378
        return subdict
379

    
380
    ## Return dictionary with host name as key and attribute as value for the required host name
381
    def getattributesforhost(self, hostname):
382
        subdict = dict((k, v) for k, v in self.hostattributes.items() if hostname == k)
383
        return subdict
384

    
385
    ## Return dictionary with host name as key and attribute as value for the required attribute
386
    def gethostsforattribute(self, attributename):
387
        subdict = dict((k, v) for k, v in self.hostattributes.items() if attributename in v)
388
        return subdict
389

    
390
    ## Return dictionary with group name as key and attribute as value for required the group name
391
    def getattributesforgroup(self,groupname):
392
        subdict = dict((k, v) for k, v in self.groupattributes.items() if groupname == k)
393
        return subdict
394

    
395
    ## Return dictionary with group name as key and host name as value for required the group name
396
    def gethostsforgroup(self, groupname):
397
        subdict = dict((k, v) for k, v in self.grouphosts.items() if groupname == k)
398
        return subdict
399

    
400
    ## Return a sorted list
401
    def get_items(self,liste):
402
        #return liste.items()
403
        return sorted(liste.items())
404

    
405
    ## Function to generate the ansible host file
406
    # It will first check the integrity of the date and eventually include some warnings
407
    # then it will list all ungroupped hosts
408
    # then attributes for group "all"
409
    # then groups in alphabetical order
410
    # then cgroups in alphabetical order
411
    def export_ansible(self):
412
        self.update_ungroupped()
413
        export = "# Ansible host file generated by the Ansible Hosts Manager \n"
414

    
415
        check_integrity_msg = self.check_integrity()
416
        if check_integrity_msg is not "":
417
            export += "\n#Check integrity result:"
418
            export += check_integrity_msg.replace("\n","\n#")+"\n\n"
419

    
420
        # We start by ungrouped and follow by all then the rest
421
        group = self.grouphosts.setdefault(ungrouped, [])
422
        for host in group:
423
            export += host + " "
424
            attributes = self.hostattributes.setdefault(host, [])
425
            for attribute in sorted(attributes):
426
                export += " " + attribute
427
            export += "\n"
428

    
429
        listattr = ""
430

    
431
        # Variable for group all
432
        attributes = self.groupattributes.setdefault(alls, [])
433
        if group is not None:
434
            for attribute in attributes:
435
                listattr += attribute+"\n"
436
            if listattr != "":
437
                        export += "\n[" + alls + ":vars]\n"+listattr
438

    
439
        # All groups and groups vars (attributes)
440
        for group in self.get_items(self.grouphosts):
441
            #if group[0] == alls:
442
            #    if group[0] in self.groupattributes:
443
            #        for attribute in sorted(self.groupattributes[group[0]]):
444
            #            listattr += attribute+"\n"
445
            #        if listattr != "":
446
            #            export += "\n[" + group[0] + ":vars]\n"+listattr
447

    
448
            if group[0] not in specials:
449
                export += "\n[" + group[0] + "]\n"
450
                for host in group[1]:
451
                    export += host
452
                    attributes = self.hostattributes.setdefault(host, [])
453
                    for attribute in sorted(attributes):
454
                        export += " " + attribute
455
                    export += "\n"
456
                listattr = ""
457
                if group[0] in self.groupattributes:
458
                    for attribute in sorted(self.groupattributes[group[0]]):
459
                        listattr += attribute+"\n"
460
                    if listattr != "":
461
                        export += "\n[" + group[0] + ":vars]\n"+listattr
462

    
463
        # All cgroups and vars
464
        for cgroup in self.get_items(self.cgroupgroups):
465
            export += "\n[" + cgroup[0] + ":children]\n"
466
            for group in cgroup[1]:
467
                export += group + "\n"
468
                #attributes = self.hostattributes.setdefault(group, [])
469
                #for attribute in sorted(attributes):
470
                #    export += " ------------------------------ " + attribute
471
                #export += "\n"
472
            listattr = ""
473
            if cgroup[0] in self.groupattributes:
474
                for attribute in sorted(self.groupattributes[cgroup[0]]):
475
                    listattr += attribute+"\n"
476
                if listattr != "":
477
                    export += "\n[" + cgroup[0] + ":vars]\n"+listattr
478
        return export
479

    
480
    ## Function to manage the list of "ungroupped" hosts.
481
    # It will search for the presence of any host in the grouphost list
482
    # and put hosts which are not in a group in the "ungroupped" group
483
    def update_ungroupped(self):
484
        for host in self.gethosts():
485
            for group in self.getgroupsforhost(host):
486
                # If host is in a group (not ungroupped) then remove it from ungroupped
487
                if group != ungrouped:
488
                    self.delhosttogroup(host,ungrouped)
489
                    break
490
            else:
491
                self.addhosttogroup(host,ungrouped)
492

    
493
## Class to manage the graphic interface
494
class ImageViewer(QtGui.QMainWindow, interface_ansible.Ui_MainWindow):
495
    def __init__(self, parent=None):
496
        super(ImageViewer, self).__init__(parent)
497
        self.ansible = AnsibleHostFile()
498

    
499
        self.setupUi(self)
500

    
501
        # Define connectors
502
        self.tabs.currentChanged.connect(self.ontabchange)
503
        self.actionload_hostsfile.triggered.connect(self.load_hostsfile)
504
        self.Loadhostfile.clicked.connect(self.load_hostsfile)
505
        self.actionSauver.triggered.connect(self.save_hostsfile)
506
        self.Savehostfile.clicked.connect(self.save_hostsfile)
507
        self.actionQuitter.triggered.connect(self.quit)
508

    
509
        # Tab Hosts
510
        QtCore.QObject.connect(self.addhost, QtCore.SIGNAL("clicked()"), self.clic_addnewhost)
511
        QtCore.QObject.connect(self.hosttoadd, QtCore.SIGNAL("returnPressed()"), self.clic_addnewhost)
512
        self.hostslist.itemSelectionChanged.connect(self.update_listhostgroups)
513
        QtCore.QObject.connect(self.hostgroupslist, QtCore.SIGNAL("dropped"), self.add_group_to_host)
514
        QtCore.QObject.connect(self.availablegroupslist, QtCore.SIGNAL("dropped"), self.remove_group_to_host)
515
        QtCore.QObject.connect(self.hostattributeslist, QtCore.SIGNAL("dropped"), self.add_attribute_to_host)
516
        QtCore.QObject.connect(self.availableattributeslist, QtCore.SIGNAL("dropped"), self.remove_attribute_to_host)
517
        QtCore.QObject.connect(self.hostslist, QtCore.SIGNAL("rightclick"), self.delhost)
518

    
519
        # Tab Groups
520
        QtCore.QObject.connect(self.addgroup, QtCore.SIGNAL("clicked()"), self.clic_addnewgroup)
521
        QtCore.QObject.connect(self.grouptoadd, QtCore.SIGNAL("returnPressed()"), self.clic_addnewgroup)
522
        self.groupslist.itemSelectionChanged.connect(self.update_listgrouphosts)
523
        #QtCore.QObject.connect(self.grouphostslist, QtCore.SIGNAL("dropped"), self.add_host_to_group)
524
        QtCore.QObject.connect(self.grouphostslist, QtCore.SIGNAL("modified"), self.reload_grouphosts)
525
        QtCore.QObject.connect(self.availablehostslist_2, QtCore.SIGNAL("modified"), self.reload_grouphosts)
526
        #QtCore.QObject.connect(self.availablehostslist_2, QtCore.SIGNAL("dropped"), self.remove_host_to_group)
527
        QtCore.QObject.connect(self.groupslist, QtCore.SIGNAL("rightclick"), self.delgroup)
528
        QtCore.QObject.connect(self.groupattributeslist, QtCore.SIGNAL("dropped"), self.add_attribute_to_group)
529
        QtCore.QObject.connect(self.availableattributeslist_2, QtCore.SIGNAL("dropped"), self.remove_attribute_to_group)
530

    
531
        # Tab cgroups
532
        QtCore.QObject.connect(self.addcgroup, QtCore.SIGNAL("clicked()"), self.clic_addnewcgroup)
533
        QtCore.QObject.connect(self.cgrouptoadd, QtCore.SIGNAL("returnPressed()"), self.clic_addnewcgroup)
534
        self.cgroupslist.itemSelectionChanged.connect(self.update_listcgroupgroups)
535
        #QtCore.QObject.connect(self.groupcgroupslist, QtCore.SIGNAL("dropped"), self.add_group_to_cgroup)
536
        QtCore.QObject.connect(self.groupcgroupslist, QtCore.SIGNAL("modified"), self.reload_cgroupgroups)
537
        QtCore.QObject.connect(self.availablegroupslist_2, QtCore.SIGNAL("modified"), self.reload_cgroupgroups)
538
        #QtCore.QObject.connect(self.availablegroupslist_2, QtCore.SIGNAL("dropped"), self.remove_group_to_cgroup)
539
        QtCore.QObject.connect(self.cgroupslist, QtCore.SIGNAL("rightclick"), self.delcgroup)
540
        QtCore.QObject.connect(self.cgroupattributeslist, QtCore.SIGNAL("dropped"), self.add_attribute_to_cgroup)
541
        QtCore.QObject.connect(self.availableattributeslist_3, QtCore.SIGNAL("dropped"), self.remove_attribute_to_cgroup)
542

    
543
        # Tab attributes
544
        QtCore.QObject.connect(self.addattribute, QtCore.SIGNAL("clicked()"), self.addnewattribute)
545
        QtCore.QObject.connect(self.attributetoadd, QtCore.SIGNAL("returnPressed()"), self.addnewattribute)
546
        self.attributeslist.itemSelectionChanged.connect(self.update_listattributegroup)
547
        QtCore.QObject.connect(self.attributehostslist, QtCore.SIGNAL("dropped"), self.add_host_to_attribute)
548
        QtCore.QObject.connect(self.availablehostslist_3, QtCore.SIGNAL("dropped"), self.remove_host_to_attribute)
549
        QtCore.QObject.connect(self.attributegroupslist, QtCore.SIGNAL("dropped"), self.add_group_to_attribute)
550
        QtCore.QObject.connect(self.availablegroupslist_3, QtCore.SIGNAL("dropped"), self.remove_group_to_attribute)
551
        QtCore.QObject.connect(self.attributecgroupslist, QtCore.SIGNAL("dropped"), self.add_cgroup_to_attribute)
552
        QtCore.QObject.connect(self.availablecgroupslist, QtCore.SIGNAL("dropped"), self.remove_cgroup_to_attribute)
553
        QtCore.QObject.connect(self.attributeslist, QtCore.SIGNAL("rightclick"), self.delattribute)
554

    
555
        ## This to define the list between which drag and drop is authorized
556
        self.hostgroupslist.acceptfrom = ["availablegroupslist"]
557
        self.availablegroupslist.acceptfrom = ["hostgroupslist"]
558
        self.groupattributeslist.acceptfrom = ["availableattributeslist_2"]
559
        self.availableattributeslist_2.acceptfrom = ["groupattributeslist"]
560
        self.hostattributeslist.acceptfrom = ["availableattributeslist"]
561
        self.availableattributeslist.acceptfrom = ["hostattributeslist"]
562
        self.grouphostslist.acceptfrom = ["availablehostslist_2"]
563
        self.availablehostslist_2.acceptfrom = ["grouphostslist"]
564
        self.groupcgroupslist.acceptfrom = ["availablegroupslist_2"]
565
        self.availablegroupslist_2.acceptfrom = ["groupcgroupslist"]
566
        self.cgroupattributeslist.acceptfrom = ["availableattributeslist_3"]
567
        self.availableattributeslist_3.acceptfrom = ["cgroupattributeslist"]
568
        self.attributegroupslist.acceptfrom = ["availablegroupslist_3"]
569
        self.availablegroupslist_3.acceptfrom = ["attributegroupslist"]
570
        self.attributecgroupslist.acceptfrom =["availablecgroupslist"]
571
        self.availablecgroupslist.acceptfrom = ["attributecgroupslist"]
572
        self.attributehostslist.acceptfrom = ["availablehostslist_3"]
573
        self.availablehostslist_3.acceptfrom = ["attributehostslist"]
574

    
575
        ## Set the current tab to host view
576
        self.tabs.setCurrentIndex(0)
577

    
578

    
579
    def quit(self):
580
        print("bye bye")
581
        sys.exit(0)
582

    
583
    def save_hostsfile(self):
584
        filename = QtGui.QFileDialog.getOpenFileName(self, 'File to save')
585
        if filename != "":
586
            if os.path.isfile(filename):
587
                warn_msg = "File exists. Do you want to overwrite ?"
588
                reply = QtGui.QMessageBox.question(self, 'Message',warn_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
589

    
590
                if reply == QtGui.QMessageBox.No:
591
                    return
592

    
593
            file = open(filename, 'w')
594
            result = self.ansible.export_ansible()
595
            file.write(result)
596
            file.close()
597

    
598

    
599
    ## On tab change event catcher
600
    # will update lists of the new tab to get modification made previously
601
    # will generate the new hosts file when tab "view hosts file" is activated
602
    def ontabchange(self,i):
603
        if i == 0:
604
            self.update_liststabhosts()
605
        elif i == 1:
606
            self.update_liststabgroups()
607
        elif i == 2:
608
            self.update_liststabcgroups()
609
        elif i == 3:
610
            self.update_liststabattributes()
611
        elif i == 4:
612
            result = self.ansible.export_ansible()
613
            self.textEdit.setText(result)
614
            #QtGui.QMessageBox.information(self,
615
            #      "Tab Index Changed!",
616
            #      "Current Tab Index: %d" % i ) #changed!
617

    
618

    
619
    ## Delete a host
620
    # will ask for confirmation
621
    def delhost(self, pos):
622
        menu = QtGui.QMenu(self)
623
        texte = ""
624
        for host in self.hostslist.selectedItems():
625
            texte += host.text() + " "
626
        delAction = menu.addAction("Delete " + texte)
627
        action = menu.exec_(self.hostslist.mapToGlobal(pos))
628
        if action == delAction:
629
            for hostname in self.hostslist.selectedItems():
630
                warn_msg = "Are you sure you want to delete host " + hostname.text() + " ?"
631
                reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
632
                                                   QtGui.QMessageBox.No)
633
                if reply == QtGui.QMessageBox.Yes:
634
                    self.ansible.delhost(hostname.text())
635
        self.update_listallhosts()
636

    
637
    ## Delete a group
638
    # will ask for confirmation
639
    def delgroup(self, pos):
640
        menu = QtGui.QMenu(self)
641
        texte = ""
642
        for group in self.groupslist.selectedItems():
643
            if group.text() in specials:
644
                return
645
            texte += group.text() + " "
646
        delAction = menu.addAction("Delete " + texte)
647
        action = menu.exec_(self.groupslist.mapToGlobal(pos))
648
        if action == delAction:
649
            for groupname in self.groupslist.selectedItems():
650
                # hostname = self.hostslist.selectedItem.text()
651
                # host=host(hostname)
652
                warn_msg = "Are you sure you want to delete group "+groupname.text()+" ?"
653
                reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
654
                if reply == QtGui.QMessageBox.Yes:
655
                    self.ansible.delgroup(groupname.text())
656
        self.update_listallgroups()
657

    
658
    ## Delete a cgroup
659
    # will ask for confirmation
660
    def delcgroup(self, pos):
661
        menu = QtGui.QMenu(self)
662
        texte = ""
663
        for cgroup in self.cgroupslist.selectedItems():
664
            texte += cgroup.text() + " "
665
        delAction = menu.addAction("Delete " + texte)
666
        action = menu.exec_(self.cgroupslist.mapToGlobal(pos))
667
        if action == delAction:
668
            for cgroupname in self.cgroupslist.selectedItems():
669
                warn_msg = "Are you sure you want to delete Cgroup " + cgroupname.text() + " ?"
670
                reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
671
                                                   QtGui.QMessageBox.No)
672
                if reply == QtGui.QMessageBox.Yes:
673
                    self.ansible.delcgroup(cgroupname.text())
674
        self.update_listallcgroups()
675

    
676
    ## Delete an attribute
677
    # will ask for confirmation
678
    def delattribute(self,pos):
679
        menu = QtGui.QMenu(self)
680
        texte = ""
681
        for attribute in self.attributeslist.selectedItems():
682
            texte += attribute.text() + " "
683
        delAction = menu.addAction("Delete " + texte)
684
        action = menu.exec_(self.attributeslist.mapToGlobal(pos))
685
        if action == delAction:
686
            for attributename in self.attributeslist.selectedItems():
687
                warn_msg = "Are you sure you want to delete attribute " + attributename.text() + " ?"
688
                reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
689
                                                   QtGui.QMessageBox.No)
690
                if reply == QtGui.QMessageBox.Yes:
691
                    self.ansible.delattribute(attributename.text())
692
        self.update_listallattributes()
693

    
694

    
695
    ## function to load a hosts file
696
    def load_hostsfile(self):
697
        self.ansible.reset()
698
        filename = QtGui.QFileDialog.getOpenFileName(self,'File to open')
699
        #filename = "hosts"
700
        file = open(filename, 'r')
701
        current_group = ungrouped
702
        cgroup = False
703
        vargroup = False
704
        while 1:
705
            line = file.readline()
706
            if not line:
707
                break
708
            line = line.strip(" \t\n\r")
709

    
710
            if len(line) > 1:
711
                # Pass comments and empty lines
712
                if line[0] == "#":
713
                    pass
714
                elif line == '':
715
                    pass
716
                # Detect groups
717
                elif line[0] == "[":
718
                    # Get name of group (part between []
719
                    newgroupname = re.search('^\[(.*)\]', line).group(1)
720
                    # Search for cgroup
721
                    lcgroup = re.search('(.*):children', newgroupname)
722

    
723
                    if lcgroup is None:
724
                        cgroup = False
725
                        groupvar = re.search('(.*):vars', newgroupname)
726
                        if groupvar is None:
727
                            self.ansible.addgroup(newgroupname)
728
                            vargroup = False
729
                        else:
730
                            newgroupname = groupvar.group(1)
731
                            vargroup = True
732
                            #self.ansible.addgroup(newgroupname) #PB No way to know if group or cgroup if not defined before !
733

    
734
                    else:
735
                        newgroupname = lcgroup.group(1)
736
                        self.ansible.addcgroup(lcgroup.group(1))
737
                        cgroup = True
738
                        vargroup = False
739
                    current_group = newgroupname
740
                    # newgroup = group(newgroupname)
741
                    # Add group to the list of groups if not already here
742

    
743

    
744
                # Read host and attributes
745
                else:
746
                    if not vargroup:
747
                        self.addnewhost(line, current_group, cgroup)
748
                    else:
749
                        self.addgroupattribute(line,current_group)
750
        self.update_alllists()
751
        check_result=self.ansible.check_integrity()
752
        if  check_result:
753
            message = QtGui.QMessageBox()
754
            message.setIcon(QtGui.QMessageBox.Warning)
755
            message.setText(check_result)
756
            message.exec()
757

    
758
    def addgroupattribute(self,line=None, group=None):
759
        self.ansible.addattributetogroup(line,group)
760

    
761
    ## Function activated for manual add a new host from the interface
762
    # Check the existence of host name as group or cgroup name and popup a warning before calling addnewhost()
763
    def clic_addnewhost(self):
764
        hostname = self.hosttoadd.text()
765
        warn_msg = self.ansible.check_hostname(hostname)
766
        if warn_msg is not "":
767
            warn_msg += "\nAre you sure you want to add host " + hostname + " ?"
768
            reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
769
                                               QtGui.QMessageBox.No)
770
            if reply == QtGui.QMessageBox.Yes:
771
                self.addnewhost(hostname)
772
        else:
773
            self.addnewhost(hostname)
774

    
775
    ## Add a new host
776
    # called by clic_addnewhost() or load_hostsfile()
777
    def addnewhost(self, line=None, group=None, cgroup=False):
778
        if line is None:
779
            line = self.hosttoadd.text()
780
        if line == "":
781
            return
782

    
783
        if group is None:
784
            group = ungrouped
785
            cgroup = False
786
        hostname = line.split()
787

    
788
        if cgroup:
789
            newgroup = hostname[0]
790
            cgroup = group
791
            #Not adding group because we don't know if it's a group or cgroup
792
            #self.ansible.addgroup(newgroup)
793
            self.ansible.addgrouptocgroup(newgroup, cgroup)
794
        else:
795
            newhost = hostname[0]
796
            self.ansible.addhost(newhost)
797
            if len(hostname) > 1:
798
                for i in range(1, len(hostname)):
799
                    attribute = hostname[i].split('=')
800
                    if len(attribute) > 1:
801
                        newattribute = attribute[0] + "=" + attribute[1]
802
                        self.ansible.addattribute(newattribute)
803
                        self.ansible.addattributetohost(newattribute, newhost)
804
            self.ansible.addhosttogroup(newhost, group)
805
        self.update_listallattributes()
806
        self.update_listallhosts()
807
        self.hosttoadd.clear()
808

    
809
    ## Function activated for manual add a new group from the interface
810
    # Check the existence of group name as host or cgroup name and popup a warning before calling addnewgroup()
811
    def clic_addnewgroup(self):
812
        groupname = self.grouptoadd.text()
813
        warn_msg = self.ansible.check_groupname(groupname)
814
        if warn_msg is not "":
815
            warn_msg += "\nAre you sure you want to add group " + groupname + " ?"
816
            reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
817
                                               QtGui.QMessageBox.No)
818
            if reply == QtGui.QMessageBox.Yes:
819
                self.addnewgroup(groupname)
820
        else:
821
            self.addnewgroup(groupname)
822

    
823
    ## Add a new group
824
    # called by clic_addnewgroup() or load_hostsfile()
825
    def addnewgroup(self, newgroupname=None):
826
        if newgroupname is None:
827
            newgroupname = self.grouptoadd.text()
828
        if newgroupname == "":
829
            return
830
        # Add group to the list of groups if not already here
831
        if newgroupname not in self.ansible.groups:
832
            self.ansible.addgroup(newgroupname)
833
        self.update_listallgroups()
834
        self.grouptoadd.clear()
835

    
836
    ## Function activated for manual add a new cgroup from the interface
837
    # Check the existence of cgroup name as host or group name and popup a warning before calling addnewcgroup()
838
    def clic_addnewcgroup(self):
839
        cgroupname = self.cgrouptoadd.text()
840
        warn_msg = self.ansible.check_cgroupname(cgroupname)
841
        if warn_msg is not "":
842
            warn_msg += "\nAre you sure you want to add Cgroup " + cgroupname + " ?"
843
            reply = QtGui.QMessageBox.question(self, 'Message', warn_msg, QtGui.QMessageBox.Yes,
844
                                               QtGui.QMessageBox.No)
845
            if reply == QtGui.QMessageBox.Yes:
846
                self.addnewcgroup(cgroupname)
847
        else:
848
            self.addnewcgroup(cgroupname)
849

    
850
    ## Add a new cgroup
851
    # called by clic_addnewcgroup() or load_hostsfile()
852
    def addnewcgroup(self, newcgroupname=None):
853
        if newcgroupname is None:
854
            newcgroupname = self.cgrouptoadd.text()
855
        if newcgroupname == "":
856
            return
857
        # Add group to the list of groups if not already here
858
        if newcgroupname not in self.ansible.cgroups:
859
            self.ansible.addcgroup(newcgroupname)
860
        self.update_listallcgroups()
861
        self.cgrouptoadd.clear()
862

    
863
    ## Add a new attribute
864
    def addnewattribute(self, newattributename=None):
865
        if newattributename is None:
866
            newattributename = self.attributetoadd.text()
867
        if newattributename == "":
868
            return
869
        if newattributename not in self.ansible.attributes:
870
            self.ansible.addattribute(newattributename)
871
        self.update_listallattributes()
872
        self.attributetoadd.clear()
873

    
874
    def update_listhostgroups(self):
875
        self.hostgroupslist.clear()
876
        self.update_listavailablegroups()
877
        if self.hostslist.currentItem() is not None:
878
            selected_host = str(self.hostslist.currentItem().text())
879
            for group in self.ansible.getgroupsforhost(selected_host).items():
880
                # Add it to the list of groups for this host
881
                # self.hostgroupslist.addItem(group[0])
882
                if group[0] not in specials:
883
                    QtGui.QListWidgetItem(group[0], self.hostgroupslist)
884
                    # Remove it from the available groups
885
                    for item in self.availablegroupslist.findItems(group[0], QtCore.Qt.MatchExactly):
886
                        self.availablegroupslist.takeItem(self.availablegroupslist.row(item))
887
            self.update_listhostattributes()
888

    
889
    def update_listgrouphosts(self):
890
        self.grouphostslist.clear()
891
        self.update_listavailablehosts()
892
        if self.groupslist.currentItem() is not None:
893
            selected_group = str(self.groupslist.currentItem().text())
894
            for hosts in self.ansible.gethostsforgroup(selected_group).items():
895
                for host in hosts[1]:
896
                    # Add it to the list of hosts for this group
897
                    QtGui.QListWidgetItem(host, self.grouphostslist)
898
                    # Remove it from the available groups
899
                    for item in self.availablehostslist_2.findItems(host, QtCore.Qt.MatchExactly):
900
                        self.availablehostslist_2.takeItem(self.availablehostslist_2.row(item))
901
        self.update_listgroupattributes()
902

    
903
    def update_listcgroupgroups(self):
904
        self.groupcgroupslist.clear()
905
        self.update_listavailablegroups_2()
906
        if self.cgroupslist.currentItem() is not None:
907
            selected_cgroup = str(self.cgroupslist.currentItem().text())
908
            for item in self.availablegroupslist_2.findItems(selected_cgroup, QtCore.Qt.MatchExactly):
909
                self.availablegroupslist_2.takeItem(self.availablegroupslist_2.row(item))
910
            for groups in self.ansible.getgroupforcgroup(selected_cgroup).items():
911
                # Add it to the list of hosts for this group
912
                for group in groups[1]:
913
                    QtGui.QListWidgetItem(group, self.groupcgroupslist)
914
                    # Remove it from the available groups
915
                    for item in self.availablegroupslist_2.findItems(group, QtCore.Qt.MatchExactly):
916
                        self.availablegroupslist_2.takeItem(self.availablegroupslist_2.row(item))
917
        self.update_listcgroupattributes()
918

    
919
    def update_listattributehosts(self):
920
        self.attributehostslist.clear()
921
        self.update_listavailablehosts_3()
922
        if self.attributeslist.currentItem() is not None:
923
            selected_attribute = str(self.attributeslist.currentItem().text())
924
            for hosts in self.ansible.gethostsforattribute(selected_attribute).items():
925
                # Add it to the list of hosts for this group
926
                QtGui.QListWidgetItem(hosts[0], self.attributehostslist)
927
                # Remove it from the available groups
928
                for item in self.availablehostslist_3.findItems(hosts[0], QtCore.Qt.MatchExactly):
929
                        self.availablehostslist_3.takeItem(self.availablehostslist_3.row(item))
930

    
931
    def update_listattributegroup(self):
932
        self.attributegroupslist.clear()
933
        self.update_listavailablegroups_3()
934
        if self.attributeslist.currentItem() is not None:
935
            selected_attribute = str(self.attributeslist.currentItem().text())
936
            for groups in self.ansible.getgroupforattribute(selected_attribute).items():
937
                # Add it to the list of hosts for this group
938
                QtGui.QListWidgetItem(groups[0], self.attributegroupslist)
939
                # Remove it from the available groups
940
                for item in self.availablegroupslist_3.findItems(groups[0], QtCore.Qt.MatchExactly):
941
                        self.availablegroupslist_3.takeItem(self.availablegroupslist_3.row(item))
942
        self.update_listattributecgroup()
943
        self.update_listattributehosts()
944

    
945
    def update_listattributecgroup(self):
946
        self.attributecgroupslist.clear()
947
        self.update_listavailablecgroups()
948
        if self.attributeslist.currentItem() is not None:
949
            selected_attribute = str(self.attributeslist.currentItem().text())
950
            for cgroups in self.ansible.getcgroupforattribute(selected_attribute).items():
951
                # Add it to the list of hosts for this group
952
                QtGui.QListWidgetItem(cgroups[0], self.attributecgroupslist)
953
                # Remove it from the available groups
954
                for item in self.availablecgroupslist.findItems(cgroups[0], QtCore.Qt.MatchExactly):
955
                        self.availablecgroupslist.takeItem(self.availablecgroupslist.row(item))
956
        #self.update_listgroupattributes()
957

    
958
    def update_listhostattributes(self):
959
        self.hostattributeslist.clear()
960
        self.update_listavailableattributes()
961
        if self.hostslist.currentItem() is not None:
962
            selected_host = str(self.hostslist.currentItem().text())
963
            for attributes in self.ansible.getattributesforhost(selected_host).items():
964
                for attribute in attributes[1]:
965
                    QtGui.QListWidgetItem(attribute, self.hostattributeslist)
966
                    for item in self.availableattributeslist.findItems(attribute, QtCore.Qt.MatchExactly):
967
                        self.availableattributeslist.takeItem(self.availableattributeslist.row(item))
968

    
969
    def update_listgroupattributes(self):
970
        self.groupattributeslist.clear()
971
        self.update_listallattributes()
972
        if self.groupslist.currentItem() is not None:
973
            selected_group = str(self.groupslist.currentItem().text())
974
            for attributes in self.ansible.getattributesforgroup(selected_group).items():
975
                for attribute in attributes[1]:
976
                    QtGui.QListWidgetItem(attribute, self.groupattributeslist)
977
                    for item in self.availableattributeslist_2.findItems(attribute, QtCore.Qt.MatchExactly):
978
                        self.availableattributeslist_2.takeItem(self.availableattributeslist_2.row(item))
979

    
980
    def update_listcgroupattributes(self):
981
        self.cgroupattributeslist.clear()
982
        self.update_listallattributes()
983
        if self.cgroupslist.currentItem() is not None:
984
            selected_group = str(self.cgroupslist.currentItem().text())
985
            for attributes in self.ansible.getattributesforgroup(selected_group).items():
986
                for attribute in attributes[1]:
987
                    QtGui.QListWidgetItem(attribute, self.cgroupattributeslist)
988
                    for item in self.availableattributeslist_3.findItems(attribute, QtCore.Qt.MatchExactly):
989
                        self.availableattributeslist_3.takeItem(self.availableattributeslist_3.row(item))
990

    
991
    def update_listhosts(self):
992
        self.hostslist.clear()
993
        for host in self.ansible.gethosts():
994
            # self.hostslist.addItem(host[0])
995
            QtGui.QListWidgetItem(host, self.hostslist)
996

    
997
    def update_listgroups(self):
998
        self.groupslist.clear()
999
        for group in self.ansible.getgroups():
1000
            self.groupslist.addItem(group)
1001

    
1002
    def update_listcgroups(self):
1003
        self.cgroupslist.clear()
1004
        for cgroup in self.ansible.getcgroups():
1005
            # self.cgroupslist.addItem(cgroup[0])
1006
            QtGui.QListWidgetItem(cgroup, self.cgroupslist)
1007

    
1008
    def update_listattributes(self):
1009
        self.attributeslist.clear()
1010
        for attribute in self.ansible.gethostsattributes():
1011
            # self.attributeslist.addItem(attribute[1].name)
1012
            QtGui.QListWidgetItem(attribute, self.attributeslist)
1013
            #    self.attributeslist.addItem(key+"="+value.value)
1014

    
1015
    def update_listavailablegroups(self):
1016
        self.availablegroupslist.clear()
1017
        # for group in self.ansible.groups.items():
1018
        for group in self.ansible.getgroups():
1019
            if group not in specials:
1020
                # self.availablegroupslist.addItem(group[0])
1021
                QtGui.QListWidgetItem(group, self.availablegroupslist)
1022

    
1023
    def update_listavailablegroups_2(self):
1024
        self.availablegroupslist_2.clear()
1025
        # for group in self.ansible.groups.items():
1026
        for group in self.ansible.getgroups():
1027
            if group not in specials:
1028
                # self.availablegroupslist.addItem(group[0])
1029
                QtGui.QListWidgetItem(group, self.availablegroupslist_2)
1030
        for cgroup in self.ansible.getcgroups():
1031
            QtGui.QListWidgetItem(cgroup, self.availablegroupslist_2)
1032

    
1033
    def update_listavailablegroups_3(self):
1034
        self.availablegroupslist_3.clear()
1035
        # for group in self.ansible.groups.items():
1036
        for group in self.ansible.getgroups():
1037
            if group != ungrouped:
1038
                # self.availablegroupslist.addItem(group[0])
1039
                QtGui.QListWidgetItem(group, self.availablegroupslist_3)
1040
        self.update_listavailablecgroups()
1041

    
1042
    def update_listavailablecgroups(self):
1043
        self.availablecgroupslist.clear()
1044
        for cgroup in self.ansible.getcgroups():
1045
            # self.cgroupslist.addItem(cgroup[0])
1046
            QtGui.QListWidgetItem(cgroup, self.availablecgroupslist)
1047

    
1048
    def update_listavailablehosts(self):
1049
        self.availablehostslist_2.clear()
1050
        # for group in self.ansible.groups.items():
1051
        for host in self.ansible.gethosts():
1052
            # self.availablehostslist_2.addItem(host[0])
1053
            QtGui.QListWidgetItem(host, self.availablehostslist_2)
1054

    
1055
    def update_listavailablehosts_3(self):
1056
        self.availablehostslist_3.clear()
1057
        # for group in self.ansible.groups.items():
1058
        for host in self.ansible.gethosts():
1059
            # self.availablehostslist_2.addItem(host[0])
1060
            QtGui.QListWidgetItem(host, self.availablehostslist_3)
1061

    
1062
    def update_listavailableattributes(self):
1063
        self.availableattributeslist.clear()
1064
        for attribute in self.ansible.gethostsattributes():
1065
            # self.availableattributeslist.addItem(attribute[1].name)
1066
            QtGui.QListWidgetItem(attribute, self.availableattributeslist)
1067

    
1068
    def update_listavailableattributes_2(self):
1069
        self.availableattributeslist_2.clear()
1070
        for attribute in self.ansible.gethostsattributes():
1071
            # self.availableattributeslist.addItem(attribute[1].name)
1072
            QtGui.QListWidgetItem(attribute, self.availableattributeslist_2)
1073

    
1074
    def update_listavailableattributes_3(self):
1075
        self.availableattributeslist_3.clear()
1076
        for attribute in self.ansible.gethostsattributes():
1077
            # self.availableattributeslist.addItem(attribute[1].name)
1078
            QtGui.QListWidgetItem(attribute, self.availableattributeslist_3)
1079

    
1080
    def add_attribute_to_host(self):
1081
        #currenthost = None
1082
        for host in self.hostslist.selectedItems():
1083
            currenthost = host.text()
1084
            for attribute in self.availableattributeslist.selectedItems():
1085
                currentattribute = attribute.text()
1086
                self.ansible.addattributetohost(currentattribute, currenthost)
1087

    
1088
    def remove_attribute_to_host(self):
1089
        for host in self.hostslist.selectedItems():
1090
            currenthost = host.text()
1091
            for attribute in self.hostattributeslist.selectedItems():
1092
                currentattribute = attribute.text()
1093
                self.ansible.delattributetohost(currentattribute, currenthost)
1094

    
1095
    def add_attribute_to_group(self):
1096
        #currenthost = None
1097
        for group in self.groupslist.selectedItems():
1098
            currentgroup = group.text()
1099
            for attribute in self.availableattributeslist_2.selectedItems():
1100
                currentattribute = attribute.text()
1101
                self.ansible.addattributetogroup(currentattribute, currentgroup)
1102

    
1103
    def remove_attribute_to_group(self):
1104
        for group in self.groupslist.selectedItems():
1105
            currentgroup = group.text()
1106
            for attribute in self.groupattributeslist.selectedItems():
1107
                currentattribute = attribute.text()
1108
                self.ansible.delattributetogroup(currentattribute, currentgroup)
1109

    
1110
    def add_host_to_attribute(self):
1111
        #currenthost = None
1112
        for attribute in self.attributeslist.selectedItems():
1113
            currentattribute = attribute.text()
1114
            for host in self.availablehostslist_3.selectedItems():
1115
                currenthost = host.text()
1116
                self.ansible.addattributetohost(currentattribute, currenthost)
1117

    
1118
    def remove_host_to_attribute(self):
1119
        for attribute in self.attributeslist.selectedItems():
1120
            currentattribute = attribute.text()
1121
            for host in self.attributehostslist.selectedItems():
1122
                currenthost = host.text()
1123
                self.ansible.delattributetohost(currentattribute, currenthost)
1124

    
1125
    def add_group_to_attribute(self):
1126
        #currenthost = None
1127
        for attribute in self.attributeslist.selectedItems():
1128
            currentattribute = attribute.text()
1129
            for group in self.availablegroupslist_3.selectedItems():
1130
                currentgroup = group.text()
1131
                self.ansible.addattributetogroup(currentattribute, currentgroup)
1132

    
1133
    def remove_group_to_attribute(self):
1134
        for attribute in self.attributeslist.selectedItems():
1135
            currentattribute = attribute.text()
1136
            for group in self.attributegroupslist.selectedItems():
1137
                currentgroup = group.text()
1138
                self.ansible.delattributetogroup(currentattribute, currentgroup)
1139

    
1140
    def add_attribute_to_cgroup(self):
1141
        #currenthost = None
1142
        for cgroup in self.cgroupslist.selectedItems():
1143
            currentcgroup = cgroup.text()
1144
            for attribute in self.availableattributeslist_3.selectedItems():
1145
                currentattribute = attribute.text()
1146
                self.ansible.addattributetocgroup(currentattribute, currentcgroup)
1147

    
1148
    def remove_attribute_to_cgroup(self):
1149
        for cgroup in self.cgroupslist.selectedItems():
1150
            currentcgroup = cgroup.text()
1151
            for attribute in self.cgroupattributeslist.selectedItems():
1152
                currentattribute = attribute.text()
1153
                self.ansible.delattributetocgroup(currentattribute, currentcgroup)
1154

    
1155
    def add_cgroup_to_attribute(self):
1156
        #currenthost = None
1157
        for attribute in self.attributeslist.selectedItems():
1158
            currentattribute = attribute.text()
1159
            for cgroup in self.availablecgroupslist.selectedItems():
1160
                currentcgroup = cgroup.text()
1161
                self.ansible.addattributetocgroup(currentattribute, currentcgroup)
1162

    
1163
    def remove_cgroup_to_attribute(self):
1164
        for attribute in self.attributeslist.selectedItems():
1165
            currentattribute = attribute.text()
1166
            for cgroup in self.attributecgroupslist.selectedItems():
1167
                currentcgroup = cgroup.text()
1168
                self.ansible.delattributetocgroup(currentattribute, currentcgroup)
1169

    
1170
    def add_group_to_host(self):
1171
        # Read groups in hostgroupslist and add host to the group
1172
        #currenthost = None
1173
        for host in self.hostslist.selectedItems():
1174
            currenthost = host.text()
1175
            for group in self.availablegroupslist.selectedItems():
1176
                currentgroup = group.text()
1177
                self.ansible.addhosttogroup(currenthost, currentgroup)
1178
        self.ansible.update_ungroupped()
1179

    
1180
    def remove_group_to_host(self):
1181
        for host in self.hostslist.selectedItems():
1182
            currenthost = host.text()
1183
            for group in self.hostgroupslist.selectedItems():
1184
                currentgroup = group.text()
1185
                self.ansible.delhosttogroup(currenthost, currentgroup)
1186
        self.ansible.update_ungroupped()
1187

    
1188
    def add_group_to_cgroup(self):
1189
        # Read groups in hostgroupslist and add host to the group
1190
        #currentcgroup = None
1191
        for cgroup in self.cgroupslist.selectedItems():
1192
            currentcgroup = cgroup.text()
1193
            for group in self.availablegroupslist_2.selectedItems():
1194
                currentgroup = group.text()
1195
                self.ansible.addgrouptocgroup(currentgroup, currentcgroup)
1196

    
1197
    def remove_group_to_cgroup(self):
1198
        for cgroup in self.cgroupslist.selectedItems():
1199
            currentcgroup = cgroup.text()
1200
            for group in self.groupcgroupslist.selectedItems():
1201
                currentgroup = group.text()
1202
                self.ansible.delgrouptocgroup(currentgroup, currentcgroup)
1203

    
1204
    def add_host_to_group(self,prov):
1205
        #print("provenance",prov)
1206
        # Read groups in hostgroupslist and add host to the group
1207
        #currenthost = None
1208
        for group in self.groupslist.selectedItems():
1209
            currentgroup = group.text()
1210
            for host in self.availablehostslist_2.selectedItems():
1211
                currenthost = host.text()
1212
                self.ansible.addhosttogroup(currenthost, currentgroup)
1213
        self.ansible.update_ungroupped()
1214

    
1215
    def reload_grouphosts(self):
1216
        for group in self.groupslist.selectedItems():
1217
            currentgroup = group.text()
1218
            grouphost = self.ansible.grouphosts.setdefault(currentgroup, [])
1219
            grouphost.clear()
1220
            for host in self.grouphostslist.getallitems():
1221
                self.ansible.addhosttogroup(host.text(), currentgroup)
1222

    
1223
    def reload_cgroupgroups(self):
1224
        for cgroup in self.cgroupslist.selectedItems():
1225
            currentcgroup = cgroup.text()
1226
            cgroupgroup = self.ansible.cgroupgroups.setdefault(currentcgroup, [])
1227
            cgroupgroup.clear()
1228
            for group in self.groupcgroupslist.getallitems():
1229
                self.ansible.addgrouptocgroup(group.text(), currentcgroup)
1230

    
1231
    def remove_host_to_group(self):
1232
        for group in self.groupslist.selectedItems():
1233
            currentgroup = group.text()
1234
            for host in self.grouphostslist.selectedItems():
1235
                currenthost = host.text()
1236
                self.ansible.delhosttogroup(currenthost, currentgroup)
1237
        self.ansible.update_ungroupped()
1238

    
1239
    def update_alllists(self):
1240
        self.update_listallhosts()
1241
        self.update_listallgroups()
1242
        self.update_listallcgroups()
1243
        self.update_listallattributes()
1244

    
1245
    def update_liststabhosts(self):
1246
        #self.update_listhosts()
1247
        self.update_listhostgroups()
1248
        self.update_listavailablegroups()
1249
        self.update_listhostattributes()
1250
        self.update_listavailableattributes()
1251

    
1252
    def update_liststabgroups(self):
1253
        #self.update_listgroups()
1254
        self.update_listgrouphosts()
1255
        self.update_listavailablehosts()
1256
        self.update_listgroupattributes()
1257
        self.update_listavailableattributes_2()
1258

    
1259
    def update_liststabcgroups(self):
1260
        #self.update_listcgroups()
1261
        self.update_listcgroupgroups()
1262
        self.update_listavailablegroups_2()
1263
        self.update_listcgroupattributes()
1264
        self.update_listavailableattributes_3()
1265

    
1266
    def update_liststabattributes(self):
1267
        #self.update_listattributes()
1268
        self.update_listattributehosts()
1269
        self.update_listavailablehosts_3()
1270
        self.update_listattributegroup()
1271
        self.update_listavailablegroups_3()
1272
        self.update_listattributecgroup()
1273
        self.update_listavailablecgroups()
1274

    
1275
    def update_listallhosts(self):
1276
        self.update_listhosts()
1277
        self.update_listavailablehosts()
1278

    
1279
    def update_listallgroups(self):
1280
        self.update_listgroups()
1281
        self.update_listavailablegroups()
1282
        self.update_listavailablegroups_2()
1283
        self.update_listavailablegroups_3()
1284

    
1285
    def update_listallcgroups(self):
1286
        self.update_listavailablecgroups()
1287
        self.update_listcgroups()
1288

    
1289
    def update_listallattributes(self):
1290
        self.update_listattributes()
1291
        self.update_listavailableattributes()
1292
        self.update_listavailableattributes_2()
1293
        self.update_listavailableattributes_3()
1294

    
1295
    def main(self):
1296
        self.show()
1297

    
1298
if __name__ == '__main__':
1299
    app = QtGui.QApplication(sys.argv)
1300
    imageViewer = ImageViewer()
1301
    imageViewer.main()
1302
    app.exec_()
(2-2/2)