Ideas for parsing this text?

Eric Wertman ewertman at gmail.com
Wed Apr 23 21:00:49 EDT 2008


I have a set of files with this kind of content (it's dumped from WebSphere):

[propertySet "[[resourceProperties "[[[description "This is a required
property. This is an actual database name, and its not the locally
catalogued database name. The Universal JDBC Driver does not rely on
information catalogued in the DB2 database directory."]
[name databaseName]
[required true]
[type java.lang.String]
[value DB2Foo]] [[description "The JDBC connectivity-type of a data
source. If you want to use a type 4 driver, set the value to 4. If you
want to use a type 2 driver, set the value to 2. Use of driverType 2
is not supported on WAS z/OS."]
[name driverType]
[required true]
[type java.lang.Integer]
[value 4]] [[description "The TCP/IP address or host name for the DRDA server."]
[name serverName]
[required false]
[type java.lang.String]
[value ServerFoo]] [[description "The TCP/IP port number where the
DRDA server resides."]
[name portNumber]
[required false]
[type java.lang.Integer]
[value 007]] [[description "The description of this datasource."]
[name description]
[required false]
[type java.lang.String]
[value []]] [[description "The DB2 trace level for logging to the
logWriter or trace file. Possible trace levels are: TRACE_NONE =
0,TRACE_CONNECTION_CALLS = 1,TRACE_STATEMENT_CALLS =
2,TRACE_RESULT_SET_CALLS = 4,TRACE_DRIVER_CONFIGURATION =
16,TRACE_CONNECTS = 32,TRACE_DRDA_FLOWS =
64,TRACE_RESULT_SET_META_DATA = 128,TRACE_PARAMETER_META_DATA =
256,TRACE_DIAGNOSTICS = 512,TRACE_SQLJ = 1024,TRACE_ALL = -1, ."]
[name traceLevel]
[required false]
[type java.lang.Integer]
[value []]] [[description "The trace file to store the trace output.
If you specify the trace file, the DB2 Jcc trace will be logged in
this trace file. If this property is not specified and the
WAS.database trace group is enabled, then both WebSphere trace and DB2
trace will be logged into the WebSphere trace file."]

I'm trying to figure out the best way to feed it all into
dictionaries, without having to know exactly what the contents of the
file are.  There are a number of things going on,  The nesting is
preserved in [] pairs, and in some cases in between double quotes.
There are also cases where double quotes are only there to preserve
spaces in a string though.  I managed to get what I needed in the
short term by just stripping the nesting all together, and flattening
out the key/value pairs, but I had to do some things that were
specific to the file contents to make it work.

Any ideas?  I was considering making a list of string combinations, like so:

junk = ['[[','"[',']]']

and just using re.sub to covert them into a single character that I
could start to do split() actions on.  There must be something else I
can do.. those brackets can't be a coincidence.  The output came from
a jython script.

Thanks!



More information about the Python-list mailing list