-
Notifications
You must be signed in to change notification settings - Fork 0
/
expandkw.py
396 lines (363 loc) · 12.7 KB
/
expandkw.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# $Id: expandkw.py bc37aaa 2018-08-18 20:49:40Z <[email protected]> $
#
# Copyright (c) 2018, Yasuhito FUTASUKI
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import os.path
import sys
import getopt
import io
import codecs
import subprocess
import re
import datetime
# temporary, read setting as a module
_default_kw = { 'Date' : '%D',
'LastChangeDate' : '%D',
'Revision' : '%r',
'LastChangeRevision' : '%r',
'Rev' : '%r',
'Author' : '%a',
'LastChangedBy' : '%a',
'HeadURL' : '%u',
'URL' : '%u',
'Id' : '%I',
'Header' : '%H'}
try:
import kw_conf
try:
default_kw = kw_conf.default_kw
except AttributeError:
default_kw = _default_kw
try:
custom_kw = kw_conf.custom_kw
except AttributeError:
custom_kw = {}
except ImportError:
default_kw = _default_kw
custom_kw = {}
#
# absorb Python 2 incompatibilities
#
try:
PathLike = os.PathLike
except AttributeError:
class PathLike(object):
pass
try:
UTC = datetime.timezone.utc
except AttributeError:
class _UTC(datetime.tzinfo):
def utcoffset(self, dt):
return datetime.dimedelta(0)
def dst(self, dt):
return None
def tzname(self, dt):
return 'UTC'
def fromutc(self, dt):
return dt
UTC = _UTC()
try:
fixedtimezone = datetime.timezone
except AttributeError:
class fixedtimezone(datetime.tzinfo):
def __init__(self, offset, name=None):
if isinstance(offset, int):
self._offset = datetime.timedelta(minutes=offset)
elif isinstance(offset, datetime.timedelta):
self._offset = offset
self._name=name
def utcoffset(self, dt):
return self._offset
def dst(self, dt):
return None
def tzname(self, dt):
if self._name:
return self._name
if self._offset == datetime.timedelta(0):
return 'UTC'
elif self._offset > datetime.timedelta(0):
return 'UTC+%02d%02d'% (self._offset.seconds // 3600,
self._offset.seconds % 3600)
else:
return 'UTC-%02d%02d'% ((- self._offset.seconds) // 3600,
(- self._offset.seconds) % 3600)
def parse_iso8601(dtstr):
try:
dt = datetime.datetime.strptime(dtstr, '%Y-%m-%d %H:%M:%S %z')
except ValueError:
dt = datetime.datetime.strptime(dtstr[0:19], '%Y-%m-%d %H:%M:%S')
if dtstr[20] == '-':
td = - datetime.timedelta(hours=int(dtstr[21:23]),
minutes=int(dtstr[23:25]))
else:
assert(dtstr[20] == '+')
td = datetime.timedelta(hours=int(dtstr[21:23]),
minutes=int(dtstr[23:25]))
dt = dt.replace(tzinfo=fixedtimezone(td))
return dt
_default_encoding = sys.getdefaultencoding()
def setdefaultencoding(enc):
codecs.lookup(enc)
_default_encoding = enc
return
def _norm(s, encoding=_default_encoding, errors='strict'):
return (s.decode(encoding, errors)
if not isinstance(s, str) and isinstance(s, bytes) else s)
#
class NoLog(Exception): pass
class FormatError(Exception): pass
def get_git_prefix():
return _norm(subprocess.check_output(
[b'git', b'rev-parse',
b'--show-prefix'])[:-1])
def get_git_topdir():
return _norm(subprocess.check_output(
[b'git', b'rev-parse',
b'--show-toplevel'])[:-1])
def get_git_log_attrs(path, isfilter=True):
arg = [b'git', b'log', b'-1',
b'--format=%H%x00%an%x00%ae%x00%ai%x00%cn%x00%ce%x00%ci%x00%s']
if isinstance(path, bytes):
arg += [b'--', path]
elif isinstance(path, str):
arg += [b'--', path.encode()]
elif isinstance(paths, PathLike):
p = path.__fspath__()
if isinstance(p, bytes):
arg += [b'--', p]
elif isinstance(p, str):
arg += [b'--', p.encode()]
else:
# foolproof
raise AttributeError('unknown PathLike object {0}'.format(repr(p)))
op = subprocess.check_output(arg)
if not op:
raise NoLog
gldic = dict(zip(['H', 'an', 'ae', 'ai', 'cn', 'ce', 'ci', 's'],
map(_norm, re.split(b'\0', op[:-1]))))
gldic['ad'] = parse_iso8601(gldic['ai'])
gldic['cd'] = parse_iso8601(gldic['ci'])
if isfilter:
gldic['gt'] = os.getcwd()
gldic['gp'] = ''
else:
gldic['gt'] = get_git_topdir()
gldic['gp'] = get_git_prefix()
return gldic
_f = { 'a' : (lambda pt, gd: '<' + gd['ae'] + '>'),
'b' : (lambda pt, gd: os.path.basename(pt)),
'd' : (lambda pt, gd:
gd['ad'].astimezone(UTC).strftime('%Y-%m-%d %H:%M:%SZ')),
'D' : (lambda pt, gd:
gd['ad'].strftime('%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)')),
'P' : (lambda pt, gd: os.path.join(gd['gp'], pt)),
'r' : (lambda pt, gd: gd['H'][0:7]),
'R' : (lambda pt, gd: gd['gt']),
'u' : (lambda pt, gd:
'file:///' + os.path.join(gd['gt'], gd['gp'], pt)),
'_' : (lambda pt, gd: ' '),
'%' : (lambda pt, gd: '%'),
'H' : (lambda pt, gd:
' '.join(map((lambda p: _f[p](pt, gd)),
['P', 'r', 'd', 'a']))),
'I' : (lambda pt, gd:
' '.join(map((lambda p: _f[p](pt, gd)),
['b', 'r', 'd', 'a'])))}
def compose_repl_str(path, gd, format):
rep = ''
sp = False
for c in format:
if sp:
rep += _f[c](path, gd)
sp = False
elif c != '%':
rep += c
else:
sp = True
if sp:
raise FormatError()
return rep
def substkw(ist, ost, kwdict):
for line in ist:
elms = line.split('$')
if len(elms) < 3:
ost.write(line)
continue
oelms = elms[0:1]
pelm = None
pkw = None
pmatch = False
for elm in elms[1:]:
if pmatch:
oelms.append(pkw)
oelms.append(elm)
pmatch = False
continue
for kw in kwdict.keys():
if elm.startswith(kw):
klen = len(kw)
if ( elm == kw
or elm == kw + ':'
or (elm[klen:klen+2] == ': ' and elm[-1] == ' ')):
pelm = elm
pkw = kw + ': ' + kwdict[kw] + ' '
pmatch = True
break
elif elm[klen:klen+3] == ':: ' and elm[-1] in ' #':
plen = len(elm) - klen - 3
if plen >= 0:
pelm = elm
slen = len(kwdict[kw])
if plen > slen:
pkw = (kw + ':: ' + kwdict[kw]
+ ' ' * (plen - slen))
else:
pkw = kw + ':: ' + kwdict[kw][0:plen-1] + '#'
pmatch = True
break
if not pmatch:
oelms.append(elm)
# if last element is matched, it is not added in oelms, and
# it should not to be unexpanded
if pmatch:
oelms.append(pelm)
ost.write('$'.join(oelms))
return
def unsubstkw(ist, ost, kwdict):
for line in ist:
elms = line.split('$')
if len(elms) < 3:
ost.write(line)
continue
oelms = elms[0:1]
pelm = None
pkw = None
pmatch = False
for elm in elms[1:]:
if pmatch:
oelms.append(pkw)
oelms.append(elm)
pmatch = False
continue
for kw in kwdict.keys():
if elm.startswith(kw):
klen = len(kw)
if ( elm == kw
or elm == kw + ':'
or (elm[klen:klen+2] == ': ' and elm[-1] == ' ')):
pelm = elm
pkw = kw
pmatch = True
break
elif elm[klen:klen+3] == ':: ' and elm[-1] in ' #':
plen = len(elm) - klen - 3
if plen >= 0:
pelm = elm
pkw = kw + ':: ' + ' ' * plen
pmatch = True
break
if not pmatch:
oelms.append(elm)
# if last element is matched, it is not added in oelms, and
# it should not to be unexpanded
if pmatch:
oelms.append(pelm)
ost.write('$'.join(oelms))
return
def read_conf(path):
try:
return custom_kw[path]
except KeyError:
return default_kw
def expand_kwdic(path, kwdic):
gldic = get_git_log_attrs(path)
return dict([(kw, compose_repl_str(path, gldic, kwdic[kw]))
for kw in kwdic.keys()])
#
# main() ... entry point
#
def main():
def usage():
sys.stderr.write(
"""usage: {0} [[-e|encoding <encoding>] -c|--clean|-s|--smudge] path
""".format(sys.argv[0]))
return
# end of usage()
# main function body
try:
opts, args = getopt.getopt(
sys.argv[1:], "ce:hs", ['clean', 'encoding', 'smudge', 'help'])
except getopt.GetoptErrror as err:
sys.stderr.write(str(err))
usage()
sys.exit(2)
smudge = True
enc = None
hf = False
for (o, a) in opts:
if o in ('-c', '--clean'):
smudge = False
elif o in ('-e', '--encoding'):
enc = a
elif o in ('-h', '--help'):
hf = True
elif o in ('-s', '--smudge'):
smudge = True
else:
assert False, "unhandled option {0}".format(o)
if hf:
usage()
sys.exit(0)
if len(args) != 1:
sys.stderr.write('error:just one file path is needed')
usage()
sys.exit(1)
if enc is not None:
setdefaultencoding(enc)
if isinstance(sys.stdin, io.TextIOWrapper):
try:
sys.stdin.reconfigure(
encoding=_default_encoding, errors='surrogateescape')
sys.stdout.reconfigure(
encoding=_default_encoding, errors='surrogateescape')
stdin = sys.stdin
stdout = sys.stdout
except AttributeError:
stdin = codecs.getreader(_default_encoding)(sys.stdin.buffer,
'surrogateescape')
stdout = codecs.getwriter(_default_encoding)(sys.stdout.buffer,
'surrogateescape')
else:
stdin = sys.stdin
stdout = sys.stdout
if smudge:
substkw(stdin, stdout, expand_kwdic(args[0], read_conf(args[0])))
else:
unsubstkw(stdin, stdout, read_conf(args[0]))
# end of main()
if __name__ == "__main__":
main()