Commit 02fd61bafc16ca3d945334c2cf911f71652a6135

Authored by Bartłomiej Nitoń
1 parent 2747b8fc

Minor thrift_client code changes.

Showing 1 changed file with 11 additions and 3 deletions
python/thrift_client.py
@@ -26,7 +26,8 @@ from tei_writer import write_as_tei_dir @@ -26,7 +26,8 @@ from tei_writer import write_as_tei_dir
26 EXTENSIONS = {'json': '.json', 26 EXTENSIONS = {'json': '.json',
27 'tei': '.xml', 27 'tei': '.xml',
28 'packagedtei': '.xml'} 28 'packagedtei': '.xml'}
29 - 29 +
  30 +
30 def createSampleRequest(text, serviceNames): 31 def createSampleRequest(text, serviceNames):
31 ttext=TText(paragraphs=[TParagraph(text=chunk) 32 ttext=TText(paragraphs=[TParagraph(text=chunk)
32 for chunk in re.split(r'\n\n+', text) if chunk]) 33 for chunk in re.split(r'\n\n+', text) if chunk])
@@ -35,6 +36,7 @@ def createSampleRequest(text, serviceNames): @@ -35,6 +36,7 @@ def createSampleRequest(text, serviceNames):
35 request = ObjectRequest(ttext, chain) 36 request = ObjectRequest(ttext, chain)
36 return request 37 return request
37 38
  39 +
38 def getThriftTransportAndClient(host, port): 40 def getThriftTransportAndClient(host, port):
39 transport = TSocket.TSocket(host, port) 41 transport = TSocket.TSocket(host, port)
40 try: 42 try:
@@ -47,10 +49,12 @@ def getThriftTransportAndClient(host, port): @@ -47,10 +49,12 @@ def getThriftTransportAndClient(host, port):
47 transport.close() 49 transport.close()
48 raise 50 raise
49 51
  52 +
50 def getResultAsJSON(result): 53 def getResultAsJSON(result):
51 jsonStr = jsonpickle.encode(result, unpicklable=False) 54 jsonStr = jsonpickle.encode(result, unpicklable=False)
52 return json.dumps(json.load(StringIO.StringIO(jsonStr)), sort_keys=True, indent=4) 55 return json.dumps(json.load(StringIO.StringIO(jsonStr)), sort_keys=True, indent=4)
53 56
  57 +
54 def go(): 58 def go():
55 parser = OptionParser() 59 parser = OptionParser()
56 parser.add_option('-p', '--port', type='int', action='store', 60 parser.add_option('-p', '--port', type='int', action='store',
@@ -85,6 +89,7 @@ def go(): @@ -85,6 +89,7 @@ def go():
85 else: 89 else:
86 process_text(sys.stdin.read(), opts, args) 90 process_text(sys.stdin.read(), opts, args)
87 91
  92 +
88 def process_directory(input, opts, service_names): 93 def process_directory(input, opts, service_names):
89 input = os.path.abspath(input) 94 input = os.path.abspath(input)
90 output = os.path.abspath(opts.output) 95 output = os.path.abspath(opts.output)
@@ -95,11 +100,13 @@ def process_directory(input, opts, service_names): @@ -95,11 +100,13 @@ def process_directory(input, opts, service_names):
95 textinput = os.path.join(input, filename) 100 textinput = os.path.join(input, filename)
96 process_file(textinput, opts, service_names, textoutput) 101 process_file(textinput, opts, service_names, textoutput)
97 102
  103 +
98 def process_file(input, opts, service_names, output): 104 def process_file(input, opts, service_names, output):
99 with codecs.open(input, 'rt', 'utf-8') as textfile: 105 with codecs.open(input, 'rt', 'utf-8') as textfile:
100 text = textfile.read() 106 text = textfile.read()
101 process_text(text, opts, service_names, output) 107 process_text(text, opts, service_names, output)
102 108
  109 +
103 def process_text(text, opts, service_names, output): 110 def process_text(text, opts, service_names, output):
104 request = createSampleRequest(text, service_names) 111 request = createSampleRequest(text, service_names)
105 transport, client = getThriftTransportAndClient(opts.host, opts.port) 112 transport, client = getThriftTransportAndClient(opts.host, opts.port)
@@ -117,8 +124,6 @@ def process_text(text, opts, service_names, output): @@ -117,8 +124,6 @@ def process_text(text, opts, service_names, output):
117 elif format == 'packagedtei': 124 elif format == 'packagedtei':
118 result = json2tei.convert(result, service_names, True) 125 result = json2tei.convert(result, service_names, True)
119 elif format == 'tei': 126 elif format == 'tei':
120 - json_result = getResultAsJSON(result)  
121 - write_result(json_result, 'json', output)  
122 result = json2tei.convert(result, service_names, False) 127 result = json2tei.convert(result, service_names, False)
123 else: 128 else:
124 print >> sys.stderr, "Unknown format changed to json!" 129 print >> sys.stderr, "Unknown format changed to json!"
@@ -130,6 +135,7 @@ def process_text(text, opts, service_names, output): @@ -130,6 +135,7 @@ def process_text(text, opts, service_names, output):
130 finally: 135 finally:
131 transport.close() 136 transport.close()
132 137
  138 +
133 def write_result(result, format, output): 139 def write_result(result, format, output):
134 if not output: 140 if not output:
135 print >> sys.stdout, result 141 print >> sys.stdout, result
@@ -139,6 +145,7 @@ def write_result(result, format, output): @@ -139,6 +145,7 @@ def write_result(result, format, output):
139 elif format == 'tei' and create_and_check_tei_dir(output): 145 elif format == 'tei' and create_and_check_tei_dir(output):
140 write_as_tei_dir(result, output) 146 write_as_tei_dir(result, output)
141 147
  148 +
142 def create_and_check_tei_dir(output): 149 def create_and_check_tei_dir(output):
143 dir_ok = False 150 dir_ok = False
144 if os.path.exists(output) and os.path.isdir(output): 151 if os.path.exists(output) and os.path.isdir(output):
@@ -150,5 +157,6 @@ def create_and_check_tei_dir(output): @@ -150,5 +157,6 @@ def create_and_check_tei_dir(output):
150 print >> sys.stderr, 'Selected output is not a folder.' 157 print >> sys.stderr, 'Selected output is not a folder.'
151 return dir_ok 158 return dir_ok
152 159
  160 +
153 if __name__ == '__main__': 161 if __name__ == '__main__':
154 go() 162 go()