From 4bd15f097dad75745b3da10267f0c47844ccaeb3 Mon Sep 17 00:00:00 2001 From: dbs Date: Sun, 16 Dec 2007 21:06:49 +0000 Subject: [PATCH 1/1] Continue the march towards a pedantic 1.0 python API. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1178 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/osrf/cache.py | 2 +- src/python/osrf/gateway.py | 4 ++-- src/python/osrf/http_translator.py | 11 ++++++----- src/python/osrf/log.py | 26 ++++++++++++++------------ src/python/osrf/net.py | 2 +- src/python/osrf/net_obj.py | 14 ++++++++------ src/python/osrf/ses.py | 20 ++++++++++---------- src/python/osrf/stack.py | 8 ++++---- 8 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/python/osrf/cache.py b/src/python/osrf/cache.py index 754a4fd..00fb831 100644 --- a/src/python/osrf/cache.py +++ b/src/python/osrf/cache.py @@ -53,7 +53,7 @@ class CacheClient(object): @staticmethod def connect(svrs): global _client - osrf.log.logDebug("cache: connecting to servers %s" % str(svrs)) + osrf.log.log_debug("cache: connecting to servers %s" % str(svrs)) _client = memcache.Client(svrs, debug=1) diff --git a/src/python/osrf/gateway.py b/src/python/osrf/gateway.py index cca2a18..11776a4 100644 --- a/src/python/osrf/gateway.py +++ b/src/python/osrf/gateway.py @@ -2,7 +2,7 @@ from xml.dom import minidom from xml.sax import handler, make_parser, saxutils from osrf.json import to_object from osrf.net_obj import NetworkObject, new_object_from_hint -from osrf.log import logError +import osrf.log import urllib, urllib2, sys, re defaultHost = None @@ -95,7 +95,7 @@ class XMLGatewayRequest(GatewayRequest): try: parser.parse(response) except Exception, e: - logError('Error parsing gateway XML: %s' % unicode(e)) + osrf.log.log_error('Error parsing gateway XML: %s' % unicode(e)) return None return handler.getResult() diff --git a/src/python/osrf/http_translator.py b/src/python/osrf/http_translator.py index a53befd..5c954be 100644 --- a/src/python/osrf/http_translator.py +++ b/src/python/osrf/http_translator.py @@ -7,8 +7,9 @@ import osrf.json import osrf.conf import osrf.set import sys -from osrf.const import * -from osrf.net import get_network_handle +from osrf.const import OSRF_MESSAGE_TYPE_DISCONNECT, OSRF_STATUS_CONTINUE, \ + OSRF_STATUS_TIMEOUT, OSRF_MESSAGE_TYPE_STATUS, +import osrf.net import osrf.log @@ -141,7 +142,7 @@ class HTTPTranslator(object): self.handle.send(net_msg) if self.disconnect_only: - osrf.log.logDebug("exiting early on DISCONNECT") + osrf.log.log_debug("exiting early on DISCONNECT") return apache.OK first_write = True @@ -228,7 +229,7 @@ class HTTPTranslator(object): self.cache.put(self.thread, \ {'ip':self.remote_host, 'jid': net_msg.sender}, CACHE_TIME) - osrf.log.logDebug("caching session [%s] for host [%s] and server " + osrf.log.log_debug("caching session [%s] for host [%s] and server " " drone [%s]" % (self.thread, self.remote_host, net_msg.sender)) @@ -248,7 +249,7 @@ class HTTPTranslator(object): code = int(last_msg.payload().statusCode()) if code == OSRF_STATUS_TIMEOUT: - osrf.log.logDebug("removing cached session [%s] and " + osrf.log.log_debug("removing cached session [%s] and " "dropping TIMEOUT message" % net_msg.thread) self.cache.delete(net_msg.thread) return False diff --git a/src/python/osrf/log.py b/src/python/osrf/log.py index 5a3a626..0550523 100644 --- a/src/python/osrf/log.py +++ b/src/python/osrf/log.py @@ -14,7 +14,9 @@ # ----------------------------------------------------------------------- import traceback, sys, os, re, threading -from osrf.const import * +from osrf.const import OSRF_LOG_DEBUG, OSRF_LOG_ERR, OSRF_LOG_INFO, \ + OSRF_LOG_INTERNAL, OSRF_LOG_TYPE_FILE, OSRF_LOG_TYPE_STDERR, \ + OSRF_LOG_TYPE_SYSLOG, OSRF_LOG_WARN LOG_SEMAPHORE = threading.BoundedSemaphore(value=1) @@ -49,16 +51,16 @@ def initialize(level, facility=None, logfile=None): # ----------------------------------------------------------------------- # Define wrapper functions for the log levels # ----------------------------------------------------------------------- -def log_internal(s): - __log(OSRF_LOG_INTERNAL, s) -def logDebug(s): - __log(OSRF_LOG_DEBUG, s) -def log_info(s): - __log(OSRF_LOG_INFO, s) -def log_warn(s): - __log(OSRF_LOG_WARN, s) -def logError(s): - __log(OSRF_LOG_ERR, s) +def log_internal(debug_str): + __log(OSRF_LOG_INTERNAL, debug_str) +def log_debug(debug_str): + __log(OSRF_LOG_DEBUG, debug_str) +def log_info(debug_str): + __log(OSRF_LOG_INFO, debug_str) +def log_warn(debug_str): + __log(OSRF_LOG_WARN, debug_str) +def log_error(debug_str): + __log(OSRF_LOG_ERR, debug_str) def __log(level, msg): """Builds the log message and passes the message off to the logger.""" @@ -120,7 +122,7 @@ def __log_syslog(level, msg): def __log_file(msg): ''' Logs the message to a file. ''' - global LOG_FILE, LOG_TYPE + global LOG_TYPE logfile = None try: diff --git a/src/python/osrf/net.py b/src/python/osrf/net.py index b636847..3169738 100644 --- a/src/python/osrf/net.py +++ b/src/python/osrf/net.py @@ -83,7 +83,7 @@ class Network(JabberClient): threading.currentThread().getName().lower() self.jid = JID(args['username'], args['host'], resource) - osrf.log.logDebug("initializing network with JID %s and host=%s, " + osrf.log.log_debug("initializing network with JID %s and host=%s, " "port=%s, username=%s" % (self.jid.as_utf8(), args['host'], \ args['port'], args['username'])) diff --git a/src/python/osrf/net_obj.py b/src/python/osrf/net_obj.py index 07e3cb0..ce1e798 100644 --- a/src/python/osrf/net_obj.py +++ b/src/python/osrf/net_obj.py @@ -54,12 +54,14 @@ class NetworkObject(object): of data, cycle through and load the data ''' self._data = {} - if len(data) == 0: return + if len(data) == 0: + return reg = self.get_registry() if reg.protocol == 'array': for entry in range(len(reg.keys)): - if len(data) > entry: break + if len(data) > entry: + break self.set_field(reg.keys[entry], data[entry]) def get_data(self): @@ -89,7 +91,7 @@ def new_object_from_hint(hint): except AttributeError: return NetworkObject.__unknown() -def __makeNetworkAccessor(cls, key): +def __make_network_accessor(cls, key): ''' Creates and accessor/mutator method for the given class. 'key' is the name the method will have and represents the field on the object whose data we are accessing ''' @@ -100,7 +102,7 @@ def __makeNetworkAccessor(cls, key): setattr(cls, key, accessor) -def NetworkRegisterHint(hint, keys, type='hash'): +def register_hint(hint, keys, type='hash'): ''' Registers a new network-serializable object class. 'hint' is the class hint @@ -122,7 +124,7 @@ def NetworkRegisterHint(hint, keys, type='hash'): # assign an accessor/mutator for each field on the object for k in keys: - __makeNetworkAccessor(cls, k) + __make_network_accessor(cls, k) # attach our new class to the NetworkObject # class so others can access it @@ -133,7 +135,7 @@ def NetworkRegisterHint(hint, keys, type='hash'): # create a unknown object to handle unregistred types -NetworkRegisterHint('__unknown', [], 'hash') +register_hint('__unknown', [], 'hash') # ------------------------------------------------------------------- # Define the custom object parsing behavior diff --git a/src/python/osrf/ses.py b/src/python/osrf/ses.py index 8933b6b..f8b2c21 100644 --- a/src/python/osrf/ses.py +++ b/src/python/osrf/ses.py @@ -29,11 +29,11 @@ import random, os, time, threading # ----------------------------------------------------------------------- # Go ahead and register the common network objects # ----------------------------------------------------------------------- -osrf.net_obj.NetworkRegisterHint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash') -osrf.net_obj.NetworkRegisterHint('osrfMethod', ['method', 'params'], 'hash') -osrf.net_obj.NetworkRegisterHint('osrfResult', ['status', 'statusCode', 'content'], 'hash') -osrf.net_obj.NetworkRegisterHint('osrfConnectStatus', ['status', 'statusCode'], 'hash') -osrf.net_obj.NetworkRegisterHint('osrfMethodException', ['status', 'statusCode'], 'hash') +osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash') +osrf.net_obj.register_hint('osrfMethod', ['method', 'params'], 'hash') +osrf.net_obj.register_hint('osrfResult', ['status', 'statusCode', 'content'], 'hash') +osrf.net_obj.register_hint('osrfConnectStatus', ['status', 'statusCode'], 'hash') +osrf.net_obj.register_hint('osrfMethodException', ['status', 'statusCode'], 'hash') class Session(object): @@ -127,7 +127,7 @@ class ClientSession(Session): if self.state != OSRF_APP_SESSION_CONNECTED: self.reset_remote_id() - osrf.log.logDebug("Sending request %s -> %s " % (self.service, method)) + osrf.log.log_debug("Sending request %s -> %s " % (self.service, method)) req = Request(self, self.next_id, method, arr, self.locale) self.requests[str(self.next_id)] = req self.next_id += 1 @@ -190,7 +190,7 @@ class ClientSession(Session): def push_response_queue(self, message): """Pushes the message payload onto the response queue for the request associated with the message's ID.""" - osrf.log.logDebug("pushing %s" % message.payload()) + osrf.log.log_debug("pushing %s" % message.payload()) try: self.find_request(message.threadTrace()).pushResponse(message.payload()) except Exception, e: @@ -201,7 +201,7 @@ class ClientSession(Session): try: return self.requests[str(rid)] except KeyError: - osrf.log.logDebug('find_request(): non-existent request %s' % str(rid)) + osrf.log.log_debug('find_request(): non-existent request %s' % str(rid)) return None @@ -269,13 +269,13 @@ class Request(object): if len(self.queue) > 0: if not self.first_response_time: self.first_response_time = now - osrf.log.logDebug("time elapsed before first response: %f" \ + osrf.log.log_debug("time elapsed before first response: %f" \ % (self.first_response_time - self.send_time)) if self.complete: if not self.complete_time: self.complete_time = now - osrf.log.logDebug("time elapsed before complete: %f" \ + osrf.log.log_debug("time elapsed before complete: %f" \ % (self.complete_time - self.send_time)) # ----------------------------------------------------------------- diff --git a/src/python/osrf/stack.py b/src/python/osrf/stack.py index 23fbf7e..e3da3d9 100644 --- a/src/python/osrf/stack.py +++ b/src/python/osrf/stack.py @@ -29,7 +29,7 @@ def push(net_msg): if not ses: # This is an incoming request from a client, create a new server session - osrf.log.logError("server-side sessions don't exist yet") + osrf.log.log_error("server-side sessions don't exist yet") ses.set_remote_id(net_msg.sender) @@ -75,7 +75,7 @@ def handle_message(session, message): if status_code == OSRF_STATUS_OK: # We have connected successfully - osrf.log.logDebug("Successfully connected to " + session.service) + osrf.log.log_debug("Successfully connected to " + session.service) session.state = OSRF_APP_SESSION_CONNECTED return @@ -85,12 +85,12 @@ def handle_message(session, message): return if status_code == OSRF_STATUS_TIMEOUT: - osrf.log.logDebug("The server did not receive a request from us in time...") + osrf.log.log_debug("The server did not receive a request from us in time...") session.state = OSRF_APP_SESSION_DISCONNECTED return if status_code == OSRF_STATUS_NOTFOUND: - osrf.log.logError("Requested method was not found on the server: %s" % status_text) + osrf.log.log_error("Requested method was not found on the server: %s" % status_text) session.state = OSRF_APP_SESSION_DISCONNECTED raise osrf.ex.OSRFServiceException(status_text) -- 2.43.2