From 02fd61bafc16ca3d945334c2cf911f71652a6135 Mon Sep 17 00:00:00 2001 From: Bartlomiej Niton <bartek.niton@gmail.com> Date: Tue, 30 May 2017 12:58:59 +0200 Subject: [PATCH] Minor thrift_client code changes. --- python/thrift_client.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/python/thrift_client.py b/python/thrift_client.py index 0262dd5..0419663 100644 --- a/python/thrift_client.py +++ b/python/thrift_client.py @@ -26,7 +26,8 @@ from tei_writer import write_as_tei_dir EXTENSIONS = {'json': '.json', 'tei': '.xml', 'packagedtei': '.xml'} - + + def createSampleRequest(text, serviceNames): ttext=TText(paragraphs=[TParagraph(text=chunk) for chunk in re.split(r'\n\n+', text) if chunk]) @@ -35,6 +36,7 @@ def createSampleRequest(text, serviceNames): request = ObjectRequest(ttext, chain) return request + def getThriftTransportAndClient(host, port): transport = TSocket.TSocket(host, port) try: @@ -47,10 +49,12 @@ def getThriftTransportAndClient(host, port): transport.close() raise + def getResultAsJSON(result): jsonStr = jsonpickle.encode(result, unpicklable=False) return json.dumps(json.load(StringIO.StringIO(jsonStr)), sort_keys=True, indent=4) + def go(): parser = OptionParser() parser.add_option('-p', '--port', type='int', action='store', @@ -85,6 +89,7 @@ def go(): else: process_text(sys.stdin.read(), opts, args) + def process_directory(input, opts, service_names): input = os.path.abspath(input) output = os.path.abspath(opts.output) @@ -95,11 +100,13 @@ def process_directory(input, opts, service_names): textinput = os.path.join(input, filename) process_file(textinput, opts, service_names, textoutput) + def process_file(input, opts, service_names, output): with codecs.open(input, 'rt', 'utf-8') as textfile: text = textfile.read() process_text(text, opts, service_names, output) + def process_text(text, opts, service_names, output): request = createSampleRequest(text, service_names) transport, client = getThriftTransportAndClient(opts.host, opts.port) @@ -117,8 +124,6 @@ def process_text(text, opts, service_names, output): elif format == 'packagedtei': result = json2tei.convert(result, service_names, True) elif format == 'tei': - json_result = getResultAsJSON(result) - write_result(json_result, 'json', output) result = json2tei.convert(result, service_names, False) else: print >> sys.stderr, "Unknown format changed to json!" @@ -130,6 +135,7 @@ def process_text(text, opts, service_names, output): finally: transport.close() + def write_result(result, format, output): if not output: print >> sys.stdout, result @@ -139,6 +145,7 @@ def write_result(result, format, output): elif format == 'tei' and create_and_check_tei_dir(output): write_as_tei_dir(result, output) + def create_and_check_tei_dir(output): dir_ok = False if os.path.exists(output) and os.path.isdir(output): @@ -150,5 +157,6 @@ def create_and_check_tei_dir(output): print >> sys.stderr, 'Selected output is not a folder.' return dir_ok + if __name__ == '__main__': go() -- libgit2 0.22.2