# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def unicode_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p
#     return p.decode(enc)


from .compatibility_utils import PY2, unicode_str
import struct
import re
        self.pagenames, self.pageMap = _parseNames(self.pm_nn, self.pmstr)

    def getPageMap(self):
        return self.pageMap

    def getNames(self):
        return self.pagenames

    def getOffsets(self):
        return self.pageoffsets

    # page-map.xml will be unicode but encoded to utf-8 immediately before being written to a file
    def generateKF8PageMapXML(self, k8proc):
        pagemapxml = '<page-map xmlns="http://www.idpf.org/2007/opf">\n'
        for i in range(len(self.pagenames)):
            pos = self.pageoffsets[i]
            name = self.pagenames[i]
            if name is not None and name != "":
                [pn, dir, filename, skelpos, skelend, aidtext] = k8proc.getSkelInfo(pos)
                idtext = unicode_str(k8proc.getPageIDTag(pos))
                linktgt = unicode_str(filename)
                if idtext != '':
                    linktgt += '#' + idtext
                pagemapxml += '<page name="%s" href="%s/%s" />\n' % (name, dir, linktgt)
        pagemapxml += "</page-map>\n"
        return pagemapxml

    def generateAPNX(self, apnx_meta):
        if apnx_meta['format'] == 'MOBI_8':
            content_header = '{"contentGuid":"%(contentGuid)s","asin":"%(asin)s","cdeType":"%(cdeType)s","format":"%(format)s","fileRevisionId":"1","acr":"%(acr)s"}' %apnx_meta

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def unicode_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p
#     return p.decode(enc)


# Path: resources/lib/kindleunpack/mobi_pagemap.py
from .compatibility_utils import PY2, unicode_str
import struct
import re

        self.pagenames, self.pageMap = _parseNames(self.pm_nn, self.pmstr)

    def getPageMap(self):
        return self.pageMap

    def getNames(self):
        return self.pagenames

    def getOffsets(self):
        return self.pageoffsets

    # page-map.xml will be unicode but encoded to utf-8 immediately before being written to a file
    def generateKF8PageMapXML(self, k8proc):
        pagemapxml = '<page-map xmlns="http://www.idpf.org/2007/opf">\n'
        for i in range(len(self.pagenames)):
            pos = self.pageoffsets[i]
            name = self.pagenames[i]
            if name is not None and name != "":
                [pn, dir, filename, skelpos, skelend, aidtext] = k8proc.getSkelInfo(pos)
                idtext = unicode_str(k8proc.getPageIDTag(pos))
                linktgt = unicode_str(filename)
                if idtext != '':
                    linktgt += '#' + idtext
                pagemapxml += '<page name="%s" href="%s/%s" />\n' % (name, dir, linktgt)
        pagemapxml += "</page-map>\n"
        return pagemapxml

    def generateAPNX(self, apnx_meta):
        if apnx_meta['format'] == 'MOBI_8':
            content_header = '{"contentGuid":"%(contentGuid)s","asin":"%(asin)s","cdeType":"%(cdeType)s","format":"%(format)s","fileRevisionId":"1","acr":"%(acr)s"}' %apnx_meta

        else: