def test_init_2(self): f100 = RecordField(100, 'Field 100') f200 = RecordField(200, 'Field 200') record = MarcRecord(f100, f200) self.assertEqual(len(record.fields), 2) self.assertEqual(record.fields[0].tag, 100) self.assertEqual(record.fields[1].tag, 200)
def test_setitem_1(self): f100 = RecordField(100, 'Field 100') f200 = RecordField(200, SubField('a', 'SubA'), SubField('b', 'SubB')) record = MarcRecord() record.fields.append(f100) record.fields.append(f200) new_value = 'New value' record[100] = new_value self.assertEqual(f100.value, new_value) record[200] = '^aNewA^bNewB' self.assertEqual(f200.subfields[0].value, 'NewA') self.assertEqual(f200.subfields[1].value, 'NewB') record[300] = new_value self.assertEqual(len(record.fields), 3) self.assertEqual(record.fields[2].tag, 300) self.assertEqual(record.fields[2].value, new_value) record[400] = RecordField().add('a', 'NewA').add('b', 'NewB') self.assertEqual(len(record.fields), 4) self.assertEqual(record.fields[3].tag, 400) self.assertEqual(record.fields[3].subfields[0].value, 'NewA') self.assertEqual(record.fields[3].subfields[1].value, 'NewB') record[300] = SubField('a', 'OtherA') self.assertEqual(len(record.fields), 4) self.assertEqual(record.fields[2].tag, 300) self.assertIsNone(record.fields[2].value) self.assertEqual(record.fields[2].subfields[0].value, 'OtherA')
def test_iadd_1(self): record = MarcRecord() self.assertEqual(len(record.fields), 0) record += RecordField(100, 'Field 100') self.assertEqual(len(record.fields), 1) record += RecordField(200, SubField('a', 'SubA')) self.assertEqual(len(record.fields), 2)
def test_first_value_1(self): sf1 = SubField('a', 'First value') sf2 = SubField('b', 'Second value') field = RecordField(100, sf1, sf2) self.assertEqual(field.first_value('a'), sf1.value) self.assertEqual(field.first_value('b'), sf2.value) self.assertIsNone(field.first_value('c'))
def test_clone_1(self): original = RecordField(100, 'Some value') original.add('a', 'Some text') clone = original.clone() self.assertEqual(original.tag, clone.tag) self.assertEqual(original.value, clone.value) self.assertEqual(len(original.subfields), len(clone.subfields))
def read_text_record(stream: TextIO) -> Optional[MarcRecord]: """ Чтение записи из файла в текстовом обменном формате ИРБИС. :param stream: Файл :return: Запись или None """ result = MarcRecord() while True: line: str = stream.readline() if not line: break line = line.strip() if line.startswith(STOP_MARKER): break if not line.startswith('#'): break parts = line[1:].split(':', 1) if len(parts) != 2: break tag = int(parts[0]) text = parts[1][1:] field = RecordField(tag) field.parse(text) result.fields.append(field) if not result.fields: # Если в записи нет полей, возвращаем None return None return result
def test_isub_1(self): f100 = RecordField(100, 'Field 100') f200 = RecordField(200, SubField('a', 'SubA')) record = MarcRecord(f100, f200) record -= (f100, f200) self.assertEqual(len(record.fields), 0) record -= (f100, f200) self.assertEqual(len(record.fields), 0)
def test_assign_from_1(self): first = RecordField(100, 'Some value', SubField('a', 'SubA')) second = RecordField(200, 'Other value', SubField('b', 'SubB')) second.assign_from(first) self.assertEqual(second.tag, 200) self.assertEqual(first.value, second.value) self.assertEqual(len(first.subfields), len(second.subfields)) self.assertEqual(first.subfields[0].code, second.subfields[0].code) self.assertEqual(first.subfields[0].value, second.subfields[0].value)
def test_setitem_2(self): f100 = RecordField(100, 'Field 100') f200 = RecordField(200, SubField('a', 'SubA'), SubField('b', 'SubB')) record = MarcRecord() record.fields.append(f100) record.fields.append(f200) self.assertEqual(len(record.fields), 2) record[100] = None self.assertEqual(len(record.fields), 1) record[200] = None self.assertEqual(len(record.fields), 0)
def test_getitem_2(self): sfa = SubField('a', 'SubA') sfb = SubField('b', 'SubB') field = RecordField(100, sfa, sfb) found = field[0] self.assertIs(found, sfa) found = field[1] self.assertIs(found, sfb)
def test_isub_2(self): sfa = SubField('a', 'SubA') sfb = SubField('b', 'SubB') field = RecordField(100, sfa, sfb) field -= (sfa, sfb) self.assertEqual(len(field.subfields), 0) field -= (sfa, sfb) self.assertEqual(len(field.subfields), 0)
def test_iadd_1(self): field = RecordField(100) self.assertEqual(len(field.subfields), 0) field += SubField('a', 'SubA') self.assertEqual(len(field.subfields), 1) field += SubField('b', 'SubB') self.assertEqual(len(field.subfields), 2) self.assertEqual(str(field), '100#^aSubA^bSubB')
def test_iter_1(self): field = RecordField(100, SubField('a', 'SubA'), SubField('b', 'SubB')) s = list(field) self.assertEqual(len(s), 2) self.assertEqual(s[0].code, field.subfields[0].code) self.assertEqual(s[0].value, field.subfields[0].value) self.assertEqual(s[1].code, field.subfields[1].code) self.assertEqual(s[1].value, field.subfields[1].value)
def test_getitem_1(self): sfa = SubField('a', 'SubA') sfb = SubField('b', 'SubB') field = RecordField(100, sfa, sfb) found = field['a'] self.assertIs(found, sfa) found = field['b'] self.assertIs(found, sfb) found = field['c'] self.assertIsNone(found)
def test_setitem_2(self): sfa = SubField('a', 'SubA') sfb = SubField('b', 'SubB') field = RecordField(100, sfa, sfb) self.assertEqual(len(field.subfields), 2) field['a'] = None self.assertEqual(len(field.subfields), 1) field['b'] = None self.assertEqual(len(field.subfields), 0) field['c'] = None self.assertEqual(len(field.subfields), 0)
def test_setitem_1(self): sfa = SubField('a', 'SubA') sfb = SubField('b', 'SubB') field = RecordField(100, sfa, sfb) new_value = 'New value' field['a'] = new_value self.assertEqual(sfa.value, new_value) field['b'] = new_value self.assertEqual(sfb.value, new_value) field['c'] = new_value self.assertEqual(len(field.subfields), 3) sfc = field.subfields[2] self.assertEqual(sfc.value, new_value)
def test_iter_3(self): field = RecordField(100, SubField('a', 'SubA'), SubField('b', 'SubB')) i = iter(field) sf = next(i) self.assertEqual(sf.code, 'a') self.assertEqual(sf.value, 'SubA') sf = next(i) self.assertEqual(sf.code, 'b') self.assertEqual(sf.value, 'SubB') ok = False try: next(i) except StopIteration: ok = True self.assertTrue(ok)
def test_all_values_1(self): field = RecordField() field.add('a', 'A1').add('a', 'A2').add('b', 'B1') self.assertEqual(2, len(field.all_values('A'))) self.assertEqual(1, len(field.all_values('B')))
def read_record(stream: BinaryIO, charset=ANSI) -> Optional[MarcRecord]: """ Чтение записи из файла в формате ISO 2709. :param stream: Файл или файлоподобный объект :param charset: Кодировка :return: Декодированная запись либо None """ # Считываем длину записи marker = stream.read(5) if len(marker) != 5: return None # а затем и ее остаток record_length = parse_int(marker) need = record_length - 5 tail = stream.read(need) if len(tail) != need: return None # Простая проверка, что мы имеем дело с нормальной ISO-записью record = marker + tail if record[record_length - 1] != RECORD_DELIMITER: return None # Превращаем запись в Unicode indicator_length = parse_int(record[10:11]) base_address = parse_int(record[12:17]) # Начинаем собственно конверсию result = MarcRecord() # Пошли по полям при помощи справочника directory = MARKER_LENGTH while record[directory] != FIELD_DELIMITER: # если нарвались на разделитель, значит, справочник закончился tag = parse_int(record[directory:directory + 3]) field_length = parse_int(record[directory + 3:directory + 7]) field_offset = parse_int( record[directory + 7:directory + 12]) + base_address field = RecordField(tag) result.fields.append(field) if tag < 10: # фиксированное поле # не может содержать подполей и индикаторов field.value = record[field_offset:field_offset + field_length - 1].decode(charset) else: # поле переменной длины # содержит два однобайтных индикатора # может содержать подполя start = field_offset + indicator_length stop = field_offset + field_length - indicator_length + 1 position = start # ищем значение поля до первого разделителя while position < stop: if record[start] == SUBFIELD_DELIMITER: break position += 1 # если есть текст до первого раздлителя, запоминаем его if position != start: field.value = record[start:position].decode(charset) # просматриваем подполя start = position while start < stop: position = start + 1 while position < stop: if record[position] == SUBFIELD_DELIMITER: break position += 1 subfield = SubField(chr(record[start + 1]), record[start + 2:position].decode(charset)) field.subfields.append(subfield) start = position # переходим к следующему полю в справочнике directory += 12 return result
def test_clear_1(self): field = RecordField(100, 'Some value') field.add('a', 'Some text') field.clear() self.assertEqual(len(field.subfields), 0)
def test_str_1(self): field = RecordField(100, 'Some value') field.add('a', 'Some text') field.add('b', 'Other text') self.assertEqual(str(field), '100#Some value^aSome text^bOther text')
def test_str_2(self): field = RecordField() self.assertEqual(str(field), '')
def test_iadd_2(self): record = MarcRecord() record += (RecordField(100, 'Field 100'), RecordField(200, 'Field 200')) self.assertEqual(len(record.fields), 2)
def test_iter_2(self): field = RecordField() s = list(field) self.assertEqual(len(s), 0)
def test_bool_4(self): field = RecordField() field.value = 'Value' self.assertFalse(bool(field))
def test_bool_3(self): field = RecordField(100) self.assertFalse(bool(field))
def test_bool_2(self): field = RecordField(100, 'Value') self.assertTrue(bool(field))
def test_bool_1(self): field = RecordField(100, SubField('a', 'SubA'), SubField('b', 'SubB')) self.assertTrue(bool(field))
def test_len_2(self): field = RecordField() self.assertEqual(len(field), 0)
def test_len_1(self): field = RecordField(100, SubField('a', 'SubA'), SubField('b', 'SubB')) self.assertEqual(len(field), 2)