1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-11 07:33:35 +02:00

Sorted out mime initialization.

Every mime related function in main __init__.py has a flag check for the
check if initialization has already done. This is nonsense, since it
should be done implicitly early on the converter is starting.

This commit straight the things out, and initialization is done in cli
module.

Also, function guess_type was removed, since it's just a proxy for
mimetypes.guess_type function.
This commit is contained in:
2020-06-14 15:41:18 +02:00
parent b23a59f223
commit 1465e4267f
22 changed files with 94 additions and 112 deletions

View File

@@ -1,10 +1,11 @@
import mimetypes
import os
import shutil
import sys
from lxml import etree
from ebook_converter import walk, guess_type
from ebook_converter import walk
from ebook_converter.ebooks.metadata import authors_to_sort_string
from ebook_converter.ebooks.metadata import string_to_authors
from ebook_converter.ebooks.metadata.book.base import Metadata
@@ -150,7 +151,7 @@ class DOCX(object):
ext = name.rpartition('.')[-1].lower()
if ext in self.default_content_types:
return self.default_content_types[ext]
return guess_type(name)[0]
return mimetypes.guess_type(name)[0]
def read_package_relationships(self):
try:

View File

@@ -1,5 +1,6 @@
import sys, os, re, math, errno, uuid, numbers
from collections import OrderedDict, defaultdict
import mimetypes
from lxml import etree
from lxml import html
@@ -24,8 +25,6 @@ from ebook_converter.ebooks.metadata.opf2 import OPFCreator
from ebook_converter.utils.localization import canonicalize_lang, lang_as_iso639_1
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
NBSP = '\xa0'
@@ -384,7 +383,7 @@ class Convert(object):
opf.create_manifest_from_files_in([self.dest_dir])
for item in opf.manifest:
if item.media_type == 'text/html':
item.media_type = guess_type('a.xhtml')[0]
item.media_type = mimetypes.guess_type('a.xhtml')[0]
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)

View File

@@ -1,9 +1,9 @@
import mimetypes
import textwrap, os
from lxml import etree
from lxml.builder import ElementMaker
from ebook_converter import guess_type
from ebook_converter.constants_old import numeric_version, __appname__
from ebook_converter.ebooks.docx.names import DOCXNamespace
from ebook_converter.ebooks.metadata import authors_to_string
@@ -179,16 +179,20 @@ class DOCX(object):
types.append(E.Override(PartName=partname, ContentType=mt))
added = {'png', 'gif', 'jpeg', 'jpg', 'svg', 'xml'}
for ext in added:
types.append(E.Default(Extension=ext, ContentType=guess_type('a.'+ext)[0]))
for ext, mt in {"rels": "application/vnd.openxmlformats-package.relationships+xml",
"odttf": "application/vnd.openxmlformats-officedocument.obfuscatedFont"}.items():
types.append(E.Default(Extension=ext,
ContentType=mimetypes.guess_type('a.' +
ext)[0]))
for ext, mt in {"rels": "application/vnd.openxmlformats-package"
".relationships+xml",
"odttf": "application/vnd.openxmlformats-"
"officedocument.obfuscatedFont"}.items():
added.add(ext)
types.append(E.Default(Extension=ext, ContentType=mt))
for fname in self.images:
ext = fname.rpartition(os.extsep)[-1]
if ext not in added:
added.add(ext)
mt = guess_type('a.' + ext)[0]
mt = mimetypes.guess_type('a.' + ext)[0]
if mt:
types.append(E.Default(Extension=ext, ContentType=mt))
return xml2str(types)