import json import sys import urllib.request import logging logging.basicConfig(filename='tomcat/logs/collation_editor.log',level=logging.DEBUG) def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) logging.debug(*args, **kwargs) class VMRCollate (object) : def doCollate(self, data, options) : eprint('Hey!!! We\'re calling our special gift from Cat!!!') #use collateX Java microservices if 'algorithm' in options: data['algorithm'] = options['algorithm'] #'needleman-wunsch'#'dekker' #'needleman-wunsch'#'dekker-experimental'# if 'tokenComparator' in options: data['tokenComparator'] = options['tokenComparator'] #{"type": "levenshtein", "distance": 2}#{'type': 'equality'} if 'collatexHost' in options: target = 'http://%s/collate' % options['collatexHost'] else: target = 'http://localhost:7369//collate' json_witnesses = json.dumps(data)#, default=json_util.default) if 'outputFormat' in options: accept_header = self.convert_header_argument(options['outputFormat']) else: accept_header = "application/json" req = urllib.request.Request(target) req.add_header('content-type', 'application/json') req.add_header('Accept', accept_header) eprint('about to open target: ' + target) # with open("small_witness.json", "w") as write_file: # json.dump(payload, write_file) try: response = urllib.request.urlopen(req, json_witnesses.encode('utf-8')) except urllib.error.HTTPError as e: eprint(e.code) eprint(e.read()) return #print(response.read().decode('utf-8')) return response.read() def convert_header_argument(self, accept): """Convert shortname to MIME type.""" if accept == 'json' or accept == 'lcs': return "application/json" elif accept == 'tei': return "application/tei+xml" elif accept == 'graphml': return 'application/graphml+xml' elif accept == 'dot': return 'text/plain' elif accept == 'svg': return 'image/svg+xml'