[Opensrf-commits] r1175 - in trunk/src/python: . osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Dec 16 15:19:51 EST 2007


Author: dbs
Date: 2007-12-16 14:58:38 -0500 (Sun, 16 Dec 2007)
New Revision: 1175

Modified:
   trunk/src/python/osrf/ses.py
   trunk/src/python/srfsh.py
Log:
Add rudimentary locale support to srfsh.py


Modified: trunk/src/python/osrf/ses.py
===================================================================
--- trunk/src/python/osrf/ses.py	2007-12-16 19:55:41 UTC (rev 1174)
+++ trunk/src/python/osrf/ses.py	2007-12-16 19:58:38 UTC (rev 1175)
@@ -77,7 +77,7 @@
 class ClientSession(Session):
     """Client session object.  Use this to make server requests."""
 
-    def __init__(self, service):
+    def __init__(self, service, locale='en_US'):
         
         # call superclass constructor
         Session.__init__(self)
@@ -85,6 +85,9 @@
         # the remote service we want to make requests of
         self.service = service
 
+        # the locale we want requests to be returned in
+        self.locale = locale
+
         # find the remote service handle <router>@<domain>/<service>
         domain = osrf.conf.get('domains.domain', 0)
         router = osrf.conf.get('router_name')
@@ -125,7 +128,7 @@
             self.reset_remote_id()
 
         osrf.log.logDebug("Sending request %s -> %s " % (self.service, method))
-        req = Request(self, self.next_id, method, arr)
+        req = Request(self, self.next_id, method, arr, self.locale)
         self.requests[str(self.next_id)] = req
         self.next_id += 1
         req.send()

Modified: trunk/src/python/srfsh.py
===================================================================
--- trunk/src/python/srfsh.py	2007-12-16 19:55:41 UTC (rev 1174)
+++ trunk/src/python/srfsh.py	2007-12-16 19:58:38 UTC (rev 1175)
@@ -1,5 +1,27 @@
 #!/usr/bin/python
 # vim:et:ts=4
+"""
+srfsh.py - provides a basic shell for issuing OpenSRF requests
+
+  help
+    - show this menu
+
+  math_bench <count>
+    - runs <count> opensrf.math requests and reports the average time
+
+  request <service> <method> [<param1>, <param2>, ...]
+    - performs an opensrf request
+
+  set VAR=<value>
+    - sets an environment variable
+
+  Environment variables:
+    SRFSH_OUTPUT = pretty - print pretty JSON and key/value pairs for network objects
+                 = raw - print formatted JSON 
+
+    SRFSH_LOCALE = <locale> - request responses to be returned in locale <locale> if available
+"""
+
 import os, sys, time, readline, atexit, re
 import osrf.json
 import osrf.system
@@ -7,7 +29,6 @@
 import osrf.conf
 import osrf.log
 
-
 # -------------------------------------------------------------------
 # main listen loop
 # -------------------------------------------------------------------
@@ -61,7 +82,8 @@
 # Set env variables to control behavior
 # -------------------------------------------------------------------
 def handle_set(parts):
-    pattern = re.compile('(.*)=(.*)').match(parts[0])
+    cmd = "".join(parts)
+    pattern = re.compile('(.*)=(.*)').match(cmd)
     key = pattern.group(1)
     val = pattern.group(2)
     set_var(key, val)
@@ -78,33 +100,15 @@
 # Prints help info
 # -------------------------------------------------------------------
 def handle_help():
-    print """
-  help
-    - show this menu
+    print __doc__
 
-  math_bench <count>
-    - runs <count> opensrf.math requests and reports the average time
-
-  request <service> <method> [<param1>, <param2>, ...]
-    - performs an opensrf request
-
-  set VAR=<value>
-    - sets an environment variable
-
-  Environment variables:
-    SRFSH_OUTPUT = pretty - print pretty JSON and key/value pairs for network objects
-                 = raw - print formatted JSON 
-    """
-
-        
-
-
 # -------------------------------------------------------------------
 # performs an opensrf request
 # -------------------------------------------------------------------
 def handle_request(parts):
     service = parts.pop(0)
     method = parts.pop(0)
+    locale = __get_locale()
     jstr = '[%s]' % "".join(parts)
     params = None
 
@@ -114,7 +118,7 @@
         print "Error parsing JSON: %s" % jstr
         return
 
-    ses = osrf.ses.ClientSession(service)
+    ses = osrf.ses.ClientSession(service, locale=locale)
 
     start = time.time()
 
@@ -254,17 +258,42 @@
     if not get_var('SRFSH_OUTPUT'):
         set_var('SRFSH_OUTPUT', 'pretty')
 
+    # XXX Do we need to differ between LANG and LC_MESSAGES?
+    if not get_var('SRFSH_LOCALE'):
+        set_var('SRFSH_LOCALE', get_var('LC_ALL'))
 
 def set_var(key, val):
     os.environ[key] = val
 
-
 def get_var(key):
     try:
         return os.environ[key]
     except:
         return ''
     
+def __get_locale():
+    """
+    Return the defined locale for this srfsh session.
+
+    A locale in OpenSRF is currently defined as a [a-z]{2}-[A-Z]{2} pattern.
+    This function munges the LC_ALL setting to conform to that pattern; for
+    example, trimming en_CA.UTF-8 to en-CA.
+
+    >>> import srfsh
+    >>> srfsh.set_var('SRFSH_LOCALE', 'zz-ZZ')
+    >>> print __get_locale()
+    zz-ZZ
+    >>> srfsh.set_var('SRFSH_LOCALE', 'en_CA.UTF-8')
+    >>> print __get_locale()
+    en-CA
+    """
+
+    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)
+    return locale
     
 def print_green(string):
     sys.stdout.write("\033[01;32m")
@@ -279,14 +308,12 @@
     sys.stdout.flush()
 
 
+if __name__ == '__main__':
 
+    # Kick it off
+    set_vars()
+    setup_readline()
+    do_connect()
+    load_plugins()
+    do_loop()
 
-# Kick it off
-set_vars()
-setup_readline()
-do_connect()
-load_plugins()
-do_loop()
-
-
-



More information about the opensrf-commits mailing list