configurationengine/source/cone/storage/resources.py
author terytkon
Thu, 11 Mar 2010 17:04:37 +0200
changeset 0 2e8eeb919028
permissions -rw-r--r--
Adding EPL version of configurationengine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     1
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     3
# All rights reserved.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     8
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     9
# Initial Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    11
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    12
# Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    13
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    14
# Description: 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    15
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    16
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    17
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import zipfile,os,re,zlib
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
from cone.public.api import Resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
class CpfRootResource(Resource):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
    Parses a single CPF configuration project root confml file. Parses the XInclude statements to 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
    find out the layers inside the project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
    """ 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
    def __init__(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
        self.configuration_namespace = "http://www.s60.com/xml/confml/1"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
        self.include_namespace       = "http://www.w3.org/2001/xinclude"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
        self.desc                    = ""
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
        self.includes                = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
        self.meta                    = {}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
        self.filename                = "defaultroot.confml"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
        return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
    def parse_file(self, xmlfile):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
        self.filename = xmlfile
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
        self.etree    = ElementTree.parse(xmlfile)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
        self.parse_includes()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
        self.parse_meta()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
        self.parse_desc()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
        return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
    def parse_str(self, xml_as_string):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
        self.etree = ElementTree.fromstring(xml_as_string)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
        self.parse_includes()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
        self.parse_meta()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
        self.parse_desc()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
        return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
    def parse_includes(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
        includes = self.etree.getiterator("{%s}include" % self.include_namespace)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
        for inc in includes:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
            self.includes.append(inc.get('href'))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
    def parse_meta(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
        meta = self.etree.find("{%s}meta" % self.configuration_namespace)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
        if meta:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
            for elem in meta.getiterator():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
                m = re.match("{.*}(?P<tagname>.*)",elem.tag)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
                if m:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
                    self.meta[m.group('tagname')] = elem.text
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
    def parse_desc(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
        desc_elem = self.etree.find("{%s}desc" % self.configuration_namespace)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
        if desc_elem != None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
            self.desc = desc_elem.text
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
      
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
    def get_layers(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
        return self.includes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
    def get_meta(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
        return self.meta
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
    def get_desc(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
        return self.desc
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
    def get_configuration(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
        configuration = CpfConfiguration(self.filename)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
        for inc in self.includes:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
            configuration.add_layer(CpfLayer(inc))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
        return configuration
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82