forked from udieckmann/Kielipankki-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrt-deep
executable file
·184 lines (149 loc) · 5.56 KB
/
vrt-deep
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /usr/bin/env python3
# -*- mode: Python; -*-
from argparse import ArgumentParser, ArgumentTypeError
from itertools import groupby, chain
from tempfile import mkstemp
import enum, os, re, sys, traceback
from vrtnamelib import isbinnames, binmakenames
from vrtnamelib import binnamelist
from vrtargslib import VERSION
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
class BadData(Exception): pass # stack trace is just noise
class BadCode(Exception): pass # this cannot happen
parser = ArgumentParser(description = '''
Put "flattened" vrt into ordinary form where markup and comments
within sentence appear as markup and comments. Input need not be flat
any more but it must have a stored meta column.
''')
parser.add_argument('infile', nargs = '?', metavar = 'file',
help = 'input file (default stdin)')
parser.add_argument('--out', '-o',
dest = 'outfile', metavar = 'file',
help = 'output file (default stdout)')
parser.add_argument('--in-place', '-i',
dest = 'inplace', action = 'store_true',
help = 'overwrite input file with output')
parser.add_argument('--backup', '-b', metavar = 'bak',
help = 'keep input file with suffix bak')
parser.add_argument('--version', action = 'store_true',
help = 'print {} and exit'.format(VERSION))
class Kind(enum.Enum):
# kind of line (group of them)
meta = 1
data = 2
begin = 3
end = 4
names = 5
def identify(line):
# used to tag lines with their kind
if line.startswith(b'<!--'):
if isbinnames(line): return Kind.names, line
else: return Kind.meta, line
if line.startswith(b'<sentence'): return Kind.begin, line
if line.startswith(b'</sentence'): return Kind.end, line
if line.startswith(b'<'): return Kind.meta, line
return Kind.data, line
def shipnames(ouf, line):
names = binnamelist(line)
try:
pos = names.index(b'+')
except IndexError:
raise BadData('error: no stored meta column (+)')
names.pop(pos)
ouf.write(binmakenames(*names))
return pos
def wrap_main():
if (args.backup is not None) and '/' in args.backup:
print('usage: --backup suffix cannot contain /', file = sys.stderr)
exit(1)
if (args.backup is not None) and not args.backup:
print('usage: --backup suffix cannot be empty', file = sys.stderr)
exit(1)
if (args.backup is not None) and not args.inplace:
print('usage: --backup requires --in-place', file = sys.stderr)
exit(1)
if args.inplace and (args.infile is None):
print('usage: --in-place requires input file', file = sys.stderr)
exit(1)
if args.inplace and (args.outfile is not None):
print('usage: --in-place not allowed with --out', file = sys.stderr)
exit(1)
if (args.outfile is not None) and os.path.exists(args.outfile):
# easier to check this than that output file is different than
# input file, though it be annoying when overwrite is wanted
print('usage: --out file must not exist', file = sys.stderr)
exit(1)
try:
if args.inplace or (args.outfile is not None):
head, tail = os.path.split(args.infile
if args.inplace
else args.outfile)
fd, temp = mkstemp(dir = head, prefix = tail)
os.close(fd)
else:
temp = None
with ((args.infile and open(args.infile, mode = 'br'))
or sys.stdin.buffer) as inf:
with ((temp and open(temp, mode = 'bw'))
or sys.stdout.buffer) as ouf:
status = main(inf, ouf)
args.backup and os.rename(args.infile, args.infile + args.backup)
args.inplace and os.rename(temp, args.infile)
args.outfile and os.rename(temp, args.outfile)
exit(status)
except IOError as exn:
print(exn, file = sys.stderr)
exit(1)
def main(inf, ouf):
status = 1
try:
implement_main(inf, ouf)
status = 0
except BadData as exn:
print(parser.prog + ':', exn, file = sys.stderr)
except Exception as exn:
print(traceback.format_exc(), file = sys.stderr)
return status
def implement_main(inf, ouf):
pos = None
def isnotspace(line): return not line.isspace()
for kind, line in map(identify, filter(isnotspace, inf)):
if kind is Kind.begin:
if pos is None:
raise BadData('error: no names before sentence')
ouf.write(line)
continue
if kind is Kind.data:
ship(ouf, pos, line)
continue
if kind is Kind.names:
pos = shipnames(ouf, line)
continue
if kind is Kind.meta:
ouf.write(line)
continue
if kind is Kind.end:
ouf.write(line)
continue
raise BadCode('this cannot happen')
def ship(ouf, pos, line):
record = line.rstrip(b'\n').split(b'\t')
stored = record.pop(pos)
stored = (m.group() for m in re.finditer(br'<[^>]*>|[+]', stored))
for meta in stored:
if meta == b'+': break
ouf.write(meta)
ouf.write(b'\n')
ouf.write(b'\t'.join(record))
ouf.write(b'\n')
for meta in stored:
ouf.write(meta)
ouf.write(b'\n')
if __name__ == '__main__':
args = parser.parse_args()
if args.version:
print('vrt tools', VERSION)
else:
wrap_main()