from __future__ import print_function import sys import io import os import imp import json from collation.core.preprocessor import PreProcessor from collation.legacy_regularisation.preprocessor_preOct20 import PreProcessor as LegacyPreProcessor from collation.core.exporter_factory import ExporterFactory from collation.core.settings_applier import SettingsApplier import logging logging.basicConfig( handlers=[logging.FileHandler('tomcat/logs/collation_editor.log', 'w', 'utf-8')], level=logging.DEBUG ) def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) logging.debug(*args, **kwargs) def collation(params): eprint('in collate') eprint('params.keys: {}'.format(params.keys())) if 'legacyRegularisation' in params and params['legacyRegularisation'] is True: eprint('constructing LegacyPreProcessor') p = LegacyPreProcessor(params['configs']) else: eprint('constructing non-LegacyPreProcessor') p = PreProcessor(params['configs']) eprint('calling PreProcessor::process_witness_list') output = p.process_witness_list(params['data']) eprint('returned from process_witness_list') #eprint(output) # response.content_type = 'application/json' return json.dumps(output)#, default=json_util.default) def apply_settings(params): data = params['data'] applier = SettingsApplier(data['options']) tokens = applier.apply_settings_to_token_list(data['tokens']) # response.content_type = 'application/json' eprint('about to return from apply_settings, tokens: ' + json.dumps({'tokens': tokens})) return json.dumps({'tokens': tokens}) def apparatus(params): data = params['data'] format = params['format'] if not format: format = 'negative_xml' if format == 'negative_xml' or format == 'positive_xml': file_ext = 'xml' else: file_ext = 'txt' exporter_settings = json.loads(params['settings']) #eprint(exporter_settings) exf = ExporterFactory(exporter_settings) app = exf.export_data(data, format) return app.decode("utf-8") action = 'export' args = sys.argv for item in args: if item.find('--action=') == 0: action = item.split('=')[1] input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') data = input_stream.read() params = json.loads(data) #eprint(params) if action == 'applysettings': eprint('python applysettings started') print(apply_settings(params)) eprint('python applysettings finished') elif action == 'collation': eprint('python collation started') print(collation(params)) eprint('python collation finished') elif action == 'export': eprint('python app export started') print(apparatus(params)) eprint('python app export finished')