configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/hcrml_parser.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    16 
    16 
    17 
    17 
    18 
    18 
    19 
    19 
    20 import re
    20 import re
    21 import os
       
    22 import sys
       
    23 import logging
    21 import logging
    24 import xml.parsers.expat
    22 import pkg_resources
    25 import codecs
    23 
    26 from hcrplugin.hcr_exceptions import *
    24 from hcrplugin.hcr_exceptions import *
    27 from hcrplugin.hcrrepository import HcrRecord, HcrRepository
    25 from hcrplugin.hcrrepository import HcrRecord, HcrRepository
    28 from hcrplugin.hcr_writer import HcrWriter
    26 from hcrplugin.hcr_writer import HcrWriter
    29 from hcrplugin.header_writer import *
    27 from hcrplugin.header_writer import *
    30 try:
    28 
    31     from cElementTree import ElementTree
    29 from cone.public import plugin
    32 except ImportError:
       
    33     try:    
       
    34         from elementtree import ElementTree
       
    35     except ImportError:
       
    36         try:
       
    37             from xml.etree import cElementTree as ElementTree
       
    38         except ImportError:
       
    39             from xml.etree import ElementTree
       
    40 
       
    41 import __init__
       
    42 
       
    43 from cone.public import exceptions,plugin,utils,api
       
    44 
    30 
    45 class HcrmlImpl(plugin.ImplBase):
    31 class HcrmlImpl(plugin.ImplBase):
    46     """
    32     """
    47     <class description>
    33     <class description>
    48     """
    34     """
    60         """
    46         """
    61         Generate the given implementation. 
    47         Generate the given implementation. 
    62         @return: 
    48         @return: 
    63         """
    49         """
    64         outputfile = self.__get_output_filename()
    50         outputfile = self.__get_output_filename()
    65         if outputfile != None:
       
    66             # Create the path to the output file
       
    67             output_path = os.path.dirname(outputfile)
       
    68             if output_path != '' and not os.path.exists(output_path):
       
    69                 os.makedirs(output_path)
       
    70         
       
    71         # For output type 'hcr', write the binary repository file
    51         # For output type 'hcr', write the binary repository file
    72         if self.output_obj.type == 'hcr':
    52         if self.output_obj.type == 'hcr':
    73             self.logger.info("Generating binary repository to '%s'" % outputfile)
    53             self.logger.info("Generating binary repository to '%s'" % outputfile)
    74             writer = HcrWriter()
    54             writer = HcrWriter()
    75             repo = self.output_obj.get_hcr_repository()
    55             repo = self.output_obj.get_hcr_repository()
    76             data = writer.get_repository_bindata(repo)
    56             data = writer.get_repository_bindata(repo)
    77             f = open(outputfile,'wb')
    57             f = context.create_file(outputfile, mode='wb')
       
    58             #f = open(outputfile,'wb')
    78             try:        f.write(data)
    59             try:        f.write(data)
    79             finally:    f.close()
    60             finally:    f.close()
    80         elif self.output_obj.type == 'header':
    61         elif self.output_obj.type == 'header':
    81             self.logger.info("Generating header file to '%s'" % outputfile)
    62             self.logger.info("Generating header file to '%s'" % outputfile)
    82             writer = HeaderWriter(outputfile, self.output_obj)
    63             writer = HeaderWriter(outputfile, self.output_obj)
    83             writer.write()
    64             writer.write(context)
    84         elif self.output_obj.type == None:
    65         elif self.output_obj.type == None:
    85             # The HCRML file contains no <output> element, so no output should
    66             # The HCRML file contains no <output> element, so no output should
    86             # be generated
    67             # be generated
    87             pass
    68             pass
    88 
    69 
   103             return None
    84             return None
   104 
    85 
   105 
    86 
   106 class HcrmlReader(plugin.ReaderBase):
    87 class HcrmlReader(plugin.ReaderBase):
   107     NAMESPACE = 'http://www.symbianfoundation.org/xml/hcrml/1'
    88     NAMESPACE = 'http://www.symbianfoundation.org/xml/hcrml/1'
       
    89     NAMESPACE_ID = 'hcrml'
       
    90     ROOT_ELEMENT_NAME = 'hcr'
   108     FILE_EXTENSIONS = ['hcrml']
    91     FILE_EXTENSIONS = ['hcrml']
   109     
    92     
   110     def __init__(self, resource_ref, configuration):
    93     def __init__(self, resource_ref, configuration):
   111         self.configuration = configuration
    94         self.configuration = configuration
   112         self.hcrml_file = resource_ref
    95         self.hcrml_file = resource_ref
   121         
   104         
   122         impl = HcrmlImpl(resource_ref, configuration)
   105         impl = HcrmlImpl(resource_ref, configuration)
   123         impl.output_obj = reader.read_hcrml_output()
   106         impl.output_obj = reader.read_hcrml_output()
   124         impl.refs = reader.refs
   107         impl.refs = reader.refs
   125         return impl
   108         return impl
   126 
   109     
       
   110     @classmethod
       
   111     def get_schema_data(cls):
       
   112         return pkg_resources.resource_string('hcrplugin', 'xsd/hcrml.xsd')
       
   113     
   127     def read_hcrml_output(self, ignore_includes=False):
   114     def read_hcrml_output(self, ignore_includes=False):
   128         output = Output()
   115         output = Output()
   129         
   116         
   130         # There should only be one <output> element, so use find()
   117         # There should only be one <output> element, so use find()
   131         out_elem = self.doc.find("{%s}output" % self.namespaces[0])
   118         out_elem = self.doc.find("{%s}output" % self.namespaces[0])