]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/system.py
removing overly agressive locale normalization
[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             osrf.conf.get_no_ex('client') == 'true')
49
50         # connect to the opensrf network
51         network = Network(
52             host = osrf.conf.get('domain'),
53             port = osrf.conf.get('port'),
54             username = osrf.conf.get('username'), 
55             password = osrf.conf.get('passwd'),
56             resource = kwargs.get('resource'))
57
58         network.set_receive_callback(osrf.stack.push)
59         osrf.net.set_network_handle(network)
60         network.connect()
61
62         return network
63
64
65     @staticmethod
66     def connect(**kwargs):
67         """ Connects to the opensrf network 
68             Options:
69                 config_file
70                 config_context
71                 connect_cache
72                 resource
73         """
74
75         network = System.net_connect(**kwargs)
76
77         # load the domain-wide settings file
78         osrf.set.load(osrf.conf.get('domain'))
79
80         if kwargs.get('connect_cache'):
81             System.connect_cache()
82
83         return network
84
85
86     @staticmethod
87     def connect_cache():
88         ''' Initializes the cache connections '''
89         cache_servers = osrf.set.get('cache.global.servers.server')
90         if cache_servers:
91             if not isinstance(cache_servers, list):
92                 cache_servers = [cache_servers]
93             if not osrf.cache.CacheClient.get_client():
94                 osrf.cache.CacheClient.connect(cache_servers)
95
96     @staticmethod
97     def daemonize():
98         pid = os.fork() 
99         if pid == 0:
100             os.chdir('/')
101             os.setsid()
102             sys.stdin.close()
103             sys.stdout.close()
104             sys.stderr.close()
105         else:
106             os._exit(0)
107
108