-
Notifications
You must be signed in to change notification settings - Fork 7
/
nodes.py
145 lines (118 loc) · 4.64 KB
/
nodes.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
# -*- coding: utf-8 -*-
"""
This module defines classes and methods to manage data associated with VAMDC nodes. It provides
functionality to retrieve a list of registered VAMDC nodes and to handle data such as urls of these
nodes.
"""
try:
from registry import *
except:
print "Suds package not available. Load Vamdc-Nodes from static file"
from local_registry import *
import query as q
import request as r
class Nodelist(object):
"""
This class provides a list of database nodes and methods
which are related to a list of nodes.
"""
def __init__(self):
"""
An instance of Nodelist contains a list of registered VAMDC nodes. This list is retrieved
via querying the VAMDC registry by default during initialization of the instance.
"""
self.nodes = []
for node in getNodeList():
self.nodes.append(Node(node['name'], url=node['url'], referenceUrl=node['referenceUrl'],identifier = node['identifier'], maintainer=node['maintainer'], returnables=node['returnables']))
def __repr__(self):
"""
Returns a string which contains a list of all VAMDC node names
"""
returnstring = ""
for node in self.nodes:
returnstring +="%s\n" % node.name
return returnstring
def getnode(self, identifier):
"""
Return a nodes.Node instance for the given ivo-identifier.
"""
for node in self.nodes:
if node.identifier == identifier:
return node
return None
def findnode(self, searchstring):
"""
This method tries to identify a VAMDC node by the specified 'searchstring'. It will return
a instance of nodes.Node if the node could be uniquely identified. Otherwise it will print out all
nodes which match the searchstring.
:param str searchstring: The string which looked for in the nodes name and ivo-identifier
:return: The node or a list of nodes which matches the searchstring.
:rtype: nodes.Node
"""
nodes_match = []
for node in self.nodes:
if searchstring in node.identifier or searchstring in node.name:
nodes_match.append(node)
if len(nodes_match) == 1:
return nodes_match[0]
else:
return nodes_match
def __iter__(self):
return self.nodes.__iter__()
class Node(object):
"""
This class contains informations and methods associated with one (VAMDC) database node,
such as its access url (for database queries) and its name.
:ivar name: Name of the VAMDC node
:ivar url: Url of the VAMDC node
:ivar identifier: IVO-Identifier of the Node
"""
def __init__(self, name, url=None, referenceUrl = None, identifier = None , maintainer = None, returnables = None):
self.name=name
self.url=url
self.referenceUrl=referenceUrl
self.identifier = identifier
self.maintainer = maintainer
self.returnables = returnables
def __repr__(self):
"""
Returns the node's name.
"""
return self.name
def get_species(self):
"""
Queries all species from the database-node via TAP-XSAMS request and
Query 'Select Species'. The list of species is saved in object species.
Note: This does not work for all species !
"""
# Some nodes do not understand this query at all
# others do not understand the query SELECT SPECIES
# therefore the following query is a small workaround for some nodes
query=q.Query("SELECT SPECIES WHERE ((InchiKey!='UGFAIRIUMAVXCW'))")
req = r.Request()
req.setnode(self)
req.setquery(query)
result = req.dorequest()
try:
self.Molecules = result.data['Molecules']
except:
pass
try:
self.Atoms = result.data['Atoms']
except:
pass
def print_species(self):
"""
prints out the list of species to stdout if they can be accessed with a SELECT SPECIES - Query
"""
if not (hasattr(self, 'Atoms') or hasattr(self, 'Molecules')):
self.get_species()
try:
print "List of Atoms: "
for atom in self.Atoms:
print("%s" % self.Atoms[atom])
print "List of Molecules: "
for molecule in self.Molecules:
print("%s" % self.Molecules[molecule])
except Exception, e:
print "Could not retrieve list of species: %s" % e