# Path: resources/lib/kindleunpack/compatibility_utils.py
# def unicode_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p
#     return p.decode(enc)
# 
# def unescapeit(sval):
#     return _h.unescape(sval)
# 
# Path: resources/lib/kindleunpack/compatibility_utils.py
# def lzip(*args, **kwargs):
#     return list(zip(*args, **kwargs))
# 
# Path: resources/lib/kindleunpack/unipath.py
# def pathof(s, enc=fsencoding):
#     if s is None:
#         return None
#     if isinstance(s, text_type):
#         return s
#     if isinstance(s, binary_type):
#         try:
#             return s.decode(enc)
#         except:
#             pass
#     return s


from .compatibility_utils import unicode_str, unescapeit
from .compatibility_utils import lzip
from .unipath import pathof
from xml.sax.saxutils import escape as xmlescape
from datetime import datetime
import os
import uuid
            package = '<package version="3.0" xmlns="http://www.idpf.org/2007/opf" prefix="rendition: http://www.idpf.org/vocab/rendition/#" unique-identifier="uid">\n'
            tours = ''
            metadata_tag = '<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">'

        data = []
        data.append('<?xml version="1.0" encoding="utf-8"?>\n')
        data.append(package)
        opf_metadata = self.buildOPFMetadata(metadata_tag, has_obfuscated_fonts)
        data += opf_metadata
        [opf_manifest, spinerefs] = self.buildOPFManifest(ncxname, navname)
        data += opf_manifest
        opf_spine = self.buildOPFSpine(spinerefs, has_ncx)
        data += opf_spine
        data.append(tours)
        if has_guide:
            guide ='<guide>\n' + self.guidetext + '</guide>\n'
            data.append(guide)
        data.append('</package>\n')
        return ''.join(data)

    def writeOPF(self, has_obfuscated_fonts=False):
        if self.isK8:
            data = self.buildEPUBOPF(has_obfuscated_fonts)
            outopf = os.path.join(self.files.k8oebps, EPUB_OPF)
            with open(pathof(outopf), 'wb') as f:
                f.write(data.encode('utf-8'))
            return self.BookId
        else:
            data = self.buildMobi7OPF()
            outopf = os.path.join(self.files.mobi7dir, 'content.opf')

# Path: resources/lib/kindleunpack/compatibility_utils.py
# def unicode_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p
#     return p.decode(enc)
# 
# def unescapeit(sval):
#     return _h.unescape(sval)
# 
# Path: resources/lib/kindleunpack/compatibility_utils.py
# def lzip(*args, **kwargs):
#     return list(zip(*args, **kwargs))
# 
# Path: resources/lib/kindleunpack/unipath.py
# def pathof(s, enc=fsencoding):
#     if s is None:
#         return None
#     if isinstance(s, text_type):
#         return s
#     if isinstance(s, binary_type):
#         try:
#             return s.decode(enc)
#         except:
#             pass
#     return s


# Path: resources/lib/kindleunpack/mobi_opf.py
from .compatibility_utils import unicode_str, unescapeit
from .compatibility_utils import lzip
from .unipath import pathof
from xml.sax.saxutils import escape as xmlescape
from datetime import datetime
import os
import uuid

            package = '<package version="3.0" xmlns="http://www.idpf.org/2007/opf" prefix="rendition: http://www.idpf.org/vocab/rendition/#" unique-identifier="uid">\n'
            tours = ''
            metadata_tag = '<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">'

        data = []
        data.append('<?xml version="1.0" encoding="utf-8"?>\n')
        data.append(package)
        opf_metadata = self.buildOPFMetadata(metadata_tag, has_obfuscated_fonts)
        data += opf_metadata
        [opf_manifest, spinerefs] = self.buildOPFManifest(ncxname, navname)
        data += opf_manifest
        opf_spine = self.buildOPFSpine(spinerefs, has_ncx)
        data += opf_spine
        data.append(tours)
        if has_guide:
            guide ='<guide>\n' + self.guidetext + '</guide>\n'
            data.append(guide)
        data.append('</package>\n')
        return ''.join(data)

    def writeOPF(self, has_obfuscated_fonts=False):
        if self.isK8:
            data = self.buildEPUBOPF(has_obfuscated_fonts)
            outopf = os.path.join(self.files.k8oebps, EPUB_OPF)
            with open(pathof(outopf), 'wb') as f:
                f.write(data.encode('utf-8'))
            return self.BookId
        else:
            data = self.buildMobi7OPF()
            outopf = os.path.join(self.files.mobi7dir, 'content.opf')

            with open(pathof(outopf), 'wb') as f: