-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathciq-tag.py
More file actions
executable file
·377 lines (318 loc) · 11.7 KB
/
ciq-tag.py
File metadata and controls
executable file
·377 lines (318 loc) · 11.7 KB
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
#!/usr/bin/env python3
import ciq_tag
import click
import os
from enum import Enum
import sys
import logging
DEFAULT_LOGLEVEL = "INFO"
LOGLEVEL = os.environ.get("LOGS", DEFAULT_LOGLEVEL).upper()
logger = logging.getLogger(__name__)
logger.propagate = False
logger.setLevel(LOGLEVEL)
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter("%(levelname)s:%(name)s:%(funcName)s: %(message)s"))
logger.addHandler(log_handler)
CIQ_TAGS_LIST = ", ".join(c.arg_name for c in ciq_tag.CiqTag)
class CmdException(Exception):
def __init__(self, exit_code, *rest):
super().__init__(*rest)
self._exit_code = exit_code
def open_input(filename, **rest):
return sys.stdin if filename == "-" else open(filename, "r", **rest)
def open_output(filename, **rest):
return sys.stdout if filename == "-" else open(filename, "w", **rest)
def process_in_out(input, output, result_to_output_map, ciq_msg_method, *method_args_pos, **method_args_key):
with open_input(input) as in_file:
input_str = "".join(in_file.readlines())
msg = ciq_tag.CiqMsg(input_str)
ret, out = result_to_output_map(msg, ciq_msg_method(msg, *method_args_pos, **method_args_key))
if out:
with open_output(output) as out_file:
print(out, file=out_file, end="")
if ret != 0:
raise CmdException(ret)
def parse_tag(tag_name):
tag = ciq_tag.CiqTag.get_by_arg_name(tag_name)
if tag:
return tag
else:
raise CmdException(1, f"Wrong TAG value. Must be one of: {CIQ_TAGS_LIST}")
def read_value(value_arg, val_from_file_arg, trim_arg):
if val_from_file_arg:
with open_input(value_arg) as inFile:
value = "".join(inFile.readlines())
else:
value = value_arg
return value.strip() if trim_arg else value
def getter_map(msg, result):
return (0, result + "\n") if result else (1, "")
def setter_map(msg, modified):
out = msg.get_message()
return (0, out) if modified else (1, out)
def args(*positional, **keyword):
return (positional, keyword)
class ClickDef(Enum):
TAG = args("tag", type=str)
VALUE = args("value", type=str)
INDEX = args("index", type=int, required=False, default=0)
VAL_FROM_FILE = args(
"--val-from-file",
"-f",
flag_value=True,
help="""
Treat the VALUE argument as a path to a file from which an actual value will be read (useful for
multi-line formatted texts)
""",
)
TRIM = args(
"--trim",
"-t",
help="""
Trim the value from whitespaces at the beginning and end before inserting to a commit message as a
tag value. Useful when reading the tag value from a file, which can have trailing newlines
""",
)
INDENT = args(
"--indent",
"-t",
type=int,
default=ciq_tag.DEFAULT_INDENT,
help="""
When inserting multi-line values indent them by this many spaces. Special value -1 means value
indenting equal to the width of the tag keyword.
""",
)
DEDENT = args("--dedent", "-T", flag_value=True, help="For the multi-line value remove the indent, if it has any.")
WRAP = args("--wrap", "-w", flag_value=(not ciq_tag.DEFAULT_INDENT), help="Enable value wrapping")
UNWRAP = args("--unwrap", "-W", flag_value=True, help="Unwrap multi-line values to a single line. Implies DEDENT.")
WRAP_WIDTH = args(
"--wrap-width",
"-c",
type=int,
default=ciq_tag.DEFAULT_WRAP_WIDTH,
help="If WRAP flag is given wrap the value text to this many columns.",
)
DELETE = args(
"--delete",
"-d",
type=str,
multiple=True,
help="<tag> to delete, exactly as would be done with the 'delete' command.",
)
SET = args(
"--set",
"-s",
type=(str, str),
multiple=True,
help="<tag> <value> pair to set in the message, exactly as would be done with the 'set' command.",
)
SET_FROM_FILE = args(
"--set-from-file",
"-S",
type=(str, str),
multiple=True,
help="""
<tag> <file> pair, where <file> contains the <value> to set for the <tag> in the message, exactly as
would be done with the 'set' command using --val-from-file option.
""",
)
def __init__(self, positional, keyword):
self.positional = positional
self.keyword = keyword
OPTIONS = {}
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
@click.option(
"--input", "-i", type=click.Path(), default="-", show_default=True, help="File path to read, or '-' for stdin"
)
@click.option(
"--output", "-o", type=click.Path(), default="-", show_default=True, help="File path to write, or '-' for stdout"
)
def cli(input, output):
OPTIONS["input"] = input
OPTIONS["output"] = output
@cli.command(
"get",
help=f"""
Print to the output (--output) the value of the INDEXth TAG in the commit message given on
the input (--input). If INDEX is not given assume it's 0, which is the first occurence of
the TAG. Exit with nonzero if TAG not found. TAG can be one of: {CIQ_TAGS_LIST}
""",
)
@click.argument(*ClickDef.TAG.positional, **ClickDef.TAG.keyword)
@click.argument(*ClickDef.INDEX.positional, **ClickDef.INDEX.keyword)
@click.option(*ClickDef.UNWRAP.positional, **ClickDef.UNWRAP.keyword)
@click.option(*ClickDef.DEDENT.positional, **ClickDef.DEDENT.keyword)
def command_get(tag, index, unwrap, dedent):
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
getter_map,
ciq_tag.CiqMsg.get_tag_value,
parse_tag(tag),
index,
unwrap=unwrap,
dedent=dedent,
)
@cli.command(
"modify",
help="""
Set the value of TAG, in its current place, using the current keyword. Return nonzero if the TAG
wasn't defined already.
""",
)
@click.argument(*ClickDef.TAG.positional, **ClickDef.TAG.keyword)
@click.argument(*ClickDef.VALUE.positional, **ClickDef.VALUE.keyword)
@click.argument(*ClickDef.INDEX.positional, **ClickDef.INDEX.keyword)
@click.option(*ClickDef.VAL_FROM_FILE.positional, **ClickDef.VAL_FROM_FILE.keyword)
@click.option(*ClickDef.TRIM.positional, **ClickDef.TRIM.keyword)
@click.option(*ClickDef.INDENT.positional, **ClickDef.INDENT.keyword)
@click.option(*ClickDef.WRAP.positional, **ClickDef.WRAP.keyword)
@click.option(*ClickDef.WRAP_WIDTH.positional, **ClickDef.WRAP_WIDTH.keyword)
def command_modify(tag, value, index, val_from_file, trim, indent, wrap, wrap_width):
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
setter_map,
ciq_tag.CiqMsg.modify_tag_value,
parse_tag(tag),
read_value(value, val_from_file, trim),
index,
indent=indent,
wrap=wrap,
wrap_width=wrap_width,
)
@cli.command(
"add",
help="""
Add a TAG with VALUE to the commit message. Attempt to locate the proper place to insert the tag then do it
using the default keyword and value formatting defined by the options.
""",
)
@click.argument(*ClickDef.TAG.positional, **ClickDef.TAG.keyword)
@click.argument(*ClickDef.VALUE.positional, **ClickDef.VALUE.keyword)
@click.option(*ClickDef.VAL_FROM_FILE.positional, **ClickDef.VAL_FROM_FILE.keyword)
@click.option(*ClickDef.TRIM.positional, **ClickDef.TRIM.keyword)
@click.option(*ClickDef.INDENT.positional, **ClickDef.INDENT.keyword)
@click.option(*ClickDef.WRAP.positional, **ClickDef.WRAP.keyword)
@click.option(*ClickDef.WRAP_WIDTH.positional, **ClickDef.WRAP_WIDTH.keyword)
def command_add(tag, value, val_from_file, trim, indent, wrap, wrap_width):
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
setter_map,
ciq_tag.CiqMsg.add_tag,
parse_tag(tag),
read_value(value, val_from_file, trim),
indent=indent,
wrap=wrap,
wrap_width=wrap_width,
)
@cli.command(
"set",
help="""
Attempt to set TAG to the VALUE in place as it would be done with the 'modify' action, using INDEX
(default 0). If that fails insert it as with the 'add' action.
""",
)
@click.argument(*ClickDef.TAG.positional, **ClickDef.TAG.keyword)
@click.argument(*ClickDef.VALUE.positional, **ClickDef.VALUE.keyword)
@click.argument(*ClickDef.INDEX.positional, **ClickDef.INDEX.keyword)
@click.option(*ClickDef.VAL_FROM_FILE.positional, **ClickDef.VAL_FROM_FILE.keyword)
@click.option(*ClickDef.TRIM.positional, **ClickDef.TRIM.keyword)
@click.option(*ClickDef.INDENT.positional, **ClickDef.INDENT.keyword)
@click.option(*ClickDef.WRAP.positional, **ClickDef.WRAP.keyword)
@click.option(*ClickDef.WRAP_WIDTH.positional, **ClickDef.WRAP_WIDTH.keyword)
def command_set(tag, value, index, val_from_file, trim, indent, wrap, wrap_width):
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
setter_map,
ciq_tag.CiqMsg.set_tag,
parse_tag(tag),
read_value(value, val_from_file, trim),
index,
indent=indent,
wrap=wrap,
wrap_width=wrap_width,
)
@cli.command(
"delete",
help="""
Delete a tag from the commit message. Attempt to keep the message formatted nicely.
""",
)
@click.argument(*ClickDef.TAG.positional, **ClickDef.TAG.keyword)
@click.argument(*ClickDef.INDEX.positional, **ClickDef.INDEX.keyword)
def command_delete(tag, index):
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
setter_map,
ciq_tag.CiqMsg.delete_tag,
ciq_tag.CiqTag.get_by_arg_name(tag),
index,
)
@cli.command(
"seq",
help="""
Set / delete multiple tags in sequence. The tags and their values (where applicable) are specified
with options --delete, --set and --set-from-file, which can be provided multiple times. First
process all --delete tags, then --set, then --set-from-file. Within the same group the tags are
processed in the order given on the command line. For the --set-from-file tags the trimming is
always on. The --wrap-width and --indent options apply to all tags specified by --set and
--set-from-file.
""",
epilog="<tag> ::= " + " | ".join(t.arg_name for t in ciq_tag.CiqTag),
)
@click.option(*ClickDef.DELETE.positional, **ClickDef.DELETE.keyword)
@click.option(*ClickDef.SET.positional, **ClickDef.SET.keyword)
@click.option(*ClickDef.SET_FROM_FILE.positional, **ClickDef.SET_FROM_FILE.keyword)
@click.option(*ClickDef.INDENT.positional, **ClickDef.INDENT.keyword)
@click.option(*ClickDef.WRAP.positional, **ClickDef.WRAP.keyword)
@click.option(*ClickDef.WRAP_WIDTH.positional, **ClickDef.WRAP_WIDTH.keyword)
def command_seq(delete, set, set_from_file, indent, wrap, wrap_width):
def process(msg):
modified = True
for delete_tag in delete:
modified = msg.delete_tag(parse_tag(delete_tag)) or modified
for set_tag, set_value in set:
modified = (
msg.set_tag(
parse_tag(set_tag),
read_value(set_value, False, False),
indent=indent,
wrap=wrap,
wrap_width=wrap_width,
suspend_ignore_warns=True,
)
or modified
)
for fset_tag, fset_value in set_from_file:
modified = (
msg.set_tag(
parse_tag(fset_tag),
read_value(fset_value, True, True),
indent=indent,
wrap=wrap,
wrap_width=wrap_width,
suspend_ignore_warns=True,
)
or modified
)
return msg, modified
process_in_out(
OPTIONS["input"],
OPTIONS["output"],
setter_map,
process,
)
def main():
try:
cli()
return 0
except CmdException as exc:
logger.error(str(exc))
return exc._exit_code
if __name__ == "__main__":
exit(main())