Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > dca483b59ba61f3fa092de932ddd570e > files > 813

nuface-2.0.14-2mdv2009.1.i586.rpm

#
# Copyright(C) 2006 INL
# Written by Jean Gillaux <jean@inl.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#


"""XML parsing functions"""

from nupyf.nuelt import EltGrpList
from nupyf.nuacl_func import xml_new_eltgrp
import sys

def _parse_xml(xmldoc, parenttag, childtag):
    periods = xmldoc.getElementsByTagName(parenttag)
    if not periods:
        raise ValueError('no <%s> tag' % parenttag)

    listpobj=EltGrpList() #object list of periods
    period_node = periods[0]
    periods_nodes = period_node.getElementsByTagName(childtag)
    for period_n in periods_nodes:
        elts = period_n.getElementsByTagName('elt')
        try:
            mygrp = xml_new_eltgrp(int(period_n.getAttribute('ID')),
                period_n.getAttribute('name'), elts)
            listpobj.add(mygrp)
        except ValueError, e:
            print e
            sys.exit(1)
    return listpobj

def parse_periodicities(xmldoc):
    """Get <periodicities>"""
    return _parse_xml(xmldoc, 'periodicities', 'periodicity')


def parse_durations(xmldoc):
    """Get <durations>"""
    return _parse_xml(xmldoc, 'durations', 'duration')


def parse_descsort(acl_node, descsort_list=None):
    """Parse descsort tags from acls xml file"""
    if descsort_list is None:
        descsort_list = []
    descsorts = acl_node.getElementsByTagName('descsorts')
    acl_id = acl_node.getAttribute('ID')
    acl_name = acl_node.getAttribute('name')
    #xml validity
    if descsorts is None:
        raise ValueError('no descsorts node for acl %s (%s)'%(acl_id, acl_name))
    descsort_nodes = descsorts[0].getElementsByTagName('descsort')
    if descsort_nodes is None:
        raise ValueError('no descsort node for acl %s (%s)'%(acl_id, acl_name))
    #get values
    res = {}
    for descsort_node in descsort_nodes:
        id_desc = descsort_node.getAttribute('ID')
        res[id_desc] = {}
        for elt_node in descsort_node.getElementsByTagName('elt'):
            elt_from = int(elt_node.getAttribute('from'))
            elt_to = int(elt_node.getAttribute('to'))
            elt_order = int(elt_node.getAttribute('order'))
            unit = (int(id_desc), elt_from, elt_to, elt_order)
            if unit in descsort_list:
                raise ValueError('Duplicated descsort node <ID=%s from=%s to=%s order=%s>'%unit)
            else:
                descsort_list.append(unit)
            cpl = (elt_from, elt_to)

            res[id_desc][cpl] = elt_order

    return res