Skip to content

Commit f2a2351

Browse files
committed
cli: Split apart _format_output
No functional change, just a clarity cleanup Signed-off-by: Cole Robinson <[email protected]>
1 parent 0fc4eae commit f2a2351

1 file changed

Lines changed: 86 additions & 78 deletions

File tree

bugzilla/_cli.py

Lines changed: 86 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -642,90 +642,98 @@ def _convert_to_outputformat(output):
642642
return fmt
643643

644644

645-
def _format_output(bz, opt, buglist):
646-
if opt.output == 'raw':
647-
buglist = bz.getbugs([b.bug_id for b in buglist])
648-
for b in buglist:
649-
print("Bugzilla %s: " % b.bug_id)
650-
SKIP_NAMES = ["bugzilla"]
651-
for attrname in sorted(b.__dict__):
652-
if attrname in SKIP_NAMES:
653-
continue
654-
if attrname.startswith("_"):
645+
def _format_output_raw(buglist):
646+
for b in buglist:
647+
print("Bugzilla %s: " % b.bug_id)
648+
SKIP_NAMES = ["bugzilla"]
649+
for attrname in sorted(b.__dict__):
650+
if attrname in SKIP_NAMES:
651+
continue
652+
if attrname.startswith("_"):
653+
continue
654+
print(to_encoding(u"ATTRIBUTE[%s]: %s" %
655+
(attrname, b.__dict__[attrname])))
656+
print("\n\n")
657+
658+
659+
def _bug_field_repl_cb(bz, b, matchobj):
660+
# whiteboard and flag allow doing
661+
# %{whiteboard:devel} and %{flag:needinfo}
662+
# That's what 'rest' matches
663+
(fieldname, rest) = matchobj.groups()
664+
665+
if fieldname == "whiteboard" and rest:
666+
fieldname = rest + "_" + fieldname
667+
668+
if fieldname == "flag" and rest:
669+
val = b.get_flag_status(rest)
670+
671+
elif fieldname in ["flags", "flags_requestee"]:
672+
tmpstr = []
673+
for f in getattr(b, "flags", []):
674+
requestee = f.get('requestee', "")
675+
if fieldname == "flags":
676+
requestee = ""
677+
if fieldname == "flags_requestee":
678+
if requestee == "":
655679
continue
656-
print(to_encoding(u"ATTRIBUTE[%s]: %s" %
657-
(attrname, b.__dict__[attrname])))
658-
print("\n\n")
659-
return
680+
tmpstr.append("%s" % requestee)
681+
else:
682+
tmpstr.append("%s%s%s" %
683+
(f['name'], f['status'], requestee))
684+
685+
val = ",".join(tmpstr)
686+
687+
elif fieldname == "cve":
688+
cves = []
689+
for key in getattr(b, "keywords", []):
690+
# grab CVE from keywords and blockers
691+
if key.find("Security") == -1:
692+
continue
693+
for bl in b.blocks:
694+
cvebug = bz.getbug(bl)
695+
for cb in cvebug.alias:
696+
if (cb.find("CVE") != -1 and
697+
cb.strip() not in cves):
698+
cves.append(cb)
699+
val = ",".join(cves)
700+
701+
elif fieldname == "comments":
702+
val = ""
703+
for c in getattr(b, "comments", []):
704+
val += ("\n* %s - %s:\n%s\n" % (c['time'],
705+
c.get("creator", c.get("author", "")), c['text']))
706+
707+
elif fieldname == "external_bugs":
708+
val = ""
709+
for e in getattr(b, "external_bugs", []):
710+
url = e["type"]["full_url"].replace("%id%", e["ext_bz_bug_id"])
711+
if not val:
712+
val += "\n"
713+
val += "External bug: %s\n" % url
714+
715+
elif fieldname == "__unicode__":
716+
val = b.__unicode__()
717+
else:
718+
val = getattr(b, fieldname, "")
660719

661-
def bug_field(matchobj):
662-
# whiteboard and flag allow doing
663-
# %{whiteboard:devel} and %{flag:needinfo}
664-
# That's what 'rest' matches
665-
(fieldname, rest) = matchobj.groups()
666-
667-
if fieldname == "whiteboard" and rest:
668-
fieldname = rest + "_" + fieldname
669-
670-
if fieldname == "flag" and rest:
671-
val = b.get_flag_status(rest)
672-
673-
elif fieldname in ["flags", "flags_requestee"]:
674-
tmpstr = []
675-
for f in getattr(b, "flags", []):
676-
requestee = f.get('requestee', "")
677-
if fieldname == "flags":
678-
requestee = ""
679-
if fieldname == "flags_requestee":
680-
if requestee == "":
681-
continue
682-
tmpstr.append("%s" % requestee)
683-
else:
684-
tmpstr.append("%s%s%s" %
685-
(f['name'], f['status'], requestee))
686-
687-
val = ",".join(tmpstr)
688-
689-
elif fieldname == "cve":
690-
cves = []
691-
for key in getattr(b, "keywords", []):
692-
# grab CVE from keywords and blockers
693-
if key.find("Security") == -1:
694-
continue
695-
for bl in b.blocks:
696-
cvebug = bz.getbug(bl)
697-
for cb in cvebug.alias:
698-
if (cb.find("CVE") != -1 and
699-
cb.strip() not in cves):
700-
cves.append(cb)
701-
val = ",".join(cves)
702-
703-
elif fieldname == "comments":
704-
val = ""
705-
for c in getattr(b, "comments", []):
706-
val += ("\n* %s - %s:\n%s\n" % (c['time'],
707-
c.get("creator", c.get("author", "")), c['text']))
708-
709-
elif fieldname == "external_bugs":
710-
val = ""
711-
for e in getattr(b, "external_bugs", []):
712-
url = e["type"]["full_url"].replace("%id%", e["ext_bz_bug_id"])
713-
if not val:
714-
val += "\n"
715-
val += "External bug: %s\n" % url
716-
717-
elif fieldname == "__unicode__":
718-
val = b.__unicode__()
719-
else:
720-
val = getattr(b, fieldname, "")
720+
vallist = isinstance(val, list) and val or [val]
721+
val = ','.join([to_encoding(v) for v in vallist])
721722

722-
vallist = isinstance(val, list) and val or [val]
723-
val = ','.join([to_encoding(v) for v in vallist])
723+
return val
724724

725-
return val
725+
726+
def _format_output(bz, opt, buglist):
727+
if opt.output == 'raw':
728+
buglist = bz.getbugs([b.bug_id for b in buglist])
729+
_format_output_raw(buglist)
730+
return
726731

727732
for b in buglist:
728-
print(format_field_re.sub(bug_field, opt.outputformat))
733+
# pylint: disable=cell-var-from-loop
734+
def cb(matchobj):
735+
return _bug_field_repl_cb(bz, b, matchobj)
736+
print(format_field_re.sub(cb, opt.outputformat))
729737

730738

731739
def _parse_triset(vallist, checkplus=True, checkminus=True, checkequal=True,

0 commit comments

Comments
 (0)