Several wsadmin commands return 'object-like' strings as responses. That is, they return strings which look like objects, but are just strings. Very annoying.
We wrote several helper methods in wsadminlib.py to convert these object-like strings into real python lists and dictionaries. The helpers may seem silly at first, but having real objects makes searching and extracting values from responses much easier. Here are two examples...
Example 1
Consider this string:
'[ [key0 value0] [key1 value1] [key2 value2] ]'
This is the format of the response string which you get from several wsadmin commands. To a human, it is visually recognizable as a set of keys and values. But to python, it is nothing. It is a string which is tedious to parse and search, and the code is confusing to duplicate inline in various functional methods. What to do?
The second time I encountered this format, I wrote a helper method in wsadminlib.py to abstract it.
Method propsToDictionary() accepts this type of string as input, and returns a python dictionary as output, where each key and value are elements of the dictionary.
To demonstrate, we first start wsadmin and load wsadminlib.py:
root@ding6:/tmp# /opt/WAS70/bin/wsadmin.sh -lang jython -host ding4 -port 8879
WASX7209I: Connected to process "dmgr" on node dmgr using SOAP connector; The type of process is: DeploymentManager
WASX7031I: For help, enter: "print Help.help()"
wsadmin>
wsadmin>execfile('/tmp/wsadminlib.py')
$Id: wsadminlib.py 104 2010-02-15 19:06:18Z ding $
wsadmin>
wsadmin>enableDebugMessages()
[2010-0709-1540-2100] enableDebugMessages Verbose trace messages are now enabled; future debug messages will now be printed.
wsadmin>
Now let's define an input string in the format of responses from several wsadmin commands, and convert to a python dictionary:
wsadmin>propString = '[ [key0 value0] [key1 value1] [key2 value2] ]'
wsadmin>
wsadmin>propDict = propsToDictionary(propString)
wsadmin>
wsadmin>print propDict
{'key2': 'value2', 'key1': 'value1', 'key0': 'value0'}
wsadmin>
It is really easy to call this method from your code and then extract desired values. Here we get the value for key1:
wsadmin>myValue1 = propDict['key1']
wsadmin>
wsadmin>print myValue1
value1
wsadmin>
Example 2
Another example is helper method stringListToList(). It takes a string which looks like a list as input:
'[element0 element1 element2]'
and returns a python list object containing each element as a string:
['element0', 'element1', 'element2']
It is much easier and natural to deal with a list of information in python list object, rather than in a string.
Availablilty
These and other helper methods are used internally within wsadminlib.py. You are able to make use of them from your own external methods as well.
Please look for these and similar helper methods in wsadminlib.py the next time you encounter an ugly 'object-like' string from wsadmin. The input/output syntax of each helper method is well-documented at the top of each method.
Friday Quiz
As a community-contribution project, wsadminlib.py is not perfect. As I was typing this blog entry, I discovered a second method in wsadminlib which seems to implements the same functionality as propsToDictionary(). Maybe I will deprecate one someday. Meanwhile, see if you can find it while you are savoring all the valuables in wsadminlib.py!