How to pass command line argument to imported file

Asked by Neo

I have one question regd passing argument to an imported file.
My env is like this. I have main.sikuli file which has test cases/suite and reporting configuration. Each test case, will directly use methods, which are imported from common.sikuli. The common.sikuli in turn imports images folder. I have different folders for this. If I want to run for EN i manually use import EN_Images in common.sikuli file. If I want to run for other lang, again will change here. But, I want to handle this from command line to run on which localization I wish to (just passing an arg)

The problem is that passed arg can only be used in main.sikuli file. How do I pass this to common.sikuli, so that I configure the localization I wish to run.

Thanks in adv.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Neo
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

I understand:
To run the EN language case for example, you manually edit the statement
import EN_Images

which is in commons.sikuli and then run your stuff.

If you want to run the test for another language, you again edit the statement accordingly and run the test.

If this is correct, so what can you do?

--1. auto-edit commons.sikuli
make a whatever script, that reads commons.sikuli/commons.py, changes the statement accordingly based on a command line parameter and saves the changed .py again (you can do this using a Sikuli script).
Make this the first step in your batch sequence before the main stuff is run.

--2. using a global variable
In your main module read a command line parameter that defines the language to use (e.g. DE) and store it this way as a global variable:
Settings.myLang = sys.argv[1]

this will save the given language string ("DE" in this case) in Sikuli's global Settings class.

In commons.sikuli change the import statement this way:
exec("import " + Settings.myLang + "_images")

Revision history for this message
Neo (simamhussain) said :
#2

Yes. you are correct.

I like the second approach. I didn't understand why did you declare global variable as Settings.myLang ?
Also, when I run the script it throws the following error. Looks like some import is missing.. ?

 exec("import " + Settings.myLang)
AttributeError: type object 'org.sikuli.script.Settings' has no attribute 'myLang'

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Sorry for misleading - one should always test suggestions ;-)

Using Sikuli's Settings class as a replacement for global variables (less to write and to take care for),only works within the scope of a module (main or imported), but there over all used functions.

In your case the solution is somewhat different:

In your main module you just evaluate the command line parameter to the name of the appropriate language module
e.g.
"DE_images"

In your commons.sikuli, add a statement like this:
currLang = "EN_images" # default setting

So the import statement now has to be:
exec("import " + currLang)

If you do nothing else, then the EN version would be run.

To change the language selection dynamically in main based on the given command line parameter, you have to add the following after the import commons.sikuli:
commons.currLang = currLangFromParameter # e.g. "DE_images"

which changes the value of currLang in commons.sikuli to what you want.

In this case finally the executed import would read
import DE_images

Revision history for this message
Neo (simamhussain) said :
#4

oh.. yaa. Thanks much. Its solved my issue.