def generate(self,
                 context,
                 input,
                 output,
                 xslt,
                 settings,
                 enc=sys.getdefaultencoding(),
                 line_ending_style=None):
        """
        Generates output
        """
        self.context = context
        self.logger = logging.getLogger('cone.gcfml{%s}' % input.path)

        if line_ending_style == 'LF':
            linesep = '\n'
        else:
            linesep = '\r\n'

        try:
            tf = xslttransformer.XsltTransformer()
            result = tf.transform_lxml(os.path.abspath(self.temp_confml_file),
                                       os.path.abspath(xslt), enc, linesep)
            if not self.filter_file_writing(result):
                self.write_string_to_file(result, output, enc)

        except (exceptions.ConeException, TypeError, Exception), e:
            logging.getLogger('cone.gcfml').warning(
                'Failed to do XSLT tranformation. Exception: %s' % e)
            raise exceptions.ConeException(
                'Failed to do XSLT tranformation. Exception: %s' % e)
def post_process_result(string, enc, linesep):
    """
    Does post process for result from XSLT transform
        - encoding
        - removes extra line separators
        - changes line separators
    """
    output_string = None

    try:
        output_string = string.decode(enc)
        if not output_string.startswith('<'):
            output_string = '\n' + output_string
        output_string = output_string.replace(
            '<?xml version="1.0" encoding="UTF-16"?>',
            '<?xml version="1.0" encoding="UTF-16"?>\n\n')
        output_string = output_string.replace(
            '<?xml version="1.0" encoding="UTF-8"?>',
            '<?xml version="1.0" encoding="UTF-8"?>\n\n')
        output_string = output_string.replace('\n\n', '\n')
        output_string = output_string.replace('\n', linesep)
        output_string += linesep
    except Exception, e:
        logging.getLogger('cone.gcfml').error(
            'Cannot post process result: %s \nException: %s' % (string, e))
        raise exceptions.ConeException(
            'Cannot post process result: %s \nException: %s' % (string, e))
 def test_create_from_cone_exception_simple(self):
     ex = exceptions.ConeException('foobar')
     self.assertEquals(
         api.Problem.from_exception(ex),
         api.Problem('foobar',
                     type     = '',
                     severity = api.Problem.SEVERITY_ERROR))
 def test_create_from_cone_exception_all_attributes(self):
     ex = exceptions.ConeException('foobar',
                                   problem_lineno = 101,
                                   problem_msg    = 'foobar2',
                                   problem_type   = 'foo.bar')
     self.assertEquals(
         api.Problem.from_exception(ex),
         api.Problem('foobar2',
                     type     = 'foo.bar',
                     line     = 101,
                     severity = api.Problem.SEVERITY_ERROR))
def write_element_tempfile(element, tempfile):
    """
    Writes element to temp file
    """
    if element != None and ElementTree.iselement(element):

        try:
            tempfile.write(ElementTree.tostring(element))
        except Exception, e:
            raise exceptions.ConeException(
                'Cannot write Element to file (%s). Exception: %s' %
                (tempfile, e))
def write_element(element, output, linesep=os.linesep):
    """
    """
    if element != None and ElementTree.iselement(element):
        enc = None

        try:
            out_file = open(output, 'w')
            out_string = ElementTree.tostring(element)
            out_string = out_string.replace('\r\n', linesep)
            out_string = out_string.replace('\n', linesep)
            out_file.write(out_string)
            out_file.close()
        except Exception, e:
            raise exceptions.ConeException(
                'Cannot write Element to file (%s). Exception: %s' %
                (output, e))
 def get_flatconfig(self):
     """ 
     Create a flat configuration from the current configuration with the given setting refs.
     Take the last configuration element, which will contain the data elements
     """
     if not self.flatconfig:
         try:
             cf = confflattener.ConfigurationFlattener()
             self.flatconfig = api.Configuration()
             cf.flat(self.configuration, self.reader.settings,
                     self.flatconfig)
         except (exceptions.ConeException, TypeError, Exception), e:
             utils.log_exception(
                 self.logger,
                 'Failed to flat configuration with settings %s. Exception: %s'
                 % (self.reader.settings, e))
             raise exceptions.ConeException(
                 'Failed to flat configuration. Exception: %s' % e)
def write_element_enc(element, output, enc, linesep=os.linesep):
    """
    Writes element to file
    """
    if element != None and ElementTree.iselement(element):
        enc = None

        try:
            remove_namespace(element, 'http://www.s60.com/xml/genconfml/1')

            out_file = codecs.open(output, 'w', enc)
            output_string = ElementTree.tostring(element)
            output_string = output_string.replace('\r\n', linesep)
            output_string = output_string.replace('\n', linesep)
            out_file.write(output_string)
            out_file.close()
        except Exception, e:
            raise exceptions.ConeException(
                'Cannot write Element to file (%s). Exception: %s' %
                (output, e))
    def write_string_to_file(self, string, output, enc):
        """
        Writes string to file
        """
        try:

            #fl = codecs.open(outfile, 'w', enc)
            fl = self.context.create_file(output,
                                          implementation=self.implml,
                                          mode='w',
                                          encoding=enc)
            fl.write(string)
            fl.close()

        except Exception, e:
            logging.getLogger('cone.gcfml').error(
                'Cannot write Element to file (%s). Exception: %s' %
                (output, e))
            raise exceptions.ConeException(
                'Cannot write Element to file (%s). Exception: %s' %
                (output, e))
    def transform_lxml(self, input, xslt, enc, linesep=os.linesep):
        """
        XSLT transform with lxml.
        """
        from lxml import etree

        if not enc:
            enc = sys.getdefaultencoding()
        try:
            xslt_doc = etree.parse(xslt)
            transform = etree.XSLT(xslt_doc)

            input_doc = etree.parse(input)
            result = str(transform(input_doc))
            postprocessed_result = post_process_result(result, enc, linesep)
            return postprocessed_result

        except Exception, e:
            logging.getLogger('cone.gcfml').error(
                'Failed to do XSLT transformation: %s' % e)
            raise exceptions.ConeException(
                'Failed to do XSLT transformation: %s' % (e))
        enc = None

        try:
            out_file = open(output, 'w')
            out_string = ElementTree.tostring(element)
            out_string = out_string.replace('\r\n', linesep)
            out_string = out_string.replace('\n', linesep)
            out_file.write(out_string)
            out_file.close()
        except Exception, e:
            raise exceptions.ConeException(
                'Cannot write Element to file (%s). Exception: %s' %
                (output, e))
    else:
        raise exceptions.ConeException(
            'Cannot write element to file, because None element passed or not Element passed.'
        )


def remove_namespace(doc, namespace):
    """Remove namespace in the passed document in place."""
    ns = u'{%s}' % namespace
    nsl = len(ns)
    for elem in doc.getiterator():
        if elem.tag.startswith(ns):
            elem.tag = elem.tag[nsl:]


def write_element_enc(element, output, enc, linesep=os.linesep):
    """
    Writes element to file