]> git.evergreen-ils.org Git - working/random.git/blob - contrib/sip2/sip2_client.py
new SIP2 client contrib module. currently supports login and item information requests
[working/random.git] / contrib / sip2 / sip2_client.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2010 Equinox Software, Inc
3 # Bill Erickson <erickson@esilibrary.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 3
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import sys, socket
17 import constrictor.task
18 import constrictor.script
19 import constrictor.log as log
20
21 line_terminator = '\r\n'
22
23 class SIP2Client(object):
24     
25     def __init__(self, server, port):
26         self.server = server
27         self.port = port
28         self.sock = None
29
30     def init_socket(self):
31
32         try:
33             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
34         except socket.error, msg:
35             log.log_error("%s\n" % msg[1])
36             return False
37
38         try:
39             self.sock.connect((self.server, self.port))
40         except socket.error, msg:
41             log.log_error("%s\n" % msg[1])
42             return False
43
44         return True
45
46     def send_msg(self, msg):
47         log.log_debug('SIP2 socket sending: %s' % msg)
48         self.sock.send(msg + line_terminator)
49
50     def recv_msg(self):
51         data = ''
52         buf = ''
53
54         while True:
55             buf = self.sock.recv(2048)
56             data = data + buf
57             log.log_debug("SIP2 socket read: %s" % buf)
58             if data[-(len(line_terminator)):] == line_terminator:
59                 break
60
61         return data
62
63     def login(self, username, password, institution):
64         client = self
65
66         class LoginTask(constrictor.task.Task):
67
68             def __init__(self):
69                 constrictor.task.Task.__init__(self, self.__class__.__name__)
70
71             def run(self, **kw):
72
73                 log.log_info("LoginTask: %s %s %s" % (username, password, institution))
74
75                 msg = '9300CN%s|CO%s|CP%s|' % (username, password, institution)
76                 client.send_msg(msg)
77                 data = client.recv_msg()
78
79                 if data[:3] == '941':
80                     log.log_info("SIP2 login OK")
81                     return True
82                 else:
83                     log.log_error("SIP2 login failed: %s" % data)
84                     return False
85
86         return LoginTask().start()
87
88     def item_info_request(self, institution, copy_barcode):
89
90         client = self
91
92         class ItemInfoTask(constrictor.task.Task):
93
94             def __init__(self, name=None):
95                 constrictor.task.Task.__init__(self, self.__class__.__name__)
96
97             def run(self, **kw):
98                 # TODO: use a generated date
99                 log.log_info("ItemInfoTask: %s" % copy_barcode)
100                 msg = '1720060110    215612AO%s|AB%s|' % (institution, copy_barcode)
101                 client.send_msg(msg)
102                 data = client.recv_msg()
103                 # TODO: check for valid response
104                 log.log_info('SIP2 item info response: %s' % data)
105                 return data
106
107         return ItemInfoTask().start()
108
109