Question

User Parameters from file


Hello,

 

 

Is it possible to read user parameters from a text/xml file. I need to store user parameters in a file which it turn read by a workbench to set a connection info which will be used to connect to db.

 

 

Regards,

9 replies

Userlevel 2
Badge +17
Hi John,

 

 

1. WorkspaceRunner approach

 

Create another workspace. It reads parameter values from the text/xml file, and passes them to the main workspace through a WorkspaceRunner.

 

 

2. Scripted parameter approach

 

If the number of required parameter is just 1, define a scripted parameter which reads a parameter value from the file and returns the value, and then link the db connection parameter to the scripted parameter.

 

Otherwise, it'll be a little complicated as the followings.

 

 

1) Define a scripted Tcl parameter (e.g. PARAM1).

 

The script reads parameter values from the text file, puts them other than the first one as macros, and returns the first one.

 

-----

 

...

 

puts {MACRO __PARAM2 $value2}

 

puts {MACRO __PARAM3 $value3}

 

...

 

return $value1

 

-----

 

Scripted Tcl parameter can set macros at run-time. Python cannot do it.

 

Tcl xml package seems not to be installed in standard FME installation, so it's difficult to read parameter values from an XML document.

 

 

2) Define multiple scripted parameters to fetch and return the macro values seperately.

 

PARAM2

 

return $FME_MacroValues(__PARAM2)

 

 

PARAM3

 

return $FME_MacroValues(__PARAM3)

 

...

 

 

3) Link the DB connection parameters to the scripted parameters (PARAM1, PARAM2, PARAM3 ...).

 

 

Takashi
Thanks Takashi.

 

First approach is out of question as I have to publish it on FME server.

 

 

I will be having 3 parameter username, pwd and db_instance.  I am going for scripted param approach. But I'm no expert on TCL at all (hardly ever used it) So it would be great if you could take a look at a script I created and let me know where I am going wrong ot is there any better way do it.

 

---------------------

 

set infile [open "/tmp/anyFile.txt" r]

 

while {![eof $infile]} {

 

set part [split [gets $infile] " "]

 

set props([string trimright [lindex $part 0]]) [string trimleft [lindex $part 1]]

 

}

 

close $infile

 

set pwd $props(USERNAME)  #This is not working

 

set inst $props(INSTANCE)      #This is not working

 

 

#assign to macro. These will be used in other params

 

puts {MACRO __PARAM2 $pwd}

 

puts {MACRO __PARAM3 $inst}

 

 

return $props(PASSWORD) #this works

 

------------------------

 

 

John
Userlevel 2
Badge +17
How are the parameters written in the file?

 

Line by line?

 

-----

 

username

 

password

 

instance

 

-----

 

 

or all in one line?

 

-----

 

username password instance

 

-----
File is in the format

 

 

INSTANCE MYINSTANCE

 

USERNAME SCOTT

 

PASSWORD TIGER

 

 

john
Thanks Takashi. It's working.
Userlevel 2
Badge +17
Good to hear it works.

 

 

Regarding this part:

 

-----

 

set part [split [gets $infile] " "]

 

set props([string trimright [lindex $part 0]]) [string trimleft [lindex $part 1]]

 

-----

 

 

If you intend to resolve multiple spaces between tag and value using "string trim***" command, I think unexpected result could occur.

 

"split" command doesn't collapse consecutive white spaces. So if the input line is "USRENAME<space><space><space>MYNAME", the result would be:

 

part 0 = USERNAME

 

part 1 = <empty>

 

part 2 = <empty>

 

part 3 = MYNAME

 

 

When multiple spaces can occur between tag and value, a workaround is:

 

-----

 

set part [split [string trim [gets $infile]]]

 

set props([lindex $part 0]) [lindex $part [expr [llength $part] - 1]]

 

-----

 

 

Alternatively, you can also use "regexp" command. For example:

 

-----

 

regexp {([^\\s]+)\\s+([^\\s]+)} [gets $infile] m tag value

 

set props($tag) $value

 

-----

 

FYI.
Userlevel 2
Badge +17
This is also available.

 

-----

 

set part [split [string trim [gets $infile]]]

 

set props([lindex $part 0]) [lindex $part end]

 

-----
Badge
Hi,

 

This is under consideration already, so I will add the details of this thread so you are informed if and when it is implemented. It is targeted for FME2015, so we may be lucky and this is done sooner rather than later.

 

 

For your info, the reference number is PR#38675. If you contact support about this issue be sure to mention this PR number.

 

 

Regards

 

 

Mark

 

 

Mark Ireland

 

Product Evangelist

 

Safe Software Inc
Userlevel 2
Badge +17
Hi Mark,

 

 

Do you perhaps consider new parameter type that can read an external file to configure the parameter value at run-time?

 

If so, that's great!

 

 

Takashi

Reply