mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-02-22 18:15:49 +01:00
Here is the first batch of modules, which are needed for converting several formats to LRF. Some of the logic has been change, more cleanups will follow.
57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
from __future__ import unicode_literals, absolute_import, print_function, division
|
|
#########################################################################
|
|
# #
|
|
# #
|
|
# copyright 2002 Paul Henry Tremblay #
|
|
# #
|
|
# This program is distributed in the hope that it will be useful, #
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
|
|
# General Public License for more details. #
|
|
# #
|
|
# #
|
|
#########################################################################
|
|
import os
|
|
|
|
from ebook_converter.ebooks.rtf2xml import copy
|
|
from ebook_converter.utils.cleantext import clean_ascii_chars
|
|
from ebook_converter.ptempfile import better_mktemp
|
|
|
|
|
|
class FixLineEndings:
|
|
"""Fix line endings"""
|
|
|
|
def __init__(self,
|
|
bug_handler,
|
|
in_file=None,
|
|
copy=None,
|
|
run_level=1,
|
|
replace_illegals=1,
|
|
):
|
|
self.__file = in_file
|
|
self.__bug_handler = bug_handler
|
|
self.__copy = copy
|
|
self.__run_level = run_level
|
|
self.__write_to = better_mktemp()
|
|
self.__replace_illegals = replace_illegals
|
|
|
|
def fix_endings(self):
|
|
# read
|
|
with open(self.__file, 'rb') as read_obj:
|
|
input_file = read_obj.read()
|
|
# calibre go from win and mac to unix
|
|
input_file = input_file.replace(b'\r\n', b'\n')
|
|
input_file = input_file.replace(b'\r', b'\n')
|
|
# remove ASCII invalid chars : 0 to 8 and 11-14 to 24-26-27
|
|
if self.__replace_illegals:
|
|
input_file = clean_ascii_chars(input_file)
|
|
# write
|
|
with open(self.__write_to, 'wb') as write_obj:
|
|
write_obj.write(input_file)
|
|
# copy
|
|
copy_obj = copy.Copy(bug_handler=self.__bug_handler)
|
|
if self.__copy:
|
|
copy_obj.copy_file(self.__write_to, "line_endings.data")
|
|
copy_obj.rename(self.__write_to, self.__file)
|
|
os.remove(self.__write_to)
|