Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.pyc
*.pyc
dist
build
*.egg-info
19 changes: 19 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Elliot Kroo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
File renamed without changes.
7 changes: 4 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from mobi import Mobi

book = Mobi("test/CharlesDarwin.mobi");
import os
path = os.path.dirname(__file__)
book = Mobi(f"{path}/test/CharlesDarwin.mobi");
book.parse();

for record in book:
print record,
print(record)

import pprint
pprint.pprint(book.config)
18 changes: 9 additions & 9 deletions mobi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import unittest
from struct import *
from pprint import pprint
import utils
from lz77 import uncompress_lz77
from . import utils
from .lz77 import uncompress_lz77

class Mobi:
def parse(self):
Expand Down Expand Up @@ -52,7 +52,7 @@ def __init__(self, filename):
self.f = open(filename, "rb");
else:
self.f = filename;
except IOError,e:
except IOError as e:
sys.stderr.write("Could not open %s! " % filename);
raise e;
self.offset = 0;
Expand All @@ -73,7 +73,7 @@ def parseRecordInfoList(self):
"UniqueID",
]
# create tuple with info
results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen]))
results = list(zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])))

# increment offset into file
self.offset += headerlen
Expand Down Expand Up @@ -112,7 +112,7 @@ def parseHeader(self):
]

# unpack header, zip up into list of tuples
results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen]))
results = list(zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])))

# increment offset into file
self.offset += headerlen
Expand Down Expand Up @@ -146,7 +146,7 @@ def parseEXTHHeader(self):
]

# unpack header, zip up into list of tuples
results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen]))
results = list(zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])))

# convert tuple array to dictionary
resultsDict = utils.toDict(results);
Expand Down Expand Up @@ -212,7 +212,7 @@ def parseMobiHeader(self):
]

# unpack header, zip up into list of tuples
results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen]))
results = list(zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])))

# convert tuple array to dictionary
resultsDict = utils.toDict(results);
Expand All @@ -230,7 +230,7 @@ def parseMobiHeader(self):
self.offset += resultsDict['header length'];

def onebits(x, width=16):
return len(filter(lambda x: x == "1", (str((x>>i)&1) for i in xrange(width-1,-1,-1))));
return len([x for x in (str((x>>i)&1) for i in range(width-1,-1,-1)) if x == "1"]);

resultsDict['extra bytes'] = 2*onebits(unpack(">H", self.contents[self.offset-2:self.offset])[0] & 0xFFFE)

Expand All @@ -250,7 +250,7 @@ def parsePalmDOCHeader(self):
]
offset = self.records[0]['record Data Offset'];
# create tuple with info
results = zip(fields, unpack(headerfmt, self.contents[offset:offset+headerlen]))
results = list(zip(fields, unpack(headerfmt, self.contents[offset:offset+headerlen])))

# convert tuple array to dictionary
resultsDict = utils.toDict(results);
Expand Down
14 changes: 7 additions & 7 deletions mobi/lz77.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ def uncompress_lz77(data):
# char = substr($data,$offset++,1);
char = data[offset];
offset += 1;
ord_ = ord(char);
ord_ = char;

# print " ".join([repr(char), hex(ord_)])

# The long if-elsif chain is the best logic for $ord handling
## no critic (Cascading if-elsif chain)
if (ord_ == 0):
# Nulls are literal
text += char;
text += str(char);
elif (ord_ <= 8):
# Next $ord bytes are literal
text += data[offset:offset+ord_] # text .=substr($data,$offset,ord);
text += str(data[offset:offset+ord_]) # text .=substr($data,$offset,ord);
offset += ord_;
elif (ord_ <= 0x7f):
# Values from 0x09 through 0x7f are literal
text += char;
text += str(char);
elif (ord_ <= 0xbf):
# Data is LZ77-compressed

Expand All @@ -48,7 +48,7 @@ def uncompress_lz77(data):

offset += 1;
if (offset > len(data)):
print("WARNING: offset to LZ77 bits is outside of the data: %d" % offset);
print(("WARNING: offset to LZ77 bits is outside of the data: %d" % offset));
return text;

lz77, = struct.unpack('>H', data[offset-2:offset])
Expand All @@ -73,8 +73,8 @@ def uncompress_lz77(data):
for lz77pos in range(lz77length): # for($lz77pos = 0; $lz77pos < $lz77length; $lz77pos++)
textpos = textlength - lz77offset;
if (textpos < 0):
print("WARNING: LZ77 decompression reference is before"+
" beginning of text! %x" % lz77);
print(("WARNING: LZ77 decompression reference is before"+
" beginning of text! %x" % lz77));
return;

text += text[textpos:textpos+1]; #text .= substr($text,$textpos,1);
Expand Down
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name='mobi-python',
version='0.0.1',
scripts=[] ,
author="Elliot Kroo",
author_email="elliot@kroo.net",
description="Mobi Python Library",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/kroo/mobi-python",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)