1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # -----------------------------------------------------------------------
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
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.
27 @param init_cache If true, connect to the cache servers
30 if get_network_handle():
31 ''' This thread already has a handle '''
34 # parse the config file
35 configParser = Config(configFile, configContext)
36 configParser.parseConfig()
40 osrf.conf.get('loglevel'),
41 osrf.conf.get_no_ex('syslog'),
42 osrf.conf.get_no_ex('logfile'))
44 # connect to the opensrf 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'))
51 network.set_receive_callback(osrf.stack.push)
52 osrf.net.set_network_handle(network)
55 # load the domain-wide settings file
56 osrf.set.load(osrf.conf.get('domains.domain'))
63 ''' Initializes the cache connections '''
64 cache_servers = osrf.set.get('cache.global.servers.server')
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)