From: erickson Date: Sat, 14 Aug 2010 14:40:54 +0000 (+0000) Subject: created generic sip request task class to reduce code footprint; added patron info... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=e03644ef4b5d4da6dd4f1334db0564710d02389f;p=working%2Frandom.git created generic sip request task class to reduce code footprint; added patron info request task w/ script; added mixed batch script git-svn-id: svn://svn.open-ils.org/ILS-Contrib/constrictor/trunk@959 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- diff --git a/contrib/sip2/sip2_client.py b/contrib/sip2/sip2_client.py index f71749bd6..dd6ce9b7d 100644 --- a/contrib/sip2/sip2_client.py +++ b/contrib/sip2/sip2_client.py @@ -18,9 +18,27 @@ import constrictor.task import constrictor.script import constrictor.log as log -line_terminator = '\r\n' +line_terminator = '\r' class SIP2Client(object): + + # generic request class + class SIP2RequestTask(constrictor.task.Task): + + def __init__(self, client, msg): + constrictor.task.Task.__init__(self, self.__class__.__name__) + self.client = client + self.msg = msg + + def run(self, **kw): + log.log_info("%s" % self.name) + self.client.send_msg(self.msg) + data = self.client.recv_msg() + if data is None: + return False + log.log_info('SIP2 %s response: %s' % (self.name, data)) + return data + def __init__(self, server, port): self.server = server @@ -74,7 +92,7 @@ class SIP2Client(object): return None if buf is None or buf == '': # server kicked us off - log.log_error("SIP2 server ejected us") + log.log_error("SIP2 server rejected us!") return None data = data + buf @@ -128,47 +146,34 @@ class SIP2Client(object): def item_info_request(self, institution, copy_barcode): client = self + msg = '1720060110 215612AO%s|AB%s|' % (institution, copy_barcode) - class ItemInfoTask(constrictor.task.Task): - - def __init__(self, name=None): - constrictor.task.Task.__init__(self, self.__class__.__name__) - - def run(self, **kw): - log.log_info("ItemInfoTask: %s" % copy_barcode) - msg = '1720060110 215612AO%s|AB%s|' % (institution, copy_barcode) - client.send_msg(msg) - data = client.recv_msg() - if data is None: - return False - log.log_info('SIP2 item info response: %s' % data) - return data + class ItemInfoTask(SIP2Client.SIP2RequestTask): + def __init__(self): + SIP2Client.SIP2RequestTask.__init__(self, client, msg) return ItemInfoTask().start() def patron_status_request(self, institution, user_barcode, user_password): client = self + msg = '2300120060101 084237AO%s|AA%s|AD%s|AC|' % (institution, user_barcode, user_password) - class PatronStatusTask(constrictor.task.Task): - - def __init__(self, name=None): - constrictor.task.Task.__init__(self, self.__class__.__name__) - - def run(self, **kw): - log.log_info("PatronStatusTask: %s" % user_barcode) - msg = '2300120060101 084237AO%s|AA%s|AD%s|AC|' % (institution, user_barcode, user_password) - client.send_msg(msg) - data = client.recv_msg() - if data is None: - return False - log.log_info('SIP2 patron status response: %s' % data) - return data + class PatronStatusTask(SIP2Client.SIP2RequestTask): + def __init__(self): + SIP2Client.SIP2RequestTask.__init__(self, client, msg) return PatronStatusTask().start() - + def patron_info_request(self, institution, user_barcode): + + client = self + msg = '6300020060329 201700 AO%s|AA%s|' %(institution, user_barcode) + class PatronInfoTask(SIP2Client.SIP2RequestTask): + def __init__(self): + SIP2Client.SIP2RequestTask.__init__(self, client, msg) + return PatronInfoTask().start() diff --git a/contrib/sip2/sip2_mixed_batch.py b/contrib/sip2/sip2_mixed_batch.py new file mode 100644 index 000000000..0b1b9bdf9 --- /dev/null +++ b/contrib/sip2/sip2_mixed_batch.py @@ -0,0 +1,62 @@ +# ----------------------------------------------------------------------- +# Copyright (C) 2010 Equinox Software, Inc +# Bill Erickson +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# ----------------------------------------------------------------------- + +import constrictor.script +import constrictor.properties +import constrictor.log as log +import sip2_client + + +class SIP2MixedBatchScript(constrictor.script.Script): + + def __init__(self): + constrictor.script.Script.__init__(self) + + def run(self): + props = constrictor.properties.Properties.get_properties() + + username = props.get_property('sip2.username') + password = props.get_property('sip2.password') + institution = props.get_property('sip2.institution') + server = props.get_property('sip2.server') + port = int(props.get_property('sip2.port')) + + barcodes = props.get_property('sip2.copyBarcodes').split(',') + copy_barcode = barcodes[constrictor.script.ScriptThread.get_thread_id()] + + user_barcodes = props.get_property('sip2.userBarcodes').split(',') + user_passwords = props.get_property('sip2.userPasswords').split(',') + + user_barcode = user_barcodes[constrictor.script.ScriptThread.get_thread_id()] + user_password = user_passwords[constrictor.script.ScriptThread.get_thread_id()] + + + client = sip2_client.SIP2Client(server, port) + client.init_socket() + + if client.login(username, password, institution): + for i in range(100): + if not client.item_info_request(institution, copy_barcode): + break + if not client.patron_status_request(institution, user_barcode, user_password): + break + if not client.patron_info_request(institution, user_barcode): + break + + client.disconnect() + +constrictor.script.ScriptManager.go(SIP2MixedBatchScript()) + + diff --git a/contrib/sip2/sip2_patron_info_endurance.py b/contrib/sip2/sip2_patron_info_endurance.py new file mode 100644 index 000000000..90c1059af --- /dev/null +++ b/contrib/sip2/sip2_patron_info_endurance.py @@ -0,0 +1,50 @@ +# ----------------------------------------------------------------------- +# Copyright (C) 2010 Equinox Software, Inc +# Bill Erickson +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# ----------------------------------------------------------------------- + +import constrictor.script +import constrictor.properties +import constrictor.log as log +import sip2_client + + +class SIP2PatronInfoEnduranceScript(constrictor.script.Script): + + def __init__(self): + constrictor.script.Script.__init__(self) + + def run(self): + props = constrictor.properties.Properties.get_properties() + + username = props.get_property('sip2.username') + password = props.get_property('sip2.password') + institution = props.get_property('sip2.institution') + server = props.get_property('sip2.server') + port = int(props.get_property('sip2.port')) + + user_barcodes = props.get_property('sip2.userBarcodes').split(',') + user_barcode = user_barcodes[constrictor.script.ScriptThread.get_thread_id()] + + client = sip2_client.SIP2Client(server, port) + + if client.init_socket() and client.login(username, password, institution): + for i in range(100): + if not client.patron_info_request(institution, user_barcode): + break + + client.disconnect() + +constrictor.script.ScriptManager.go(SIP2PatronInfoEnduranceScript()) + +