]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/system.py
updated to fit new opensrf updates
[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 import osrf.conf
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, os
20
21 class System(object):
22
23     config_file = None
24     config_context = None
25
26     @staticmethod
27     def net_connect(**kwargs):
28         if get_network_handle():
29             ''' This thread already has a handle '''
30             return
31
32         config_file = kwargs.get('config_file') or System.config_file
33         config_context = kwargs.get('config_context') or System.config_context
34
35         # store the last config file info for later
36         System.config_file = config_file
37         System.config_context = config_context
38
39         # parse the config file
40         config_parser = osrf.conf.Config(config_file, config_context)
41         config_parser.parse_config()
42         
43         # set up logging
44         osrf.log.initialize(
45             osrf.conf.get('loglevel'), 
46             osrf.conf.get_no_ex('syslog'),
47             osrf.conf.get_no_ex('logfile'))
48
49         # connect to the opensrf network
50         network = Network(
51             host = osrf.conf.get('domains.domain'),
52             port = osrf.conf.get('port'),
53             username = osrf.conf.get('username'), 
54             password = osrf.conf.get('passwd'),
55             resource = kwargs.get('resource'))
56
57         network.set_receive_callback(osrf.stack.push)
58         osrf.net.set_network_handle(network)
59         network.connect()
60
61         return network
62
63
64     @staticmethod
65     def connect(**kwargs):
66         """ Connects to the opensrf network 
67             Options:
68                 config_file
69                 config_context
70                 connect_cache
71                 resource
72         """
73
74         network = System.net_connect(**kwargs)
75
76         # load the domain-wide settings file
77         osrf.set.load(osrf.conf.get('domains.domain'))
78
79         if kwargs.get('connect_cache'):
80             System.connect_cache()
81
82         return network
83
84
85     @staticmethod
86     def connect_cache():
87         ''' Initializes the cache connections '''
88         cache_servers = osrf.set.get('cache.global.servers.server')
89         if cache_servers:
90             if not isinstance(cache_servers, list):
91                 cache_servers = [cache_servers]
92             if not osrf.cache.CacheClient.get_client():
93                 osrf.cache.CacheClient.connect(cache_servers)
94
95     @staticmethod
96     def daemonize():
97         pid = os.fork() 
98         if pid == 0:
99             os.chdir('/')
100             os.setsid()
101             sys.stdin.close()
102             sys.stdout.close()
103             sys.stderr.close()
104         else:
105             os._exit(0)
106
107