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