Commit 3b43b7c7298b16aab1adfead1b49aa1aed6f3fce
1 parent
0606bdc5
Make minor thrift client modifications
Showing
1 changed file
with
20 additions
and
19 deletions
python/thrift_client.py
| ... | ... | @@ -21,7 +21,7 @@ from multiservice.facade.ttypes import * |
| 21 | 21 | from multiservice.types.ttypes import * |
| 22 | 22 | |
| 23 | 23 | import json2tei |
| 24 | -from tei_writer import write_as_tei_dir | |
| 24 | +import tei_writer | |
| 25 | 25 | |
| 26 | 26 | EXTENSIONS = {'json': '.json', |
| 27 | 27 | 'tei': '.xml', |
| ... | ... | @@ -78,14 +78,13 @@ def go(): |
| 78 | 78 | print "Processing chain was not specified!" |
| 79 | 79 | return |
| 80 | 80 | |
| 81 | - if opts.output and not os.path.isdir(opts.output): | |
| 82 | - print >> sys.stderr, "Output must be a directory!" | |
| 83 | - return | |
| 84 | - | |
| 85 | - if os.path.isdir(opts.input): | |
| 86 | - process_directory(opts.input, opts, args) | |
| 87 | - elif os.path.isfile(opts.input): | |
| 88 | - process_file(opts.input, opts, args, opts.output) | |
| 81 | + if opts.input and os.path.isdir(opts.input): | |
| 82 | + if opts.output and os.path.isdir(opts.output): | |
| 83 | + process_directory(opts.input, opts, args) | |
| 84 | + else: | |
| 85 | + print >> sys.stderr, "Output must be a directory!" | |
| 86 | + elif opts.input and os.path.isfile(opts.input): | |
| 87 | + process_file(opts.input, opts, args, False, opts.output) | |
| 89 | 88 | else: |
| 90 | 89 | process_text(sys.stdin.read(), opts, args) |
| 91 | 90 | |
| ... | ... | @@ -98,16 +97,16 @@ def process_directory(input, opts, service_names): |
| 98 | 97 | textname = os.path.splitext(os.path.basename(filename))[0] |
| 99 | 98 | textoutput = os.path.join(output, textname) |
| 100 | 99 | textinput = os.path.join(input, filename) |
| 101 | - process_file(textinput, opts, service_names, textoutput) | |
| 100 | + process_file(textinput, opts, service_names, True, textoutput) | |
| 102 | 101 | |
| 103 | 102 | |
| 104 | -def process_file(input, opts, service_names, output): | |
| 103 | +def process_file(input, opts, service_names, in_dir, output): | |
| 105 | 104 | with codecs.open(input, 'rt', 'utf-8') as textfile: |
| 106 | 105 | text = textfile.read() |
| 107 | - process_text(text, opts, service_names, output) | |
| 106 | + process_text(text, opts, service_names, in_dir, output) | |
| 108 | 107 | |
| 109 | 108 | |
| 110 | -def process_text(text, opts, service_names, output=None): | |
| 109 | +def process_text(text, opts, service_names, in_dir, output=None): | |
| 111 | 110 | request = createSampleRequest(text, service_names) |
| 112 | 111 | transport, client = getThriftTransportAndClient(opts.host, opts.port) |
| 113 | 112 | try: |
| ... | ... | @@ -126,24 +125,26 @@ def process_text(text, opts, service_names, output=None): |
| 126 | 125 | elif format == 'tei': |
| 127 | 126 | result = json2tei.convert(result, service_names, False) |
| 128 | 127 | else: |
| 129 | - print >> sys.stderr, "Unknown format changed to json!" | |
| 128 | + print >> sys.stderr, "Unknown format, changed to json!" | |
| 130 | 129 | format = 'json' |
| 131 | 130 | result = getResultAsJSON(result) |
| 132 | - write_result(result, format, output) | |
| 131 | + write_result(result, format, in_dir, output) | |
| 133 | 132 | else: |
| 134 | 133 | print >> sys.stderr, client.getException(token) |
| 135 | 134 | finally: |
| 136 | 135 | transport.close() |
| 137 | 136 | |
| 138 | 137 | |
| 139 | -def write_result(result, format, output): | |
| 138 | +def write_result(result, format, in_dir, output): | |
| 140 | 139 | if not output: |
| 141 | 140 | print >> sys.stdout, result |
| 142 | 141 | elif format == 'packagedtei' or format == 'json': |
| 143 | - with codecs.open(output+EXTENSIONS[format], 'wt', 'utf-8') as outfile: | |
| 142 | + if in_dir: | |
| 143 | + output = output+EXTENSIONS[format] | |
| 144 | + with codecs.open(output, 'wt', 'utf-8') as outfile: | |
| 144 | 145 | outfile.write(result) |
| 145 | 146 | elif format == 'tei' and create_and_check_tei_dir(output): |
| 146 | - write_as_tei_dir(result, output) | |
| 147 | + tei_writer.write_as_tei_dir(result, output) | |
| 147 | 148 | |
| 148 | 149 | |
| 149 | 150 | def create_and_check_tei_dir(output): |
| ... | ... | @@ -154,7 +155,7 @@ def create_and_check_tei_dir(output): |
| 154 | 155 | os.mkdir(output) |
| 155 | 156 | dir_ok = True |
| 156 | 157 | else: |
| 157 | - print >> sys.stderr, 'Selected output is not a folder.' | |
| 158 | + print >> sys.stderr, 'Selected output is not a directory.' | |
| 158 | 159 | return dir_ok |
| 159 | 160 | |
| 160 | 161 | |
| ... | ... |