Source

psilib / som.py

Full commit
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
#! /usr/bin/env python

## Copyright (c) 1999 - 2003 L. C. Rees.  All rights reserved.
## See COPYRIGHT file for license terms.

__title__ = 'som'
__version__ = '0.03-test'
__author__  = 'L. C. Rees (xanimal@users.sf.net)'

'''SOM (Site Object Model) is a DOM wrapper for building PSI maps. SOM
completely implements the current PSI data model (0.41). Each PSI element has a
corresponding class that ensures correct PSI output.'''

## Element and Document classes from DOM
from psiutils import attdel, attset, attget
from xml.dom.minidom import Element, Document


class Root(Document):

    '''Adds creates a document to add PSI markup too'''

    def __init__(self):
        '''Initializes base Document DOM class'''
        Document.__init__(self)

    def __repr__(self):
        '''Returns representation of instance as XML'''
        return self.toprettyxml('  ')
        

class Base(Element):

    '''Base class providing utility functions.'''    

    def __init__(self, nodeName):
        '''Initializes base Element DOM class
        
        Arguments:
        nodeName -- the name of the PSI element'''        
        Element.__init__(self, nodeName, None)        
    
    def __repr__(self):
        '''Returns representation of instance as XML'''
        return self.toprettyxml('  ')

    def _add(self, Class, *vargs):
        '''Ensures child elements are added in the proper order

        Arguments:
        Class -- class for individual PSI node
        vargs -- attributes (amount can vary)'''
        node = Class(*vargs)
        ## Insert nodes in front of parameter, result & exception elements
        if self.hasChildNodes():            
            try:                
                if self.parameters:
                   self.insertBefore(node, self.parameters[0])
                elif self.results:
                    self.insertBefore(node, self.results[0])
                elif self.exceptions:
                    self.insertBefore(node, self.exceptions[0])
                else: self.appendChild(node)
            except AttributeError: self.appendChild(node)
        ## Otherwise append child to end
        else: self.appendChild(node)
        return node

    def _attchecker(self, name, value, msg, av):
        '''Verifies attribute has acceptable values

        name -- name of attribute
        value -- value of attribute
        msg -- message raised if value is not acceptable
        av -- list of acceptable values'''
        ## Set the class attribute
        setattr(self, name, value)
        ## Raise error if unacceptable value entered
        if value not in av: raise AttributeError, msg
        ## Otherwise, set the DOM attribute
        else: attset(self, name, value)         

    def removeChildren(self, childList=None):
        '''Removes unwanted child nodes

        Arguments:
        childList -- list of specific nodes (default: None)'''
        ## If a element has a list of children, delete the list
        if childList:
            for child in childList: self._remove(child)
        ## Otherwise, delete any and all child nodes
        else:
            for child in self.childNodes:
                self.childNodes.remove(child)
                self.removeChild(child)


