Package logilab :: Package common :: Module xmlutils

[frames] | no frames]

# Source Code for Module logilab.common.xmlutils

     1  # -*- coding: utf-8 -*-
     2  # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     3  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     4  #
     5  # This file is part of logilab-common.
     6  #
     7  # logilab-common is free software: you can redistribute it and/or modify it under
     8  # the terms of the GNU Lesser General Public License as published by the Free
     9  # Software Foundation, either version 2.1 of the License, or (at your option) any
    10  # later version.
    11  #
    12  # logilab-common is distributed in the hope that it will be useful, but WITHOUT
    13  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    15  # details.
    16  #
    17  # You should have received a copy of the GNU Lesser General Public License along
    18  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
    19  """XML utilities.
    20
    21  This module contains useful functions for parsing and using XML data. For the
    22  moment, there is only one function that can parse the data inside a processing
    23  instruction and return a Python dictionary.
    24
    25
    26
    27
    28  """
    29  __docformat__ = "restructuredtext en"
    30
    31  import re
    32
    33  RE_DOUBLE_QUOTE = re.compile('([\w\-\.]+)="([^"]+)"')
    34  RE_SIMPLE_QUOTE = re.compile("([\w\-\.]+)='([^']+)'")
    35
    36 -def parse_pi_data(pi_data):
    37      """
    38      Utility function that parses the data contained in an XML
    39      processing instruction and returns a dictionary of keywords and their
    40      associated values (most of the time, the processing instructions contain
    41      data like ``keyword="value"``, if a keyword is not associated to a value,
    42      for example ``keyword``, it will be associated to ``None``).
    43
    44      :param pi_data: data contained in an XML processing instruction.
    45      :type pi_data: unicode
    46
    47      :returns: Dictionary of the keywords (Unicode strings) associated to
    48                their values (Unicode strings) as they were defined in the
    49                data.
    50      :rtype: dict
    51      """
    52      results = {}
    53      for elt in pi_data.split():
    54          if RE_DOUBLE_QUOTE.match(elt):
    55              kwd, val = RE_DOUBLE_QUOTE.match(elt).groups()
    56          elif RE_SIMPLE_QUOTE.match(elt):
    57              kwd, val = RE_SIMPLE_QUOTE.match(elt).groups()
    58          else:
    59              kwd, val = elt, None
    60          results[kwd] = val
    61      return results
    62