def iter_tl( mst_filename, encoding=DEFAULT_MST_ENCODING, only_active=True, prepend_mfn=False, prepend_status=False, ftf=FieldTagFormatter(DEFAULT_FTF_TEMPLATE.decode("ascii"), int_tags=True), ): check_bruma() with jvm(domains=["bruma"], classpath=BRUMA_JAR): from bruma.master import MasterFactory mf = MasterFactory.getInstance(mst_filename).setEncoding(encoding) with closing(mf.open()) as mst: for record in mst: status_name = record.getStatus().name() if status_name != "ACTIVE" and (only_active or status_name == "PHYDEL"): continue result = [] if prepend_mfn: result.append(("mfn", "%d" % record.getMfn())) if prepend_status: status = {"ACTIVE": "0", "LOGDEL": "1"}[status_name] result.append(("status", status)) for idx, field in enumerate(record.getFields()): tag = ftf(field.getId(), idx) result.append((tag, field.getContent())) yield result
def iterMstRecords(master_file_name, isis_json_type): try: from bruma.master import MasterFactory, Record except ImportError: print( "IMPORT ERROR: Jython 2.5 and Bruma.jar are required to read .mst files" ) raise SystemExit mst = MasterFactory.getInstance(master_file_name).open() for record in mst: fields = {} if SKIP_INACTIVE: if record.getStatus() != Record.Status.ACTIVE: continue else: # save status only there are non-active records fields[ISIS_ACTIVE_KEY] = record.getStatus( ) == Record.Status.ACTIVE fields[ISIS_MFN_KEY] = record.getMfn() for field in record.getFields(): field_key = str(field.getId()) field_occurrences = fields.setdefault(field_key, []) if isis_json_type == 3: content = {} for subfield in field.getSubfields(): subfield_key = subfield.getId() if subfield_key == "*": content["_"] = subfield.getContent() else: if subfield_key in SUBFIELD_KEYS: content.setdefault(subfield_key, subfield.getContent()) else: subfield_occurrences = content.setdefault( subfield_key, []) subfield_occurrences.append(subfield.getContent()) field_occurrences.append(content) elif isis_json_type == 1: content = [] for subfield in field.getSubfields(): subfield_key = subfield.getId() if subfield_key == "*": content.insert(0, subfield.getContent()) else: content.append(SUBFIELD_DELIMITER + subfield_key + subfield.getContent()) field_occurrences.append("".join(content)) else: raise NotImplementedError( "ISIS-JSON type %s conversion not yet implemented for .mst input" % isis_json_type) yield fields mst.close()
def iter_records(mst_filename, encoding=DEFAULT_MST_ENCODING): check_bruma() with jvm(domains=["bruma"], classpath=BRUMA_JAR): from bruma.master import MasterFactory, Record mf = MasterFactory.getInstance(mst_filename).setEncoding(encoding) with closing(mf.open()) as mst: for record in mst: result = defaultdict(list) result["active"] = record.getStatus() == Record.Status.ACTIVE result["mfn"] = record.getMfn() for field in record.getFields(): tag = field.getId() result[tag].append(field.getContent()) yield result
def iter_mst_records(master_file_name, isis_json_type): try: from bruma.master import MasterFactory, Record except ImportError: print('IMPORT ERROR: Jython 2.5 and Bruma.jar ' 'are required to read .mst files') raise SystemExit mst = MasterFactory.getInstance(master_file_name).open() for record in mst: fields = {} if SKIP_INACTIVE: if record.getStatus() != Record.Status.ACTIVE: continue else: # save status only there are non-active records fields[ISIS_ACTIVE_KEY] = (record.getStatus() == Record.Status.ACTIVE) fields[ISIS_MFN_KEY] = record.getMfn() for field in record.getFields(): field_key = str(field.getId()) field_occurrences = fields.setdefault(field_key, []) if isis_json_type == 3: content = {} for subfield in field.getSubfields(): subfield_key = subfield.getId() if subfield_key == '*': content['_'] = subfield.getContent() else: subfield_occurrences = content.setdefault(subfield_key, []) subfield_occurrences.append(subfield.getContent()) field_occurrences.append(content) elif isis_json_type == 1: content = [] for subfield in field.getSubfields(): subfield_key = subfield.getId() if subfield_key == '*': content.insert(0, subfield.getContent()) else: content.append(SUBFIELD_DELIMITER + subfield_key + subfield.getContent()) field_occurrences.append(''.join(content)) else: raise NotImplementedError('ISIS-JSON type %s conversion ' 'not yet implemented for .mst input' % isis_json_type) yield fields mst.close()