-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetadataRecord.py
227 lines (164 loc) · 8.21 KB
/
MetadataRecord.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
# Updated 8 January 2010 to remove any direct database calls with psycopg2
# all calls now go through GeoNetwork xml -BC
import requests
from xml.etree import ElementTree as etree
from collections import OrderedDict
from lxml.builder import E
class metadataRecord(object):
def GetUserInfo(self, user, pword):
tree = GNConnection.xmlcall('xml.user.list')
unames = tree.findall('.//username')
record = None
for uname in unames:
if uname.text == user:
record = uname.getparent()
name = "%s %s" % (record.find('name').text, record.find('surname').text)
orgpos = record.find('organisation').text
if orgpos:
x = orgpos.find("--")
if x:
org = orgpos[:x]
pos = orgpos[x+2:]
else:
org = orgpos
pos = None
else:
org = None
pos = None
citstat = record.find('state').text
if citstat:
x = citstat.find("--")
if x:
city = citstat[:x]
state = citstat[x+2:]
else:
city = orgpos
state = None
else:
city = None
state = None
contact = OrderedDict()
CIresparty = OrderedDict()
csname = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',name)])
CIresparty['{http://www.isotc211.org/2005/gmd}individualName'] = csname
csorg = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',org)])
CIresparty['{http://www.isotc211.org/2005/gmd}organisationName'] = csorg
CIaddress = OrderedDict()
csdeliv = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',record.find('address').text)])
CIaddress['{http://www.isotc211.org/2005/gmd}deliveryPoint'] = csdeliv
cscity = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',city)])
CIaddress['{http://www.isotc211.org/2005/gmd}city'] = cscity
csregion = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',state)])
CIaddress['{http://www.isotc211.org/2005/gmd}administrativeArea'] = csregion
cspost = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',record.find('zip').text)])
CIaddress['{http://www.isotc211.org/2005/gmd}postalCode'] = cspost
cscountry = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',record.find('country').text)])
CIaddress['{http://www.isotc211.org/2005/gmd}country'] = cscountry
csemail = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',record.find('email').text)])
CIaddress['{http://www.isotc211.org/2005/gmd}electronicMailAddress'] = csemail
address = OrderedDict([('{http://www.isotc211.org/2005/gmd}CI_Address',CIaddress)])
CIcontact = OrderedDict([('{http://www.isotc211.org/2005/gmd}address',address)])
contactInfo = OrderedDict([('{http://www.isotc211.org/2005/gmd}CI_Contact',CIcontact)])
CIresparty['{http://www.isotc211.org/2005/gmd}contactInfo'] = contactInfo
cspos = OrderedDict([('{http://www.isotc211.org/2005/gco}CharacterString',pos)])
CIresparty['{http://www.isotc211.org/2005/gmd}positionName'] = cspos
contact['{http://www.isotc211.org/2005/gmd}CI_ResponsibleParty'] = CIresparty
return contact
def GetTemplateList(self, user, pword):
tmpltList = {}
# Select iso 19139 template metadata from GeoNework xml.search
tree = GNConnection.xmlcall('q', template='y', fast='index')
rows = tree.findall('.//metadata')
n = 1
for row in rows:
title = row.find('title').text
guid = row.find('{http://www.fao.org/geonetwork}info/uuid').text
schema = row.find('{http://www.fao.org/geonetwork}info/schema').text
id = row.find('{http://www.fao.org/geonetwork}info/id').text
if title and guid:
tmpltList[title + ' (' + id + ')'] = guid
n = n + 1
return tmpltList
def getTemplateMDRecord(self, user, pword, fileID):
# Select template metadata from the data column.
geoinfo = GNConnection.xmlcall('q', uuid=fileID, template='y')
schema = geoinfo.find('.//schema').text
return GNConnection.xmlcall('xml.metadata.get', uuid=fileID), schema
def submitMDRecord(self, mdRecord, user, pword):
tree = GNConnection.xmlcall('xml.metadata.insert',
data=mdRecord,
title='',
stylesheet='__non__',
schema='iso19139',
group=2, # get user group?
category=1, # ask for category?
validate='off', #validation would need additional interfaces
uuidAction='generateUUID')
id = tree.find('./id').text
uuid = tree.find('./uuid').text
return uuid, id, GNConnection.cookie
def mergeInfo(self, node, tree, path):
for item, value in node.items():
node = '/' + item
path = path + node
if type(value).__name__ == 'OrderedDict':
self.mergeInfo(value, tree, path)
else:
values = str(value).split('--')
element = tree.find(path)
if (element is not None):
for val in values:
elpar = element.getparent()
if elpar.get('{http://www.isotc211.org/2005/gco}nilReason'):
del elpar.attrib['{http://www.isotc211.org/2005/gco}nilReason']
ival = str(val)
element.text = ival
if elpar.getnext() is not None:
element = elpar.getnext().getchildren()[0]
lnode = len(node)
path = path[:-lnode]
return etree.tostring(tree)
class GNConnection(object):
# Should these be in SMETconfig.xml -- check?
GNServer = ""
user = ""
password = ""
projData = ""
rootDir = "c:\\"
connection = None
cookie = ""
def getSettings(self, cdir):
GN = GNConnection
xfile = cdir + '/SMETconfig.xml'
doc = etree.parse ( xfile )
GN.GNServer = doc.find("./GNServer").text
GN.rootDir = doc.find("./rootDir").text
GN.projData = doc.find("./projData").text
@staticmethod
def setUser(username):
GNConnection.user = username
@staticmethod
def setPass(pword):
GNConnection.password = pword
@staticmethod
def connect(username, pword):
# Do we have security issues with clear case passwords?
path = '/geonetwork/srv/xml.user.login'
payload = {'username':username, 'password':pword}
r = requests.post("http://" + GNConnection.GNServer + path, params=payload)
GNConnection.cookie = r.cookies['JSESSIONID']
print(r.status_code)
return r.status_code
@staticmethod
def xmlcall(_service, _param=lambda:etree.Element("request"), **_kwargs):
if callable(_param): _param = _param()
for key, value in list(_kwargs.items()):
etree.SubElement(_param, key).text = value
param = etree.tostring(_param)
print(param)
path ='/geonetwork/srv/' + _service
r = requests.post("http://" + GNConnection.GNServer + path,
data=param,
headers={"Content-type":"text/xml"},
cookies={'JSESSIONID':GNConnection.cookie})
return etree.XML(str(r.content))