-
Notifications
You must be signed in to change notification settings - Fork 7
/
results.py
139 lines (105 loc) · 3.92 KB
/
results.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
# -*- coding: utf-8 -*-
"""
This module contains classes and methods to handle and process the result obtained
by a VAMDC request.
"""
try:
from lxml import objectify
is_available_xml_objectify = True
except ImportError:
is_available_xml_objectify = False
from xml.etree import ElementTree
import urllib2
from specmodel import *
import query as q
from urlparse import urlparse
from dateutil.parser import parse
XSD = "http://vamdc.org/xml/xsams/1.0"
class Result(object):
"""
An Result instance contains the data returned by querying a VAMDC database node (XSAMS - Document).
:ivar Source: Source
:ivar Xml: XSAMS - Document (XML) as string as it is returned by the node.
:ivar root: XSAMS document in an objectified structure (lxml.objectify)
"""
def __init__(self, xml=None, source=None):
"""
Result instances contain the data returned by a request send to a VAMDC node (XSAMS-Document) and provide
methods to process this data in various ways (Validation, Parse the data and store it in table-like objects)
:param str xml: XSAMS-String of the document
:param str source: ???
"""
self.Source = source
self.Xml = xml
#if self.Xml is None:
# self.get_xml(self.Source)
def objectify(self):
"""
Parses the XML string and generates an objectified structure of the document, which
is stored in the variable root.
The source can be any of the following:
- a file name/path
- a file object
- a file-like object
- a URL using the HTTP or FTP protocol
"""
if not is_available_xml_objectify:
print "Module lxml.objectify not available"
return
try:
self.root = objectify.XML(self.Xml)
except ValueError:
self.Xml=etree.tostring(self.Xml)
self.root = objectify.XML(self.Xml)
except Exception, e:
print "Objectify error: %s " % e
def populate_model(self):
"""
Populates classes of specmodel
"""
if not hasattr(self, 'root') or self.root == None:
self.root = ElementTree.fromstring(self.Xml)
# self.objectify()
self.data = populate_models(self.root, add_states=True)
def get_vibstates(self):
vibs = {}
for qn in self.States:
vib = ''
for l in self.States[qn].QuantumNumbers.qns:
if isVibrationalStateLabel(l):
vib += "%s=%s, " % (l,self.States[qn].QuantumNumbers.qns[l])
# remove last ', ' from the string
vib = vib[:-2]
try:
if vib not in vibs[self.States[qn].SpecieID]:
vibs[self.States[qn].SpecieID].append(vib)
except KeyError:
vibs[self.States[qn].SpecieID] = [vib]
return vibs
def get_process_class(self):
classes = {}
for trans in self.root.Processes.Radiative.RadiativeTransition:
codes = []
for code in trans.ProcessClass.Code:
codes.append(code)
try:
if codes not in classes[str(trans.SpeciesRef)]:
classes[str(trans.SpeciesRef)].append(codes)
except KeyError:
classes[str(trans.SpeciesRef)] = [codes]
return classes
def validate(self):
if not hasattr(self, 'xsd'):
self.xsd=etree.XMLSchema(etree.parse(XSD))
xml = etree.fromstring(self.Xml)
return self.xsd.validate(xml)
def apply_stylesheet(xslt):
"""
Applys a stylesheet to the xml object and returns
the result as string.
:param xslt: url or file which contains the stylesheet
:return: the output string of the operation
:rtype: str
"""
# To be implemented
pass