Package translate :: Package storage :: Module odf_io

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.odf_io

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21  #
    22
    23  import zipfile
    24
    25  from lxml import etree
    26
    27  from translate.storage.xml_name import XmlNamer
    28
    29
    30 -def open_odf(filename):
    31      z = zipfile.ZipFile(filename, 'r')
    32      return {'content.xml': z.read("content.xml"),
    33              'meta.xml': z.read("meta.xml"),
    34              'styles.xml': z.read("styles.xml")}
    35
    36
    37 -def copy_odf(input_zip, output_zip, exclusion_list):
    38      for name in [name for name in input_zip.namelist() if name not in exclusion_list]:
    39          output_zip.writestr(name, input_zip.read(name))
    40      return output_zip
    41
    42
    43 -def namespaced(nsmap, short_namespace, tag):
    44      return '{%s}%s' % (nsmap[short_namespace], tag)
    45
    46
    47 -def add_file(output_zip, manifest_data, new_filename, new_data):
    48      root = etree.fromstring(manifest_data)
    49      namer = XmlNamer(root)
    50      namespacer = namer.namespace('manifest')
    51      file_entry_tag = namespacer.name('file-entry')
    52      media_type_attr = namespacer.name('media-type')
    53      full_path_attr = namespacer.name('full-path')
    54
    55      root.append(etree.Element(file_entry_tag, {media_type_attr: 'application/x-xliff+xml',
    56                                                 full_path_attr: new_filename}))
    57      output_zip.writestr(new_filename, new_data)
    58      output_zip.writestr('META-INF/manifest.xml', etree.tostring(root, xml_declaration=True, encoding="UTF-8"))
    59      return output_zip
    60