modifiable config files in compiled code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gaudetteje@gmail.com

    #1

    modifiable config files in compiled code?

    Hi All,

    I've been trying to come up with an elegant solution to this problem,
    but can't seem to think of anything better than my solution below.

    I have a Python program that needs to be converted into an executable.
    The problem is that I have a "config" template file that I've been
    using to modify initialization constants such as paths, global
    variables, structures, etc. Obviously, once I convert my code into an
    executable I can no longer read/write to my configuration file.

    My solution was to convert this config file into a standard ASCII text
    document and have Python parse it for user set variables. This doesn't
    seem like the most elegant of solutions; however, and I thought others
    must have implemented a better way than this.

    Anyone have a better solution for this problem?

    Jay

  • Tom Willis

    #2
    Re: modifiable config files in compiled code?

    10 Mar 2005 06:02:22 -0800, gaudetteje@gmai l.com <gaudetteje@gma il.com> wrote:[color=blue]
    > Hi All,
    >
    > I've been trying to come up with an elegant solution to this problem,
    > but can't seem to think of anything better than my solution below.
    >
    > I have a Python program that needs to be converted into an executable.
    > The problem is that I have a "config" template file that I've been
    > using to modify initialization constants such as paths, global
    > variables, structures, etc. Obviously, once I convert my code into an
    > executable I can no longer read/write to my configuration file.
    >
    > My solution was to convert this config file into a standard ASCII text
    > document and have Python parse it for user set variables. This doesn't
    > seem like the most elegant of solutions; however, and I thought others
    > must have implemented a better way than this.
    >
    > Anyone have a better solution for this problem?
    >
    > Jay
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]

    Don't know if you knew about this.....


    #example
    from ConfigParser import ConfigParser




    def getconfig():
    """
    initialize configuration settings
    """
    result = ConfigParser()
    result.read("co nfig.ini")
    return result



    def main():
    """
    runs the app
    """
    config = getconfig()


    ....
    dbsettings = {}

    for key,value in config.items("d b"):
    print "%s: %s" % (key,value)

    #etc...


    #-------------------config.ini--------------
    ;db settings
    [db]
    server = 127.0.0.1
    db = db
    user = user
    password = password
    #--------------------end config.ini------------

    There are other projects for dealing with config information but
    ConfigParser is included wth python I think.

    Hope that helps...




    --
    Thomas G. Willis

    Comment

    • Larry Bates

      #3
      Re: modifiable config files in compiled code?

      Note: my comments assume Windows distribution.

      Why do you think you can't you have a config file after you convert
      your program to an executable? I do it all the time and so do many
      other programs. The .INI config file is just a separate file that
      provides a good way to pass client supplied information into an
      executable (I'm assuming when you say executable you mean something
      that goes through a program like py2exe). You can also pass in
      information as arguments to your program or as I normally do some
      combination of the two.

      Steps I take:

      1) Run program through py2exe
      2) Build an Inno Installer script to gather all my program parts
      and my .INI and README.TEXT, etc. files together into a single
      setup.exe. This may include things like data files or other
      "extra" files that the program requires.
      3) Insert Inno Installer commands to make any install-time
      changes that are required to registry or my .INI file.
      4) Have Inno Installer compile everything together into setup.exe


      viola' you have a single file that can be installed on any computer
      that will run without Python installation.

      -Larry Bates


      gaudetteje@gmai l.com wrote:[color=blue]
      > Hi All,
      >
      > I've been trying to come up with an elegant solution to this problem,
      > but can't seem to think of anything better than my solution below.
      >
      > I have a Python program that needs to be converted into an executable.
      > The problem is that I have a "config" template file that I've been
      > using to modify initialization constants such as paths, global
      > variables, structures, etc. Obviously, once I convert my code into an
      > executable I can no longer read/write to my configuration file.
      >
      > My solution was to convert this config file into a standard ASCII text
      > document and have Python parse it for user set variables. This doesn't
      > seem like the most elegant of solutions; however, and I thought others
      > must have implemented a better way than this.
      >
      > Anyone have a better solution for this problem?
      >
      > Jay
      >[/color]

      Comment

      • Steve Holden

        #4
        Re: modifiable config files in compiled code?

        Larry Bates wrote:[color=blue]
        > Note: my comments assume Windows distribution.
        >
        > Why do you think you can't you have a config file after you convert
        > your program to an executable? I do it all the time and so do many[/color]

        I suspect the OP's config file is a Python module.

        regards
        Steve

        Comment

        • Tom Willis

          #5
          Re: modifiable config files in compiled code?

          On Thu, 10 Mar 2005 13:01:28 -0500, Steve Holden <steve@holdenwe b.com> wrote:[color=blue]
          > Larry Bates wrote:[color=green]
          > > Note: my comments assume Windows distribution.
          > >
          > > Why do you think you can't you have a config file after you convert
          > > your program to an executable? I do it all the time and so do many[/color]
          >
          > I suspect the OP's config file is a Python module.
          >
          > regards
          > Steve
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          >[/color]

          That's what was thinking when I posted my response.


          --
          Thomas G. Willis

          Comment

          • Ksenia Marasanova

            #6
            Re: modifiable config files in compiled code?

            You can also do:

            settings = {}
            execfile('/path/to/file/myconfig.conf', settings)

            myconfig.conf is a Python file. After this all variables from
            myconfig.conf are stored in settings dictionary.


            --
            Ksenia

            Comment

            • gaudetteje@gmail.com

              #7
              Re: modifiable config files in compiled code?

              Larry,

              I am using py2exe to package my application into an executable, but did
              not have the need for an installer such as Inno Installer. This is
              definately good info for future use, though. Thank you!

              Tom,

              No, I didn't know about the ConfigParser module and was exactly what I
              was trying to find. Py2exe will convert all *.py modules in my program
              and this was the reason I couldn't simply use an import command.

              I'm curious to know if the built-in execfile() command is supported by
              the compiled Python code. I'll have to check this out later.

              Thanks for your help, everyone!

              Jay

              Comment

              • gaudetteje@gmail.com

                #8
                Re: modifiable config files in compiled code?

                Since this utility will also be ported to the linux world, does anyone
                know what the linux/unix counterpart of a Windows .INI configuration
                file is?

                I suppose I could get away with using XML for my config files and avoid
                having two different tools altogether.

                Comment

                • Tom Willis

                  #9
                  Re: modifiable config files in compiled code?

                  ConfigParser works on linux I'm pretty sure. I just ran Ipython
                  imported it and loaded a config file.

                  I don't remember anything in the docs that said otherwise.


                  I would prefer an xml style config file myself. But I can get by with
                  and ini right now. The logging framework seems to me to be the
                  hairiest configurations and it's able to work in an ini format. If I
                  need anything fancier than that, I might consider doing it for a
                  living and getting someone to pay me to compe up with it. :)






                  On 10 Mar 2005 19:34:46 -0800, gaudetteje@gmai l.com
                  <gaudetteje@gma il.com> wrote:[color=blue]
                  > Since this utility will also be ported to the linux world, does anyone
                  > know what the linux/unix counterpart of a Windows .INI configuration
                  > file is?
                  >
                  > I suppose I could get away with using XML for my config files and avoid
                  > having two different tools altogether.
                  >
                  > --
                  > http://mail.python.org/mailman/listinfo/python-list
                  >[/color]


                  --
                  Thomas G. Willis

                  Comment

                  • Stephen Thorne

                    #10
                    Re: modifiable config files in compiled code?

                    On 10 Mar 2005 06:02:22 -0800, gaudetteje@gmai l.com
                    <gaudetteje@gma il.com> wrote:[color=blue]
                    > Hi All,
                    >
                    > I've been trying to come up with an elegant solution to this problem,
                    > but can't seem to think of anything better than my solution below.
                    >
                    > I have a Python program that needs to be converted into an executable.
                    > The problem is that I have a "config" template file that I've been
                    > using to modify initialization constants such as paths, global
                    > variables, structures, etc. Obviously, once I convert my code into an
                    > executable I can no longer read/write to my configuration file.
                    >
                    > My solution was to convert this config file into a standard ASCII text
                    > document and have Python parse it for user set variables. This doesn't
                    > seem like the most elegant of solutions; however, and I thought others
                    > must have implemented a better way than this.
                    >
                    > Anyone have a better solution for this problem?[/color]

                    Package config.py outside the zipfile containing all the python bytecode.

                    1) Remove config.pyc from dist\Library.zi p as the last thing you do in
                    your setup.py.

                    2) As the first thing you do in main.py (or whatever your main is), do:

                    import sys, os
                    try:
                    import config
                    except ImportError:
                    # We're in a py2exe, so we'll append an element to the (one element)
                    # sys.path which points to Library.zip, to the directory that contains
                    # Library.zip, allowing us to import config.py
                    sys.path.append (os.path.split( sys.path[0]))
                    import config

                    3) Put in your setup.py

                    setug( ....
                    data=[('.', ['config.py'])]# Package config.py seperately.
                    )


                    Regards,
                    Stephen Thorne

                    Comment

                    • Thomas Heller

                      #11
                      Re: modifiable config files in compiled code?

                      Stephen Thorne <stephen.thorne @gmail.com> writes:
                      [color=blue]
                      > On 10 Mar 2005 06:02:22 -0800, gaudetteje@gmai l.com
                      > <gaudetteje@gma il.com> wrote:[color=green]
                      >> Hi All,
                      >>
                      >> I've been trying to come up with an elegant solution to this problem,
                      >> but can't seem to think of anything better than my solution below.
                      >>
                      >> I have a Python program that needs to be converted into an executable.
                      >> The problem is that I have a "config" template file that I've been
                      >> using to modify initialization constants such as paths, global
                      >> variables, structures, etc. Obviously, once I convert my code into an
                      >> executable I can no longer read/write to my configuration file.
                      >>
                      >> My solution was to convert this config file into a standard ASCII text
                      >> document and have Python parse it for user set variables. This doesn't
                      >> seem like the most elegant of solutions; however, and I thought others
                      >> must have implemented a better way than this.
                      >>
                      >> Anyone have a better solution for this problem?[/color]
                      >
                      > Package config.py outside the zipfile containing all the python bytecode.
                      >
                      > 1) Remove config.pyc from dist\Library.zi p as the last thing you do in
                      > your setup.py.[/color]

                      You can save this step by using the exclude module option of py2exe.
                      [color=blue]
                      > 2) As the first thing you do in main.py (or whatever your main is), do:
                      >
                      > import sys, os
                      > try:
                      > import config
                      > except ImportError:
                      > # We're in a py2exe, so we'll append an element to the (one element)
                      > # sys.path which points to Library.zip, to the directory that contains
                      > # Library.zip, allowing us to import config.py
                      > sys.path.append (os.path.split( sys.path[0]))
                      > import config
                      >
                      > 3) Put in your setup.py
                      >
                      > setug( ....
                      > data=[('.', ['config.py'])]# Package config.py seperately.
                      > )
                      >
                      >
                      > Regards,
                      > Stephen Thorne[/color]

                      Comment

                      • Maarten Sneep

                        #12
                        Re: modifiable config files in compiled code?

                        In article <1110512086.762 856.158170@f14g 2000cwb.googleg roups.com>,
                        "gaudetteje@gma il.com" <gaudetteje@gma il.com> wrote:
                        [color=blue]
                        > Since this utility will also be ported to the linux world, does anyone
                        > know what the linux/unix counterpart of a Windows .INI configuration
                        > file is?[/color]

                        ConfigParser works on Linux and Mac as well. Configuration files on
                        Linux/Unix have a high 'roll your own' value: the format basically
                        depends on the needs of the program.
                        [color=blue]
                        > I suppose I could get away with using XML for my config files and avoid
                        > having two different tools altogether.[/color]

                        I think you could do worse than adopt the Apple .plist format. There
                        is already a pure python module for these:



                        The advantage of the plist module is that the type of the variables is
                        stored as well, and that you can store complex variables (lists,
                        dictionaries) without mangling.

                        Maarten

                        Comment

                        • Fuzzyman

                          #13
                          Re: modifiable config files in compiled code?


                          Tom Willis wrote:[color=blue]
                          > ConfigParser works on linux I'm pretty sure. I just ran Ipython
                          > imported it and loaded a config file.
                          >
                          > I don't remember anything in the docs that said otherwise.
                          >[/color]

                          ConfigParser is pure python - so it's cross platform.
                          ConfigObj is nicer though :-)
                          http://www.voidspace.org.uk/python/configobj.html

                          Regards,

                          Fuzzy
                          http://www.voidspace.org.uk/python/index.shtml

                          [snip..]

                          Comment

                          • gaudetteje@gmail.com

                            #14
                            Re: modifiable config files in compiled code?

                            There are a lot of good options here. Much more than I thought I had.
                            Since my program's configuration file will need to be read/writeable
                            from an existing Java Netbeans GUI, I'll most likely move the Python
                            module to an INI or XML document. The deciding factor will be
                            whichever the other programmer is most comfortable with, of course.

                            Thanks, everyone.

                            Comment

                            Working...