Use the variables in a python script.

Hi,

I have a defined some variables in a configuration file and I want to use those variables in a python script. For example, I have the following variables in a configuration file.

[test_ini]
Region = North
Country = USA

[ptfm_dgnst]
CONN_FL=`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`
CONN=`$HOME/opt/bin/decrypt $CONN_FL`

Now I want to execute those variables whenever the configuration file is being called from python script and print the value of CONN variable using the python script.

I am new to python and appreciate if you could help me fix the following script I wrote.

import os
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('pltfm.ini')
conn_str=os.system("echo $CONN")
print conn_str

Using Python 2.7 on Ubuntu 16.04 (Xenial) - What is your system?

Does this help?

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from ConfigParser import SafeConfigParser
>>> parser = SafeConfigParser()
>>> parser.read('pltfm.ini')
['pltfm.ini']
>>> help(parser)

>>> parser.sections()
['test_ini', 'ptfm_dgnst']
>>> parser.items('test_ini')
[('region', 'North'), ('country', 'USA')]
>>> parser.items('ptfm_dgnst')
[('conn_fl', "`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`"), 
('conn', '`$HOME/opt/bin/decrypt $CONN_FL`')]
>>> ptfm_dgnst = dict(parser.items('ptfm_dgnst'))
>>> conn_str = ptfm_dgnst['conn']
>>> print conn_str
`$HOME/opt/bin/decrypt $CONN_FL`
>>> 

As you can see I am using the Python CLI - it's so much easier for testing snippets of code. Note the use of the help function - it shows the equivalent of a man page of Python classes and their methods. I don't know what you were trying to achieve with

conn_str=os.system("echo $CONN")

but I hope the above helps you with achieving that.

A quick look at the statements I typed in:
parser.sections() lists the section of the ini file.
parser.items('test_ini') lists the section test_ini as a list of tuples; each tuple a name/value pair.
ptfm_dgnst = dict(parser.items('ptfm_dgnst')) creates a dictionary (simplistically Python's hash table) so you can now access the values through their names.
conn_str = ptfm_dgnst['conn'] does what I think you were trying to do with the conn_str = os.system() statement.

I hope this helps.

Andrew

Thanks for your time Andrew.

I am working on Python 2.6.9 (unknown, Aug 5 2016, 11:15:31)

Basically I want to execute the following statements in sequence. It can be done inside the python code but I prefer to have these values defined on the pltfm.ini file and then let python execute these statements and store the output in CONN variable.

CONN_FL=`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`
CONN=`$HOME/opt/bin/decrypt $CONN_FL`

I know how to handle this in ksh or bash but I am new to Python so I am looking for assistance here. Thank you

Look at using the following:

import shlex
import subprocess
conn_fl = ptfm_dgnst['conn_fl']
subprocess.call(shlex.split(conn_fl))

Not checked. You will need to read up on shlex and subprocess to get them to work as you would expect. Note that the "proper" way of making system calls in Python is now through the use of subprocess .

Have fun with that :slight_smile:

Andrew

1 Like

So you want to run shell from python?

Yes that's pretty much what I would like to do.