This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
1086 lines (822 loc) · 34.8 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
**models.py**
**Platform:**
Windows, Linux, Mac Os X.
**Description:**
This module defines the :class:`umbra.components.factory.scriptEditor.scriptEditor.ScriptEditor`
Component Interface class Models.
**Others:**
"""
#**********************************************************************************************************************
#*** External imports.
#**********************************************************************************************************************
import os
import re
from PyQt4.QtCore import QAbstractListModel
from PyQt4.QtCore import QModelIndex
from PyQt4.QtCore import QVariant
from PyQt4.QtCore import Qt
from PyQt4.QtCore import pyqtSignal
#**********************************************************************************************************************
#*** Internal imports.
#**********************************************************************************************************************
import foundations.exceptions
import foundations.verbose
import foundations.walkers
import umbra.ui.models
import umbra.ui.nodes
from umbra.components.factory.scriptEditor.nodes import ProjectNode
from umbra.components.factory.scriptEditor.nodes import DirectoryNode
from umbra.components.factory.scriptEditor.nodes import EditorNode
from umbra.components.factory.scriptEditor.nodes import FileNode
from umbra.components.factory.scriptEditor.nodes import PatternNode
from umbra.ui.languages import Language
#**********************************************************************************************************************
#*** Module attributes.
#**********************************************************************************************************************
__author__ = "Thomas Mansencal"
__copyright__ = "Copyright (C) 2008 - 2012 - Thomas Mansencal"
__license__ = "GPL V3.0 - http://www.gnu.org/licenses/"
__maintainer__ = "Thomas Mansencal"
__email__ = "thomas.mansencal@gmail.com"
__status__ = "Production"
__all__ = ["LOGGER", "ProjectsModel",
"LanguagesModel",
"PatternsModel",
"SearchResultsModel"]
LOGGER = foundations.verbose.installLogger()
#**********************************************************************************************************************
#*** Module classes and definitions.
#**********************************************************************************************************************
class ProjectsModel(umbra.ui.models.GraphModel):
"""
This class defines the Model used by :class:`umbra.components.factory.scriptEditor.scriptEditor.ScriptEditor`
Component Interface class.
"""
fileRegistered = pyqtSignal(FileNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a file is registered. ( pyqtSignal )
:return: Registered file FileNode. ( FileNode )
"""
fileUnregistered = pyqtSignal(FileNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a file is unregistered. ( pyqtSignal )
:return: Unregistered file FileNode. ( FileNode )
"""
editorRegistered = pyqtSignal(EditorNode)
"""
This signal is emited by the :class:`ProjectsModel` class when an editor is registered. ( pyqtSignal )
:return: Registered editor EditorNode. ( EditorNode )
"""
editorUnregistered = pyqtSignal(EditorNode)
"""
This signal is emited by the :class:`ProjectsModel` class when an editor is unregistered. ( pyqtSignal )
:return: Unregistered editor EditorNode. ( EditorNode )
"""
directoryRegistered = pyqtSignal(DirectoryNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a directory is registered. ( pyqtSignal )
:return: Registered directory DirectoryNode. ( DirectoryNode )
"""
directoryUnregistered = pyqtSignal(DirectoryNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a directory is unregistered. ( pyqtSignal )
:return: Unregistered directory DirectoryNode. ( DirectoryNode )
"""
projectRegistered = pyqtSignal(ProjectNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a project is registered. ( pyqtSignal )
:return: Registered project ProjectNode. ( ProjectNode )
"""
projectUnregistered = pyqtSignal(ProjectNode)
"""
This signal is emited by the :class:`ProjectsModel` class when a project is unregistered. ( pyqtSignal )
:return: Unregistered project ProjectNode. ( ProjectNode )
"""
def __init__(self,
parent=None,
rootNode=None,
horizontalHeaders=None,
verticalHeaders=None,
defaultNode=None,
defaultProject=None):
"""
This method initializes the class.
:param defaultProject: Default project name. ( String )
:param parent: Object parent. ( QObject )
:param rootNode: Root node. ( AbstractCompositeNode )
:param horizontalHeaders: Headers. ( OrderedDict )
:param verticalHeaders: Headers. ( OrderedDict )
:param defaultNode: Default node. ( GraphModelNode )
"""
LOGGER.debug("> Initializing '{0}()' class.".format(self.__class__.__name__))
umbra.ui.models.GraphModel.__init__(self, parent, rootNode, horizontalHeaders, verticalHeaders, defaultNode)
# --- Setting class attributes. ---
self.__defaultProject = None
self.defaultProject = defaultProject or "defaultProject"
self.__defaultProjectNode = None
ProjectsModel.__initializeModel(self)
#******************************************************************************************************************
#*** Attributes properties.
#******************************************************************************************************************
@property
def defaultProject(self):
"""
This method is the property for **self.__defaultProject** attribute.
:return: self.__defaultProject. ( String )
"""
return self.__defaultProject
@defaultProject.setter
@foundations.exceptions.handleExceptions(AssertionError)
def defaultProject(self, value):
"""
This method is the setter method for **self.__defaultProject** attribute.
:param value: Attribute value. ( String )
"""
if value is not None:
assert type(value) in (str, unicode), \
"'{0}' attribute: '{1}' type is not 'str' or 'unicode'!".format("defaultProject", value)
self.__defaultProject = value
@defaultProject.deleter
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def defaultProject(self):
"""
This method is the deleter method for **self.__defaultProject** attribute.
"""
raise foundations.exceptions.ProgrammingError(
"{0} | '{1}' attribute is not deletable!".format(self.__class__.__name__, "defaultProject"))
@property
def defaultProjectNode(self):
"""
This method is the property for **self.__defaultProjectNode** attribute.
:return: self.__defaultProjectNode. ( String )
"""
return self.__defaultProjectNode
@defaultProjectNode.setter
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def defaultProjectNode(self, value):
"""
This method is the setter method for **self.__defaultProjectNode** attribute.
:param value: Attribute value. ( String )
"""
raise foundations.exceptions.ProgrammingError(
"{0} | '{1}' attribute is read only!".format(self.__class__.__name__, "defaultProjectNode"))
@defaultProjectNode.deleter
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def defaultProjectNode(self):
"""
This method is the deleter method for **self.__defaultProjectNode** attribute.
"""
raise foundations.exceptions.ProgrammingError(
"{0} | '{1}' attribute is not deletable!".format(self.__class__.__name__, "defaultProjectNode"))
#******************************************************************************************************************
#*** Class methods.
#******************************************************************************************************************
def __initializeModel(self):
"""
This method initializes the Model.
"""
LOGGER.debug("> Initializing model.")
self.beginResetModel()
self.rootNode = umbra.ui.nodes.DefaultNode(name="InvisibleRootNode")
self.__defaultProjectNode = ProjectNode(name=self.__defaultProject,
parent=self.rootNode,
nodeFlags=int(Qt.ItemIsEnabled),
attributesFlags=int(Qt.ItemIsEnabled))
self.enableModelTriggers(True)
self.endResetModel()
def listEditorNodes(self, node=None):
"""
This method returns the Model :class:`umbra.components.factory.scriptEditor.nodes.EditorNode` class nodes.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:return: EditorNode nodes. ( List )
"""
return self.findFamily("EditorNode", node=node or self.__defaultProjectNode)
def listFileNodes(self, node=None):
"""
This method returns the Model :class:`umbra.components.factory.scriptEditor.nodes.FileNode` class nodes.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:return: FileNode nodes. ( List )
"""
return self.findFamily("FileNode", node=node or self.__defaultProjectNode)
def listDirectoryNodes(self):
"""
This method returns the Model :class:`umbra.components.factory.scriptEditor.nodes.DirectoryNode` class nodes.
:return: DirectoryNode nodes. ( List )
"""
return self.findFamily("DirectoryNode")
def listProjectNodes(self, ignoreDefaultProjectNode=True):
"""
This method returns the Model :class:`umbra.components.factory.scriptEditor.nodes.ProjectNode` class nodes.
:param ignoreDefaultProjectNode: Default ProjectNode will be ignored. ( Boolean )
:return: ProjectNode nodes. ( List )
"""
projectNodes = self.findFamily("ProjectNode")
return filter(lambda x: x != self.__defaultProjectNode, projectNodes) \
if ignoreDefaultProjectNode else projectNodes
def listEditors(self, node=None):
"""
This method returns the Model editors.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:return: Editors. ( List )
"""
return [editorNode.editor for editorNode in self.listEditorNodes(node) if editorNode.editor]
def listFiles(self, node=None):
"""
This method returns the Model files.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:return: FileNode nodes. ( List )
"""
return [fileNode.path for fileNode in self.listFileNodes(node) if fileNode.path]
def listDirectories(self):
"""
This method returns the Model directories.
:return: DirectoryNode nodes. ( List )
"""
return [directoryNode.path for directoryNode in self.listDirectoryNodes() if directoryNode.path]
def listProjects(self, ignoreDefaultProjectNode=True):
"""
This method returns the Model projects.
:param ignoreDefaultProjectNode: Default ProjectNode will be ignored. ( Boolean )
:return: ProjectNode nodes. ( List )
"""
return [projectNode.path for projectNode in self.listProjectNodes(ignoreDefaultProjectNode) if projectNode.path]
def getEditorNodes(self, editor, node=None):
"""
This method returns the :class:`umbra.components.factory.scriptEditor.nodes.EditorNode` class Nodes with given editor.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:param editor: Editor. ( Editor )
:return: EditorNode nodes. ( List )
"""
return [editorNode for editorNode in self.listEditorNodes(node) if editorNode.editor == editor]
def getFileNodes(self, path, node=None):
"""
This method returns the :class:`umbra.components.factory.scriptEditor.nodes.FileNode` class Nodes with given path.
:param node: Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
:param path: File path. ( String )
:return: FileNode nodes. ( List )
"""
return [fileNode for fileNode in self.listFileNodes(node) if fileNode.path == path]
def getDirectoryNodes(self, path):
"""
This method returns the :class:`umbra.components.factory.scriptEditor.nodes.DirectoryNode` class Nodes with given path.
:param path: Directory path. ( String )
:return: DirectoryNode nodes. ( List )
"""
return [directoryNode for directoryNode in self.listDirectoryNodes() if directoryNode.path == path]
def getProjectNodes(self, path):
"""
This method returns the :class:`umbra.components.factory.scriptEditor.nodes.ProjectNode` class Nodes with given path.
:param path: Project path. ( String )
:return: ProjectNode nodes. ( List )
"""
return [projectNode for projectNode in self.listProjectNodes() if projectNode.path == path]
def moveNode(self, parent, fromIndex, toIndex):
"""
This method moves given parent child to given index.
:param toIndex: Index to. ( Integer )
:param fromIndex: Index from. ( Integer )
:return: Method success. ( Boolean )
"""
# TODO: This method should be refactored once this ticket is fixed:
# https://bugreports.qt-project.org/browse/PYSIDE-78
if not fromIndex >= 0 or \
not fromIndex < parent.childrenCount() or \
not toIndex >= 0 or \
not toIndex < parent.childrenCount():
return False
parentIndex = self.getNodeIndex(parent)
self.beginRemoveRows(parentIndex, fromIndex, fromIndex)
child = parent.removeChild(fromIndex)
self.endRemoveRows()
startIndex = parent.childrenCount() - 1
endIndex = toIndex - 1
tail = []
for i in range(startIndex, endIndex, -1):
self.beginRemoveRows(parentIndex, i, i)
tail.append(parent.removeChild(i))
self.endRemoveRows()
tail = list(reversed(tail))
tail.insert(0, child)
for node in tail:
row = parent.childrenCount()
self.beginInsertRows(parentIndex, row, row)
parent.addChild(node)
self.endInsertRows()
return True
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def registerFile(self, file, parent, ensureUniqueness=False):
"""
This method registers given file in the Model.
:param file: File to register. ( String )
:param parent: FileNode parent. ( GraphModelNode )
:param ensureUniqueness: Ensure registrar uniqueness. ( Boolean )
:return: FileNode. ( FileNode )
"""
if ensureUniqueness:
if self.getFileNodes(file):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' file is already registered!".format(
self.__class__.__name__, file))
LOGGER.debug("> Registering '{0}' file.".format(file))
row = parent.childrenCount()
self.beginInsertRows(self.getNodeIndex(parent), row, row)
fileNode = FileNode(name=os.path.basename(file),
path=file,
parent=parent)
self.endInsertRows()
self.fileRegistered.emit(fileNode)
return fileNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def unregisterFile(self, fileNode, raiseException=False):
"""
This method unregisters given :class:`umbra.components.factory.scriptEditor.nodes.FileNode` class Node from the Model.
:param fileNode: FileNode to unregister. ( FileNode )
:param raiseException: Raise the exception. ( Boolean )
:return: FileNode. ( FileNode )
"""
if raiseException:
if not fileNode in self.listFileNodes():
raise foundations.exceptions.ProgrammingError("{0} | '{1}' file 'FileNode' isn't registered!".format(
self.__class__.__name__, fileNode))
LOGGER.debug("> Unregistering '{0}' file 'FileNode'.".format(fileNode))
parent = fileNode.parent
row = fileNode.row()
self.beginRemoveRows(self.getNodeIndex(parent), row, row)
parent.removeChild(row)
self.endRemoveRows()
self.fileUnregistered.emit(fileNode)
return fileNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def registerDirectory(self, directory, parent, ensureUniqueness=False):
"""
This method registers given directory in the Model.
:param directory: Directory to register. ( String )
:param parent: DirectoryNode parent. ( GraphModelNode )
:param ensureUniqueness: Ensure registrar uniqueness. ( Boolean )
:return: DirectoryNode. ( DirectoryNode )
"""
if ensureUniqueness:
if self.getDirectoryNodes(directory):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' directory is already registered!".format(
self.__class__.__name__, directory))
LOGGER.debug("> Registering '{0}' directory.".format(directory))
row = parent.childrenCount()
self.beginInsertRows(self.getNodeIndex(parent), row, row)
directoryNode = DirectoryNode(name=os.path.basename(directory),
path=directory,
parent=parent)
self.endInsertRows()
self.directoryRegistered.emit(directoryNode)
return directoryNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def unregisterDirectory(self, directoryNode, raiseException=False):
"""
This method unregisters given :class:`umbra.components.factory.scriptEditor.nodes.DirectoryNode` class Node from the Model.
:param directoryNode: DirectoryNode to unregister. ( DirectoryNode )
:param raiseException: Raise the exception. ( Boolean )
:return: DirectoryNode. ( DirectoryNode )
"""
if raiseException:
if not directoryNode in self.listDirectoryNodes():
raise foundations.exceptions.ProgrammingError("{0} | '{1}' directory 'DirectoryNode' isn't registered!".format(
self.__class__.__name__, directoryNode))
LOGGER.debug("> Unregistering '{0}' directory 'DirectoryNode'.".format(directoryNode))
parent = directoryNode.parent
row = directoryNode.row()
self.beginRemoveRows(self.getNodeIndex(parent), row, row)
parent.removeChild(row)
self.endRemoveRows()
self.directoryUnregistered.emit(directoryNode)
return directoryNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def registerEditor(self, editor, parent, ensureUniqueness=False):
"""
This method registers given :class:`umbra.components.factory.scriptEditor.editor.Editor` class editor in the Model.
:param editor: Editor to register. ( Editor )
:param parent: EditorNode parent. ( GraphModelNode )
:param ensureUniqueness: Ensure registrar uniqueness. ( Boolean )
:return: EditorNode. ( EditorNode )
"""
if ensureUniqueness:
if self.getEditorNodes(editor):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' editor is already registered!".format(
self.__class__.__name__, editor))
LOGGER.debug("> Registering '{0}' editor.".format(editor))
row = parent.childrenCount()
self.beginInsertRows(self.getNodeIndex(parent), row, row)
editorNode = EditorNode(editor=editor,
parent=parent)
self.endInsertRows()
self.editorRegistered.emit(editorNode)
return editorNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def unregisterEditor(self, editorNode, raiseException=False):
"""
This method unregisters given :class:`umbra.components.factory.scriptEditor.nodes.EditorNode` class Node from the Model.
:param editorNode: EditorNode to unregister. ( EditorNode )
:param raiseException: Raise the exception. ( Boolean )
:return: EditorNode. ( EditorNode )
"""
if raiseException:
if not editorNode in self.listEditorNodes():
raise foundations.exceptions.ProgrammingError("{0} | '{1}' editor 'EditorNode' isn't registered!".format(
self.__class__.__name__, editorNode))
LOGGER.debug("> Unregistering '{0}' editor 'EditorNode'.".format(editorNode))
parent = editorNode.parent
row = editorNode.row()
self.beginRemoveRows(self.getNodeIndex(parent), row, row)
parent.removeChild(row)
self.endRemoveRows()
self.editorUnregistered.emit(editorNode)
return editorNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def registerProject(self, path, ensureUniqueness=False):
"""
This method registers given path in the Model as a project.
:param path: Project path to register. ( String )
:param ensureUniqueness: Ensure registrar uniqueness. ( Boolean )
:return: ProjectNode. ( ProjectNode )
"""
if ensureUniqueness:
if self.getProjectNodes(path):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' project is already registered!".format(
self.__class__.__name__, path))
LOGGER.debug("> Registering '{0}' project.".format(path))
row = self.rootNode.childrenCount()
self.beginInsertRows(self.getNodeIndex(self.rootNode,), row, row)
projectNode = ProjectNode(name=os.path.basename(path),
path=path,
parent=self.rootNode)
self.endInsertRows()
self.projectRegistered.emit(projectNode)
return projectNode
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def unregisterProject(self, projectNode, raiseException=False):
"""
This method unregisters given :class:`umbra.components.factory.scriptProject.nodes.ProjectNode` class Node from the Model.
:param projectNode: ProjectNode to unregister. ( ProjectNode )
:param raiseException: Raise the exception. ( Boolean )
:return: ProjectNode. ( ProjectNode )
"""
if raiseException:
if not projectNode in self.listProjectNodes():
raise foundations.exceptions.ProgrammingError("{0} | '{1}' project 'ProjectNode' isn't registered!".format(
self.__class__.__name__, projectNode))
LOGGER.debug("> Unregistering '{0}' project 'ProjectNode'.".format(projectNode))
parent = projectNode.parent
row = projectNode.row()
self.beginRemoveRows(self.getNodeIndex(parent), row, row)
parent.removeChild(row)
self.endRemoveRows()
self.projectUnregistered.emit(projectNode)
return projectNode
def isAuthoringNode(self, node):
"""
This method returns if given Node is an authoring node.
:param node: Node. ( ProjectNode / DirectoryNode / FileNode )
:return: Is authoring node. ( Boolean )
"""
for parentNode in foundations.walkers.nodesWalker(node, ascendants=True):
if parentNode is self.__defaultProjectNode:
return True
return False
def setAuthoringNodes(self, editor):
"""
This method sets the Model authoring Nodes using given editor.
:param editor: Editor to set. ( Editor )
:return: Method success. ( Boolean )
"""
projectNode = self.defaultProjectNode
fileNode = self.registerFile(editor.file, projectNode)
editorNode = self.registerEditor(editor, fileNode)
return True
def deleteAuthoringNodes(self, editor):
"""
This method deletes the Model authoring Nodes associated with given editor.
:param editor: Editor. ( Editor )
:return: Method success. ( Boolean )
"""
editorNode = foundations.common.getFirstItem(self.getEditorNodes(editor))
fileNode = editorNode.parent
self.unregisterEditor(editorNode)
self.unregisterFile(fileNode, raiseException=False)
return True
def updateAuthoringNodes(self, editor):
"""
This method updates given editor Model authoring nodes.
:param editor: Editor. ( Editor )
:return: Method success. ( Boolean )
"""
editorNode = foundations.common.getFirstItem(self.getEditorNodes(editor))
fileNode = editorNode.parent
file = editor.file
fileNode.name = editorNode.name = os.path.basename(file)
fileNode.path = editorNode.path = file
self.nodeChanged(fileNode)
return True
def setProjectNodes(self, rootNode, maximumDepth=1):
"""
This method sets the project Model children Nodes using given root node.
:param rootNode: Root node. ( ProjectNode / DirectoryNode )
:param maximumDepth: Maximum nodes nesting depth. ( Integer )
"""
rootDirectory = rootNode.path
for parentDirectory, directories, files in foundations.walkers.depthWalker(rootDirectory, maximumDepth):
if parentDirectory == rootDirectory:
parentNode = rootNode
else:
parentNode = foundations.common.getFirstItem(
[node for node in foundations.walkers.nodesWalker(rootNode) \
if node.family == "DirectoryNode" and node.path == parentDirectory])
if not parentNode:
continue
paths = [node.path for node in parentNode.children]
for directory in sorted(directories):
if directory.startswith("."):
continue
path = os.path.join(parentDirectory, directory)
if path in paths:
continue
directoryNode = self.registerDirectory(path, parentNode)
for file in sorted(files):
if file.startswith("."):
continue
path = os.path.join(parentDirectory, file)
if path in paths:
continue
if foundations.common.isBinaryFile(path):
continue
fileNode = self.registerFile(path, parentNode)
def deleteProjectNodes(self, node):
"""
This method deletes the Model project Nodes associated with given node.
:param node: Node. ( ProjectNode )
"""
self.unregisterProjectNodes(node)
self.unregisterProject(node)
def unregisterProjectNodes(self, node):
"""
This method unregisters given Node children.
:param node: Node. ( ProjectNode / DirectoryNode )
"""
for node in reversed(list(foundations.walkers.nodesWalker(node))):
if node.family == "DirectoryNode":
self.unregisterDirectory(node)
elif node.family == "FileNode":
self.unregisterFile(node)
def updateProjectNodes(self, node):
"""
This method updates given root Node children.
:param node: Node. ( ProjectNode / DirectoryNode )
"""
self.unregisterProjectNodes(node)
self.setProjectNodes(node)
class LanguagesModel(QAbstractListModel):
"""
This class is a `QAbstractListModel <http://doc.qt.nokia.com/qabstractListmodel.html>`_ subclass used
to store the :class:`umbra.components.factory.scriptEditor.scriptEditor.ScriptEditor`
Component Interface class languages.
"""
def __init__(self, parent=None, languages=None):
"""
This method initializes the class.
:param parent: Parent object. ( QObject )
:param languages: Languages. ( List )
"""
LOGGER.debug("> Initializing '{0}()' class.".format(self.__class__.__name__))
QAbstractListModel.__init__(self, parent)
# --- Setting class attributes. ---
self.__languages = []
self.languages = languages
#******************************************************************************************************************
#*** Attributes properties.
#******************************************************************************************************************
@property
def languages(self):
"""
This method is the property for **self.__languages** attribute.
:return: self.__languages. ( List )
"""
return self.__languages
@languages.setter
@foundations.exceptions.handleExceptions(AssertionError)
def languages(self, value):
"""
This method is the setter method for **self.__languages** attribute.
:param value: Attribute value. ( List )
"""
if value is not None:
assert type(value) is list, "'{0}' attribute: '{1}' type is not 'list'!".format("languages", value)
for element in value:
assert type(element) is Language, "'{0}' attribute: '{1}' type is not 'Language'!".format("languages", element)
self.beginResetModel()
self.__languages = value
self.endResetModel()
@languages.deleter
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def languages(self):
"""
This method is the deleter method for **self.__languages** attribute.
"""
raise foundations.exceptions.ProgrammingError(
"{0} | '{1}' attribute is not deletable!".format(self.__class__.__name__, "languages"))
#******************************************************************************************************************
#*** Class methods.
#******************************************************************************************************************
def rowCount(self, parent=QModelIndex()):
"""
This method reimplements the :meth:`QAbstractListModel.rowCount` method.
:param parent: Parent. ( QModelIndex )
:return: Row count. ( Integer )
"""
return len(self.__languages)
def data(self, index, role=Qt.DisplayRole):
"""
This method reimplements the :meth:`QAbstractListModel.data` method.
:param index: Index. ( QModelIndex )
:param role: Role. ( Integer )
:return: Data. ( QVariant )
"""
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
return QVariant(self.__languages[index.row()].name)
return QVariant()
def sortLanguages(self, order=Qt.AscendingOrder):
"""
This method sorts the Model languages.
:param order: Order. ( Qt.SortOrder )
"""
self.beginResetModel()
self.__languages = sorted(self.__languages, key=lambda x: (x.name), reverse=order)
self.endResetModel()
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def registerLanguage(self, language):
"""
This method registers given language in the :obj:`LanguagesModel.languages` class property.
:param language: Language to register. ( Language )
:return: Method success. ( Boolean )
"""
if self.getLanguage(language):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' language is already registered!".format(
self.__class__.__name__, language.name))
LOGGER.debug("> Registering '{0}' language.".format(language.name))
self.__languages.append(language)
self.sortLanguages()
return True
@foundations.exceptions.handleExceptions(foundations.exceptions.ProgrammingError)
def unregisterLanguage(self, name):
"""
This method unregisters language with given name from the :obj:`LanguagesModel.languages` class property.
:param name: Language to unregister. ( String )
:return: Method success. ( Boolean )
"""
if not self.getLanguage(name):
raise foundations.exceptions.ProgrammingError("{0} | '{1}' language isn't registered!".format(
self.__class__.__name__, name))
LOGGER.debug("> Unregistering '{0}' language.".format(name))
for i, language in enumerate(self.__languages):
if not language.name == name:
continue
del(self.__languages[i])
self.sortLanguages()
return True
def getLanguage(self, name):
"""
This method returns the language with given name.
:param name: Language name. ( String )
:return: File language. ( Language )
"""
for language in self.__languages:
if language.name == name:
LOGGER.debug("> Language '{0}': '{1}'.".format(name, language))
return language
def getFileLanguage(self, file):
"""
This method returns the language of given file.
:param file: File to get language of. ( String )
:return: File language. ( Language )
"""
for language in self.__languages:
if re.search(language.extensions, file):
LOGGER.debug("> '{0}' file detected language: '{1}'.".format(file, language.name))
return language
class PatternsModel(umbra.ui.models.GraphModel):
"""
This class defines the Model used the by
:class:`umbra.patterns.factory.scriptEditor.searchAndReplace.SearchAndReplace` class to store the search and \
replace patterns.
"""
# Custom signals definitions.
patternInserted = pyqtSignal(PatternNode)
"""
This signal is emited by the :class:`PatternsModel` class when a pattern has been inserted. ( pyqtSignal )
:return: Inserted pattern node. ( PatternNode )
"""
# Custom signals definitions.
patternRemoved = pyqtSignal(PatternNode)
"""
This signal is emited by the :class:`PatternsModel` class when a pattern has been removed. ( pyqtSignal )
:return: Removed pattern node. ( PatternNode )
"""
def __init__(self, parent=None, rootNode=None, horizontalHeaders=None, verticalHeaders=None):
"""
This method initializes the class.
:param parent: Object parent. ( QObject )
:param rootNode: Root node. ( AbstractCompositeNode )
:param horizontalHeaders: Headers. ( OrderedDict )
:param verticalHeaders: Headers. ( OrderedDict )
"""
LOGGER.debug("> Initializing '{0}()' class.".format(self.__class__.__name__))
umbra.ui.models.GraphModel.__init__(self, parent, rootNode, horizontalHeaders, verticalHeaders, PatternNode)
#******************************************************************************************************************
#*** Class methods.
#******************************************************************************************************************
def initializeModel(self, rootNode):
"""
This method initializes the Model using given root node.
:param rootNode: Graph root node. ( DefaultNode )
:return: Method success ( Boolean )
"""
LOGGER.debug("> Initializing model with '{0}' root node.".format(rootNode))
self.beginResetModel()
self.rootNode = rootNode
self.enableModelTriggers(True)
self.endResetModel()
return True
def insertPattern(self, pattern, index):
"""
This method inserts given pattern into the Model.
:param pattern: Pattern. ( String )
:param index: Insertion index. ( Integer )
:return: Method success. ( Boolean )
"""
LOGGER.debug("> Inserting '{0}' at '{1}' index.".format(pattern, index))