Debian Codegraph @

python/obitools3/parsers/tab.pyx

raw
1 #cython: language_level=3
2
3 '''
4 Created on feb 20th 2018
5
6 @author: cmercier
7 '''
8
9 import types
10 from obitools3.utils cimport __etag__
11 from obitools3.utils cimport str2bytes
12
13 def tabIterator(lineiterator,
14 bint header = False,
15 bytes sep = None,
16 bytes dec = b".", # TODO don't know how to use this to parse
17 bint stripwhite=True,
18 bint blanklineskip=True,
19 bytes commentchar=b"#",
20 bytes nastring=b"NA",
21 int skip=0,
22 only=None,
23 firstline=None,
24 int buffersize=100000000
25 ):
26
27 cdef LineBuffer lb
28 cdef int lines_to_skip, ionly, read
29 cdef list data
30 cdef dict view_line
31 cdef list keys
32 cdef list key_types
33
34 keys = []
35 key_types = []
36 skipped = 0
37 read = 0
38
39 if only is None:
40 ionly = -1
41 else:
42 ionly = int(only)
43
44 if isinstance(lineiterator, (str, bytes)):
45 lineiterator=uopen(lineiterator)
46 if isinstance(lineiterator, LineBuffer):
47 iterator = iter(lineiterator)
48 else:
49 if hasattr(lineiterator, "readlines"):
50 iterator = iter(LineBuffer(lineiterator, buffersize))
51 elif hasattr(lineiterator, '__next__'):
52 iterator = lineiterator
53 else:
54 raise Exception("Invalid line iterator")
55
56 if firstline is None:
57 line = next(iterator)
58 else:
59 line = firstline
60
61 while True:
62
63 if (not line.strip() and blanklineskip) or line[:1] == commentchar:
64 line = next(iterator)
65
66 if ionly >= 0 and read >= ionly:
67 break
68
69 if not keys:
70 if header:
71 # TODO read types eventually
72 keys = line.split(sep)
73 keys = [x.strip() for x in keys]
74 line = next(iterator)
75 continue
76 else:
77 # TODO ??? default column names? like R?
78 keys = [str2bytes(str(i)) for i in range(len(line.split(sep)))]
79
80 while skipped < skip :
81 line = next(iterator)
82 skipped += 1
83
84 view_line = {}
85
86 # Parse
87 data = line.split(sep)
88
89 if stripwhite or key_types:
90 data = [x.strip() for x in data]
91
92 for i in range(len(data)):
93 if key_types: # TODO handle None when key types are actually read
94 view_line[keys[i]] = key_types[i](data[i])
95 else:
96 view_line[keys[i]] = __etag__(data[i], nastring=nastring)
97
98 yield view_line
99
100 read+=1
101
102 try:
103 line = next(iterator)
104 except StopIteration:
105 return
106
107
108