class Ent:

    '''Container for miscellaneous entity and utility classes'''
       
    class Name:

        '''Stores the setName function'''

        def setName(self, name):
            '''Sets the name attribute of a PSI node

            Arguments:
            name -- name of PSI node'''
            self.name = name
            attset(self, u'name', name)

        def _removeByName(self, childList, name):
            '''Removes unwanted child nodes by name

            Arguments:
            childlist -- list of specific nodes
            name -- specific child node'''
            for i in childList:
                if i.getAttributeNS(None, u'name') == name:
                    childList.remove(i)
                    self.removeChild(i)

        def _getByName(self, childList, name):
            '''Retrieves child nodes by name

            Arguments:
            childlist -- list of specific nodes
            child -- specific child node'''
            for i in childList:
                if attget(i, u'name') == name: return i
        
    class Ref:

        '''Stores the setRef function'''        

        def setRef(self, ref):
            '''Sets the ref attribute of a PSI node

            Arguments:
            ref -- reference to another PSI node'''
            self.ref = ref
            attset(self, u'ref', ref)
            
    class Class:

        '''Stores the setClass function'''            

        def setClass(self, Class):
            '''Sets the class attribute of a PSI node

            Arguments:
            Class -- A node charateristics are inherited from'''
            self.parentClass = Class
            attset(self, u'class', Class)

    class NCR(Name, Ref, Class):

        '''Combines the setName, setRef, and setClass functions'''    
                
        pass 

    class SE:

        '''Stores the setStart and setEnd functions'''        

        def setStart(self, start):
            '''Sets the start attribute of a PSI node

            Arguments:
            start -- a PSI node's start time'''
            self.start = start
            attset(self, u'start', start)

        def setEnd(self, end):
            '''Sets the end attribute of a PSI node
            
            Arguments:
            end -- a PSI node's end time'''
            self.end = end
            attset(self, u'end', end)
            
    class Match:

        '''Stores the setMatch function'''        

        def setMatch(self, match):
            '''Sets the match attribute of a PSI node
            
            Arguments:
            match -- a value a PSI node must match'''
            self.match = match
            attset(self, u'match', match)
            
    class State:

        '''Stores the setState function'''

        def setState(self, state='planned'):
            '''Sets the state attribute of a PSI node
            
            Arguments:
            state -- a PSI node's state (default: "planned")'''
            av = ('planned', 'readlock', 'writelock', 'nolock')
            msg = '''value must be string "planned", "readlock", 
                "writelock", or "nolock"'''
            self._attchecker(u'state', state, msg, av)          

    class AT:

        '''Stores the setType function for access-related PSI nodes'''        

        def setType(self, type='read'):
            '''Sets the type attribute of a PSI node
            
            Arguments:
            type -- a PSI node's type (default: "read")'''
            av = ('read', 'write', 'create', 'delete', 'acl')
            msg = '''value must be string "read", "write", "create" 
                    "delete", or "acl"'''
            ## Raise error if unacceptable value entered
            self._attchecker(u'type', type, msg, av)

    class IfBase:

        '''Base class for elements with the child element "psi:if"'''

        ifs = []        
        
        def addIf(self, value, match):
            '''Creates and adds a psi:if element to a parent PSI node
            
            Arguments:
            value -- the condition evaluated
            match -- the value that must be true'''
            node = self._add(If, value, match)
            ## Add to list of all ifs under a parent PSI node
            self.ifs.append(node)

        def removeIfs(self):
            '''Removes a conditional statement
            Arguments:
            If - if to be removed'''            
            self.removeChildren(self.ifs)

    class TextBase:

        '''Base class for elements with the child element "psi:text"'''         

        ## import Text node class
        from xml.dom.minidom import Text as _txt    
    
        def setData(self, txt):
            '''Creates a text node

            Arguments:
            txt -- text data to inserted in new text node'''
            self.text = self._txt()
            self.text.data = txt
            self.appendChild(self.text)

        def appendData(self, txt):
            '''Append text to existing text data in text node

            Arguments:
            txt -- text data to be appended'''            
            self.text.appendData(txt)   

        def removeData(self):
            '''Removes text data'''
            self.removeChild(self.text)       

    class TimeBase:

        '''Base class for elements with the child element "psi:time"'''         
    
        def setTime(self, t=None, le=None, s=None, e=None, lo=None):
            '''Creates and adds a psi:time element to a parent PSI node
            
            Arguments:
            t -- the time measurement (default: None)
            le -- the length of the type of time (default: None)
            s -- the start time (default: None)
            e -- the end time (default: None)
            lo -- how many times a PSI node should loop (default: None)'''
            self.time = Time(t, le, s, e, lo)
            msg = 'element may only have one child element "time"'
            ## Ensure parent can only have one time element as child
            if self.hasChildNodes():
                for e in self.childNodes:
                    if e.tagName == u'psi:time': raise AttributeError, msg
                self.insertBefore(self.time, self.firstChild)
            else: self.appendChild(self.time)

        def removeTime(self):
            '''Removes time setting'''
            self.removeChild(self.time)
            del self.time
            
    
    class StructureBase(Base, NCR):

        '''Base class for PSI strucure nodes'''         

        ## Create placeholders for child lists        
        metadata, filters, fields, includes, links = None, [], [], [], []

        def __init__(self, name, Class=None, ref=None):
            '''Initializes Structure class
            
            Arguments:
            name -- the name of the PSI structure node
            Class -- the class of the PSI structure node (default: None)
            ref -- a reference to another PSI node (default: None)'''
            Base.__init__(self, self.nodeName)
            self.setName(name)
            if Class: self.setClass(Class)
            if ref: self.setRef(ref)
    
        def setMetadata(self, state=None):
            '''Sets metadata for a PSI node
            
            Arguments:
            state -- a PSI node's state (default: None)'''
            self.metadata = Metadata(state)
            msg = 'element may only have one child element "metadata"'
            ## Ensure a structure node can only have one metadata child
            if self.hasChildNodes():
                for e in self.childNodes:
                    if e.tagName == u'psi:metadata': raise AttributeError, msg
                self.insertBefore(self.metadata, self.firstChild)
            else: self.appendChild(self.metadata)

        def removeMetadata(self):
            '''Removes metadata'''
            self.removeChild(self.metadata)
            del self.metadata

        def addFilter(self, name, Class=None, ref=None):
            '''Creates and adds a filter to a PSI structure node
            
            Arguments:
            name -- the name of the filter
            Class -- the class of the filter (default: None)
            ref -- the next filter in a chain (default: None)'''
            node = Filter(name, Class, ref)
            if self.hasChildNodes():
                ## Ensure filters always follow metadata or come first
                if self.firstChild.tagName == u'psi:metadata':
                    try: self.insertBefore(node, self.firstChild.nextSibling)
                    except AttributeError: self.appendChild(node)
                else: self.insertBefore(node, self.firstChild)
            else: self.appendChild(node)
            ## Add to filters list
            self.filters.append(node)

        def getFilterByName(self, name):
            '''Retrieves a filter

            Arguments:
            name -- name of filter'''
            return self._getByName(self.filters, name)            

        def removeFilterByName(self, name):
            '''Removes a filter

            Arguments:
            name -- name of filter to be removed'''
            self._removeByName(self.filters, name)

        def removeFilters(self):
            '''Removes all child filters'''
            self.removeChildren(self.filters)
            
        def addField(self, name):
            '''Creates and adds a field to a PSI structure node
            
            Arguments:
            name -- the name of the field'''
            node = self._add(Field, name)
            ## Add to fields list
            self.fields.append(node)

        def getFieldByName(self, name):
            '''Retrieves a field

            Arguments:
            name -- name of field'''
            return self._getByName(self.fields, name)            

        def removeFieldByName(self, name):
            '''Removes a field

            Arguments:
            name -- name of field to be removed'''
            self._removeByName(self.fields, name)

        def removeFields(self):
            '''Removes all child fields'''
            self.removeChildren(self.fields)           
    
        def addInclude(self, Class):
            '''Creates and adds an include to a PSI structure node
            
            Arguments:
            Class -- the class of the include'''
            node = self._add(Include, Class)
            ## Add to includes list
            self.includes.append(node)

        def removeIncludes(self):
            '''Removes all child includes'''
            self.removeChildren(self.includes)            
    
        def addLink(self, ref):
            '''Creates and adds a link to a PSI structure node
            
            Arguments:
            ref -- references another PSI node'''
            node = self._add(Link, ref)
            ## Add to links list
            self.links.append(node)

        def removeLinks(self):
            '''Removes all child fields'''
            self.removeChildren(self.links)

    class CollectionBase:

        nodeName = u'psi:collection'
        
        ## Placeholders for child elements
        collections = []

        def addCollection(self, name, Class=None, ref=None):
            '''Creates and adds a collection to a PSI structure node
                
            Arguments:
            name -- the name of the collection
            Class -- the class of the collection (default: None)
            ref -- reference to another node (default: None)'''
            node = Collection(name, Class, ref)
            if self.hasChildNodes():
                if self.collections:
                    self.insertBefore(node, self.collections[-1].nextSibling)
                elif self.filters:
                    self.insertBefore(node, self.filters[-1].nextSibling)
                elif self.metadata:
                    self.insertBefore(node, self.metadata.nextSibling)
                else: self.insertBefore(node, self.firstChild)
            else: self.appendChild(node)
            ## Add to collections list
            self.collections.append(node)
    
        def getCollectionByName(self, name):
            '''Retrieves a collection
    
            Arguments:
            name -- name of collection'''
            return self._getByName(self.collections, name)        
    
        def removeCollectionByName(self, name):
            '''Removes a collection
    
            Arguments:
            name -- name of collection to be removed'''
            self._removeByName(self.collections, name)
    
        def removeCollections(self):
            '''Removes all collections'''
            self.removeChildren(self.collections)         
            
    class ResourceBase(StructureBase):

        '''Base class for PSI resource nodes'''

        textnodes = []        
    
        def addText(self, n, c=None, r=None, ws=u'preserve', text=None):
            '''Creates and adds text to a PSI structure node
            
            Arguments:
            n -- the name of the text
            c -- the class of the text (default: None)
            r -- reference to another node (default: None)
            ws -- sets whitespace handling
            text -- text stored inside the PSI node'''
            node = self._add(Text, n, c, r, ws, text)
            ## Add to links list
            self.textnodes.append(node)

        def getTextByName(self, name):
            '''Retrieves a text

            Arguments:
            name -- name of text'''
            return self._getByName(self.textnodes, name)         

        def removeTextByName(self, name):
            '''Removes a field

            Arguments:
            name -- name of text node to be removed'''
            self._removeByName(self.textnodes, name)

        def removeTextNodes(self):
            '''Removes all text nodes'''
            self.removeChildren(self.textnodes)            


