Package libxyz :: Package parser :: Module regexp |
[hide private]
[frames] | no frames]

# Source Code for Module libxyz.parser.regexp

     1  #-*- coding: utf8 -*
     2  #
     3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
     4  #
     5  # This file is part of XYZCommander.
     6  # XYZCommander is free software: you can redistribute it and/or modify
     7  # it under the terms of the GNU Lesser Public License as published by
     8  # the Free Software Foundation, either version 3 of the License, or
     9  # (at your option) any later version.
    10  # XYZCommander is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  # GNU Lesser Public License for more details.
    14  # You should have received a copy of the GNU Lesser Public License
    15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
    16
    17  from libxyz.parser import BaseParser
    18  from libxyz.parser import SourceData
    19  from libxyz.exceptions import XYZValueError
    20  from libxyz.exceptions import ParseError
    21
    22 -class RegexpParser(BaseParser):
    23      """
    24      RegexpParser is used to parse statements based on regular expressions
    25      It is only useful for parsing linear, non-structured files.
    26      """
    27
    28 -    def __init__(self, cbpool):
    29          """
    30          @param cbpool: Dictionary with compiled regexp as keys and
    31                         callback functions as values.
    32                         Upon matching regexp, callback will be called with
    33                         MatchObject as an argument. Callback function should
    34                         raise XYZValueError in case of any error and return
    35                         whatever otherwise.
    36          """
    37
    38          super(RegexpParser, self).__init__()
    39
    40          self.cbpool = cbpool
    41
    42      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    43
    44 -    def parse(self, source):
    45          """
    46          Parse config
    47          """
    48
    49          _lineno = 0
    50          _source = SourceData(source, bytes=False)
    51
    52          for _line in _source:
    53              _lineno += 1
    54              _line = _line.strip()
    55
    56              # Empty line
    57              if not _line:
    58                  continue
    59
    60              _matched = False
    61
    62              for _regexp in self.cbpool:
    63                  _res = _regexp.search(_line)
    64
    65                  if _res is not None:
    66                      _matched = True
    67                      try:
    68                          self.cbpool[_regexp](_res)
    69                      except XYZValueError, e:
    70                          raise ParseError(_(u"%s: parse error on line %d: %s"\
    71                                           % (_source.desc(), _lineno, e)))
    72                      else:
    73                          break
    74
    75              if not _matched:
    76                  raise ParseError(_(u"Unmatched line %d: %s" % (_lineno, _line)))
    77