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  from lxml import etree
    25  from translate.storage.xml_name import XmlNamer
    26
    27 -def open_odf(filename):
    28      z = zipfile.ZipFile(filename, 'r')
    29      return {'content.xml': z.read("content.xml"),
    30              'meta.xml':    z.read("meta.xml"),
    31              'styles.xml':  z.read("styles.xml")}
    32
    33 -def copy_odf(input_zip, output_zip, exclusion_list):
    34      for name in [name for name in input_zip.namelist() if name not in exclusion_list]:
    35          output_zip.writestr(name, input_zip.read(name))
    36      return output_zip
    37
    38 -def namespaced(nsmap, short_namespace, tag):
    39      return '{%s}%s' % (nsmap[short_namespace], tag)
    40
    41 -def add_file(output_zip, manifest_data, new_filename, new_data):
    42      root = etree.fromstring(manifest_data)
    43      namer = XmlNamer(root)
    44      namespacer = namer.namespace('manifest')
    45      file_entry_tag  = namespacer.name('file-entry')
    46      media_type_attr = namespacer.name('media-type')
    47      full_path_attr  = namespacer.name('full-path')
    48
    49      root.append(etree.Element(file_entry_tag, {media_type_attr: 'application/x-xliff+xml',
    50                                                 full_path_attr: new_filename}))
    51      output_zip.writestr(new_filename, new_data)
    52      output_zip.writestr('META-INF/manifest.xml', etree.tostring(root, xml_declaration=True, encoding="UTF-8"))
    53      return output_zip
    54