class Psi(Base, Ent.CollectionBase):

    '''Creates the root of a PSI document'''

    nodeName = u'psi:psi'
    ## PSI namespace
    namespace = u'urn:psilib:PSI:0:4:core'    

    def __init__(self):
        Base.__init__(self, self.nodeName)
        self.setAttribute(u'xmlns:psi', self.namespace)       
        
    
class Collection(Ent.CollectionBase, Ent.StructureBase):

    '''Class for building PSI collection nodes'''
    
    resources = []           

    def addResource(self, name, Class=None, ref=None):
        '''Creates and adds a resource to a PSI structure node
            
        Arguments:
        name -- the name of the resource
        Class -- the class of the resource (default: None)
        ref -- reference to another node (default: None)'''
        node = self._add(Resource, name, Class, ref)
        ## Add to resources list
        self.resources.append(node)

    def getResourceByName(self, name):
        '''Retrieves a resource

        Arguments:
        name -- name of resource'''
        return self._getByName(self.resources, name)        

    def removeResourceByName(self, name):
        '''Removes a resource

        Arguments:
        name -- name of resource to be removed'''
        self._removeByName(self.resources, name)

    def removeResources(self):
        '''Removes all resources'''
        self.removeChildren(self.resources)        


class Text(Ent.TextBase, Ent.StructureBase):

    '''Class for building PSI text nodes'''    

    nodeName = u'psi:text'

    def __init__(self, name, Class=None, ref=None, ws=u'default', txt=None):
        '''Initializes a PSI text node
            
        Arguments:
        name -- the name of the text
        Class -- the class of the text (default: None)
        ref -- reference to another node (default: None)
        ws -- sets whitespace handling
        txt -- text stored inside the PSI node'''
        Ent.StructureBase.__init__(self, name, Class, ref)
        if txt: self.setData(txt)        
        if ws == u'preserve': self.setWhitespace(u'preserve')
        else: self.setWhitespace(ws)

    def setWhitespace(self, ws):
        '''Turns whitespace preservation on or off for a PSI text node
            
        Arguments:
        ws -- the whitespace attribute'''
        av = (u'default', u'preserve')
        msg = 'value must be string "default" or "preserve"'
        ## Ensure proper value is used
        self._attchecker(u'xml:space', ws, msg, av)
          
        
