diff --git a/.gitignore b/.gitignore index 7e99e36..b0de811 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -*.pyc \ No newline at end of file +*.pyc +dist +build +*.egg-info diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..1f622da --- /dev/null +++ b/LICENCE @@ -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. \ No newline at end of file diff --git a/README.mdown b/README.md similarity index 100% rename from README.mdown rename to README.md diff --git a/example.py b/example.py index 4b3cfd6..a7e0c1e 100644 --- a/example.py +++ b/example.py @@ -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) \ No newline at end of file diff --git a/mobi/__init__.py b/mobi/__init__.py index 287ed87..65effa1 100644 --- a/mobi/__init__.py +++ b/mobi/__init__.py @@ -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): @@ -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; @@ -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 @@ -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 @@ -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); @@ -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); @@ -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) @@ -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); diff --git a/mobi/lz77.py b/mobi/lz77.py index c61ddad..72f5a4c 100644 --- a/mobi/lz77.py +++ b/mobi/lz77.py @@ -19,7 +19,7 @@ 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_)]) @@ -27,14 +27,14 @@ def uncompress_lz77(data): ## 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 @@ -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]) @@ -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); diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..785f983 --- /dev/null +++ b/setup.py @@ -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", + ], + ) \ No newline at end of file