Skip to content

Commit 446aaf7

Browse files
committed
Simplified ciq_tag lib a bit, added command 'seq' to CLI
1 parent 2442586 commit 446aaf7

2 files changed

Lines changed: 164 additions & 56 deletions

File tree

ciq-tag.py

Lines changed: 92 additions & 11 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):
@@ -96,8 +96,8 @@ class ClickDef(Enum):
9696

9797
TRIM = args(
9898
"--trim",
99-
"-t",
100-
flag_value=(not ciq_tag.DEFAULT_TRIM),
99+
"-x",
100+
flag_value=True,
101101
help="""
102102
Trim the value from whitespaces at the beginning and end before inserting to a commit message as a
103103
tag value. Useful when reading the tag value from a file, which can have trailing newlines
@@ -129,6 +129,33 @@ class ClickDef(Enum):
129129
help="If WRAP flag is given wrap the value text to this many columns.",
130130
)
131131

132+
DELETE = args(
133+
"--delete",
134+
"-d",
135+
type=str,
136+
multiple=True,
137+
help="<tag> to delete, exactly as would be done with the 'delete' command.",
138+
)
139+
140+
SET = args(
141+
"--set",
142+
"-s",
143+
type=(str, str),
144+
multiple=True,
145+
help="<tag> <value> pair to set in the message, exactly as would be done with the 'set' command.",
146+
)
147+
148+
SET_FROM_FILE = args(
149+
"--set-from-file",
150+
"-S",
151+
type=(str, str),
152+
multiple=True,
153+
help="""
154+
<tag> <file> pair, where <file> contains the <value> to set for the <tag> in the message, exactly as
155+
would be done with the 'set' command using --val-from-file option.
156+
""",
157+
)
158+
132159
def __init__(self, positional, keyword):
133160
self.positional = positional
134161
self.keyword = keyword
@@ -198,7 +225,6 @@ def command_modify(tag, value, index, val_from_file, trim, indent, wrap, wrap_wi
198225
parse_tag(tag),
199226
read_value(value, val_from_file, trim),
200227
index,
201-
trim=trim,
202228
indent=indent,
203229
wrap=wrap,
204230
wrap_width=wrap_width,
@@ -227,7 +253,6 @@ def command_add(tag, value, val_from_file, trim, indent, wrap, wrap_width):
227253
ciq_tag.CiqMsg.add_tag,
228254
parse_tag(tag),
229255
read_value(value, val_from_file, trim),
230-
trim=trim,
231256
indent=indent,
232257
wrap=wrap,
233258
wrap_width=wrap_width,
@@ -258,7 +283,6 @@ def command_set(tag, value, index, val_from_file, trim, indent, wrap, wrap_width
258283
parse_tag(tag),
259284
read_value(value, val_from_file, trim),
260285
index,
261-
trim=trim,
262286
indent=indent,
263287
wrap=wrap,
264288
wrap_width=wrap_width,
@@ -284,6 +308,63 @@ def command_delete(tag, index):
284308
)
285309

286310

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

0 commit comments

Comments
 (0)