class Resource(Ent.ResourceBase):

    '''Class for building PSI resource nodes'''     

    nodeName = u'psi:resource'
    ## Create placeholders for lists
    fragments, parameters, results, exceptions = [], [], [], []

    def addFragment(self, name, Class=None, ref=None):
        '''Creates and adds a fragment to a PSI structure node
            
        Arguments:
        name -- the name of the fragment
        Class -- the class of the fragment (default: None)
        ref -- reference to another node (default: None)'''
        node = self._add(Fragment, name, Class)
        ## Add to fragments list
        self.fragments.append(node)

    def getFragmentByName(self, name):
        '''Retrieves a fragment

        Arguments:
        name -- name of fragment'''
        return self._getByName(self.fragments, name)         

    def removeFragmentByName(self, name):
        '''Removes a fragment

        Arguments:
        name -- name of fragment to be removed'''
        self._removeByName(self.fragments, name)

    def removeFragments(self):
        '''Removes all fragments'''
        self.removeChildren(self.fragments)         

    def addParameter(self, name, Class=None, ref=None):
        '''Creates and adds a parameter to a PSI structure node
            
        Arguments:
        name -- the name of the parameter
        Class -- the class of the parameter (default: None)
        ref -- the next filter in a parameter (default: None)'''
        node = Parameter(name, Class=None, ref=None)
        ## Make sure parameter is last but before result or exception elements
        if self.hasChildNodes():
            if self.parameters:
                self.insertBefore(node, self.parameters[-1].nextSibling)
            elif self.results:
                self.insertBefore(node, self.results[0])
            elif self.exceptions:
                self.insertBefore(node, self.exceptions[0])
            else: self.appendChild(node)
        else: self.appendChild(node)
        ## Add to parameters list
        self.parameters.append(node)

    def getParameterByName(self, name):
        '''Retrieves a parameter

        Arguments:
        name -- name of parameter'''
        return self._getByName(self.parameters, name)         

    def removeParameterByName(self, name):
        '''Removes a parameter

        Arguments:
        name -- name of parameter to be removed'''
        self._removeByName(self.parameters, name)

    def removeParameters(self):
        '''Removes all parameters'''
        self.removeChildren(self.parameters)        

    def addResult(self, name, Class=None, ref=None):
        '''Creates and adds a result to a PSI structure node
            
        Arguments:
        name -- the name of the result
        Class -- the class of the result (default: None)
        ref -- reference to another node (default: None)'''
        node = Result(name, Class=None, ref=None)
        ## Ensure result is inserted between any parameters or exceptions
        if self.hasChildNodes():
            if self.results:
                self.insertBefore(node, self.results[0])
            elif self.parameters:
                self.insertBefore(node, self.parameters[-1].nextSibling)
            elif self.exceptions:
                self.insertBefore(node, self.exceptions[0])
            else: self.appendChild(node)
        else: self.appendChild(node)
        ## Add to results list
        self.results.append(node)

    def getResultByName(self, name):
        '''Retrieves a result

        Arguments:
        name -- name of result'''
        return self._getByName(self.results, name)        

    def removeResultByName(self, name):
        '''Removes a result

        Arguments:
        name -- name of result to be removed'''
        self._removeByName(self.results, name)

    def removeResults(self):
        '''Removes all results'''
        self.removeChildren(self.results)        

    def addException(self, name, Class=None, ref=None):
        '''Creates and adds an exception to a PSI structure node
            
        Arguments:
        name -- the name of the exception
        Class -- the class of the exception (default: None)
        ref -- reference to another node (default: None)'''
        node = Exception(name, Class=None, ref=None)
        ## Exceptions are always last
        self.appendChild(node)
        ## Add to exceptions list
        self.exceptions.append(node)

    def getExceptionByName(self, name):
        '''Retrieves a exception

        Arguments:
        name -- name of exception'''
        return self._getByName(self.exceptions, name)        

    def removeExceptionByName(self, name):
        '''Removes a exception

        Arguments:
        name -- name of exception to be removed'''
        self._removeByName(self.exceptions, name)

    def removeExceptions(self):
        '''Removes all exceptions'''
        self.removeChildren(self.exceptions)        
        

