예제 #1
0
 def test_init(self):
     """Kinemage should init with any of its usual fields"""
     k = Kinemage(1)
     self.assertEqual(str(k), '@kinemage 1')
     k.Header = '@perspective'
     self.assertEqual(str(k), '@kinemage 1\n@perspective')
     k.Count = 2
     self.assertEqual(str(k), '@kinemage 2\n@perspective')
     k.Header = ''
     k.Caption = 'test caption'
     self.assertEqual(str(k), '@kinemage 2\n@caption\ntest caption')
     k.Caption = None
     k.Text = 'some text'
     self.assertEqual(str(k), '@kinemage 2\n@text\nsome text')
     k.Groups = [self.mg1]
     k.Header = '@test_header'
     k.Caption = 'This is\nThe caption'
     k.Text = 'some text here'
     self.assertEqual(str(k), '@kinemage 2\n@test_header\n@text\n' +\
     'some text here\n' + \
     '@caption\nThis is\nThe caption\n@group {z} recessiveon\n' + \
     '@dotlist {y} color=green\n{x} 0 0 0')
예제 #2
0
def MageParser(infile):
    """MageParser returns a new kinemage object, created from a string repr.

    infile: should be an iterable file object
    
    The MageParser works only on ONE kinemage object, so files containing more
    than one kinemage should be split beforehand. This can easily be adjusted
    if it would be useful in the future.

    The MageParser handles only certain keywords (@kinemage, @text, @caption,
    @___group, @____list) and MagePoints at this point in time. All unkown 
    keywords are assumed to be part of the header, so you can find them in 
    the header information. 
    The lists that are part of the Simplex header are treated as normal lists.
    The 'text' and 'caption' are printed between the header and the first group
    (see cogent.format.mage). All text found after @text keywords is grouped
    and outputted as Kinemage.Text.
    
    WARNING: MageParser should work on all .kin files generated by the code in
    cogent.format.mage. There are no guarantees using it on other kinemage
    files, so always check your output!!!
    """
    text = []
    caption = []
    header = []
    group_pat = re.compile('@(sub)?group')
    list_pat = re.compile('@\w{3,8}list')
    last_group = None
    
    for rec in KeywordFinder(infile):
        first = rec[0]
        other=None
        if len(rec)>1:
            other = rec[1:]
        if first.startswith('@kinemage'): #kinemage
            k = Kinemage()
            count = int(first.replace('@kinemage','').strip())
            k.Count = count
        elif first.startswith('@text'): #text
            if other:
                text.extend(other)
        elif first.startswith('@caption'): #caption
            if other:
                caption.extend(other)
        elif group_pat.match(first): #group
            m = MageGroupFromString(first)
            if m.Subgroup: #subgroup, should be appended to some other group
                if last_group is None:
                    raise ValueError,"Subgroup found before first group"
                last_group.append(m)
            else: #normal group
                k.Groups.append(m)
            last_group = m
        elif list_pat.match(first): #list
            l = MageListFromString(first)
            if other:
                points = [MagePointFromString(p) for p in other]
                l.extend(points)
            last_group.append(l)
        else: #something else
            header.append(first)
            if other:
                header.extend(other)
    
    if text:
        k.Text = '\n'.join(text)
    if caption:
        k.Caption = '\n'.join(caption)
    if header:
        k.Header = '\n'.join(header)
    return k