Created
December 1, 2024 23:08
-
-
Save mesailde/fe9a051261d0accfe4a07416b748757e to your computer and use it in GitHub Desktop.
Converts Ex Libris Aleph custom format to the MarcEdit textual MARC format (MRK)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import re | |
| state = 'file_hdr' | |
| record_name, record_value = None, '' | |
| for line in sys.stdin: | |
| line = line.rstrip('\r\n') | |
| if state == 'file_hdr' and line.strip() == '*****': | |
| state = 'record_hdr' | |
| elif state == 'record_hdr' and line == '': | |
| state = 'record' | |
| record_name, record_value = None, '' | |
| elif state == 'record': | |
| if line.strip() == '*****': | |
| state = 'record_hdr' | |
| print() | |
| elif line != '': | |
| if line.startswith(6*' '): | |
| line = line[6:] | |
| if record_name is None: | |
| record_name = line | |
| else: | |
| record_value += line | |
| elif record_name is not None: | |
| record_name, indicator = record_name[:3], record_name[3:] | |
| assert len(record_name) == 3 | |
| if record_name.startswith('00') or record_name in {'LDR', 'SYS'}: | |
| # fields started with 00 don't have indicators because they are control fields | |
| assert indicator == '' | |
| else: | |
| # all data fields have two indicators | |
| if indicator == '': | |
| indicator = ' ' | |
| elif len(indicator) == 1: | |
| indicator += ' ' | |
| indicator = indicator.replace(' ', '\\') | |
| assert len(indicator) == 2, f'invalid indicator {indicator!r}' | |
| record_value = re.sub(r' ?\|([a-z0-9]) ', lambda m: f'${m.group(1)}', record_value) | |
| if record_name != 'FMT': | |
| print(f'={record_name} {indicator}{record_value}') | |
| record_name, record_value = None, '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment