configurationengine/source/plugins/symbian/ConeImagePlugin/imageplugin/imageml.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 '''
       
    17 A plugin implementation for image selection from ConfigurationLayers.
       
    18 '''
       
    19 
       
    20 
       
    21 import re
       
    22 import os
       
    23 import sys
       
    24 import logging
       
    25 import shutil
       
    26 import xml.parsers.expat
       
    27 
       
    28 try:
       
    29     from cElementTree import ElementTree
       
    30 except ImportError:
       
    31     try:    
       
    32         from elementtree import ElementTree
       
    33     except ImportError:
       
    34         try:
       
    35             from xml.etree import cElementTree as ElementTree
       
    36         except ImportError:
       
    37             from xml.etree import ElementTree
       
    38             
       
    39 import __init__
       
    40 
       
    41 from cone.public import exceptions,plugin,utils,api
       
    42 from imageplugin.generators import OutputGenerator,InputFile,InputDir,InvalidInputFileException
       
    43 
       
    44 class ImageImpl(plugin.ImplBase):
       
    45     """
       
    46     ContentImpl plugin finds all image resources from each layer and copies
       
    47     them to the output correctly. It follows the Configuration project override
       
    48     rules, so that the topmost layer files override files on the previous layers.
       
    49     """
       
    50     
       
    51     IMPL_TYPE_ID = "imageml"
       
    52     
       
    53     def __init__(self,ref,configuration):
       
    54         """
       
    55         Overloading the default constructor
       
    56         """
       
    57         plugin.ImplBase.__init__(self,ref,configuration)
       
    58         self.include = {}
       
    59         self.exclude = {}
       
    60         self.input = ""
       
    61         self.desc = ""
       
    62         self.output_file = ""
       
    63         self.logger = logging.getLogger('cone.imageml(%s)' % self.ref)
       
    64         self.errors = False
       
    65 
       
    66     def get_include_pattern(self):
       
    67         include_pattern = ""
       
    68         if self.include.has_key('pattern'): 
       
    69             include_pattern = self.include['pattern'][0] 
       
    70         return include_pattern
       
    71     
       
    72     def get_exclude_pattern(self):
       
    73         exclude_pattern = ""
       
    74         if self.exclude.has_key('pattern'): 
       
    75             exclude_pattern = self.exclude['pattern'][0] 
       
    76         return exclude_pattern
       
    77     
       
    78     def list_output_files(self):
       
    79         """
       
    80         Return a list of output files as an array. 
       
    81         """
       
    82         return []
       
    83     
       
    84     def generate(self, context=None):
       
    85         """
       
    86         Generate the given implementation.
       
    87         """
       
    88         self.logger.info('Generating')
       
    89         ret = True
       
    90         for generator in self.generators:
       
    91             self.logger.info(generator)
       
    92             generator.subpath =  self.output
       
    93             try:
       
    94                 ret = generator.generate() and ret
       
    95             except InvalidInputFileException, e:
       
    96                 self.logger.error(e)
       
    97         return ret
       
    98     
       
    99     def generate_layers(self,layers):
       
   100         """
       
   101         Generate the given Configuration layers.
       
   102         """
       
   103         return self.generate()
       
   104     
       
   105     def get_refs(self):
       
   106         refs = []
       
   107         for generator in self.generators:
       
   108             refs.extend(generator.get_refs())
       
   109         if refs:
       
   110             return utils.distinct_array(refs)
       
   111         else:
       
   112             return None
       
   113 
       
   114 
       
   115 class ImageImplReader(plugin.ReaderBase):
       
   116     """
       
   117     Parses a single imageml implml file
       
   118     """ 
       
   119     NAMESPACE = 'http://www.s60.com/xml/imageml/1'
       
   120     FILE_EXTENSIONS = ['imageml']
       
   121     
       
   122     INCLUDE_ATTR = ['pattern']
       
   123     EXCLUDE_ATTR = ['pattern']
       
   124     def __init__(self):
       
   125         self.desc = None
       
   126         self.output = None
       
   127         self.input_dir = None
       
   128         self.include = None
       
   129         self.exclude = None
       
   130         self.namespaces = [self.NAMESPACE]
       
   131     
       
   132     @classmethod
       
   133     def read_impl(cls, resource_ref, configuration, etree):
       
   134         reader = ImageImplReader()
       
   135         reader.desc = reader.parse_desc(etree)
       
   136         reader.outputgenerators = reader.parse_outputs(etree)
       
   137 
       
   138         impl = ImageImpl(resource_ref, configuration)
       
   139         impl.desc = reader.desc
       
   140         impl.generators = reader.outputgenerators
       
   141         for generator in impl.generators:
       
   142             generator.configuration = configuration
       
   143         
       
   144         return impl
       
   145 
       
   146     def fromstring(self, xml_as_string):
       
   147         etree = ElementTree.fromstring(xml_as_string)
       
   148         self.desc = self.parse_desc(etree)
       
   149         self.outputgenerators = self.parse_outputs(etree)
       
   150         return
       
   151 
       
   152     def parse_desc(self,etree):
       
   153         desc = ""
       
   154         desc_elem = etree.find("{%s}desc" % self.namespaces[0])
       
   155         if desc_elem != None:
       
   156             desc = desc_elem.text
       
   157         return desc
       
   158 
       
   159     def parse_input_include(self,etree):
       
   160         include_elem = etree.findall("{%s}include" % self.namespaces[0])
       
   161         include = {}
       
   162         for f in include_elem:
       
   163             for key in self.INCLUDE_ATTR:
       
   164                 # Add the attribute if it is found to include dict
       
   165                 include[key] = []
       
   166                 attr = f.get(key)
       
   167                 if attr: include[key].append((attr))
       
   168         return include
       
   169 
       
   170     def parse_input_exclude(self,etree):
       
   171         elem = etree.findall("{%s}exclude" % self.namespaces[0])
       
   172         exclude = {}
       
   173         for f in elem:
       
   174             for key in self.EXCLUDE_ATTR:
       
   175                 # Add the attribute if it is found
       
   176                 exclude[key] = []
       
   177                 attr = f.get(key)
       
   178                 if attr: exclude[key].append((attr))
       
   179         return exclude
       
   180 
       
   181     def parse_inputs(self,etree):
       
   182         inputs = etree.findall("{%s}input" % self.namespaces[0])
       
   183         inputlist = []
       
   184         for input_elem in inputs:
       
   185             if input_elem.get('dir'):
       
   186                 inputdir = InputDir(input_elem.get('dir'),**input_elem.attrib)
       
   187                 inputdir.include = self.parse_input_include(input_elem)
       
   188                 inputdir.exclude = self.parse_input_exclude(input_elem)
       
   189                 inputlist.append(inputdir)
       
   190             elif input_elem.get('file'):
       
   191                 inputlist.append(InputFile(input_elem.get('file'),**input_elem.attrib))
       
   192         return inputlist
       
   193 
       
   194     def parse_outputs(self,etree):
       
   195         outputs = etree.findall("{%s}output" % self.namespaces[0])
       
   196         outputpath = ""
       
   197         outputgenerators = []
       
   198         for output_elem in outputs:
       
   199             if output_elem.get('file'):
       
   200                 outputpath = output_elem.get('file')
       
   201             generator = OutputGenerator(outputpath,**output_elem.attrib)
       
   202             generator.inputs = self.parse_inputs(output_elem)
       
   203             outputgenerators.append(generator)
       
   204         return outputgenerators
       
   205