View Single Post
Old 11-23-2014, 01:40 PM   #14
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 8,476
Karma: 5703586
Join Date: Nov 2009
Device: many
Hi Doitsu,

Quote:
Originally Posted by Doitsu View Post
@KevinH: Does bk.addfile() support bytearrays?
Technically it is either a "bytestring" ie. a standard non-unicode string in python2.7 or it has type "bytes" in python3.4. But yes, as long as you specify the media-type properly, the binary data will not be touched and will be written properly.

A "bytearray" is a closely related but different type under both python2.7 and python3.4

Quote:
I did a quick test and came up with the following proof-of-concept code, which will add a blackletter ttf font located in the plugin's directory to the epub:

Code:
#!/usr/bin/env python
import os, inspect, uuid

def run(bk):
    binary_file = 'WallauRundgotisch-Heavy.ttf'
    binary_path = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), binary_file)
    file_handle = open(binary_path, 'rb')
    uniqueid = 'id' + str(uuid.uuid4())[24:82]
    basename = 'Fonts/' + binary_file
    data = bytearray(file_handle.read())
    mime = 'application/x-font-ttf'
    bk.addfile(uniqueid, basename, data, mime)
    
    return 0
I would simplify this only slightly and not use the bytearray class at all. Also the basename should not have the directory at all, the correct place inside of Sigil should again be determined by the mime-type/file extension automatically.

Using DiapDealer's KindleImport as a model:

Code:
SCRIPT_DIR = os.path.normpath(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))))

def run(bk):
    binary_file = 'WallauRundgotisch-Heavy.ttf'
    binary_path = os.path.join(SCRIPT_DIR, binary_file)
    data = ''
    with open(binary_path, 'rb') as f:
        data = f.read()
    uniqueid = 'id' + str(uuid.uuid4())[24:82]
    bk.addfile(uniqueid, binary_file, data, 'application/x-font-ttf')
    return 0

FWIW: I personally do not like creating uuid based manifest ids. I have my own unique id creator routine I invoke. Something along the lines of:

id = create_unique_id("fnt", 0)

Code:
def create_unique_id(base, cnt):
    id = base + "%d" % cnt
    while(bk.id_to_mime(id,None) is not None):
        cnt +=1
        id = base + "%d" % cnt
    return id
Others will simply pick a name and preface it with "x" until unique. There is of course no one "right" way.


Hope something here helps.

Kevin

Last edited by KevinH; 11-23-2014 at 04:49 PM.
KevinH is offline   Reply With Quote