class Fragment(Ent.ResourceBase):

    '''Class for building PSI fragment nodes'''    

    nodeName = u'psi:fragment'    


class Parameter(Ent.ResourceBase):

    '''Class for building PSI parameter nodes'''    

    nodeName = u'psi:parameter'

    def __init__(self, name, Class=None, ref=None, rq=u'optional'):
        '''Initializes a PSI parameter node
            
        Arguments:
        name -- the name of the parameter
        Class -- the class of the parameter (default: None)
        ref -- reference to another parameter (default: None)
        rq -- sets if a parameter is required (default: "optional")'''
        Ent.StructureBase.__init__(self, name, Class, ref)
        if rq == 'true': self.setRequired(rq)
        elif rq == 'false': self.setRequired(rq)
        else: self.setRequired(rq)

    def setRequired(self, req):
        '''Sets required attribute for a parameter node
            
        Arguments:
        req -- sets if a parameter is required'''
        av = ('true', 'false', 'optional')
        msg = 'value must be string "true", "false", or "optional"'
        self._attchecker(u'required', req, msg, av)     


class Result(Ent.ResourceBase):

    '''Class for building PSI result nodes'''     

    nodeName = u'psi:result'    


class Exception(Ent.ResourceBase):

    '''Class for building PSI exception nodes'''     

    nodeName = u'psi:exception'    


