Skip to content
Snippets Groups Projects
Commit 893a56ae authored by Robert Bossy's avatar Robert Bossy
Browse files

refactored MeSH handling code

parent 55129836
No related branches found
No related tags found
No related merge requests found
mesh.py 0 → 100644
import collections
import re
_prop_pattern = re.compile('([1A-Z_ ]+) = (.*)')
class Values(list):
def __init__(self):
super().__init__()
def __bool__(self):
return True
class Record(collections.defaultdict):
def __init__(self):
super().__init__(Values)
def __getattr__(self, item):
return self[item]
@property
def uid(self):
return self.UI[0]
def records(path):
current = Record()
with open(path) as f:
for line in f:
line = line.strip()
if line == '':
continue
if line == '*NEWRECORD':
if 'UI' in current:
yield current
current = Record()
continue
m = _prop_pattern.fullmatch(line)
if m is None:
raise RuntimeError('could not parse: ' + line)
key = m.group(1)
value = m.group(2)
current[key].append(value)
if 'UI' in current:
yield current
......@@ -3,8 +3,7 @@
import sys
import yaml
import re
import collections
import mesh
MESH_FILE = sys.argv[1]
......@@ -15,41 +14,18 @@ with open(ROOTS_FILE) as f:
ROOTS = dict((e['mesh-id'], e['name']) for e in yaml.load(f, Loader=yaml.FullLoader) if 'mesh-id' in e)
LINE = re.compile('([1A-Z_ ]+) = (.*)')
SEEN = set()
def finish(rec):
if 'UI' not in rec:
return
if rec['UI'][0] not in ROOTS:
return
if 'MN' not in rec:
raise RuntimeError('no tree for ' + rec['UI'][0])
for mtree in rec['MN']:
for rec in mesh.records(MESH_FILE):
mid = rec.uid
if mid not in ROOTS:
continue
if not rec.MN:
raise RuntimeError('no tree for ' + mid)
for mtree in rec.MN:
print(mtree)
SEEN.add(rec['UI'][0])
current = collections.defaultdict(list)
with open(MESH_FILE) as f:
for line in f:
line = line.strip()
if line == '':
continue
if line == '*NEWRECORD':
finish(current)
current = collections.defaultdict(list)
continue
m = LINE.fullmatch(line)
if m is None:
raise RuntimeError('could not parse: ' + line)
key = m.group(1)
value = m.group(2)
current[key].append(value)
finish(current)
SEEN.add(mid)
for meshid, name in ROOTS.items():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment