Parse a field into an association list of keys and subfields >>> expand('zero^1one^2two^3three') [('_', 'zero'), ('1', 'one'), ('2', 'two'), ('3', 'three')]
(content, subkeys=None)
| 27 | DEFAULT_ENCODING = u'utf-8' |
| 28 | |
| 29 | def expand(content, subkeys=None): |
| 30 | ''' Parse a field into an association list of keys and subfields |
| 31 | |
| 32 | >>> expand('zero^1one^2two^3three') |
| 33 | [('_', 'zero'), ('1', 'one'), ('2', 'two'), ('3', 'three')] |
| 34 | |
| 35 | ''' |
| 36 | if subkeys is None: |
| 37 | regex = SUBFIELD_MARKER_RE |
| 38 | elif subkeys == '': |
| 39 | return [(MAIN_SUBFIELD_KEY, content)] |
| 40 | else: |
| 41 | regex = re.compile(r'\^(['+subkeys+'])', re.IGNORECASE) |
| 42 | content = content.replace('^^', '^^ ') |
| 43 | parts = [] |
| 44 | start = 0 |
| 45 | key = MAIN_SUBFIELD_KEY |
| 46 | while True: |
| 47 | found = regex.search(content, start) |
| 48 | if found is None: break |
| 49 | parts.append((key, content[start:found.start()].rstrip())) |
| 50 | key = found.group(1).lower() |
| 51 | start = found.end() |
| 52 | parts.append((key, content[start:].rstrip())) |
| 53 | return parts |
| 54 | |
| 55 | |
| 56 | class CompositeString(object): |
no test coverage detected