class Link(Base, Ent.Ref):

    '''Class for building PSI link nodes'''    

    nodeName = u'psi:link'

    def __init__(self, ref=None):
        '''Initializes a PSI link node
            
        Arguments:
        ref -- reference to another parameter (default: None)'''
        Base.__init__(self, self.nodeName) 
        self.setRef(ref)

        
class Field(Base, Ent.Name):

    '''Class for building PSI field nodes'''
    
    nodeName = u'psi:field'

    def __init__(self, name=None):
        '''Initializes a PSI field node
            
        Arguments:
        name -- the name of the field'''
        Base.__init__(self, self.nodeName) 
        self.setName(name)   


class Include(Base, Ent.Class):

    '''Class for building PSI include nodes'''    

    nodeName = u'psi:include'

    def __init__(self, Class=None):
        '''Initializes a PSI include node
            
        Arguments:
        Class -- the class of the include (default: None)'''
        Base.__init__(self, self.nodeName) 
        self.setClass(Class)

  
class Metadata(Base, Ent.State):

    '''Class for building PSI metadata nodes'''    

    nodeName = u'psi:metadata'
    logs = []

    def __init__(self, state=None):
        '''Initializes a PSI metadata node
            
        Arguments:
        state -- the state of the node (default: None)'''
        Base.__init__(self, self.nodeName)
        if state: self.setState(state)

    def addLog(self, type, state, start, end, ref, text=None):
        '''Creates and adds a log entry to a metadata node

        Arguments:
        type -- the type of activity triggering a log entry
        state -- the state of the node
        start -- the start of activity recorded by the log
        end -- the end of activity recorded by a log entry
        ref -- a reference to another node
        text -- text for a log entry (default: None)'''
        node = self._add(Log, type, state, start, end, ref, text)
        ## Add to logs list
        self.logs.append(node)

    def removeLogs(self):
        '''Removes all logs'''
        self.removeChildren(self.logs)        
    

class Log(Base, Ent.TextBase, Ent.State, Ent.SE, Ent.Ref, Ent.AT):

    '''Class for building PSI logs'''  

    nodeName = u'psi:log'

    def __init__(self, type, state, start, end, ref, text=None):
        '''Initializes a log node

        Arguments:
        type -- the type of activity triggering a log entry
        state -- the state of the node
        start -- the start of activity recorded by the log
        end -- the end of activity recorded by a log entry
        ref -- a reference to another node
        text -- text for a log entry (default: None)'''
        Base.__init__(self, self.nodeName)
        self.setType(type)
        self.setState(state)
        self.setStart(start)        
        self.setEnd(end)
        self.setRef(ref)
        if text: self.setData(text)
        
    
class Filter(Base, Ent.NCR, Ent.TimeBase):

    '''Class for building PSI filters'''    

    nodeName = u'psi:filter'
    ## Placeholders for lists
    acl, sorts, actions, versions = [], [], [], []

    def __init__(self, name, Class=None, ref=None):
        '''Initializes a PSI filter
            
        Arguments:
        name -- the name of the filter
        Class -- the class of the filter (default: None)
        ref -- reference to next filter in chain (default: None)'''
        Base.__init__(self, self.nodeName)
        if ref: self.setRef(ref)
        if Class: self.setClass(Class)        
        self.setName(name)

    def addAccess(self, type, Class, ref):
        '''Creates and adds an access rule to a PSI filter
            
        Arguments:
        type -- grants or denys a right
        Class -- verification mechanism
        ref -- node to which access rule applies'''
        node = Access(type, Class, ref)
        ## Ensure access rules come first
        if self.hasChildNodes():
            if self.firstChild.tagName == u'psi:time':
                try: self.insertBefore(node, self.firstChild.nextSibling)
                except AttributeError: self.appendChild(node)
            else: self.insertBefore(node, self.firstChild)
        else: self.appendChild(node)
        ## Add to acl list
        self.acl.append(node)

    def removeAcl(self):
        '''Removes acl'''
        self.removeChildren(self.acl)         
        
    def addSort(self, type, ref=None):
        '''Creates and adds a sort rule to a PSI filter
            
        Arguments:
        type -- sets sort order
        ref -- sets node sort order is defined against (default: None)'''
        node = Sort(type, ref)
        ## Ensure sort falls before all other children of filter
        if self.hasChildNodes():
            if self.sorts:
                self.insertBefore(node, self.sorts[0])
            elif self.actions:
                self.insertBefore(node, self.actions[0])
            elif self.versions:
                self.insertBefore(node, self.versions[0])
            else: self.appendChild(node)
        else: self.appendChild(node)
        ## Add to sorts list
        self.sorts.append(node)

    def removeSorts(self):
        '''Removes all sorts'''
        self.removeChildren(self.sorts)        

    def addAction(self, type, Class=None, ref=None):
        '''Creates and adds an action to a PSI filter
            
        Arguments:
        type -- type of action
        Class -- class of action
        ref -- node to which action applies'''
        node = Action(type, Class, ref)
        ## Ensure rules action comes after sort rules and before version rules
        if self.hasChildNodes():
            if self.actions:
                self.insertBefore(node, self.actions[0])
            elif self.sorts:
                self.insertBefore(node, self.sorts[-1].nextSibling)
            elif self.versions:
                self.insertBefore(node, self.versions[0])        
            else: self.appendChild(node)
        else: self.appendChild(node)
        ## Add to actions list
        self.actions.append(node)

    def removeActions(self):
        '''Removes all actions'''
        self.removeChildren(self.actions)        

    def addVersion(self, type, mark=None):
        '''Creates and adds a version control rule to a PSI filter
            
        Arguments:
        type -- type of versioning
        mark -- sets how versions of a node are indicated (default: None)'''
        node = Version(type, mark)
        
        self.appendChild(node)
        ## Add to versions list
        self.versions.append(node)

    def removeVersions(self):
        '''Removes all versions'''
        self.removeChildren(self.versions)        


