Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30604 > unrolled thread

Re: parse an environment file

Started byJason Friedman <jason@powerpull.net>
First post2012-10-01 08:12 -0600
Last post2012-10-02 09:16 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: parse an environment file Jason Friedman <jason@powerpull.net> - 2012-10-01 08:12 -0600
    Re: parse an environment file Hans Mulder <hansmu@xs4all.nl> - 2012-10-01 17:35 +0200
      Re: parse an environment file xDog Walker <thudfoo@gmail.com> - 2012-10-02 09:03 -0700
        Re: parse an environment file Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:16 -0700
        Re: parse an environment file Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:16 -0700

#30604 — Re: parse an environment file

FromJason Friedman <jason@powerpull.net>
Date2012-10-01 08:12 -0600
SubjectRe: parse an environment file
Message-ID<mailman.1703.1349100773.27098.python-list@python.org>
> 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.

[toc] | [next] | [standalone]


#30612

FromHans Mulder <hansmu@xs4all.nl>
Date2012-10-01 17:35 +0200
Message-ID<5069b829$0$6949$e4fe514c@news2.news.xs4all.nl>
In reply to#30604
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

[toc] | [prev] | [next] | [standalone]


#30638

FromxDog Walker <thudfoo@gmail.com>
Date2012-10-02 09:03 -0700
Message-ID<mailman.1729.1349193842.27098.python-list@python.org>
In reply to#30612
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.

[toc] | [prev] | [next] | [standalone]


#30642

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-10-02 09:16 -0700
Message-ID<e6ae0308-ea4e-4efd-b409-52708548f4be@googlegroups.com>
In reply to#30638
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.

[toc] | [prev] | [next] | [standalone]


#30643

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-10-02 09:16 -0700
Message-ID<mailman.1731.1349194573.27098.python-list@python.org>
In reply to#30638
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web