From f0c23b2e3a1a1d7f37d9f00118632d116b3c8582 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 17 Jan 2011 16:57:53 +0100 Subject: [PATCH] text: use files, not filenames --- librarian/text.py | 21 ++++++--------------- scripts/book2txt | 3 ++- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/librarian/text.py b/librarian/text.py index 28c36d3..e1ca26b 100644 --- a/librarian/text.py +++ b/librarian/text.py @@ -5,10 +5,7 @@ # from librarian import dcparser, parser, functions from lxml import etree -import cStringIO -import codecs import os -import re functions.reg_substitute_entities() @@ -30,23 +27,17 @@ Wersja lektury w opracowaniu merytorycznym i krytycznym (przypisy i motywy) dost %(text)s """ -def transform(input_filename, output_filename, is_file=True, parse_dublincore=True, **options): - """Transforms file input_filename in XML to output_filename in TXT.""" +def transform(input_file, output_file, parse_dublincore=True, **options): + """Transforms input_file in XML to output_file in TXT.""" # Parse XSLT style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt') style = etree.parse(style_filename) - if is_file: - document = parser.WLDocument.from_file(input_filename, True, parse_dublincore=parse_dublincore) - else: - document = parser.WLDocument.from_string(input_filename, True, parse_dublincore=parse_dublincore) - + document = parser.WLDocument.from_file(input_file, True, parse_dublincore=parse_dublincore) result = document.transform(style, **options) - output_file = codecs.open(output_filename, 'wb', encoding='utf-8') - if parse_dublincore: - parsed_dc = dcparser.parse(input_filename) + parsed_dc = dcparser.BookInfo.from_element(document.edoc) description = parsed_dc.description url = parsed_dc.url license_description = parsed_dc.license_description @@ -66,11 +57,11 @@ def transform(input_filename, output_filename, is_file=True, parse_dublincore=Tr license = "" license_description = "" source = "" - output_file.write(TEMPLATE % { + output_file.write((TEMPLATE % { 'description': description, 'url': url, 'license_description': license_description, 'text': unicode(result), 'source': source, - }) + }).encode('utf-8')) diff --git a/scripts/book2txt b/scripts/book2txt index 2317688..d5e18c6 100755 --- a/scripts/book2txt +++ b/scripts/book2txt @@ -38,7 +38,8 @@ if __name__ == '__main__': output_filename = os.path.splitext(input_filename)[0] + '.txt' try: - text.transform(input_filename, output_filename, parse_dublincore=options.parse_dublincore, + output_file = open(output_filename, 'w') + text.transform(open(input_filename), output_file, parse_dublincore=options.parse_dublincore, wrapping=str(options.wrapping)) except ParseError, e: print '%(file)s:%(name)s:%(message)s' % { -- 2.20.1