def lgr_intersect_union(request, lgr_info_1, lgr_info_2, action): """ Compare 2 LGRs for union/intersection. :param request: The request object. :param lgr_info_1: The first LGR info object. :param lgr_info_2: The second LGR info object. :param action: One of "UNION", "INTERSECTION". :return: LGR id of generated LGR. If there is a validation error, LGRCompInvalidException is raised and contains the resulting XML """ result_lgr = None if action == 'INTERSECTION': result_lgr = intersect_lgrs(lgr_info_1.lgr, lgr_info_2.lgr) elif action == 'UNION': result_lgr = union_lgrs(lgr_info_1.lgr, lgr_info_2.lgr) # Generate new slug (LGR id) lgr_id = slugify(result_lgr.name) lgr_info = LGRInfo(name=lgr_id, lgr=result_lgr) lgr_info.update_xml(pretty_print=True) try: session_open_lgr(request, lgr_id, lgr_info.xml, validating_repertoire_name=None, validate=True) except LGRValidationException as e: raise LGRCompInvalidException(lgr_info.xml, e.args[0]) return lgr_id
def lgr_set_annotate_task(lgr_json, script_lgr_json, labels_json, email_address, storage_path): """ Compute dispositions of labels in a LGR. :param lgr_json: The LGRInfo as a JSON object. :param script_lgr_json: The LGRinfo for the script used to check label validity as a JSON object. :param labels_json: The LabelInfo as a JSON object. :param email_address: The e-mail address where the results will be sent :param storage_path: The place where results will be stored """ lgr_info = LGRInfo.from_dict(lgr_json) script_lgr = LGRInfo.from_dict(script_lgr_json).lgr labels_info = LabelInfo.from_dict(labels_json) set_labels_info = lgr_info.set_labels_info if set_labels_info is None: set_labels_info = LabelInfo(name='None', labels=[]) logger.info("Starting task 'annotate' for LGR set %s, with set labels %s, for file %s", lgr_info.name, set_labels_info.name, labels_info.name) body = "Hi,\nThe processing of annotation from labels provided in the " \ "file '{f}' in LGR set '{lgr}' with script '{script}' has".format(f=labels_info.name, lgr=lgr_info.lgr.name, script=script_lgr.name) _lgr_tool_task(storage_path, base_filename='annotation_{0}'.format(lgr_info.lgr.name), email_subject='LGR Toolset annotation result', email_body=body, email_address=email_address, cb=lgr_set_annotate_labels, lgr=lgr_info.lgr, script_lgr=script_lgr, set_labels=set_labels_info.labels, labels_file=labels_info.labels)
def _save_resulting_lgr(l): # Generate new slug (LGR id) lgr_id = slugify("{}_harmonized_{}".format( l.name, time.strftime('%Y%m%d_%H%M%S'))) lgr_info = LGRInfo(name=lgr_id, lgr=l) lgr_info.update_xml(pretty_print=True) session_save_lgr(request, lgr_info) return lgr_id
def validate_label_task(lgr_json, label, email_address, storage_path): """ Compute label validation variants of labels in a LGR. :param lgr_json: The LGRInfo as a JSON object. :param label: The label to validate, as a list of code points. :param email_address: The e-mail address where the results will be sent :param storage_path: The place where results will be stored """ lgr_info = LGRInfo.from_dict(lgr_json) udata = get_db_by_version(lgr_info.lgr.metadata.unicode_version) logger.info("Starting task 'validate label' for %s, for input label '%s'", lgr_info.name, label) u_label = cp_to_ulabel(label) body = "Hi,\nThe processing of label validation for label '{label}' in LGR '{lgr}' has".format(label=u_label, lgr=lgr_info.name) _lgr_tool_task(storage_path, base_filename='label_validation_{0}'.format(lgr_info.name), email_subject='LGR Toolset label validation result', email_body=body, email_address=email_address, cb=lgr_validate_label, lgr=lgr_info.lgr, label=label, udata=udata)
def cross_script_variants_task(lgr_json, labels_json, email_address, storage_path): """ Compute cross-script variants of labels in a LGR. :param lgr_json: The LGRInfo as a JSON object. :param labels_json: The LabelInfo as a JSON object. :param email_address: The e-mail address where the results will be sent :param storage_path: The place where results will be stored """ lgr_info = LGRInfo.from_dict(lgr_json) labels_info = LabelInfo.from_dict(labels_json) logger.info("Starting task 'cross-script variants' for %s, for file %s", lgr_info.name, labels_info.name) body = "Hi,\nThe processing of cross-script variants from labels provided in the " \ "file '{f}' in LGR '{lgr}' has".format(f=labels_info.name, lgr=lgr_info.name) _lgr_tool_task(storage_path, base_filename='cross_script_variants_{0}'.format(lgr_info.name), email_subject='LGR Toolset cross-script variants result', email_body=body, email_address=email_address, cb=lgr_cross_script_variants, lgr=lgr_info.lgr, labels_file=labels_info.labels)
def annotate_task(lgr_json, labels_json, email_address, storage_path): """ Compute dispositions of labels in a LGR. :param lgr_json: The LGRInfo as a JSON object. :param labels_json: The LabelInfo as a JSON object. :param email_address: The e-mail address where the results will be sent :param storage_path: The place where results will be stored """ lgr = LGRInfo.from_dict(lgr_json).lgr labels_info = LabelInfo.from_dict(labels_json) logger.info("Starting task 'annotate' for %s, for file %s", lgr.name, labels_info.name) body = "Hi,\nThe processing of annotation from labels provided in the " \ "file '{f}' in LGR '{lgr}' has".format(f=labels_info.name, lgr=lgr.name) _lgr_tool_task(storage_path, base_filename='annotation_{0}'.format(lgr.name), email_subject='LGR Toolset annotation result', email_body=body, email_address=email_address, cb=lgr_annotate_labels, lgr=lgr, labels_file=labels_info.labels)
def collision_task(lgr_json, labels_json, email_address, full_dump, with_rules, storage_path): """ Compute collision between labels in an LGR :param lgr_json: The LGRInfo as a JSON object. :param labels_json: The LabelInfo as a JSON object. :param email_address: The e-mail address where the results will be sent :param full_dump: Whether we also output a full dump :param with_rules: Whether we also output rules :param storage_path: The place where results will be stored :return: """ lgr = LGRInfo.from_dict(lgr_json).lgr labels_info = LabelInfo.from_dict(labels_json) logger.info("Starting task 'collision' for %s, for file %s", lgr.name, labels_info.name) body = "Hi,\nThe processing of collisions from labels provided in the " \ "file '{f}' in LGR '{lgr}' has".format(f=labels_info.name, lgr=lgr.name) _lgr_tool_task(storage_path, base_filename='collisions_{0}'.format(lgr.name), email_subject='LGR Toolset collisions result', email_body=body, email_address=email_address, cb=lgr_collision_labels, lgr=lgr, labels_file=labels_info.labels, full_dump=full_dump, with_rules=with_rules)
def diff_task(lgr_json_1, lgr_json_2, labels_json, email_address, collision, full_dump, with_rules, storage_path): """ Launch difference computation for a list of labels between two LGR :param lgr_json_1: The first LGRInfo as a JSON object. :param lgr_json_2: The second LGRInfo as a JSON object. :param labels_json: The LabelInfo as a JSON object. :param email_address: The e-mail address where the results will be sent :param collision: Whether we also compute collisions :param full_dump: Whether we also output a full dump :param with_rules: Whether we also output rules :param storage_path: The place where results will be stored :return: """ lgr1 = LGRInfo.from_dict(lgr_json_1).lgr lgr2 = LGRInfo.from_dict(lgr_json_2).lgr labels_info = LabelInfo.from_dict(labels_json) logger.info("Starting task 'diff' between %s and %s, for file %s", lgr1.name, lgr2.name, labels_info.name) body = "Hi,\nThe processing of diff from labels provided in the " \ "file '{f}' between LGR '{lgr1}' and " \ "LGR '{lgr2}' has".format(f=labels_info.name, lgr1=lgr1.name, lgr2=lgr2.name) _lgr_tool_task(storage_path, base_filename='diff_{0}_{1}'.format(lgr1.name, lgr2.name), email_subject='LGR Toolset diff result', email_body=body, email_address=email_address, cb=lgr_diff_labels, lgr_1=lgr1, lgr_2=lgr2, labels_file=labels_info.labels, show_collision=collision, full_dump=full_dump, with_rules=with_rules)
def lgr_set_validate_label_task(lgr_json, script_lgr_json, label, email_address, storage_path): """ Compute label validation variants of labels in a LGR. :param lgr_json: The LGRInfo as a JSON object. :param script_lgr_json: The LGRInfo for the script used to check label validity as a JSON object. :param label: The label to validate, as a list of code points. :param email_address: The e-mail address where the results will be sent :param storage_path: The place where results will be stored """ lgr_info = LGRInfo.from_dict(lgr_json) udata = get_db_by_version(lgr_info.lgr.metadata.unicode_version) script_lgr = LGRInfo.from_dict(script_lgr_json).lgr set_labels_info = lgr_info.set_labels_info if set_labels_info is None: set_labels_info = LabelInfo(name='None', labels=[]) logger.info("Starting task 'validate label' for %s, for input label '%s'", lgr_info.name, label) u_label = cp_to_ulabel(label) body = "Hi,\nThe processing of label validation for label '{label}'" \ " in LGR set '{lgr}' with script '{script}' has".format(label=u_label, lgr=lgr_info.lgr.name, script=script_lgr.name) _lgr_tool_task(storage_path, base_filename='label_validation_{0}'.format(lgr_info.name), email_subject='LGR Toolset label validation result', email_body=body, email_address=email_address, cb=lgr_set_validate_label, lgr=lgr_info.lgr, script_lgr=script_lgr, set_labels=set_labels_info.labels, label=label, udata=udata)
def handle(self, *args, **options): with open(options['xml'], 'rb') as lgr_xml: lgr_info = LGRInfo.from_dict( { 'xml': lgr_xml.read(), 'validate': options['validate'], }, None) lgr = lgr_info.lgr context = generate_context(lgr) html = render_to_string('lgr_renderer.html', context) if 'output' not in options: sys.stdout.write(html) else: with io.open(options['output'], 'w', encoding='utf-8') as output_file: output_file.write(html)