New Thread- Supporting Multiline values in ConfigParser

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

    #1

    New Thread- Supporting Multiline values in ConfigParser

    Hi,
    Am starting a new thread as I fear the old thread which more than a
    week old can go unnoticed.
    Sorry for the multiple mails.

    I took the approach of Subclassing ConfigParser to support multiline
    values without leading white-spaces, but am struct at which position
    in _read I should modify to accomodate the non-leading whitespace
    based multiline values.

    I can guess, this portion in the _read function will require change,
    any change to this affects the whole of parsing. :-( Can someone who
    has done this before or understands ConfigParser better help me?

    # Section I am talking about
    if line[0].isspace() and cursect is not None
    and optname:

    value = line.strip()
    if value:
    cursect[optname] = "%s\n%s" %
    (cursect[optname], value)

    # _read method

    def _read(self, fp, fpname):
    cursect = None
    optname = None
    lineno = 0
    e = None
    while True:
    line = fp.readline()
    if not line:
    break
    lineno = lineno + 1
    # comment or blank line?
    if line.strip() == '' or line[0] in '#;':
    continue
    if line.split(None , 1)[0].lower() == 'rem' and
    line[0] in "rR":
    # no leading whitespace
    continue
    # continuation line
    print "line:%s\tcurse ct:%s\toptname: %s"%
    (line,cursect,o ptname)

    if line[0].isspace() and cursect is not None
    and optname:

    value = line.strip()
    if value:
    cursect[optname] = "%s\n%s" %
    (cursect[optname], value)
    # a section header or option header?
    else:
    # is it a section header?
    mo = self.SECTCRE.ma tch(line)
    if mo:
    sectname = mo.group('heade r')
    if sectname in self._sections:
    cursect =
    self._sections[sectname]
    elif sectname ==
    ConfigParser.DE FAULTSECT:
    cursect =
    self._defaults
    else:
    cursect =
    {'__name__':sec tname}

    self._sections[sectname] = cursect
    # So sections can't start with
    a continuation line
    optname = None
    elif cursect is None:
    raise
    ConfigParser.Mi ssingSectionHea derError(fpname , lineno,
    line)
    # an option line?
    else:
    mo = self.OPTCRE.mat ch(line)
    if mo:
    optname, vi, optval =
    mo.group('optio n','vi','value' )
    if vi in ('=',':') and
    ';' in optval:
    # ';' is a
    comment delimiter only if it follows
    # a spacing
    character
    pos =
    optval.find(';' )
    if pos != -1
    and optval[pos-1].isspace():
    optval
    = optval[:pos]
    optval =
    optval.strip()
    # allow empty values
    if optval == '""':
    optval = ''
    optname =
    self.optionxfor m(optname.rstri p())
    cursect[optname] =
    optval
    else:
    if not e:
    e =
    ConfigParser.Pa rsingError(fpna me)
    e.append(lineno ,
    repr(line))
    if e:
    raise e

    --
    Senthil

  • Gabriel Genellina

    #2
    Re: New Thread- Supporting Multiline values in ConfigParser

    En Mon, 25 Jun 2007 11:06:04 -0300, Phoe6 <orsenthil@gmai l.comescribió:
    I took the approach of Subclassing ConfigParser to support multiline
    values without leading white-spaces, but am struct at which position
    in _read I should modify to accomodate the non-leading whitespace
    based multiline values.
    And how would you detect a multiline value?
    Because it is not a section nor looks like a new option?
    I can guess, this portion in the _read function will require change,
    any change to this affects the whole of parsing. :-( Can someone who
    has done this before or understands ConfigParser better help me?
    >
    # Section I am talking about
    if line[0].isspace() and cursect is not None
    and optname:
    >
    value = line.strip()
    if value:
    cursect[optname] = "%s\n%s" %
    (cursect[optname], value)
    Yes, I guess so.
    I'd try using this:

    if not self.SECTCRE.ma tch(line) and not self.OPTCRE.mat ch(line) and
    cursect is not None and optname:

    (that is, if the line is not a section header and it's not a new option
    and there is a current section and option; those two last conditions same
    as the previous version).
    But you'll have to experiment - I've not tested it.

    --
    Gabriel Genellina

    Comment

    • O.R.Senthil Kumaran

      #3
      Re: New Thread- Supporting Multiline values in ConfigParser

      * Gabriel Genellina <gagsl-py2@yahoo.com.a r[2007-06-25 22:26:47]:
      And how would you detect a multiline value?
      Because it is not a section nor looks like a new option?
      >
      I can guess, this portion in the _read function will require change,
      any change to this affects the whole of parsing. :-( Can someone who
      has done this before or understands ConfigParser better help me?

      # Section I am talking about
      if line[0].isspace() and cursect is not None
      and optname:

      value = line.strip()
      if value:
      cursect[optname] = "%s\n%s" %
      (cursect[optname], value)
      >
      Yes, I guess so.
      I'd try using this:
      >
      if not self.SECTCRE.ma tch(line) and not self.OPTCRE.mat ch(line) and
      cursect is not None and optname:
      >
      Thanks Gabriel, this suggestion worked and helped me understand the
      ConfigParser a bit better as well.

      --
      O.R.Senthil Kumaran

      Comment

      • Gabriel Genellina

        #4
        Re: New Thread- Supporting Multiline values in ConfigParser

        En Wed, 27 Jun 2007 01:35:27 -0300, O.R.Senthil Kumaran
        <orsenthil@user s.sourceforge.n etescribió:
        * Gabriel Genellina <gagsl-py2@yahoo.com.a r[2007-06-25 22:26:47]:
        >
        >And how would you detect a multiline value?
        >Because it is not a section nor looks like a new option?
        >>
        >I'd try using this:
        >>
        >if not self.SECTCRE.ma tch(line) and not self.OPTCRE.mat ch(line) and
        >cursect is not None and optname:
        >
        Thanks Gabriel, this suggestion worked and helped me understand the
        ConfigParser a bit better as well.
        Good to know it worked - I was not entirely sure it would :)

        --
        Gabriel Genellina

        Comment

        Working...