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,7 +21,7 @@ from multiservice.facade.ttypes import * | ||
21 | from multiservice.types.ttypes import * | 21 | from multiservice.types.ttypes import * |
22 | 22 | ||
23 | import json2tei | 23 | import json2tei |
24 | -from tei_writer import write_as_tei_dir | 24 | +import tei_writer |
25 | 25 | ||
26 | EXTENSIONS = {'json': '.json', | 26 | EXTENSIONS = {'json': '.json', |
27 | 'tei': '.xml', | 27 | 'tei': '.xml', |
@@ -78,14 +78,13 @@ def go(): | @@ -78,14 +78,13 @@ def go(): | ||
78 | print "Processing chain was not specified!" | 78 | print "Processing chain was not specified!" |
79 | return | 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 | else: | 88 | else: |
90 | process_text(sys.stdin.read(), opts, args) | 89 | process_text(sys.stdin.read(), opts, args) |
91 | 90 | ||
@@ -98,16 +97,16 @@ def process_directory(input, opts, service_names): | @@ -98,16 +97,16 @@ def process_directory(input, opts, service_names): | ||
98 | textname = os.path.splitext(os.path.basename(filename))[0] | 97 | textname = os.path.splitext(os.path.basename(filename))[0] |
99 | textoutput = os.path.join(output, textname) | 98 | textoutput = os.path.join(output, textname) |
100 | textinput = os.path.join(input, filename) | 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 | with codecs.open(input, 'rt', 'utf-8') as textfile: | 104 | with codecs.open(input, 'rt', 'utf-8') as textfile: |
106 | text = textfile.read() | 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 | request = createSampleRequest(text, service_names) | 110 | request = createSampleRequest(text, service_names) |
112 | transport, client = getThriftTransportAndClient(opts.host, opts.port) | 111 | transport, client = getThriftTransportAndClient(opts.host, opts.port) |
113 | try: | 112 | try: |
@@ -126,24 +125,26 @@ def process_text(text, opts, service_names, output=None): | @@ -126,24 +125,26 @@ def process_text(text, opts, service_names, output=None): | ||
126 | elif format == 'tei': | 125 | elif format == 'tei': |
127 | result = json2tei.convert(result, service_names, False) | 126 | result = json2tei.convert(result, service_names, False) |
128 | else: | 127 | else: |
129 | - print >> sys.stderr, "Unknown format changed to json!" | 128 | + print >> sys.stderr, "Unknown format, changed to json!" |
130 | format = 'json' | 129 | format = 'json' |
131 | result = getResultAsJSON(result) | 130 | result = getResultAsJSON(result) |
132 | - write_result(result, format, output) | 131 | + write_result(result, format, in_dir, output) |
133 | else: | 132 | else: |
134 | print >> sys.stderr, client.getException(token) | 133 | print >> sys.stderr, client.getException(token) |
135 | finally: | 134 | finally: |
136 | transport.close() | 135 | transport.close() |
137 | 136 | ||
138 | 137 | ||
139 | -def write_result(result, format, output): | 138 | +def write_result(result, format, in_dir, output): |
140 | if not output: | 139 | if not output: |
141 | print >> sys.stdout, result | 140 | print >> sys.stdout, result |
142 | elif format == 'packagedtei' or format == 'json': | 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 | outfile.write(result) | 145 | outfile.write(result) |
145 | elif format == 'tei' and create_and_check_tei_dir(output): | 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 | def create_and_check_tei_dir(output): | 150 | def create_and_check_tei_dir(output): |
@@ -154,7 +155,7 @@ def create_and_check_tei_dir(output): | @@ -154,7 +155,7 @@ def create_and_check_tei_dir(output): | ||
154 | os.mkdir(output) | 155 | os.mkdir(output) |
155 | dir_ok = True | 156 | dir_ok = True |
156 | else: | 157 | else: |
157 | - print >> sys.stderr, 'Selected output is not a folder.' | 158 | + print >> sys.stderr, 'Selected output is not a directory.' |
158 | return dir_ok | 159 | return dir_ok |
159 | 160 | ||
160 | 161 |