# ~*~ coding: utf-8
import re
from socket import gaierror, error
import cElementTree
from SOAPpy import SOAPProxy
from SOAPpy import Config
from SOAPpy import Types
from SOAPpy import HTTPError
from zope.interface import implements
from interfaces import IWaramuClient
import Errors
re_code = re.compile('(.*?)')
re_mesg = re.compile('(.*?)')
def _getStatus(resp):
return int(re_code.search(resp).group(1))
def _getMessage(resp):
return re_mesg.search(resp).group(1)
def _formatAttachmentsList(resp):
resp = resp.encode('utf-8')
xml = cElementTree.fromstring(resp)
res = {}
ats = xml.findall('attachments/attachment')
for a in ats:
aid = int(a.find('id').text)
afn = a.find('filename').text
res[aid] = afn
return res
class WaramuClient:
implements(IWaramuClient)
_ws = None
namespace = 'http://wssoap.waramu.htk.tlu.ee/'
c = Config
c.debug = 1
def _init(self, location, debug=0):
self._location = location
self.c.debug = debug
self._sp = SOAPProxy(self._location, config=self.c, namespace = self.namespace)
self._waramu_types = None
self._type_desc = {}
self._type_infos = {}
def _genAppUser(self, user):
""" generate application user information """
r = "\n"
if 'Anonymous' not in user.getRoles():
r += ""+str(user)+""
grs = []
if hasattr(user, 'getGroups'):
grs = user.getGroups()
if len(grs) > 0:
r += "\n\n"
for gr in grs:
r += "\t"+gr+"\n"
r += "\n"
r += "\n"
return r
def isConnected(self):
if self._ws is None:
return False
return True
def _getWebUrl(self):
return self._webend
def _getSuffix(self):
return self._suffix
# Session
def newSession(self, username, password):
self._uname = username
self._pwd = password
try:
sessIdresponse = self._sp.newSession(uid=self._uname, pwd=self._pwd)
except HTTPError:
raise Errors.getException(3), 'Not found'
except gaierror:
raise Errors.getException(3), 'Not found'
except error:
# this is propably a "connection refused"
raise Errors.getException(3), 'Connection refused'
respCode = _getStatus(sessIdresponse)
if respCode: # not 0
msg = _getMessage(sessIdresponse)
raise Errors.getException(respCode), msg
self._ws = re.search('(.*?)', sessIdresponse).group(1)
return self._ws
def closeSession(self):
if self._ws is None:
return
logoutResponse = self._sp.closeSession(sessId=self._ws)
respCode = _getStatus(logoutResponse)
if respCode: # not 0
msg = _getMessage(logoutResponse)
raise Errors.getException(respCode), msg
self._ws = None
# WS
def Identify(self):
"""
@WebParam(name = "sessId") String sessId)
"""
resp = self._sp.Identify(sessId = self._ws)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
# only suffix
xml = cElementTree.fromstring(resp)
suffix = xml.find('identity/suffix').text
webend = xml.find('identity/baseweburl').text
self._suffix = suffix
self._webend = webend
return suffix, webend
def newResource(self, au, data):
"""
@WebParam(name = "appUser") String appUser,
@WebParam(name = "sessId") String sessId,
@WebParam(name = "data") String data
"""
resp = self._sp.newResource(appUser = au, sessId=self._ws, data=data)
xml = cElementTree.fromstring(resp)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return xml.find('uid').text
def updateResource(self, au, uid, data):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "uid") String uid,
@WebParam(name = "data") String data
"""
resp = self._sp.updateResource(appUser = au, sessId=self._ws, uid=uid, data=data)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return 0
def newPackage(self):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "package") byte[] zipfile,
@WebParam(name = "filename") String filename
"""
# XXX: TODO: implement me
pass
def addAttachment(self, au, resourceId, attachment, filename):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "resourceId") String internal_id,
@WebParam(name = "attachment") byte[] attachment,
@WebParam(name = "filename") String filename
"""
data = Types.base64BinaryType(attachment)
resp = self._sp.addAttachment(appUser = au, sessId=self._ws, resourceId=resourceId, attachment=data, filename=filename)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return _formatAttachmentsList(resp)
def listAttachments(self, au, resourceId):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "resourceId") String internal_id
"""
resp = self._sp.listAttachments(appUser = au, sessId=self._ws, resourceId=resourceId)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return _formatAttachmentsList(resp)
def removeAttachment(self, au, resourceId, attachmentId):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "resourceId") String internal_id,
@WebParam(name = "attachmentId") String attachmentId
"""
resp = self._sp.removeAttachment(appUser = au, sessId=self._ws, resourceId=resourceId, attachmentId=attachmentId)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return _formatAttachmentsList(resp)
def getAttachment(self, resourceId, attachmentId):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "resourceId") String internal_id,
@WebParam(name = "attachmentId") String attachmentId
"""
resp = self._sp.getAttachment(sessId=self._ws, resourceId=resourceId, attachmentId=attachmentId)
return resp
def listTypes(self):
"""
@WebParam(name = "sessId") String sessId
"""
if self._waramu_types:
return self._waramu_types
resp = self._sp.listTypes(sessId=self._ws)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
xml = cElementTree.fromstring(resp)
xtypes = xml.find('types')
wtypes = {}
for t in xtypes:
tid = t.find('id').text
tname = t.find('name').text
wtypes[tid] = {'name': tname }
self._type_desc[tid] = None
self._waramu_types = wtypes
return wtypes
def describeType(self, typeId):
"""
@WebParam(name = "sessId") String sessId,
@WebParam(name = "typeId") Integer typeId
"""
if self._type_desc.has_key(typeId) and self._type_desc[typeId]:
return self._type_desc[typeId], self._type_infos[typeId]
resp = self._sp.describeType(sessId=self._ws, typeId=typeId)
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
xml = cElementTree.fromstring(resp)
d = {}
xfields = xml.find('description').find('{%s}fields'%typeId)
print resp
for f in xfields:
d[f.get('name')] = {
'name': f.get('name'),
'printable' : f.find('{%s}title' % typeId).text.strip(),
'multiLingual': f.get('multiLingual'),
'type': f.get('type'),
'vocabulary': f.get('vocabulary'),
'autovalue': f.get('autovalue'),
'fixedVocabulary': f.get('fixedVocabulary')
}
xinfo = xml.find('description/info')
infos = {}
infos['attachable'] = '1'
self._type_desc[typeId] = d
self._type_infos[typeId] = infos
return d, infos
# XXX: TODO
def deleteResource(self, au, uid):
"""
@WebParam(name = "appUser") String appUser,
@WebParam(name = "sessId") String sessId,
@WebParam(name = "uid") String uid
"""
resp = self._sp.deleteResource(appUser=au, sessId=self._ws, uid=uid)
resp = resp.encode('utf-8')
respCode = _getStatus(resp)
if respCode:
msg = _getMessage(resp)
raise Errors.getException(respCode), msg
return respCode
def getResource(self, au, uid):
"""
@WebParam(name = "appUser") String appUser,
@WebParam(name = "sessId") String sessId,
@WebParam(name = "uid") String uid
"""
resource = self._sp.getResource(appUser=au, sessId=self._ws, uid=uid)
resource = resource.encode('utf-8')
respCode = _getStatus(resource)
if respCode:
msg = _getMessage(resource)
raise Errors.getException(respCode), msg
return resource
# XXX: TODO
def getVocabulary(self, au, tid, field, value, lang=None):
"""
@WebParam(name = "appUser") String appUser,
@WebParam(name = "sessId") String sessId,
@WebParam(name = "typeId") Integer typeId,
@WebParam(name = "field") String field
"""
q = ""
q += "<_field"
if lang is not None:
q += ' lang="'+lang+'"'
q += ">"+field
q += ""
if value:
q += "<"+field
q += ">"+value.decode('utf-8')+""+field+">"
q += ""
listResp = self._sp.listIdentifiers(appUser = au, sessId = self._ws, query=q)
listResp = listResp.encode('utf-8')
respCode = _getStatus(listResp)
if respCode:
msg = _getMessage(listResp)
raise Errors.getException(respCode), msg
print listResp
xml = cElementTree.fromstring(listResp)
units = xml.findall('vocabulary/unit')
res = []
op = None
# XXX
#if 'Integer' in fieldType:
# op = int
for u in units:
l = u.get('lang')
e = u.find('element')
c = u.find('count')
if op:
res.append([int(c.text), op(e.text), l])
else:
res.append([int(c.text), e.text, l])
return res
# XXX: TODO
def listIdentifiers(self, au, query):
"""
@WebParam(name = "appUser") String appUser,
@WebParam(name = "sessId") String sessId,
@WebParam(name = "query") String query
"""
listResp = self._sp.listIdentifiers(appUser = au, sessId = self._ws, query=query)
listResp = listResp.encode('utf-8')
respCode = _getStatus(listResp)
if respCode:
msg = _getMessage(listResp)
raise Errors.getException(respCode), msg
xml = cElementTree.fromstring(listResp)
res = []
ids = xml.findall('identifiers/id')
for id in ids:
res.append(id.text.strip())
return res