Skip to main content
Question

User Parameters from file

  • Jul 14, 2014 11:23 AM UTC
  • 9 replies
  • 225 views

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,
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

9 replies

takashi
Celebrity
  • Jul 14, 2014 1:15 PM UTC
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

  • Author
  • Jul 15, 2014 4:35 AM UTC
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

takashi
Celebrity
  • Jul 15, 2014 6:08 AM UTC
How are the parameters written in the file?

 

Line by line?

 

-----

 

username

 

password

 

instance

 

-----

 

 

or all in one line?

 

-----

 

username password instance

 

-----

  • Author
  • Jul 15, 2014 8:56 AM UTC
File is in the format

 

 

INSTANCE MYINSTANCE

 

USERNAME SCOTT

 

PASSWORD TIGER

 

 

john

  • Author
  • Jul 15, 2014 9:54 AM UTC
Thanks Takashi. It's working.

takashi
Celebrity
  • Jul 15, 2014 1:03 PM UTC
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.

takashi
Celebrity
  • Jul 15, 2014 1:31 PM UTC
This is also available.

 

-----

 

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

 

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

 

-----

mark
Contributor
Forum|alt.badge.img+1
  • Contributor
  • Jul 15, 2014 9:13 PM UTC
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

takashi
Celebrity
  • Jul 16, 2014 12:03 AM UTC
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