question

mandonov avatar image
mandonov asked mandonov posted

parsing a list of lists of variable length

Hi, 

 

I am trying to build a response map for a response with the following characteristics:

 

- it is of the form     0  { <key-value list 1>}  { <key-value list 2>}  { <key-value list 3>} …,  where the key-value list consists of key-value pairs separated by space.

- it contains a variable number of key-value lists, i.e. it may contain multiple key value lists, or none at all, but will always start with the 0.

- the response is always returned as a single line.

 

I'd like to extract the key-value pairs and store some of them in variables or return them as a more user-friendly response.

 

What is the best way to parse a response of this type? It would be very easy to parse this in Tcl, but I am struggling with building a response map in iTest. I tried block and pattern method without success. Can someone offer a suggestion or example?

 

Here is a sample of the response:

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 { {display_id 30510 status 1 owner_group contractor alarmed No name Untitled series_id {} access_rights_list 12486 rsvn_id 30510 topology_path {} movable No utc_end 1257194102 priority 0 haspermission Yes deadline {} owner divya utc_start 1257183302 status_text active} {display_id 30528 status 1 owner_group contractor alarmed No name PerfCase5-24Port series_id {} access_rights_list 12486 rsvn_id 30528 topology_path {/users/Divya/IPTC Regression - October 2009} movable No utc_end 1257218894 priority 0 haspermission Yes deadline {} owner divya utc_start 1257186494 status_text active}}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

Thanks!

 

Milen

 

 

iTestresponse map
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
KumarS avatar image
KumarS answered KumarS posted

This is actually a complex task. You have a list which is enclosed with "{" and "}" and some of the elements inside themselves are surrounded by "{" if they have "space" in it. This is how TCL will dump a list. So your list output is simply a dump of a TCL variable. Is it possible for you to simply change your TCL program so that it dumps this string in a format which will make your life easier to parse?

 

If you think it is easy to parse in Tcl, I would use that.

 

In iTest, the closest mapping technology which will do this will be "Pattern". But you will have to do the complex "regex" on your own.

 

Block mapping cannot be used because there does not seem to be proper "block" boundaries.

Table mapping cannot be used because there does not seem to be tabular structure.

 

We have been thinking of exposing hooks so that people can "plug-in" extra mapping technologies based on their needs. That will allow one to use custom mappers to map special responses like this.

 

 

6 comments
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mandonov avatar image mandonov commented ·

I actually ended up doing just that: parsing this directly in Tcl. It will be transparent to the user so it will work. 

 

Now I have another issue that would be nice to resolve:

 

I have a proc that prints via puts a response in the format 

key=value

key=value

#

 

key=value

key=value

#

 

key=value

key=value

 

 

 

When I store this response in a variable and return the variable as the return value of the procedure, I get

{key=value} {} {key=value} {} {#} {} {key=value} {} {key=value} {} {#} {} {key=value} {} {key=value}

 

Apparently the newlines are put in {} and the whole response is put in one line, which again would be very difficult to parse using pattern.

Is there a way to store the response in a variable as it is printed, i.e. preserving the newline characters so it lends itself to pattern-based response map parsing?

 

0 Likes 0 ·
KumarS avatar image KumarS mandonov commented ·

You should simply use the following syntax for returning:

 

return [response var]

 

instead of

 

return $var

 

This is because when you store response in a variable "var", we store not only the response - but also all the mapped structured data in the variable. That is why you can do things like [query var xpath_query] against a variable!

0 Likes 0 ·
mandonov avatar image mandonov KumarS commented ·

Actually, storing the response using the query stdout() did exactly what I wanted. The return value preserved the newlines in the output.  The first time around I had stored the response in a variable using the .* regex, which stripped the newlines and placed the response on one line. 

 

So if you want to preserve the format of the response when storing into a variable, use the stdout() query. 

 

Interestingly, when I did 

return [response var1] 

 

there was no longer a response in the call step for the procedure where I placed the return [response var1] step. Not sure if I had to configure any properties?

 

 

 

0 Likes 0 ·
KumarS avatar image KumarS mandonov commented ·

Looking at your post again, your response you have stored is actually entirely made up of "structured data". That is why in your case, return [response var1] will return empty string because textual part of response is empty. With query command, you extracting some data from the structured data and returning that data to the caller which will work in this case.

0 Likes 0 ·
mandonov avatar image mandonov KumarS commented ·

Thanks Kumar - this discussion has been helpful and I am not able to move forward with my project.

0 Likes 0 ·
mandonov avatar image mandonov mandonov commented ·

Oops - meant "I am NOW able".

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.