class Access(Base, Ent.Ref, Ent.Class):

    '''Class for building PSI access rules'''    

    nodeName = u'psi:access'
    rights = []

    def __init__(self, type, Class, ref):
        '''Initializes an access rule to a PSI filter
            
        Arguments:
        type -- grants or denys a right
        Class -- verification mechanism
        ref -- node to which access rule applies'''
        Base.__init__(self, self.nodeName)
        self.setType(type)        
        self.setClass(Class)
        self.setRef(ref)

    def addRight(self, type):
        '''Creates and adds a right to a PSI access rule
            
        Arguments:
        type -- type of right'''
        node = self._add(Right, type)
        ## adds to rights list
        self.rights.append(node)

    def removeRights(self):
        '''Removes all rights'''
        self.removeChildren(self.rights)        

    def setType(self, type='deny'):
        '''Sets the type attribute of a right
            
        Arguments:
        type -- type of right (default: deny)'''
        av = ('grant', 'deny')
        msg = '''value must be string "grant" or "deny"'''
        ## Ensure correct value
        self._attchecker(u'type', type, msg, av)      


class Right(Base, Ent.AT):

    '''Class for building PSI rights'''    

    nodeName = u'psi:right'

    def __init__(self, type):
        '''Initializes an right for a PSI access rule
            
        Arguments:
        type -- type of right'''
        Base.__init__(self, self.nodeName)
        self.setType(type)       


class Sort(Base, Ent.Ref, Ent.IfBase):

    '''Class for building PSI sorting rules'''     

    nodeName = u'psi:sort'

    def __init__(self, type, ref=None):
        '''Initializes an access rule to a PSI filter
            
        Arguments:
        type -- sets the sorting order
        ref -- sets the node a sort order is defined against (default: None)'''
        Base.__init__(self, self.nodeName)
        self.setType(type)
        if ref: self.setRef(ref)        

    def setType(self, type='seq'):
        '''Sets the type of sorting order
            
        Arguments:
        type -- type of sorting order (default: seq)'''
        av = ('root', 'seq', 'choice', 'par')
        msg = '''value must be string "root", "seq", "choice", or "par"'''
        self._attchecker(u'type', type, msg, av)

    
class If(Base, Ent.Match):

    '''Class for building PSI conditional tests'''     

    nodeName = u'psi:if'

    def __init__(self, value, match):
        '''Initializes an conditional test
            
        Arguments:
        value -- the condition evaluated
        match -- the value that must be true'''
        Base.__init__(self, self.nodeName)
        self.setValue(value); self.setMatch(match)    

    def setValue(self, value):
        '''Sets the value a conditional test evaluates
            
        Arguments:
        value -- the condition evaluated'''
        self.value = value
        self.setAttribute(u'value', value)
        

