1 from constrictor.properties import Properties
2 from constrictor.log import *
3 from osrf.net_obj import NetworkObject
5 from osrf.gateway import GatewayRequest, XMLGatewayRequest, JSONGatewayRequest
6 from oils.utils.idl import IDLParser
7 from oils.utils.utils import md5sum
8 from oils.event import Event
9 from oils.const import *
10 import os, errno, random
12 props = Properties.getProperties()
14 default_dictionary = '/usr/share/dict/words'
22 if props.getProperty('evergreen.autologin') == 'true':
24 props.getProperty('evergreen.username'),
25 props.getProperty('evergreen.password'),
26 props.getProperty('evergreen.workstation'))
29 if not props.getProperty('evergreen.patronIDs'):
30 # if there are not configured patron IDs, go ahead
31 # and use the ID of the logged in user
32 user = fetchSessionUser()
33 logInfo("Setting evergreen.patronIDs to logged in user %s" % str(user.id()))
34 props.setProperty('evergreen.patronIDs', str(user.id()))
36 if not props.getProperty('evergreen.orgIDs'):
37 # simlilarly, if no org is provided, use the home org of the logged in user
39 user = fetchSessionUser()
40 logInfo("Setting evergreen.orgIDs to logged in user's home_ou %s" % str(user.home_ou()))
41 props.setProperty('evergreen.orgIDs', str(user.home_ou()))
44 dict_file = props.getProperty('evergreen.dictionary') or default_dictionary
46 words_file = open(dict_file)
48 logError("Unable to open dictionary file '%s'" % dict_file)
51 words = words_file.readlines()
56 # if necessary, create a connection to the opensrf network for this thread
57 if str(props.getProperty('evergreen.netProtocol')).lower() == 'jabber':
58 if props.getProperty('evergreen.osrfConfig'):
59 logInfo("Connecting to the opensrf network")
60 from osrf.system import osrfConnect
62 props.getProperty('evergreen.osrfConfig'),
63 props.getProperty('evergreen.osrfConfigContext'))
67 ''' Performs thread-specific initialization '''
70 def fetchSessionUser():
71 user = request('open-ils.auth', 'open-ils.auth.session.retrieve', authtoken()).send()
72 if Event.parse_event(user):
73 raise ILSEventException(osrf.json.to_json(user))
74 logInfo("fetched user %s" % user.usrname())
79 # XX add logic to allow IDL fetching via jabber
81 server = props.getProperty('evergreen.server')
82 cacheDir = props.getProperty('constrictor.cacheDir')
83 GatewayRequest.setDefaultHost(server)
88 filePath = '%s/evergreen/fm_IDL.xml' % cacheDir
91 # see if we have a local copy of the IDL already in the cache
92 file = open(filePath, 'r')
95 logInfo('fetching: http://%s/%s' % (server, props.getProperty('evergreen.IDLPath')))
96 f = urllib2.urlopen('http://%s/%s' % (server, props.getProperty('evergreen.IDLPath')))
98 if not os.path.exists('%s/evergreen' % cacheDir):
99 os.mkdir('%s/evergreen' % cacheDir)
101 file = open(filePath, 'w')
105 logInfo("parsing Evergreen IDL file...")
106 parser.set_IDL(filePath)
110 class AtomicReqWrapper(object):
111 ''' This wraps the built-in osrfAtomicRequest in a
112 gateway request-style class interface '''
114 def __init__(self, service, method, *args):
115 self.service = service
117 self.args = list(args)
120 from osrf.ses import osrfAtomicRequest
121 return osrfAtomicRequest(self.service, self.method, *(self.args))
125 def request(service, method, *args):
127 proto = props.getProperty('evergreen.netProtocol')
128 if str(proto).lower() == 'jabber':
129 req = AtomicReqWrapper(service, method, *args)
131 if str(proto).lower() == 'json':
132 req = JSONGatewayRequest(service, method, *args)
134 req = XMLGatewayRequest(service, method, *args)
135 req.setPath(props.getProperty('evergreen.gatewayPath'))
142 raise AuthException()
145 def login(username, password, workstation=None):
146 ''' Login to the server and get back an authtoken'''
149 logInfo("attempting login with user " + username)
153 'open-ils.auth.authenticate.init', username).send()
155 # generate the hashed password
156 password = md5sum(seed + md5sum(password))
160 'open-ils.auth.authenticate.complete',
161 { 'workstation' : workstation,
162 'username' : username,
163 'password' : password,
167 evt = Event.parse_event(result)
168 if evt and not evt.success:
169 raise AuthException(evt.text_code)
171 __authtoken = result['payload']['authtoken']
175 class AuthException(Exception):
176 def __init__(self, msg=''):
179 return 'AuthException: %s' % self.msg
181 class ILSEventException(Exception):
185 def random_phrase(num_words, max_num_words=3):
187 if num_words is None:
188 num_words = int( (random.random() * max_num_words) % max_num_words) + 1
192 for i in range(0, num_words):
194 word = words[ int(random.random() * len(words)) ]
197 phrase += '%s ' % word[0:len(word)-1]