Skip to content

Commit bf54e3e

Browse files
committed
Simplified ciq_tag lib a bit, added command 'seq' to CLI
1 parent a269fd3 commit bf54e3e

2 files changed

Lines changed: 162 additions & 55 deletions

File tree

ciq-tag.py

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def open_output(filename, **rest):
3838
def process_in_out(input, output, result_to_output_map, ciq_msg_method, *method_args_pos, **method_args_key):
3939
with open_input(input) as in_file:
4040
input_str = "".join(in_file.readlines())
41+
msg = ciq_tag.CiqMsg(input_str)
42+
ret, out = result_to_output_map(msg, ciq_msg_method(msg, *method_args_pos, **method_args_key))
43+
if out:
4144
with open_output(output) as out_file:
42-
msg = ciq_tag.CiqMsg(input_str)
43-
ret, out = result_to_output_map(msg, ciq_msg_method(msg, *method_args_pos, **method_args_key))
44-
if out:
45-
print(out, file=out_file, end="")
46-
if ret != 0:
47-
raise CmdException(ret)
45+
print(out, file=out_file, end="")
46+
if ret != 0:
47+
raise CmdException(ret)
4848

4949

5050
def parse_tag(tag_name):
@@ -97,7 +97,6 @@ class ClickDef(Enum):
9797
TRIM = args(
9898
"--trim",
9999
"-t",
100-
flag_value=(not ciq_tag.DEFAULT_TRIM),
101100
help="""
102101
Trim the value from whitespaces at the beginning and end before inserting to a commit message as a
103102
tag value. Useful when reading the tag value from a file, which can have trailing newlines
@@ -129,6 +128,33 @@ class ClickDef(Enum):
129128
help="If WRAP flag is given wrap the value text to this many columns.",
130129
)
131130

131+
DELETE = args(
132+
"--delete",
133+
"-d",
134+
type=str,
135+
multiple=True,
136+
help="<tag> to delete, exactly as would be done with the 'delete' command.",
137+
)
138+
139+
SET = args(
140+
"--set",
141+
"-s",
142+
type=(str, str),
143+
multiple=True,
144+
help="<tag> <value> pair to set in the message, exactly as would be done with the 'set' command.",
145+
)
146+
147+
SET_FROM_FILE = args(
148+
"--set-from-file",
149+
"-S",
150+
type=(str, str),
151+
multiple=True,
152+
help="""
153+
<tag> <file> pair, where <file> contains the <value> to set for the <tag> in the message, exactly as
154+
would be done with the 'set' command using --val-from-file option.
155+
""",
156+
)
157+
132158
def __init__(self, positional, keyword):
133159
self.positional = positional
134160
self.keyword = keyword
@@ -198,7 +224,6 @@ def command_modify(tag, value, index, val_from_file, trim, indent, wrap, wrap_wi
198224
parse_tag(tag),
199225
read_value(value, val_from_file, trim),
200226
index,
201-
trim=trim,
202227
indent=indent,
203228
wrap=wrap,
204229
wrap_width=wrap_width,
@@ -227,7 +252,6 @@ def command_add(tag, value, val_from_file, trim, indent, wrap, wrap_width):
227252
ciq_tag.CiqMsg.add_tag,
228253
parse_tag(tag),
229254
read_value(value, val_from_file, trim),
230-
trim=trim,
231255
indent=indent,
232256
wrap=wrap,
233257
wrap_width=wrap_width,
@@ -258,7 +282,6 @@ def command_set(tag, value, index, val_from_file, trim, indent, wrap, wrap_width
258282
parse_tag(tag),
259283
read_value(value, val_from_file, trim),
260284
index,
261-
trim=trim,
262285
indent=indent,
263286
wrap=wrap,
264287
wrap_width=wrap_width,
@@ -284,6 +307,63 @@ def command_delete(tag, index):
284307
)
285308

286309

310+
@cli.command(
311+
"seq",
312+
help="""
313+
Set / delete multiple tags in sequence. The tags and their values (where applicable) are specified
314+
with options --delete, --set and --set-from-file, which can be provided multiple times. First
315+
process all --delete tags, then --set, then --set-from-file. Within the same group the tags are
316+
processed in the order given on the command line. For the --set-from-file tags the trimming is
317+
always on. The --wrap-width and --indent options apply to all tags specified by --set and
318+
--set-from-file.
319+
""",
320+
epilog="<tag> ::= " + " | ".join(t.arg_name for t in ciq_tag.CiqTag),
321+
)
322+
@click.option(*ClickDef.DELETE.positional, **ClickDef.DELETE.keyword)
323+
@click.option(*ClickDef.SET.positional, **ClickDef.SET.keyword)
324+
@click.option(*ClickDef.SET_FROM_FILE.positional, **ClickDef.SET_FROM_FILE.keyword)
325+
@click.option(*ClickDef.INDENT.positional, **ClickDef.INDENT.keyword)
326+
@click.option(*ClickDef.WRAP.positional, **ClickDef.WRAP.keyword)
327+
@click.option(*ClickDef.WRAP_WIDTH.positional, **ClickDef.WRAP_WIDTH.keyword)
328+
def command_seq(delete, set, set_from_file, indent, wrap, wrap_width):
329+
def process(msg):
330+
modified = True
331+
for delete_tag in delete:
332+
modified = msg.delete_tag(parse_tag(delete_tag)) or modified
333+
for set_tag, set_value in set:
334+
modified = (
335+
msg.set_tag(
336+
parse_tag(set_tag),
337+
read_value(set_value, False, False),
338+
indent=indent,
339+
wrap=wrap,
340+
wrap_width=wrap_width,
341+
suspend_ignore_warns=True,
342+
)
343+
or modified
344+
)
345+
for fset_tag, fset_value in set_from_file:
346+
modified = (
347+
msg.set_tag(
348+
parse_tag(fset_tag),
349+
read_value(fset_value, True, True),
350+
indent=indent,
351+
wrap=wrap,
352+
wrap_width=wrap_width,
353+
suspend_ignore_warns=True,
354+
)
355+
or modified
356+
)
357+
return msg, modified
358+
359+
process_in_out(
360+
OPTIONS["input"],
361+
OPTIONS["output"],
362+
setter_map,
363+
process,
364+
)
365+
366+
287367
def main():
288368
try:
289369
cli()

0 commit comments

Comments
 (0)