File: header.pyx

package info (click to toggle)
obitools 3.0.1~b26%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,788 kB
  • sloc: ansic: 24,299; python: 657; sh: 27; makefile: 20
file content (45 lines) | stat: -rwxr-xr-x 919 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#cython: language_level=3

'''
Created on 25 mars 2016

@author: coissac
'''

from obitools3.utils cimport __etag__
import re


__ret__  = re.compile(b'''(([^ ]+)=('[^']*'|"[^"]*"|[^;]+); *)+?''')


cpdef tuple parseHeader(bytes header, bytes nastring=b"NA"):
    cdef list  m 
    cdef dict  tags 
    cdef bytes definition
    cdef bytes ident
    cdef bytes second
    
    m=header[1:-1].split(maxsplit=1)
   
    ident=m[0]
    if len(ident)>1 and ident[-2:-1] == b';':
        ident = ident[:-1]
    
    if len(m)==1:
        tags={}
        definition=b'' 
    else:
        second=m[1]
        m = __ret__.findall(second)
        
        if m:
            tags = dict([(a[1],__etag__(a[2], nastring=nastring)) for a in m])
            definition = second.split(m[-1][0],1)[1].strip()
        else:
            tags = {}
            definition = second.strip() 
        
    return ident,tags,definition