Debian Codegraph @

gavo/grammars/common.py

raw symbols SCIP
1 """
2 Base classes and common code for grammars.
3
4 NOTE: If you add grammars, you have to enter manually them in
5 rscdef.builtingrammars.GRAMMAR_REGISTRY (we don't want to import all
6 the mess in this package just to make that).
7 """
8
9 #c Copyright 2008-2025, the GAVO project <[email protected]>
10 #c
11 #c This program is free software, covered by the GNU GPL. See the
12 #c COPYING file in the source distribution.
13
14
15 import codecsscip-python python python-stdlib 3.11 codecs/__init__:
16 import ioscip-python python python-stdlib 3.11 io/__init__:
17 import rescip-python python python-stdlib 3.11 re/__init__:
18 import osscip-python python python-stdlib 3.11 os/__init__:
19 import selectscip-python python python-stdlib 3.11 select/__init__:
20 import subprocessscip-python python python-stdlib 3.11 subprocess/__init__:
21
22 from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__:
23 from gavoscip-python python gavodachs 2.13 gavo/__init__: import rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:
24 from gavoscip-python python gavodachs 2.13 gavo/__init__: import utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:
25 from gavo.rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__: import procdefscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/__init__:
26 from gavo.rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__: import rowtriggersscip-python python gavodachs 2.13 `gavo.rscdef.rowtriggers`/__init__:
27
28
29 class REAttributescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.UnicodeAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#):
30 """An attribute containing (compiled) RE
31 """
32 def parsescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(self), valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(value)):
33 if valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(value) is None or not valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(value):
34 return None
35 try:
36 return rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().(valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(value))
37 except rescip-python python python-stdlib 3.11 re/__init__:.errorscip-python python gavodachs 2.13 sre_constants/error# as msg:
38 raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..logOldExcscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#logOldExc().(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.LiteralParseErrorscip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(self).name_scip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#name_., valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#parse().(value),
39 hintscip-python python gavodachs 2.13 `gavo.utils.excs`/LiteralParseError#__init__().(hint)="A python regular expression was expected here. Compile"
40 " complained: %s"%strscip-python python python-stdlib 3.11 builtins/str#(msg)))
41
42 def unparsescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#unparse().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#unparse().(self), valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#unparse().(value)):
43 if valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#unparse().(value) is None:
44 return ""
45 else:
46 return valuescip-python python gavodachs 2.13 `gavo.grammars.common`/REAttribute#unparse().(value).pattern
47
48
49 class FilteredInputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#(objectscip-python python python-stdlib 3.11 builtins/object#):
50 """a pseudo-file that allows piping data through a shell command.
51
52 It supports read, readline, and close. Close closes the original
53 file, too.
54
55 Warning: the command passed in will be shell-expanded (which is fair
56 since you can pass in any command you want anyway).
57
58 If you pass silent=True, stderr will be redirected to /dev/null.
59 This is probably only attractive for unit tests and such.
60 """
61 def __init__scip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self), filterCommandscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(filterCommand), origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(origFile), silentscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(silent)=False):
62 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).filterCommandscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#filterCommand., selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#origFile. = filterCommandscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(filterCommand), origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(origFile)
63
64 processArgs = {"shell": True,
65 "stdin": subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.PIPEscip-python python python-stdlib 3.11 subprocess/PIPE.PIPE.,
66 "stdout": subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.PIPEscip-python python python-stdlib 3.11 subprocess/PIPE.PIPE.,
67 "close_fds": True}
68 if silentscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(silent):
69 processArgs["stderr"] = utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.devnullscip-python python gavodachs 2.13 functools/devnull#()
70
71 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process. = subprocessscip-python python python-stdlib 3.11 subprocess/__init__:.Popenscip-python python python-stdlib 3.11 subprocess/Popen#([selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).filterCommandscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#filterCommand.], **processArgs)
72 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).fromChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#fromChild., selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild. = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..stdoutscip-python python python-stdlib 3.11 subprocess/Popen#stdout., selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..stdinscip-python python python-stdlib 3.11 subprocess/Popen#stdin.
73 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer. = utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.StreamBufferscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#(2**14)
74 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).PIPE_BUFscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#PIPE_BUF. = selectscip-python python python-stdlib 3.11 select/__init__:.PIPE_BUFscip-python python python-stdlib 3.11 select/PIPE_BUF.
75
76 origClose = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#origFile..close
77 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self).origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#origFile..close = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__init__().(self)._makeCloserscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(origClose)
78
79 def _makeCloserscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self), origClosescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(origClose)):
80 def _():
81 origClosescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(origClose)()
82 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..returncodescip-python python python-stdlib 3.11 subprocess/Popen#returncode. is None:
83 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..terminatescip-python python python-stdlib 3.11 subprocess/Popen#terminate().()
84 # not checking the return code here; if the client closed
85 # while the child was still running, an error from it would
86 # not count as an error.
87 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..waitscip-python python python-stdlib 3.11 subprocess/Popen#wait().()
88 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).fromChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#fromChild.:
89 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).fromChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#fromChild..closescip-python python python-stdlib 3.11 typing/IO#close().()
90 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild.:
91 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_makeCloser().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild..closescip-python python python-stdlib 3.11 typing/IO#close().()
92 return _
93
94 def _fillChildBufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self)):
95 """feeds the child new data, closing the source file if all data
96 has been fed.
97 """
98 data = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self).origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#origFile..read(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self).PIPE_BUFscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#PIPE_BUF.)
99 if data==b"":
100 # source file exhausted, tell child we're done
101 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild..closescip-python python python-stdlib 3.11 typing/IO#close().()
102 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild. = None
103 else:
104 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild..writescip-python python python-stdlib 3.11 typing/IO#write().(data)
105
106 def _readAllscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_readAll().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_readAll().(self)):
107 allChunks = []
108 while True:
109 data = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_readAll().(self).readscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(2**20)
110 if data==b"":
111 break
112 allChunks.appendscip-python python python-stdlib 3.11 builtins/list#append().(data)
113 return b"".joinscip-python python python-stdlib 3.11 builtins/bytes#join().(allChunks)
114
115 def _fillBufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self)):
116 """tries to obtain another chunk of data from the child, feeding
117 it new data if possible.
118 """
119 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild.:
120 writeList = [selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).toChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#toChild.]
121 else:
122 writeList = []
123 readReady, writeReady, _ = selectscip-python python python-stdlib 3.11 select/__init__:.selectscip-python python python-stdlib 3.11 select/select().(
124 [selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).fromChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#fromChild.], writeList, [])
125
126 if writeReady:
127 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self)._fillChildBufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillChildBuffer().()
128
129 if readReady:
130 data = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).fromChildscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#fromChild..readscip-python python python-stdlib 3.11 typing/IO#read().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).PIPE_BUFscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#PIPE_BUF.)
131 if data==b"":
132 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..doneWritingscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#doneWriting().()
133 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..waitscip-python python python-stdlib 3.11 subprocess/Popen#wait().():
134 raise IOErrorscip-python python python-stdlib 3.11 builtins/IOError#("Child exited with return code %s"%
135 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).processscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#process..waitscip-python python python-stdlib 3.11 subprocess/Popen#wait().())
136 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..addscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#add().(data)
137
138 def readscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self), nBytesscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(nBytes)=None):
139 if nBytesscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(nBytes) is None:
140 return selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self)._readAllscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_readAll().()
141
142 while selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..curSizescip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#curSize.<nBytesscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(nBytes) and not selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..finishedscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#finished.:
143 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self)._fillBufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().()
144
145 return selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..getscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#get().(nBytesscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#read().(nBytes)) or b""
146
147 def readlinescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(self)):
148 # let's do a quick one ourselves rather than inherit from file
149 # just for this; this just works with unix line ends.
150 while True:
151 val = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..getToCharscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#getToChar().(b"\n")
152 if val is None:
153 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..finishedscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#finished.:
154 return selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(self).bufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#buffer..getRestscip-python python gavodachs 2.13 `gavo.utils.misctricks`/StreamBuffer#getRest().()
155 else:
156 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().(self)._fillBufferscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#_fillBuffer().()
157 else:
158 return val
159
160 def __iter__scip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__iter__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__iter__().(self)):
161 while True:
162 res = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#__iter__().(self).readlinescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#readline().()
163 if not res:
164 break
165 yield res
166
167 def closescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#close().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#close().(self)):
168 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#close().(self).origFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#origFile..close()
169
170
171 class Rowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#(procdefscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/__init__:.ProcAppscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/ProcApp#):
172 """A generator for rows coming from a grammar.
173
174 Rowfilters receive rows (i.e., dictionaries) as yielded by a grammar
175 under the name row. Additionally, the embedding row iterator is
176 available under the name rowIter.
177
178 Macros are expanded within the embedding grammar.
179
180 The procedure definition *must* result in a generator, i.e., there must
181 be at least one yield; in general, this will typically be a ``yield row``,
182 but a rowfilter may swallow or create as many rows as desired.
183
184 If you forget to have a yield in the rowfilter source, you'll get a
185 "NoneType is not iterable" error that's a bit hard to understand.
186
187 Here, you can only access whatever comes from the grammar. You can
188 access grammar keys in late parameters as row[key] or, if key is
189 like an identifier, as @key.
190 """
191 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#name_. = "rowfilter"
192 requiredTypescip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#requiredType.="rowfilter"
193 formalArgsscip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#formalArgs. = "row, rowIter"
194
195 def getFuncCodescip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#getFuncCode().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#getFuncCode().(self)):
196 return rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.replaceProcDefAtscip-python python gavodachs 2.13 `gavo.rscdef.common`/replaceProcDefAt().(procdefscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/__init__:.ProcAppscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/ProcApp#.getFuncCodescip-python python gavodachs 2.13 `gavo.rscdef.procdef`/ProcApp#getFuncCode().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#getFuncCode().(self)), "row")
197
198
199 def _identityRowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/_identityRowfilter().(rowscip-python python gavodachs 2.13 `gavo.grammars.common`/_identityRowfilter().(row), rowIterscip-python python gavodachs 2.13 `gavo.grammars.common`/_identityRowfilter().(rowIter)):
200 yield rowscip-python python gavodachs 2.13 `gavo.grammars.common`/_identityRowfilter().(row)
201
202
203 def compileRowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters)):
204 """returns an iterator that "pipes" the rowfilters in filters.
205
206 This means that the output of filters[0] is used as arguments to
207 filters[1] and so on.
208
209 If filters is empty, an identity function is returned.
210 """
211 if not filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters):
212 return _identityRowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/_identityRowfilter().
213
214 iters = [f.compile() for f in filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters)] #noflake: code gen
215 src = [
216 "def iterPipe(row, rowIter):",
217 " for item0 in iters[0](row, rowIter):"]
218 for ind in rangescip-python python python-stdlib 3.11 builtins/range#(1, len(filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters))):
219 src.appendscip-python python python-stdlib 3.11 builtins/list#append().("%s for item%d in iters[%d](item%d, rowIter):"%(
220 " "*ind, ind, ind, ind-1))
221 src.appendscip-python python python-stdlib 3.11 builtins/list#append().("%s yield item%d"%(" "*len(filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters)), len(filtersscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(filters))-1))
222 d = locals()
223 exec("\n".joinscip-python python python-stdlib 3.11 builtins/str#join().(src), d)
224 return d["iterPipe"]
225
226
227 class SourceFieldAppscip-python python gavodachs 2.13 `gavo.grammars.common`/SourceFieldApp#(rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.ProcAppscip-python python gavodachs 2.13 `gavo.rscdef.procdef`/ProcApp#):
228 """A procedure application that returns a dictionary added to all
229 incoming rows.
230
231 Use this to programmatically provide information that can be computed
232 once but that is then added to all rows coming from a single source, usually
233 a file. This could be useful to add information on the source of a
234 record or the like.
235
236 The code must return a dictionary. The source that is about to be parsed is
237 passed in as sourceToken. When parsing from files, this simply is the file
238 name. The data the rows will be delivered to is available as "data", which
239 is useful for adding or retrieving meta information.
240 """
241 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/SourceFieldApp#name_. = "sourceFields"
242
243 requriedTypescip-python python gavodachs 2.13 `gavo.grammars.common`/SourceFieldApp#requriedType. = "sourceFields"
244 formalArgsscip-python python gavodachs 2.13 `gavo.grammars.common`/SourceFieldApp#formalArgs. = "sourceToken, data"
245
246
247 class MapKeysscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.Structurescip-python python gavodachs 2.13 `gavo.base.structure`/Structure#):
248 """Mapping of names, specified in long or short forms.
249
250 mapKeys is necessary in grammars like keyValueGrammar or fitsProdGrammar.
251 In these, the source files themselves give key names. Within the GAVO
252 DC, keys are required to be valid python identifiers (i.e., match
253 ``[A-Za-z_][A-Za-z_0-9]*``). If keys coming in do not have this form,
254 mapping can force proper names.
255
256 mapKeys could also be used to make incoming names more suitable for
257 matching with shell patterns (like in rowmaker idmaps).
258 """
259 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#name_. = "mapKeys"
260
261 _contentscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_content. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.DataContentscip-python python gavodachs 2.13 `gavo.base.structure`/DataContent#(descriptionscip-python python gavodachs 2.13 `gavo.base.structure`/DataContent#__init__().(description)="Simple mappings in the form"
262 "<dest>:<src>{,<dest>:<src>}")
263 _mappingsscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_mappings. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.DictAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#("maps", keyNamescip-python python gavodachs 2.13 `gavo.base.complexattrs`/DictAttribute#__init__().(keyName)="dest", descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/DictAttribute#__init__().(description)=
264 "Map source names given in content to the name given in dest.",
265 itemAttDscip-python python gavodachs 2.13 `gavo.base.complexattrs`/DictAttribute#__init__().(itemAttD)=basescip-python python gavodachs 2.13 `gavo.base`/__init__:.UnicodeAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("map"), invertedscip-python python gavodachs 2.13 `gavo.base.complexattrs`/DictAttribute#__init__().(inverted)=True,
266 aliases=["map"], copyable=True)
267
268 def _parseShortenedMapscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(self), literalscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(literal)):
269 literalscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(literal) = literalscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(literal).strip().rstrip(",")
270 try:
271 for dest, src in (p.split(":") for p in literalscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(literal).split(",")):
272 src = src.strip()
273 if src not in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(self).maps:
274 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(self).maps[src] = dest.strip()
275 else:
276 raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructureErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#(
277 "%s clobbers an existing source within the key map."%src)
278 except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#:
279 raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..logOldExcscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#logOldExc().(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.LiteralParseErrorscip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(self).name_scip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#name_., literalscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(literal),
280 hintscip-python python gavodachs 2.13 `gavo.utils.excs`/LiteralParseError#__init__().(hint)="A key-value enumeration of the format k:v {,k:v}"
281 " is expected here"))
282
283 def onElementCompletescip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#onElementComplete().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#onElementComplete().(self)):
284 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#onElementComplete().(self).content_:
285 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#onElementComplete().(self)._parseShortenedMapscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#_parseShortenedMap().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#onElementComplete().(self).content_)
286 superscip-python python python-stdlib 3.11 builtins/super#().onElementCompletescip-python python gavodachs 2.13 `gavo.base.common`/StructCallbacks#onElementComplete().()
287
288 def doMapscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(self), aDictscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(aDict)):
289 """returns dict with the keys mapped according to the defined mappings.
290 """
291 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(self).maps:
292 newDict = {}
293 for k, v in aDictscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(aDict).items():
294 newDict[selfscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(self).maps.get(k, k)] = v
295 return newDict
296 else:
297 return aDictscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#doMap().(aDict)
298
299
300 class RowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#(objectscip-python python python-stdlib 3.11 builtins/object#):
301 """An object that encapsulates the a source being parsed by a
302 grammar.
303
304 RowIterators are returned by Grammars' parse methods. Iterate
305 over them to retrieve the rows contained in the source.
306
307 You can also call getParameters on them to retrieve document-global
308 values (e.g., the parameters of a VOTable, a global header of
309 a FITS table).
310
311 The getLocator method should return some string that aids the user
312 in finding out why something went wrong (file name, line number, etc.)
313
314 This default implementation works for when source is a sequence
315 of dictionaries. You will, in general, want to override
316 _iteRows and getLocator, plus probably __init__ (to prepare external
317 resources) and getParameters (if you have them; make sure to update
318 any parameters you have with self.sourceRow as shown in the default
319 getParameters implementation).
320
321 RowIterators are supposed to be self-destructing, i.e., they should
322 release any external resources they hold when _iterRows runs out of
323 items.
324
325 _iterRows should arrange for the instance variable recNo to be incremented
326 by one for each item returned.
327 """
328 notifyscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#notify. = True
329
330 def __init__scip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(self), grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(sourceToken), sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(sourceRow)=None):
331 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar., selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceToken. = grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(sourceToken)
332 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow. = sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(sourceRow)
333 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(self).recNoscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#recNo. = 0
334
335 def __iter__scip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self)):
336 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self).notifyscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#notify.:
337 basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifyNewSourcescip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifyNewSource().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceToken.)
338 baseIter = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self)._iterRowsProcessedscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().()
339
340 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..ignoreOn:
341 rowSource = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self)._filteredIterscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(baseIter)
342 else:
343 rowSource = baseIter
344
345 try:
346 try:
347 for row in rowSource:
348 # handle dispatched grammars here, too
349 if isinstance(row, tuplescip-python python python-stdlib 3.11 builtins/tuple#):
350 d = row[1]
351 else:
352 d = row
353
354 if isinstance(d, dictscip-python python python-stdlib 3.11 builtins/dict#):
355 d["parser_"] = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self)
356
357 yield row
358 except Exceptionscip-python python python-stdlib 3.11 builtins/Exception#:
359 basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifySourceErrorscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifySourceError().()
360 raise
361
362 finally:
363 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__iter__().(self).finalizescip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#finalize().()
364
365 def finalizescip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#finalize().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#finalize().(self)):
366 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#finalize().(self).notifyscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#notify.:
367 basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifySourceFinishedscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifySourceFinished().()
368
369 def _filteredIterscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(self), baseIterscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(baseIter)):
370 for row in baseIterscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(baseIter):
371 if not selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_filteredIter().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..ignoreOn(row):
372 yield row
373
374 def _iterRowsProcessedscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self)):
375 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..isDispatching:
376 for item in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self)._iterRowsscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRows().():
377 if not isinstance(item, tuplescip-python python python-stdlib 3.11 builtins/tuple#):
378 # That's mainly for rsc.FLUSH
379 yield item
380 else:
381 dest, row = item
382 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow. and isinstance(row, dictscip-python python python-stdlib 3.11 builtins/dict#):
383 row.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow.)
384 for procRow in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).rowfilter(row, selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self)):
385 yield dest, procRow
386 else:
387 for row in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self)._iterRowsscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRows().():
388 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow. and isinstance(row, dictscip-python python python-stdlib 3.11 builtins/dict#):
389 row.update(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow.)
390 for procRow in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self).rowfilter(row, selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRowsProcessed().(self)):
391 yield procRow
392
393 def _iterRowsscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRows().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRows().(self)):
394 if False:
395 yield None
396 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#_iterRows().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar. = None # don't wait for garbage collection
397
398 def getParametersscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getParameters().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getParameters().(self)):
399 res = {"parser_": selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getParameters().(self)}
400 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getParameters().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow.:
401 res.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getParameters().(self).sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#sourceRow.)
402 return res
403
404 def getLocatorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getLocator().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#getLocator().(self)):
405 return "(unknown position -- locator missing)"
406
407
408 def wrapFileForscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj), desiredModescip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(desiredMode), encscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(enc)):
409 """wraps or unwraps fileobj so that it matches the open mode desiredMode.
410
411 If there's a "b" in desiredMode, this will return fileobj.raw if it's
412 there. Otherwise, it'll wrap it into a codec.getreader for enc.
413 """
414 # I guess there's no good way to figure out if fileobj is binary or
415 # text; so let's have a bit of heuristics.
416 if hasattr(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj), "mode"):
417 foundMode = fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj).mode
418 elif isinstance(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj), ioscip-python python python-stdlib 3.11 io/__init__:.BytesIOscip-python python python-stdlib 3.11 io/BytesIO#):
419 foundMode = "rb"
420 elif isinstance(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj), ioscip-python python python-stdlib 3.11 io/__init__:.StringIOscip-python python python-stdlib 3.11 io/StringIO#):
421 foundMode = "r"
422 else:
423 foundMode = "r" if hasattr(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj), "raw") else "rb"
424
425 if "b" in desiredModescip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(desiredMode):
426 if "b" in foundMode:
427 return fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj)
428 else:
429 return fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj).rawscip-python python python-stdlib 3.11 io/BufferedIOBase#raw.
430
431 else:
432 if "b" in foundMode:
433 return codecsscip-python python python-stdlib 3.11 codecs/__init__:.getreaderscip-python python python-stdlib 3.11 codecs/getreader().(encscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(enc))(fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj))
434 else:
435 return fileobjscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(fileobj)
436
437
438 class FileRowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#(RowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#):
439 """is a RowIterator base for RowIterators reading files.
440
441 It analyzes the sourceToken to see if it's a string, in which case
442 it opens it as a file name and leaves the file object in self.inputFile.
443
444 Otherwise, it assumes sourceToken already is a file object and binds
445 it to self.inputFile. It then tries to come up with a sensible designation
446 for sourceToken.
447
448 It also inspects the parent grammar for a gunzip attribute. If it is
449 present and true, the input file will be unzipped transparently. Don't
450 add more features like this; preFilter is a lot more flexible.
451
452 Classes using this reading binary data will want to set fileMode
453 to rb. If they don't what's returned is strings.
454 """
455 fileModescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#fileMode. = "r"
456
457 def __init__scip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(self), grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(sourceToken), **kwargsscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(kwargs)):
458 RowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#.__init__scip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(self), grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(sourceToken), **kwargsscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(kwargs))
459 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(self).curLinescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#curLine. = 1
460 try:
461 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(self)._openFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().()
462 except IOErrorscip-python python python-stdlib 3.11 builtins/IOError# as ex:
463 raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..logOldExcscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#logOldExc().(
464 basescip-python python gavodachs 2.13 `gavo.base`/__init__:.SourceParseErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#("I/O operation failed (%s)"%strscip-python python python-stdlib 3.11 builtins/str#(ex),
465 sourcescip-python python gavodachs 2.13 `gavo.utils.excs`/SourceParseError#__init__().(source)=strscip-python python python-stdlib 3.11 builtins/str#(sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(sourceToken)), locationscip-python python gavodachs 2.13 `gavo.utils.excs`/SourceParseError#__init__().(location)="start"))
466
467 def _openFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self)):
468 preFilter = None
469 # we'll use at the end to generate a sourceToken for display purposes
470 parsingFrom = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#sourceToken.
471 if hasattr(parsingFrom, "name"):
472 # it' probably an open file
473 parsingFrom = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#sourceToken..name
474 if not isinstance(parsingFrom, strscip-python python python-stdlib 3.11 builtins/str#):
475 parsingFrom = repr(parsingFrom)
476
477 if hasattr(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar., "gunzip") and selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..gunzip:
478 preFilter = "zcat"
479 preFilter = preFilter or (
480 hasattr(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar., "preFilter") and selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..preFilter)
481
482 # need to handle preFilter first, as that needs a binary file.
483 curSrc = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#sourceToken.
484 if preFilter:
485 if isinstance(curSrc, strscip-python python python-stdlib 3.11 builtins/str#):
486 curSrc = openscip-python python python-stdlib 3.11 builtins/open().(curSrc, "rb")
487 else:
488 curSrc = curSrc
489 curSrc = FilteredInputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FilteredInputFile#(preFilter, curSrc)
490
491 if isinstance(curSrc, strscip-python python python-stdlib 3.11 builtins/str#):
492 curSrc = openscip-python python python-stdlib 3.11 builtins/open().(curSrc, "rb")
493
494 # now curSrc is a binary file. If a normal file mode
495 # is requested, wrap it into a codec.
496 if "b" not in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).fileModescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#fileMode.:
497 curSrc = wrapFileForscip-python python gavodachs 2.13 `gavo.grammars.common`/wrapFileFor().(
498 curSrc, selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).fileModescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#fileMode., selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..enc or "ascii")
499
500 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).inputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#inputFile. = curSrc
501 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#_openFile().(self).sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#sourceToken. = parsingFrom
502
503 def finalizescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#finalize().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#finalize().(self)):
504 RowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#.finalizescip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#finalize().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#finalize().(self))
505 if hasattr(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#finalize().(self).inputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#inputFile., "close"):
506 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#finalize().(self).inputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#inputFile..closescip-python python python-stdlib 3.11 io/IOBase#close().()
507
508
509 class FileRowAttributesscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructCallbacksscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#):
510 """A mixin for grammars with FileRowIterators.
511
512 This provides some attributes that FileRowIterators interpret, e.g.,
513 preFilter.
514 """
515 _gunzipscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#_gunzip. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.BooleanAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("gunzip", descriptionscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(description)="Unzip sources"
516 " while reading? (Deprecated, use preFilter='zcat')", defaultscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(default)=False)
517 _preFilterscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#_preFilter. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.UnicodeAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("preFilter", description="Shell"
518 " command to pipe the input through before passing it on to the"
519 " grammar. Classical examples include zcat or bzcat, but you"
520 " can commit arbitrary shell atrocities here.",
521 copyable=True)
522
523 def completeElementscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(self), ctxscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(ctx)):
524 if ctxscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(ctx).restrictedscip-python python gavodachs 2.13 `gavo.base.parsecontext`/ParseContext#restricted.:
525 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(self).preFilter is not None:
526 raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.RestrictedElementscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#("preFilter")
527 superscip-python python python-stdlib 3.11 builtins/super#().completeElementscip-python python gavodachs 2.13 `gavo.base.common`/StructCallbacks#completeElement().(ctxscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#completeElement().(ctx))
528
529
530 class GrammarMacroMixinscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StandardMacroMixinscip-python python gavodachs 2.13 `gavo.base.macros`/StandardMacroMixin#):
531 """A collection of macros available to rowfilters.
532
533 NOTE: All macros should return only one single physical python line,
534 or they will mess up the calculation of what constructs caused errors.
535 """
536 def macro_inputRelativePathscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputRelativePath().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputRelativePath().(self), liberalCharsscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputRelativePath().(liberalChars)="True"):
537 """returns an expression giving the current source's path
538 relative to inputsDir
539
540 liberalChars can be a boolean literal (True, False, etc); if false,
541 a value error is raised if characters that will result in trouble
542 with the product mixin are within the result path.
543
544 In rowmakers fed by grammars with //products#define, better use
545 @prodtblAccref.
546 """
547 return ('str(utils.getRelativePath(rowIter.sourceToken,'
548 ' base.getConfig("inputsDir"), liberalChars=%s))'%(
549 basescip-python python gavodachs 2.13 `gavo.base`/__init__:.parseBooleanLiteral(liberalCharsscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputRelativePath().(liberalChars))))
550
551 def macro_fullDLURLscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(self), dlServicescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(dlService), accrefscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(accref)="rowIter.sourceToken"):
552 r"""returns a python expression giving a link to the full current data
553 set retrieved through the datalink service.
554
555 You would write \fullDLURL{dlsvc} here, and the macro will expand into
556 something like http://yourserver/currd/dlsvc/dlget?ID=ivo://whatever.
557
558 dlService is the id of the datalink service in the current RD.
559
560 accref is where to the get dataset's accref from; the default takes
561 the file name, but you will need to override that when, e.g., multiple
562 datasets are produced from one file.
563
564 This is intended for "virtual" data where the dataset is generated
565 on the fly through datalink.
566 """
567 baseURL = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(self).rd.getById(dlServicescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(dlService)).getURL("dlget")
568 return ("'%%s?ID=%%s'%%(%s,"
569 f" urllib.parse.quote_plus(getStandardPubDID({accrefscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_fullDLURL().(accref)})))"%(
570 repr(baseURL)))
571
572 def macro_dlMetaURIscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_dlMetaURI().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_dlMetaURI().(self), dlServicescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_dlMetaURI().(dlService)):
573 r"""like fullDLURL, except it points to the datalink metadata.
574
575 This is intended for binding to //products#define's datalink
576 parameter.
577
578 If you need the value in a rowmaker, grab it from @prodtblDatalink.
579 """
580 baseURL = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_dlMetaURI().(self).rd.getById(dlServicescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_dlMetaURI().(dlService)).getURL("dlmeta")
581 return ("'%%s?ID=%%s'%%(%s,"
582 " urllib.parse.quote_plus(getStandardPubDID(rowIter.sourceToken)))"%(
583 repr(baseURL)))
584
585 def macro_standardPreviewPathscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_standardPreviewPath().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_standardPreviewPath().(self)):
586 """returns an expression for the standard path for a custom preview.
587
588 This consists of resdir, the name of the previewDir property on the
589 embedding DD, and the flat name of the accref (which this macro
590 assumes to see in its namespace as accref; this is usually the
591 case in //products#define, which is where this macro would typically be
592 used).
593
594 As an alternative, there is the splitPreviewPath macro, which does not
595 mogrify the file name. In particular, do not use standardPreviewPath
596 when you have more than a few 1e4 files, as it will have all these
597 files in a single, flat directory, and that can become a chore.
598
599 See the introduction to custom previews for details.
600 """
601 constantPrefix = osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..joinscip-python python python-stdlib 3.11 ntpath/join().(
602 rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.getInputsRelativePath(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_standardPreviewPath().(self).parent.rd.resdir, liberalChars=True),
603 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_standardPreviewPath().(self).parent.getProperty("previewDir"))+"/"
604 return (repr(constantPrefix)
605 +"+getFlatName(accref)")
606
607 def macro_splitPreviewPathscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(self), extscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(ext)):
608 """returns an expression for the split standard path for a custom
609 preview.
610
611 As standardPreviewPath, except that the directory hierarchy of the data
612 files will be reproduced in previews. For ext, you should typically pass
613 the extension appropriate for the preview (like {.png} or {.jpeg}).
614
615 See the introduction to custom previews for details.
616 """
617 constantPrefix = osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..joinscip-python python python-stdlib 3.11 ntpath/join().(
618 rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.getInputsRelativePath(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(self).parent.rd.resdir),
619 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(self).parent.getProperty("previewDir"))+"/"
620 return (repr(constantPrefix)
621 +"+accref+'%s'"%extscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_splitPreviewPath().(ext))
622
623 def macro_sourceDatescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_sourceDate().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_sourceDate().(self)):
624 """returns an expression giving the timestamp of the current source.
625 """
626 return ('datetime.datetime.utcfromtimestamp('
627 'os.path.getmtime(rowIter.sourceToken))')
628
629 def macro_srcstemscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_srcstem().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_srcstem().(self)):
630 """returns python code for the stem of the source file currently parsed in a rowmaker.
631
632 Example: if you're currently parsing /tmp/foo.bar, the stem is foo.
633 """
634 return 'getFileStem(rowIter.sourceToken)'
635
636 def macro_rootlessPathscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_rootlessPath().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_rootlessPath().(self)):
637 """returns an expression giving the current source's path with
638 the resource descriptor's root removed.
639 """
640 return ('utils.getRelativePath(rowIter.sourceToken,'
641 ' rowIter.grammar.rd.resdir)')
642
643 def macro_inputSizescip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputSize().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_inputSize().(self)):
644 """returns an expression giving the size of the current source.
645 """
646 return 'os.path.getsize(rowIter.sourceToken)'
647
648 def macro_colNamesscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_colNames().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_colNames().(self), tableRefscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_colNames().(tableRef)):
649 """returns a comma-separated list of column names for a table reference.
650
651 This is convenient if an input file matches the table structure; you
652 can then simply say things like <reGrammar names="\\\\colName{someTable}"/>.
653 """
654 return ",".joinscip-python python python-stdlib 3.11 builtins/str#join().(c.name for c in selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_colNames().(self).rd.getById(tableRefscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_colNames().(tableRef)))
655
656 def macro_propertyscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_property().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_property().(self), propertyscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_property().(property)):
657 """returns the value of property on the parent DD.
658 """
659 return selfscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_property().(self).parent.getProperty(propertyscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#macro_property().(property))
660
661
662 class Grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.Structurescip-python python gavodachs 2.13 `gavo.base.structure`/Structure#, GrammarMacroMixinscip-python python gavodachs 2.13 `gavo.grammars.common`/GrammarMacroMixin#):
663 """An abstract grammar.
664
665 Grammars are configured via their structure parameters. Their
666 parse(sourceToken) method returns an object that iterates over rawdicts
667 (dictionaries mapping keys to (typically) strings) that can then be fed
668 through rowmakers; it also has a method getParameters that returns
669 global properties of the whole document (like parameters in VOTables;
670 this will be empty for many kinds of grammars).
671
672 RowIterators will return a reference to themselves in the raw dicts in the
673 parser_ key unless you override their _iterRowsProcessed method (which you
674 shouldn't). This is used by rowmaker macros.
675
676 What exactly sourceToken is is up to the concrete grammar. While
677 typically it's a file name, it might be a sequence of dictionaries,
678 a twisted web request, or whatever.
679
680 To derive a concrete Grammar, define a RowIterator for your source
681 and set the rowIterator class attribute to it.
682 """
683 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#name_. = "grammar"
684
685 _encodingscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_encoding. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.UnicodeAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("enc", default=None, description=
686 "Encoding of the source file(s).", copyable=True)
687 _rowfiltersscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_rowfilters. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructListAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#("rowfilters",
688 descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#__init__().(description)="Row filters for this grammar.",
689 childFactoryscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#__init__().(childFactory)=Rowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/Rowfilter#, copyable=True)
690 _ignoreOnscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_ignoreOn. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#("ignoreOn", defaultscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(default)=None, copyable=True,
691 descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(description)="Conditions for ignoring certain input records. These"
692 " triggers drop an input record entirely. In modern RDs, prefer"
693 " rowfilters raising SkipThis.",
694 childFactoryscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(childFactory)=rowtriggersscip-python python gavodachs 2.13 `gavo.rscdef.rowtriggers`/__init__:.IgnoreOnscip-python python gavodachs 2.13 `gavo.rscdef.rowtriggers`/IgnoreOn#)
695 _sourceFieldsscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_sourceFields. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#("sourceFields", defaultscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(default)=None,
696 copyable=True, descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(description)="Code returning a dictionary of values"
697 " added to all returned rows.", childFactoryscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(childFactory)=SourceFieldAppscip-python python gavodachs 2.13 `gavo.grammars.common`/SourceFieldApp#)
698 _propertiesscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_properties. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.PropertyAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#(copyable=True)
699 _originalscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_original. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.OriginalAttributescip-python python gavodachs 2.13 `gavo.base.parsecontext`/OriginalAttribute#()
700 _rdscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_rd. = rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.RDAttributescip-python python gavodachs 2.13 `gavo.rscdef.common`/RDAttribute#()
701
702 # isDispatching is used by various special grammars to signify the
703 # grammar returns rowdicts for multiple makers. See those.
704 # Here, we just fix it to false so clients can rely on the attribute's
705 # existence.
706 isDispatchingscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#isDispatching. = False
707
708 rowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#rowIterator. = RowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#
709
710 def getSourceFieldsscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(sourceToken), datascip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(data)):
711 """returns a dict containing user-defined fields to be added to
712 all results.
713 """
714 if selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self).sourceFields is None:
715 return None
716 if not hasattr(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self), "_compiledSourceFields"):
717 selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self)._compiledSourceFieldsscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_compiledSourceFields. = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self).sourceFields.compile()
718 return selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(self)._compiledSourceFieldsscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#_compiledSourceFields.(sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(sourceToken), datascip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(data))
719
720 def parsescip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(self), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(sourceToken), targetDatascip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(targetData)=None):
721 ri = selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(self).rowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#rowIterator.(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(self), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(sourceToken),
722 sourceRowscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#__init__().(sourceRow)=selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(self).getSourceFieldsscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#getSourceFields().(sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(sourceToken), targetDatascip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(targetData)))
723 ri.rowfilter = compileRowfilterscip-python python gavodachs 2.13 `gavo.grammars.common`/compileRowfilter().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#parse().(self).rowfilters)
724 return ri
725
726
727 class NullGrammarscip-python python gavodachs 2.13 `gavo.grammars.common`/NullGrammar#(Grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#):
728 """A grammar that never returns any rows.
729 """
730 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/NullGrammar#name_. = "nullGrammar"
731
732
733 class TransparentGrammarscip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#(Grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#):
734 """A grammar that returns its sourceToken as the row iterator.
735
736 This only makes sense in extreme situations and never without custom
737 code. If you're not sure you need this, you don't want to know about
738 it.
739 """
740 name_scip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#name_. = "transparentGrammar"
741
742 def parsescip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#parse().(selfscip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#parse().(self), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#parse().(sourceToken), targetDatascip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#parse().(targetData)=None):
743 return sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.common`/TransparentGrammar#parse().(sourceToken)