class Action(Base, Ent.Ref, Ent.Class, Ent.IfBase, Ent.TimeBase):

    '''Class for building PSI actions'''     

    nodeName = u'psi:action'

    def __init__(self, type, Class=None, ref=None):
        '''Initializes an PSI action
            
        Arguments:
        type -- type of action
        Class -- class of action (default: None)
        ref -- node to which action applies (default: None)'''
        Base.__init__(self, self.nodeName)
        self.setType(type)
        if Class: self.setClass(Class)  
        if ref: self.setRef(ref)

    def setType(self, type='read'):
        '''Sets the type of action
            
        Arguments:
        type -- type of action'''
        av = ('read', 'write', 'create', 'delete', 'jump',
              'pass', 'pause', 'stop')
        msg = '''value must be string "read", "write", "create" 
                "delete", "jump", "pass", "pause", or "stop"'''
        self._attchecker(u'type', type, msg, av)     


class Time(Base, Ent.SE):

    '''Class for building a PSI time control'''

    nodeName = u'psi:time'

    def __init__(self, t=None, le=None, s=None, e=None, lo=None):
        '''Initializes a psi:time element
            
        Arguments:
        t -- the time measurement (default: None)
        le -- the length of the type of time (default: None)
        s -- the start time (default: None)
        e -- the end time (default: None)
        lo -- how many times a PSI node should loop (default: None)'''
        Base.__init__(self, self.nodeName)
        if t: self.setType(t)
        if le: self.setLength(le)
        if s: self.setStart(s)
        if e: self.setEnd(e)
        if lo: self.setLoop(lo)

    def setType(self, type):
        '''Sets the time measurement
            
        Arguments:
        type -- the time measurement'''
        av = ('month', 'week', 'day', 'hour', 'min', 'sec', 'mil')
        msg = '''value must be string "month", "week", "day" 
                "hour", "min", "sec", or "mil"'''
        self._attchecker(u'type', type, msg, av)        

    def setLength(self, length):
        '''Sets the length of time
            
        Arguments:
        length -- the length of the type of time'''
        self.length = length
        self.setAttribute(u'length', length)        

    def setLoop(self, loop):
        '''Sets how many times a PSI node should loop
            
        Arguments:
        loop -- how many times a PSI node should loop'''
        self.loop = loop
        self.setAttribute(u'loop', loop)    

    
class Version(Base):

    '''Class for building a PSI version control rule'''     

    nodeName = u'psi:version'
    changes = []

    def __init__(self, type, mark=None):
        '''Initializes a version control rule
            
        Arguments:
        type -- type of versioning
        mark -- sets how versions of a node are indicated (default: None)'''
        Base.__init__(self, self.nodeName)
        self.setType(type)
        if mark: self.setMark(mark)

    def addChange(self, type, match):
        '''Creates and adds a psi:change rule to a version control rule
            
        Arguments:
        type -- the type of version conflict evaluated
        match -- the value that must be true'''
        node = self._add(Change, type, match)
        ## Add to changes list
        self.changes.append(node)

    def removeChanges(self):
        '''Removes all changes'''
        self.removeChildren(self.changes)        

    def setType(self, type):
        '''Sets the type of version control action
            
        Arguments:
        type -- type of version control action'''
        av = ('replace', 'fork', 'archive', 'stop')
        msg = '''value must be string "replace", "fork", "day" 
                "archive", or "stop"'''
        self._attchecker(u'type', type, msg, av)

    def setMark(self, mark):
        '''Sets how new versions of a node are indicated
            
        Arguments:
        mark -- sets how versions of a node are indicated'''
        av = ('seq', 'start', 'end')
        msg = '''value must be string "seq", "start", or "end"'''
        self._attchecker(u'mark', mark, msg, av)


class Change(Base, Ent.Match):

    '''Class for building PSI version control test'''     

    nodeName = u'psi:change'

    def __init__(self, type, match):
        '''Initializes a change rule
        
        Arguments:
        type -- the tie breaker in a version conflict
        match -- the value that must be true'''
        Base.__init__(self, self.nodeName)
        self.setType(type)
        self.setMatch(match)        

    def setType(self, type):
        '''Sets the tie breaking attribute type in a version conflict
        
        Arguments:
        type -- the tie breaker in a version conflict'''
        av = ('name', 'class', 'ref', 'state', 'action', 'start', 'end')
        msg = '''value must be string "name", "class", "ref"
            "state", "action", "start", or "end"'''
        self._attchecker(u'type', type, msg, av)