12
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
-
16
- @author: Konrad Jopek
15
+
17
16
'''
18
17
19
18
31
30
class StarParser (XMLParser ):
32
31
'''
33
32
Parser for Storage Accounting Records
34
-
35
- For documentation please visit:
33
+
34
+ For documentation please visit:
36
35
https://twiki.cern.ch/twiki/bin/view/EMI/StorageAccounting
37
36
'''
38
-
37
+
39
38
NAMESPACE = "http://eu-emi.eu/namespaces/2011/02/storagerecord"
40
-
39
+
41
40
def get_records (self ):
42
- '''
41
+ """
43
42
Returns list of parsed records from STAR file.
44
-
43
+
45
44
Please notice that this parser _requires_ valid
46
45
structure of XML document, including namespace
47
46
information and prefixes in XML tag (like urf:StorageUsageRecord).
48
- '''
47
+ """
49
48
records = []
50
-
51
- xml_storage_records = self .doc .getElementsByTagNameNS (self .NAMESPACE , 'StorageUsageRecord' )
49
+
50
+ xml_storage_records = self .doc .getElementsByTagNameNS (
51
+ self .NAMESPACE , 'StorageUsageRecord' )
52
52
53
53
if len (xml_storage_records ) == 0 :
54
54
raise XMLParserException ('File does not contain StAR records!' )
@@ -61,69 +61,80 @@ def get_records(self):
61
61
records += group_attributes
62
62
63
63
return records
64
-
64
+
65
65
def parseStarRecord (self , xml_storage_record ):
66
- '''
66
+ """
67
67
Parses single entry for Storage Accounting Record.
68
- The dictionary below contains fields from record and
69
- methods to extract them from XML file.
70
- '''
71
-
68
+
69
+ Uses a dictionary containing fields from the storage record and methods
70
+ to extract them from the XML. Returns a list of StorageRecords (plus
71
+ GroupAttributeRecords if there are GroupAttributes that have an
72
+ attributeType that is NOT subgroup or role).
73
+ """
74
+
72
75
functions = {
73
- 'RecordId' : lambda nodes : self .getAttr (nodes ['RecordIdentity' ][0 ],
74
- 'recordId' ),
75
- 'CreateTime' : lambda nodes : parse_timestamp (
76
- self .getAttr (nodes ['RecordIdentity' ][0 ],
77
- 'createTime' )),
78
- 'StorageSystem' : lambda nodes : self .getText (
79
- nodes ['StorageSystem' ][0 ].childNodes ),
80
- 'Site' : lambda nodes : self .getText (
81
- nodes ['Site' ][0 ].childNodes ),
82
-
83
- 'StorageShare' : lambda nodes : self .getText (
84
- nodes ['StorageShare' ][0 ].childNodes ),
85
- 'StorageMedia' : lambda nodes : self .getText (
86
- nodes ['StorageMedia' ][0 ].childNodes ),
87
- 'StorageClass' : lambda nodes : self .getText (
88
- nodes ['StorageClass' ][0 ].childNodes ),
89
- 'FileCount' : lambda nodes : self .getText (
90
- nodes ['FileCount' ][0 ].childNodes ),
91
- 'DirectoryPath' : lambda nodes : self .getText (
92
- nodes ['DirectoryPath' ][0 ].childNodes ),
93
- 'LocalUser' : lambda nodes : self .getText (
94
- nodes ['LocalUser' ][0 ].childNodes ),
95
- 'LocalGroup' : lambda nodes : self .getText (
96
- nodes ['LocalGroup' ][0 ].childNodes ),
97
- 'UserIdentity' : lambda nodes : self .getText (
98
- nodes ['UserIdentity' ][0 ].childNodes ),
99
- 'Group' : lambda nodes : self .getText (
100
- nodes ['Group' ][0 ].childNodes ),
101
- 'StartTime' : lambda nodes : parse_timestamp (self .getText (nodes ['StartTime' ][0 ].childNodes )),
102
- 'EndTime' : lambda nodes : parse_timestamp (self .getText (nodes ['EndTime' ][0 ].childNodes )),
103
- 'ResourceCapacityUsed' : lambda nodes : self .getText (
76
+ 'RecordId' : lambda nodes : self .getAttr (
77
+ nodes ['RecordIdentity' ][0 ], 'recordId' ),
78
+ 'CreateTime' : lambda nodes : parse_timestamp (self .getAttr (
79
+ nodes ['RecordIdentity' ][0 ], 'createTime' )),
80
+ 'StorageSystem' : lambda nodes : self .getText (
81
+ nodes ['StorageSystem' ][0 ].childNodes ),
82
+ 'Site' : lambda nodes : self .getText (
83
+ nodes ['Site' ][0 ].childNodes ),
84
+ 'StorageShare' : lambda nodes : self .getText (
85
+ nodes ['StorageShare' ][0 ].childNodes ),
86
+ 'StorageMedia' : lambda nodes : self .getText (
87
+ nodes ['StorageMedia' ][0 ].childNodes ),
88
+ 'StorageClass' : lambda nodes : self .getText (
89
+ nodes ['StorageClass' ][0 ].childNodes ),
90
+ 'FileCount' : lambda nodes : self .getText (
91
+ nodes ['FileCount' ][0 ].childNodes ),
92
+ 'DirectoryPath' : lambda nodes : self .getText (
93
+ nodes ['DirectoryPath' ][0 ].childNodes ),
94
+ 'LocalUser' : lambda nodes : self .getText (
95
+ nodes ['LocalUser' ][0 ].childNodes ),
96
+ 'LocalGroup' : lambda nodes : self .getText (
97
+ nodes ['LocalGroup' ][0 ].childNodes ),
98
+ 'UserIdentity' : lambda nodes : self .getText (
99
+ nodes ['UserIdentity' ][0 ].childNodes ),
100
+ 'Group' : lambda nodes : self .getText (
101
+ nodes ['Group' ][0 ].childNodes ),
102
+ 'SubGroup' : lambda nodes : self .getText (self .getTagByAttr (
103
+ nodes ['SubGroup' ], 'attributeType' , 'subgroup' )[0 ].childNodes ),
104
+ 'Role' : lambda nodes : self .getText (self .getTagByAttr (
105
+ nodes ['Role' ], 'attributeType' , 'role' )[0 ].childNodes ),
106
+ 'StartTime' : lambda nodes : parse_timestamp (self .getText (
107
+ nodes ['StartTime' ][0 ].childNodes )),
108
+ 'EndTime' : lambda nodes : parse_timestamp (self .getText (
109
+ nodes ['EndTime' ][0 ].childNodes )),
110
+ 'ResourceCapacityUsed' : lambda nodes : self .getText (
104
111
nodes ['ResourceCapacityUsed' ][0 ].childNodes ),
105
- 'LogicalCapacityUsed' : lambda nodes : self .getText (
112
+ 'LogicalCapacityUsed' : lambda nodes : self .getText (
106
113
nodes ['LogicalCapacityUsed' ][0 ].childNodes ),
107
- 'ResourceCapacityAllocated' : lambda nodes : self .getText (
114
+ 'ResourceCapacityAllocated' : lambda nodes : self .getText (
108
115
nodes ['ResourceCapacityAllocated' ][0 ].childNodes )
109
- }
116
+ }
110
117
111
- # here we copy keys from functions
112
- # we only want to change 'RecordId' to 'RecordIdentity',
113
- nodes = {}.fromkeys ( map (lambda f : f == 'RecordId' and 'RecordIdentity' or f ,
114
- [S for S in functions ]) )
118
+ # Here we copy keys from functions.
119
+ # We only want to change 'RecordId' to 'RecordIdentity'.
120
+ nodes = {}.fromkeys (map (lambda f : f == 'RecordId' and
121
+ 'RecordIdentity' or f , [S for S in functions ]))
115
122
# nodes = {}.fromkeys(functions.keys())
116
123
data = {}
117
124
118
125
for node in nodes :
126
+ if node in ('SubGroup' , 'Role' ):
127
+ # For these attributes we need to dig into the GroupAttribute
128
+ # elements to get the values so we save the whole elements.
129
+ nodes [node ] = xml_storage_record .getElementsByTagNameNS (
130
+ self .NAMESPACE , 'GroupAttribute' )
131
+ else :
132
+ nodes [node ] = xml_storage_record .getElementsByTagNameNS (
133
+ self .NAMESPACE , node )
119
134
# empty list = element have not been found in XML file
120
- nodes [node ] = xml_storage_record .getElementsByTagNameNS (self .NAMESPACE , node )
121
-
135
+
122
136
for field in functions :
123
137
try :
124
- # if field == 'Group':
125
- # data['GroupName'] = functions[field](nodes)
126
- # else:
127
138
data [field ] = functions [field ](nodes )
128
139
except (IndexError , KeyError ), e :
129
140
log .debug ("Failed to get field %s: %s" , field , e )
@@ -132,24 +143,27 @@ def parseStarRecord(self, xml_storage_record):
132
143
sr .set_all (data )
133
144
134
145
return sr , self .parseGroupAttributes (
135
- xml_storage_record .getElementsByTagNameNS (
136
- self .NAMESPACE ,'GroupAttribute' ),
137
- sr .get_field ('RecordId' ))
138
-
139
-
146
+ xml_storage_record .getElementsByTagNameNS (
147
+ self .NAMESPACE , 'GroupAttribute' ), sr .get_field ('RecordId' ))
148
+
140
149
def parseGroupAttributes (self , nodes , star_record_id ):
141
- '''
142
- Return a list of GroupAttributes associated with StarRecord.
143
- '''
150
+ """
151
+ Return a list of GroupAttributeRecords associated with a StarRecord.
152
+
153
+ Only returns records for attributeTypes other than subgroup and role as
154
+ those two types are stored in the StarRecord.
155
+ """
144
156
ret = []
145
157
146
158
for node in nodes :
147
- group_attr = GroupAttributeRecord ()
148
- group_attr .set_field ('StarRecordID' , star_record_id )
149
159
attr_type = self .getAttr (node , 'attributeType' )
150
- group_attr .set_field ('AttributeType' , attr_type )
151
- attr_value = self .getText (node .childNodes )
152
- group_attr .set_field ('AttributeValue' , attr_value )
153
- ret .append (group_attr )
154
-
155
- return ret
160
+ # Only create records for types other than subgroup and role
161
+ if attr_type not in ('subgroup' , 'role' ):
162
+ group_attr = GroupAttributeRecord ()
163
+ group_attr .set_field ('StarRecordID' , star_record_id )
164
+ group_attr .set_field ('AttributeType' , attr_type )
165
+ attr_value = self .getText (node .childNodes )
166
+ group_attr .set_field ('AttributeValue' , attr_value )
167
+ ret .append (group_attr )
168
+
169
+ return ret
0 commit comments