diff --git a/python/thrift_client.py b/python/thrift_client.py index 17ea969..b05f1a2 100644 --- a/python/thrift_client.py +++ b/python/thrift_client.py @@ -21,7 +21,7 @@ from multiservice.facade.ttypes import * from multiservice.types.ttypes import * import json2tei -from tei_writer import write_as_tei_dir +import tei_writer EXTENSIONS = {'json': '.json', 'tei': '.xml', @@ -78,14 +78,13 @@ def go(): print "Processing chain was not specified!" return - if opts.output and not os.path.isdir(opts.output): - print >> sys.stderr, "Output must be a directory!" - return - - if os.path.isdir(opts.input): - process_directory(opts.input, opts, args) - elif os.path.isfile(opts.input): - process_file(opts.input, opts, args, opts.output) + if opts.input and os.path.isdir(opts.input): + if opts.output and os.path.isdir(opts.output): + process_directory(opts.input, opts, args) + else: + print >> sys.stderr, "Output must be a directory!" + elif opts.input and os.path.isfile(opts.input): + process_file(opts.input, opts, args, False, opts.output) else: process_text(sys.stdin.read(), opts, args) @@ -98,16 +97,16 @@ def process_directory(input, opts, service_names): textname = os.path.splitext(os.path.basename(filename))[0] textoutput = os.path.join(output, textname) textinput = os.path.join(input, filename) - process_file(textinput, opts, service_names, textoutput) + process_file(textinput, opts, service_names, True, textoutput) -def process_file(input, opts, service_names, output): +def process_file(input, opts, service_names, in_dir, output): with codecs.open(input, 'rt', 'utf-8') as textfile: text = textfile.read() - process_text(text, opts, service_names, output) + process_text(text, opts, service_names, in_dir, output) -def process_text(text, opts, service_names, output=None): +def process_text(text, opts, service_names, in_dir, output=None): request = createSampleRequest(text, service_names) transport, client = getThriftTransportAndClient(opts.host, opts.port) try: @@ -126,24 +125,26 @@ def process_text(text, opts, service_names, output=None): elif format == 'tei': result = json2tei.convert(result, service_names, False) else: - print >> sys.stderr, "Unknown format changed to json!" + print >> sys.stderr, "Unknown format, changed to json!" format = 'json' result = getResultAsJSON(result) - write_result(result, format, output) + write_result(result, format, in_dir, output) else: print >> sys.stderr, client.getException(token) finally: transport.close() -def write_result(result, format, output): +def write_result(result, format, in_dir, output): if not output: print >> sys.stdout, result elif format == 'packagedtei' or format == 'json': - with codecs.open(output+EXTENSIONS[format], 'wt', 'utf-8') as outfile: + if in_dir: + output = output+EXTENSIONS[format] + with codecs.open(output, 'wt', 'utf-8') as outfile: outfile.write(result) elif format == 'tei' and create_and_check_tei_dir(output): - write_as_tei_dir(result, output) + tei_writer.write_as_tei_dir(result, output) def create_and_check_tei_dir(output): @@ -154,7 +155,7 @@ def create_and_check_tei_dir(output): os.mkdir(output) dir_ok = True else: - print >> sys.stderr, 'Selected output is not a folder.' + print >> sys.stderr, 'Selected output is not a directory.' return dir_ok