Re: parse an environment file

Discussion in 'Python' started by Jason Friedman, Oct 1, 2012.

  1. > 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.
    Jason Friedman, Oct 1, 2012
    #1
    1. Advertising

  2. Jason Friedman

    Hans Mulder Guest

    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
    Hans Mulder, Oct 1, 2012
    #2
    1. Advertising

  3. Jason Friedman

    xDog Walker Guest

    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.
    xDog Walker, Oct 2, 2012
    #3
  4. 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.
    Ramchandra Apte, Oct 2, 2012
    #4
  5. 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.
    Ramchandra Apte, Oct 2, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Friedman

    parse an environment file

    Jason Friedman, Oct 1, 2012, in forum: Python
    Replies:
    3
    Views:
    249
    Alain Ketterlin
    Oct 1, 2012
  2. Dave Angel

    Re: parse an environment file

    Dave Angel, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    189
    Dave Angel
    Oct 1, 2012
  3. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    237
    Chris Angelico
    Oct 1, 2012
  4. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    0
    Views:
    238
    Chris Angelico
    Oct 1, 2012
  5. Chris Angelico

    Re: parse an environment file

    Chris Angelico, Oct 1, 2012, in forum: Python
    Replies:
    2
    Views:
    238
    88888 Dihedral
    Oct 1, 2012
Loading...

Share This Page