]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/system.py
f2062051e2b760d7d7088e39accb106c7faad483
[OpenSRF.git] / src / python / osrf / system.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.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 2
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 from osrf.conf import Config, get, get_no_ex
17 from osrf.net import Network, set_network_handle, get_network_handle
18 import osrf.stack, osrf.log, osrf.set, osrf.cache
19 import sys
20
21
22 def connect(configFile, configContext, init_cache=False):
23     """ Connects to the opensrf network 
24         @param configFile The OpenSRF config
25         @param configContext The path to the configuration element in the XML config.
26             e.g. 'config.opensrf'
27         @param init_cache If true, connect to the cache servers
28     """
29
30     if get_network_handle():
31         ''' This thread already has a handle '''
32         return
33
34     # parse the config file
35     configParser = Config(configFile, configContext)
36     configParser.parseConfig()
37     
38     # set up logging
39     osrf.log.initialize(
40         osrf.conf.get('loglevel'), 
41         osrf.conf.get_no_ex('syslog'),
42         osrf.conf.get_no_ex('logfile'))
43
44     # connect to the opensrf network
45     network = Network(
46         host = osrf.conf.get('domains.domain'),
47         port = osrf.conf.get('port'),
48         username = osrf.conf.get('username'), 
49         password = osrf.conf.get('passwd'))
50
51     network.set_receive_callback(osrf.stack.push)
52     osrf.net.set_network_handle(network)
53     network.connect()
54
55     # load the domain-wide settings file
56     osrf.set.load(osrf.conf.get('domains.domain'))
57
58     if init_cache:
59         connect_cache()
60
61
62 def connect_cache():
63     ''' Initializes the cache connections '''
64     cache_servers = osrf.set.get('cache.global.servers.server')
65     if cache_servers:
66         if not isinstance(cache_servers, list):
67             cache_servers = [cache_servers]
68         if not osrf.cache.CacheClient.get_client():
69             osrf.cache.CacheClient.connect(cache_servers)
70
71