ConfigParser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manlio Perillo

    #1

    ConfigParser

    Regards.

    Since sections in CongiParser files are delimited by [ and ], why
    there is not an escape (and unescape) function for escaping
    &, [, and ] characters to &, [ and ] ?




    Thanks Manlio Perillo
  • Ivo Woltring

    #2
    Re: ConfigParser

    On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
    <NOmanlio_peril loSPAM@libero.i t> wrote:
    [color=blue]
    >Regards.
    >
    >Since sections in CongiParser files are delimited by [ and ], why
    >there is not an escape (and unescape) function for escaping
    >&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
    >
    >
    >
    >
    >Thanks Manlio Perillo[/color]

    just subclass from ConfigParser and add the functionality youself

    somthing like:
    === cut here ===
    from ConfigParser import ConfigParser
    class MyConfigParser( ConfigParser):
    def get(self, section, option, raw=False, vars=None): # override
    the get() method of super
    """get(sect ion, option, [raw=False], [vars=None]) --> String"""
    repl = [('&lbrack;','['),
    ('&rbrack;',']'),
    ] # extent as
    much as U like
    t = ConfigParser.ge t(self, section, option, raw, vars) # call the
    get of super
    for x,y in repl: t=t.replace(x,y ) # do the
    replace stuff
    return t

    if __name__=="__ma in__":
    """test"""
    import os
    open('test.$$$' ,'w').write('[test]\nopt1=Hello&lb rack;Ivo
    Woltring&rbrack ;\n')
    ini = MyConfigParser( )
    ini.read('test. $$$')
    print ini.get('test', 'opt1')
    os.unlink('test .$$$')
    === End Cut ===

    you can ofcourse do this in reverse for the write function so as not
    to have to write the &lbrack; etc. yourself.

    Cheerz,
    Ivo Woltring

    Comment

    • Ivo Woltring

      #3
      Re: ConfigParser - MyConfigParser. py (0/1)

      On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
      <NOmanlio_peril loSPAM@libero.i t> wrote:
      [color=blue]
      >Regards.
      >
      >Since sections in CongiParser files are delimited by [ and ], why
      >there is not an escape (and unescape) function for escaping
      >&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
      >
      >
      >
      >
      >Thanks Manlio Perillo[/color]


      Next try with attachment MyConfigParser. py
      Just save and run to see
      Cheerz,
      Ivo.

      Comment

      • Manlio Perillo

        #4
        Re: ConfigParser

        On Wed, 10 Nov 2004 15:39:42 +0100, Ivo Woltring <Python@IvoNet. nl>
        wrote:
        [color=blue]
        >On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
        ><NOmanlio_peri lloSPAM@libero. it> wrote:
        >[color=green]
        >>Regards.
        >>
        >>Since sections in CongiParser files are delimited by [ and ], why
        >>there is not an escape (and unescape) function for escaping
        >>&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
        >>
        >>
        >>
        >>
        >>Thanks Manlio Perillo[/color]
        >
        >just subclass from ConfigParser and add the functionality youself
        >[/color]


        Actually I have written two functions:

        def escape(data):
        """Escape &, [ and ] a string of data.
        """
        data = data.replace("& ", "&amp;")
        data = data.replace("[", "&lbrack;")
        data = data.replace("]", "&rbrack;")

        return data

        def unescape(data):
        """Unescape &amp;, &lt;, and &gt; in a string of data.
        """
        data = data.replace("& lbrack;", "[")
        data = data.replace("& rbrack;", "]")

        # must do ampersand last
        return data.replace("& amp;", "&")


        Maybe also '\n' to &nl; translation should be performed.




        Thanks and regards Manlio Perillo

        Comment

        • Ivo Woltring

          #5
          Re: ConfigParser

          On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
          <NOmanlio_peril loSPAM@libero.i t> wrote:
          [color=blue]
          >Regards.
          >
          >Since sections in CongiParser files are delimited by [ and ], why
          >there is not an escape (and unescape) function for escaping
          >&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
          >
          >
          >
          >
          >Thanks Manlio Perillo[/color]

          By the way I did some testing because i have a similar thing going on
          right now. I found out that

          [section]
          option = [Ivo Woltring]

          is valid and does not have to be translated to
          [section]
          option = &lbrack;Ivo Woltring&rbrack ;

          to work.


          so my current class is a bit unnessesary:

          #
          # IniFile extents the ConfigParser with my own functions
          #
          __author__ = "Ivo Woltring"
          __version__ = "01.00"
          __copyright__ = "Copyright (c) 2004 Ivo Woltring"
          __license__ = "Python"

          from ConfigParser import *

          class IniFile(ConfigP arser):
          """IniFile
          Is an extention on the ConfigParser class.
          It overrulles the write method so it can write directly to a file
          provided as
          a parameter. The whole filehandling is done by the IniFile.write(f p)
          method.
          """
          def __init__(self, defaults=None):
          ConfigParser.__ init__(self, defaults) # supers init
          self.replace = [('&lbrack;','['),
          ('&rbrack;',']'),
          ]

          def write(self,fp):
          """write(filena me) --> written file"""
          try:
          f = open(fp,'w')
          except IOError:
          raise IOError
          #ConfigParser.w rite(self,f)
          self._write(f)
          f.close()

          def _replace(self, txt ,reverse=False) :
          """Replace the self.replace stuff"""
          for source, target in self.replace:
          if reverse: txt=txt.replace (target, source)
          else: txt=txt.replace (source, target)
          return txt

          def get(self, section, option, raw=False, vars=None): # override
          the get() method of super
          """get(sect ion, option, [raw=False], [vars=None]) --> String
          this get() is an extention on the origional ConfigParser.ge t()
          This one translates html style '&lbrack;' to '[' etc.
          """
          if raw:
          return ConfigParser.ge t(self, section, option, raw, vars) # call
          the get of super
          return self._replace(C onfigParser.get (self, section, option, raw,
          vars))

          def _write(self, fp):
          """Write an .ini-format representation of the configuration
          state."""
          if self._defaults:
          fp.write("[%s]\n" % DEFAULTSECT)
          for (key, value) in self._defaults. items():
          fp.write("%s = %s\n" %
          (key, self._replace(s tr(value).repla ce('\n',
          '\n\t'), reverse=True)))
          fp.write("\n")
          for section in self._sections:
          fp.write("[%s]\n" % section)
          for (key, value) in self._sections[section].items():
          if key != "__name__":
          fp.write("%s = %s\n" %
          (key,
          self._replace(s tr(value).repla ce('\n', '\n\t'), reverse=True)))
          fp.write("\n")



          if __name__=="__ma in__":
          import sys,os
          p = IniFile()
          p.add_section(' section')
          p.set('section' ,'option','[Ivo Woltring]')
          p.write(os.path .splitext(sys.a rgv[0])[0]+'.ini')
          print p.get('section' ,'option')
          print p.get('section' ,'option', raw=True)
          raw_input('Pres s enter to continue...')


          have fun... I do,

          Cheerz, Ivo.

          Comment

          • Ivo Woltring

            #6
            Re: ConfigParser

            On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
            <NOmanlio_peril loSPAM@libero.i t> wrote:
            [color=blue]
            >Regards.
            >
            >Since sections in CongiParser files are delimited by [ and ], why
            >there is not an escape (and unescape) function for escaping
            >&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
            >
            >
            >
            >
            >Thanks Manlio Perillo[/color]

            another discovery:

            the get() DOES translate &lbrack; etc back to '[' etc, so it seameth
            to me that the whole question is mute ;-))

            Ivo.

            Comment

            • Ivo Woltring

              #7
              Re: ConfigParser - IniFile.py (0/1)

              Damn I made a mistake...
              The translation IS needed (so sorry ;-(()

              here a functional class:

              Comment

              • Paul McGuire

                #8
                Re: ConfigParser

                "Ivo Woltring" <Python@IvoNet. nl> wrote in message
                news:35t4p0lou2 lmcigaj2ic1fv9b 253d5nq6k@4ax.c om...[color=blue]
                > <snip>
                > ...the whole question is mute ;-))
                >
                > Ivo.[/color]

                The word you are looking for is "moot", not "mute" - this is actually a
                common word misuse (known as a "malapropis m") even for native English
                speakers. :)

                Pedantical-ly yours,
                -- Paul


                Comment

                • Manlio Perillo

                  #9
                  Re: ConfigParser

                  On Wed, 10 Nov 2004 21:04:55 +0100, Ivo Woltring <Python@IvoNet. nl>
                  wrote:
                  [color=blue]
                  >On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
                  ><NOmanlio_peri lloSPAM@libero. it> wrote:
                  >[color=green]
                  >>Regards.
                  >>
                  >>Since sections in CongiParser files are delimited by [ and ], why
                  >>there is not an escape (and unescape) function for escaping
                  >>&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
                  >>
                  >>
                  >>
                  >>
                  >>Thanks Manlio Perillo[/color]
                  >
                  >By the way I did some testing because i have a similar thing going on
                  >right now. I found out that
                  >
                  >[section]
                  >option = [Ivo Woltring]
                  >
                  >is valid and does not have to be translated to
                  >[section]
                  >option = &lbrack;Ivo Woltring&rbrack ;
                  >[/color]

                  Yes, I have discovered this too.
                  But my problem is with section names.



                  Thanks and regards Manlio Perillo

                  Comment

                  • Manlio Perillo

                    #10
                    Re: ConfigParser

                    On Wed, 10 Nov 2004 21:09:39 +0100, Ivo Woltring <Python@IvoNet. nl>
                    wrote:
                    [color=blue]
                    >On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
                    ><NOmanlio_peri lloSPAM@libero. it> wrote:
                    >[color=green]
                    >>Regards.
                    >>
                    >>Since sections in CongiParser files are delimited by [ and ], why
                    >>there is not an escape (and unescape) function for escaping
                    >>&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
                    >>
                    >>
                    >>
                    >>
                    >>Thanks Manlio Perillo[/color]
                    >
                    >another discovery:
                    >
                    >the get() DOES translate &lbrack; etc back to '[' etc, so it seameth
                    >to me that the whole question is mute ;-))
                    >[/color]


                    What version are you using?
                    In Python 2.3.3 get does not handles &lbrack; (or I did not understand
                    what you are saying).



                    Thanks and regards Manlio Perillo

                    Comment

                    • Ivo Woltring

                      #11
                      Re: ConfigParser

                      On Wed, 10 Nov 2004 21:18:20 GMT, "Paul McGuire"
                      <ptmcg@austin.r r._bogus_.com> wrote:
                      [color=blue]
                      >"Ivo Woltring" <Python@IvoNet. nl> wrote in message
                      >news:35t4p0lou 2lmcigaj2ic1fv9 b253d5nq6k@4ax. com...[color=green]
                      >> <snip>
                      >> ...the whole question is mute ;-))
                      >>
                      >> Ivo.[/color]
                      >
                      >The word you are looking for is "moot", not "mute" - this is actually a
                      >common word misuse (known as a "malapropis m") even for native English
                      >speakers. :)
                      >
                      >Pedantical-ly yours,
                      >-- Paul
                      >[/color]

                      Thanks for the English lesson. I am not a native English though. I'm
                      Dutch.

                      Comment

                      • Ivo Woltring

                        #12
                        Re: ConfigParser

                        On Wed, 10 Nov 2004 22:37:50 GMT, Manlio Perillo
                        <NOmanlio_peril loSPAM@libero.i t> wrote:
                        [color=blue]
                        >On Wed, 10 Nov 2004 21:09:39 +0100, Ivo Woltring <Python@IvoNet. nl>
                        >wrote:
                        >[color=green]
                        >>On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
                        >><NOmanlio_per illoSPAM@libero .it> wrote:
                        >>[color=darkred]
                        >>>Regards.
                        >>>
                        >>>Since sections in CongiParser files are delimited by [ and ], why
                        >>>there is not an escape (and unescape) function for escaping
                        >>>&, [, and ] characters to &amp;, &lbrack; and &rbrack; ?
                        >>>
                        >>>
                        >>>
                        >>>
                        >>>Thanks Manlio Perillo[/color]
                        >>
                        >>another discovery:
                        >>
                        >>the get() DOES translate &lbrack; etc back to '[' etc, so it seameth
                        >>to me that the whole question is mute ;-))
                        >>[/color]
                        >
                        >
                        >What version are you using?
                        >In Python 2.3.3 get does not handles &lbrack; (or I did not understand
                        >what you are saying).
                        >
                        >
                        >
                        >Thanks and regards Manlio Perillo[/color]

                        You are absolutely right. My 'bad'. I made a mistake in my test.
                        I posted a correction yesterday, but my attachements don't seem to get
                        thru.

                        below my current class:



                        Be patient my connection is not all that fast.
                        cheerz,
                        Ivo

                        Comment

                        Working...