[Opensrf-commits] r1219 - in trunk/src/python: . osrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Jan 15 18:49:58 EST 2008
Author: erickson
Date: 2008-01-15 18:24:31 -0500 (Tue, 15 Jan 2008)
New Revision: 1219
Modified:
trunk/src/python/osrf/conf.py
trunk/src/python/osrf/net.py
trunk/src/python/osrf/net_obj.py
trunk/src/python/osrf/ses.py
trunk/src/python/osrf/set.py
trunk/src/python/osrf/system.py
trunk/src/python/srfsh.py
Log:
I started adding some basic bootstrap options looking ahead to server-side python.
This resulted in a small set of code cleanup including:
move from camelCase to lower_under for method names
move from global variables to class-level static variables
move from global functions to class-level static functions
Modified: trunk/src/python/osrf/conf.py
===================================================================
--- trunk/src/python/osrf/conf.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/conf.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -30,7 +30,7 @@
self.data = {}
#def parseConfig(self,file=None):
- def parseConfig(self):
+ def parse_config(self):
self.data = osrf.xml_obj.xml_file_to_object(self.file)
Config.config = self
Modified: trunk/src/python/osrf/net.py
===================================================================
--- trunk/src/python/osrf/net.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/net.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -38,6 +38,12 @@
""" Returns the thread-specific network connection handle."""
return THREAD_SESSIONS.get(threading.currentThread().getName())
+def clear_network_handle():
+ ''' Disconnects the thread-specific handle and discards it '''
+ handle = THREAD_SESSIONS.get(threading.currentThread().getName())
+ if handle:
+ handle.disconnect()
+ del THREAD_SESSIONS[threading.currentThread().getName()]
class NetworkMessage(object):
"""Network message
@@ -62,23 +68,32 @@
else:
self.sender = message.get_from().as_utf8()
else:
- if args.has_key('sender'):
- self.sender = args['sender']
- if args.has_key('recipient'):
- self.recipient = args['recipient']
- if args.has_key('body'):
- self.body = args['body']
- if args.has_key('thread'):
- self.thread = args['thread']
+ self.sender = args.get('sender')
+ self.recipient = args.get('recipient')
+ self.body = args.get('body')
+ self.thread = args.get('thread')
+ self.router_command = args.get('router_command')
+ def make_xmpp_msg(self):
+ ''' Creates a pyxmpp.message.Message and adds custom attributes '''
+
+ msg = Message(None, None, self.recipient, None, None, None, \
+ self.body, self.thread)
+ if self.router_command:
+ msg.xmlnode.newProp('router_command', self.router_command)
+ return msg
+
+ def to_xml(self):
+ ''' Turns this message into XML '''
+ return self.make_xmpp_msg().serialize()
+
+
class Network(JabberClient):
def __init__(self, **args):
self.isconnected = False
# Create a unique jabber resource
- resource = 'python'
- if args.has_key('resource'):
- resource = args['resource']
+ resource = args.get('resource') or 'python_client'
resource += '_' + gethostname() + ':' + str(os.getpid()) + '_' + \
threading.currentThread().getName().lower()
self.jid = JID(args['username'], args['host'], resource)
@@ -115,10 +130,8 @@
def send(self, message):
"""Sends the provided network message."""
- osrf.log.log_internal("jabber sending to %s: %s" % \
- (message.recipient, message.body))
- msg = Message(None, None, message.recipient, None, None, None, \
- message.body, message.thread)
+ osrf.log.log_internal("jabber sending to %s: %s" % (message.recipient, message.body))
+ msg = message.make_xmpp_msg()
self.stream.send(msg)
def message_received(self, stanza):
Modified: trunk/src/python/osrf/net_obj.py
===================================================================
--- trunk/src/python/osrf/net_obj.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/net_obj.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -7,8 +7,6 @@
# Define the global network-class registry
# -----------------------------------------------------------
-# Global object registry
-OBJECT_REGISTRY = {}
class NetworkRegistry(object):
''' Network-serializable objects must be registered. The class
@@ -16,20 +14,19 @@
of field names (keys).
'''
+ # Global object registry
+ registry = {}
+
def __init__(self, hint, keys, protocol):
- global OBJECT_REGISTRY
self.hint = hint
self.keys = keys
self.protocol = protocol
- OBJECT_REGISTRY[hint] = self
+ NetworkRegistry.registry[hint] = self
+ @staticmethod
def get_registry(hint):
- global OBJECT_REGISTRY
- return OBJECT_REGISTRY.get(hint)
+ return NetworkRegistry.registry.get(hint)
- get_registry = staticmethod(get_registry)
-
-
# -----------------------------------------------------------
# Define the base class for all network-serializable objects
# -----------------------------------------------------------
@@ -74,12 +71,10 @@
def get_field(self, field):
return self._data.get(field)
- def get_registry(cls):
+ def get_registry(self):
''' Returns the registry object for this registered class '''
- return cls.registry
- get_registry = classmethod(get_registry)
+ return self.__class__.registry
-
def new_object_from_hint(hint):
''' Given a hint, this will create a new object of that
type and return it. If this hint is not registered,
Modified: trunk/src/python/osrf/ses.py
===================================================================
--- trunk/src/python/osrf/ses.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/ses.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -192,7 +192,7 @@
for the request associated with the message's ID."""
osrf.log.log_debug("pushing %s" % message.payload())
try:
- self.find_request(message.threadTrace()).pushResponse(message.payload())
+ self.find_request(message.threadTrace()).push_response(message.payload())
except Exception, e:
osrf.log.log_warn("pushing respond to non-existent request %s : %s" % (message.threadTrace(), e))
@@ -204,8 +204,19 @@
osrf.log.log_debug('find_request(): non-existent request %s' % str(rid))
return None
+ @staticmethod
+ def atomic_request(service, method, *args):
+ ses = ClientSession(service)
+ req = ses.request2(method, list(args))
+ resp = req.recv()
+ data = resp.content()
+ req.cleanup()
+ ses.cleanup()
+ return data
+
+
class Request(object):
"""Represents a single OpenSRF request.
A request is made and any resulting respones are
@@ -286,7 +297,7 @@
return None
- def pushResponse(self, content):
+ def push_response(self, content):
"""Pushes a method response onto this requests response queue."""
self.queue.append(content)
@@ -306,14 +317,6 @@
pass
-def AtomicRequest(service, method, *args):
- ses = ClientSession(service)
- req = ses.request2(method, list(args))
- resp = req.recv()
- data = resp.content()
- req.cleanup()
- ses.cleanup()
- return data
Modified: trunk/src/python/osrf/set.py
===================================================================
--- trunk/src/python/osrf/set.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/set.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -14,8 +14,7 @@
# -----------------------------------------------------------------------
from osrf.const import OSRF_APP_SETTINGS, OSRF_METHOD_GET_HOST_CONFIG
-import osrf.ex
-import osrf.net_obj
+import osrf.ex, osrf.net_obj, osrf.ses
# global settings config object
__config = None
@@ -31,10 +30,7 @@
def load(hostname):
global __config
- from osrf.system import connect
- from osrf.ses import ClientSession
-
- ses = ClientSession(OSRF_APP_SETTINGS)
+ ses = osrf.ses.ClientSession(OSRF_APP_SETTINGS)
req = ses.request(OSRF_METHOD_GET_HOST_CONFIG, hostname)
resp = req.recv(timeout=30)
__config = resp.content()
Modified: trunk/src/python/osrf/system.py
===================================================================
--- trunk/src/python/osrf/system.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/osrf/system.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -13,59 +13,95 @@
# GNU General Public License for more details.
# -----------------------------------------------------------------------
-from osrf.conf import Config, get, get_no_ex
+import osrf.conf
from osrf.net import Network, set_network_handle, get_network_handle
import osrf.stack, osrf.log, osrf.set, osrf.cache
-import sys
+import sys, os
+class System(object):
-def connect(configFile, configContext, init_cache=False):
- """ Connects to the opensrf network
- @param configFile The OpenSRF config
- @param configContext The path to the configuration element in the XML config.
- e.g. 'config.opensrf'
- @param init_cache If true, connect to the cache servers
- """
+ config_file = None
+ config_context = None
- if get_network_handle():
- ''' This thread already has a handle '''
- return
+ @staticmethod
+ def net_connect(**kwargs):
+ if get_network_handle():
+ ''' This thread already has a handle '''
+ return
- # parse the config file
- configParser = Config(configFile, configContext)
- configParser.parseConfig()
-
- # set up logging
- osrf.log.initialize(
- osrf.conf.get('loglevel'),
- osrf.conf.get_no_ex('syslog'),
- osrf.conf.get_no_ex('logfile'))
+ config_file = kwargs.get('config_file') or System.config_file
+ config_context = kwargs.get('config_context') or System.config_context
- # connect to the opensrf network
- network = Network(
- host = osrf.conf.get('domains.domain'),
- port = osrf.conf.get('port'),
- username = osrf.conf.get('username'),
- password = osrf.conf.get('passwd'))
+ # store the last config file info for later
+ System.config_file = config_file
+ System.config_context = config_context
- network.set_receive_callback(osrf.stack.push)
- osrf.net.set_network_handle(network)
- network.connect()
+ # parse the config file
+ config_parser = osrf.conf.Config(config_file, config_context)
+ config_parser.parse_config()
+
+ # set up logging
+ osrf.log.initialize(
+ osrf.conf.get('loglevel'),
+ osrf.conf.get_no_ex('syslog'),
+ osrf.conf.get_no_ex('logfile'))
- # load the domain-wide settings file
- osrf.set.load(osrf.conf.get('domains.domain'))
+ # connect to the opensrf network
+ network = Network(
+ host = osrf.conf.get('domains.domain'),
+ port = osrf.conf.get('port'),
+ username = osrf.conf.get('username'),
+ password = osrf.conf.get('passwd'),
+ resource = kwargs.get('resource'))
- if init_cache:
- connect_cache()
+ network.set_receive_callback(osrf.stack.push)
+ osrf.net.set_network_handle(network)
+ network.connect()
+ return network
-def connect_cache():
- ''' Initializes the cache connections '''
- cache_servers = osrf.set.get('cache.global.servers.server')
- if cache_servers:
- if not isinstance(cache_servers, list):
- cache_servers = [cache_servers]
- if not osrf.cache.CacheClient.get_client():
- osrf.cache.CacheClient.connect(cache_servers)
+ @staticmethod
+ def connect(**kwargs):
+ """ Connects to the opensrf network
+ Options:
+ config_file
+ config_context
+ connect_cache
+ resource
+ """
+ network = System.net_connect(**kwargs)
+
+ # load the domain-wide settings file
+ osrf.set.load(osrf.conf.get('domains.domain'))
+
+ if kwargs.get('connect_cache'):
+ System.connect_cache()
+
+ return network
+
+
+ @staticmethod
+ def connect_cache():
+ ''' Initializes the cache connections '''
+ cache_servers = osrf.set.get('cache.global.servers.server')
+ if cache_servers:
+ if not isinstance(cache_servers, list):
+ cache_servers = [cache_servers]
+ if not osrf.cache.CacheClient.get_client():
+ osrf.cache.CacheClient.connect(cache_servers)
+
+ @staticmethod
+ def daemonize():
+ pid = os.fork()
+ if pid == 0:
+ os.chdir('/')
+ os.setsid()
+ sys.stdin.close()
+ sys.stdout.close()
+ sys.stderr.close()
+ else:
+ os._exit(0)
+
+
Modified: trunk/src/python/srfsh.py
===================================================================
--- trunk/src/python/srfsh.py 2008-01-15 18:02:41 UTC (rev 1218)
+++ trunk/src/python/srfsh.py 2008-01-15 23:24:31 UTC (rev 1219)
@@ -223,7 +223,7 @@
def do_connect():
file = os.path.join(get_var('HOME'), ".srfsh.xml")
print_green("Connecting to opensrf...")
- osrf.system.connect(file, 'srfsh')
+ osrf.system.System.connect(config_file=file, config_context='srfsh')
print_red('OK\n')
def load_plugins():
@@ -247,7 +247,7 @@
print_green("Loading module %s..." % name)
try:
- string = 'from %s import %s\n%s()' % (name, init, init)
+ string = 'import %s\n%s.%s()' % (name, name, init)
exec(string)
print_red('OK\n')
@@ -266,10 +266,7 @@
os.environ[key] = val
def get_var(key):
- try:
- return os.environ[key]
- except:
- return ''
+ return os.environ.get(key, '')
def __get_locale():
"""
@@ -289,10 +286,14 @@
"""
env_locale = get_var('SRFSH_LOCALE')
- pattern = re.compile(r'^\s*([a-z]+)[^a-zA-Z]([A-Z]+)').search(env_locale)
- lang = pattern.group(1)
- region = pattern.group(2)
- locale = "%s-%s" % (lang, region)
+ if env_locale:
+ pattern = re.compile(r'^\s*([a-z]+)[^a-zA-Z]([A-Z]+)').search(env_locale)
+ lang = pattern.group(1)
+ region = pattern.group(2)
+ locale = "%s-%s" % (lang, region)
+ else:
+ locale = 'en-US'
+
return locale
def print_green(string):
More information about the opensrf-commits
mailing list