Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: parse an environment file

Reply
Thread Tools

Re: parse an environment file

 
 
Jason Friedman
Guest
Posts: n/a
 
      10-01-2012
> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are. How?


Thank you for the feedback. A crontab line of

* * * * * . /path/to/export_file && /path/to/script.py

does indeed work, but for various reasons this approach will not
always be available to me.

Let me restate my question. I have a file that looks like this:
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

I want this:
my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}

I can roll my own, but I'm thinking there is a module or existing code
that does this. I looked at the os and sys and configparse modules
but did not see it.
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      10-01-2012
On 1/10/12 16:12:50, Jason Friedman wrote:
>> I want my python 3.2.2 script, called via cron, to know what those
>> additional variables are. How?

>
> Thank you for the feedback. A crontab line of
>
> * * * * * . /path/to/export_file && /path/to/script.py
>
> does indeed work, but for various reasons this approach will not
> always be available to me.
>
> Let me restate my question. I have a file that looks like this:
> export VAR1=foo
> export VAR2=bar
> # Comment
> export VAR3=${VAR1}${VAR2}
>
> I want this:
> my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}
>
> I can roll my own, but I'm thinking there is a module or existing code
> that does this. I looked at the os and sys and configparse modules
> but did not see it.


One tactic is to write a wrapper script in shellese that sets the
variables and then runs your script. Something like:

#/bin/bash
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

# Read some more settings from a file
.. /path/to/file/with/more/exports

# Drum roll .....
/path/to/your/script.py


This allows you to copy-and-paste all sorts of weird and wonderful
shell syntax into your wrapper script.

AFAIK, there is no Python module that can read shell syntax.
You could translate all that shell syntax manually to Python,
but that may not be worth the effort.

Hope this helps,

-- HansM

 
Reply With Quote
 
 
 
 
xDog Walker
Guest
Posts: n/a
 
      10-02-2012
On Monday 2012 October 01 08:35, Hans Mulder wrote:
> AFAIK, there is no Python module that can read shell syntax.


The stdlib's shlex might be that module.

--
Yonder nor sorghum stenches shut ladle gulls stopper torque wet
strainers.

 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      10-02-2012
On Tuesday, 2 October 2012 21:34:04 UTC+5:30, xDog Walker wrote:
> On Monday 2012 October 01 08:35, Hans Mulder wrote:
>
> > AFAIK, there is no Python module that can read shell syntax.

>
>
>
> The stdlib's shlex might be that module.
>
>
>
> --
>
> Yonder nor sorghum stenches shut ladle gulls stopper torque wet
>
> strainers.


shlex can only split shell code into tokens.
 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      10-02-2012
On Tuesday, 2 October 2012 21:34:04 UTC+5:30, xDog Walker wrote:
> On Monday 2012 October 01 08:35, Hans Mulder wrote:
>
> > AFAIK, there is no Python module that can read shell syntax.

>
>
>
> The stdlib's shlex might be that module.
>
>
>
> --
>
> Yonder nor sorghum stenches shut ladle gulls stopper torque wet
>
> strainers.


shlex can only split shell code into tokens.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: parse an environment file Chris Angelico Python 2 10-01-2012 03:29 PM
Re: parse an environment file Chris Angelico Python 0 10-01-2012 02:21 PM
parse an environment file Jason Friedman Python 3 10-01-2012 10:16 AM
Re: parse an environment file Chris Angelico Python 0 10-01-2012 04:20 AM
Re: parse an environment file Dave Angel Python 0 10-01-2012 12:58 AM



Advertisments