From 85fc231824807a94a4ad89beabf4b2bf060b90ab Mon Sep 17 00:00:00 2001 From: Till Maas Date: Mon, 22 Nov 2010 19:46:07 +0100 Subject: move some generic methods into FEK_helper class Move bodhi_update_str, wrap_paragraphs and wrap_paragraphs_prefix as static methods in the class FEK_helper to be able to debug them easier. --- fedora-easy-karma.py | 261 ++++++++++++++++++++++++++------------------------- 1 file changed, 135 insertions(+), 126 deletions(-) diff --git a/fedora-easy-karma.py b/fedora-easy-karma.py index 60127c7..7021043 100755 --- a/fedora-easy-karma.py +++ b/fedora-easy-karma.py @@ -44,6 +44,139 @@ import yum from fedora.client.bodhi import BodhiClient +class FEK_helper(object): + + @staticmethod + def bodhi_update_str(update, bodhi_base_url="https://admin.fedoraproject.org/updates/", bugzilla_bug_url="https://bugzilla.redhat.com/", wrap_bugs=True, width=80): + + # copy update to avoid side effects + values = dict(update) + format_string = ( + "%(header_line)s\n" + "%(title)s\n" + "%(header_line)s\n" + "%(updateid)s" + " Release: %(release)s\n" + " Status: %(status)s\n" + " Type: %(type)s\n" + " Karma: %(karma)d\n" + "%(request)s" + "%(bugs)s" + "%(notes)s" + " Submitter: %(submitter)s\n" + " Submitted: %(date_submitted)s\n" + "%(comments)s" + "\n%(update_url)s" + ) + + values["header_line"] = "=" * width + values["title"] = "\n".join(wrap(update["title"].replace(",", ", "), width=width, initial_indent=" "*5, subsequent_indent=" "*5)) + + if update["updateid"]: + values["updateid"] = " Update ID: %s\n" % update["updateid"] + else: + values["updateid"] = "" + + values["release"] = update["release"]["long_name"] + + if update["request"]: + values["request"] = " Request: %s\n" % update["request"] + else: + values["request"] = "" + + if len(update["bugs"]): + bugs = [] + for bug in update["bugs"]: + bz_id = bug["bz_id"] + if bugzilla_bug_url: + bz_id = "%s%d" % ( bugzilla_bug_url, bz_id) + bz_title = bug["title"] + bugs.append("%s - %s" % (bz_id, bz_title)) + + if wrap_bugs: + values["bugs"] = "%s\n" % FEK_helper.wrap_paragraphs_prefix(bugs, first_prefix=" Bugs: ", width=width, extra_newline=True) + else: + values["bugs"] = " Bugs: %s\n" % ("\n" + " " * 11 + ": ").join(bugs) + else: + values["bugs"] = "" + + if update["notes"]: + values["notes"] = "%s\n" % FEK_helper.wrap_paragraphs_prefix(update["notes"].split("\r\n"), first_prefix=" Notes: ", width=width) + else: + values["notes"] = "" + + + if len(update["comments"]): + val = " Comments: " + comments = [] + for comment in update["comments"]: + + # copy comment to avoid side effects + comment = dict(comment) + + indent = " " * 13 + comment["indent"] = indent + + if comment["anonymous"]: + comment["author"] += " (unauthenticated)" + + comments.append("%(indent)s%(author)s - %(timestamp)s (karma %(karma)s)" % comment) + + if comment["text"]: + wrapped = wrap(comment["text"], initial_indent=indent, subsequent_indent=indent, width=width) + comments.append("\n".join(wrapped)) + val += "\n".join(comments).lstrip() + "\n" + values["comments"] = val + else: + values["comments"] = "" + + + if update["updateid"]: + url_path = "%s/%s" % (update["release"]["name"], update["updateid"]) + else: + url_path = update["title"] + + values["update_url"] = " %s%s\n" % (bodhi_base_url, url_path) + + return format_string % values + + @staticmethod + def wrap_paragraphs(paragraphs, width=67, subsequent_indent=(" "*11 + ": "), second_column_indent=0): + return ("\n%s" % subsequent_indent).join(map(lambda p: "\n".join(wrap(p, width=width, subsequent_indent=(subsequent_indent + " " * second_column_indent))), paragraphs)) + + + @staticmethod + def wrap_paragraphs_prefix(paragraphs, first_prefix, width=80, extra_newline=False): + if isinstance(paragraphs, basestring): + paragraphs = paragraphs.split("\n") + + if first_prefix: + subsequent_indent = " " * (len(first_prefix) - 2) + ": " + else: + subsequent_indent = "" + + output = [] + first = True + wrapped = [] + + # remove trailing empty paragraphs + while paragraphs[-1] == "": + paragraphs.pop() + + for p in paragraphs: + if extra_newline and len(wrapped) > 1: + output.append("") + if first: + p = first_prefix + p + first = False + + wrapped = wrap(p, width=width, subsequent_indent=subsequent_indent) + output.append("\n".join(wrapped)) + + return ("\n%s" % subsequent_indent).join(output) + + + class FedoraEasyKarma(object): def __init__(self): usage = ( @@ -67,7 +200,7 @@ class FedoraEasyKarma(object): "For patches please use 'git send-email'." ) - usage = self.wrap_paragraphs_prefix(usage, first_prefix="", width=80, extra_newline=False) + usage = FEK_helper.wrap_paragraphs_prefix(usage, first_prefix="", width=80, extra_newline=False) parser = OptionParser(usage=usage) parser.add_option("", "--bodhi-cached", dest="bodhi_cached", help="Use cached bodhi query", action="store_true", default=False) @@ -207,7 +340,7 @@ class FedoraEasyKarma(object): if not self.options.list_rpms_only: print self.bodhi_update_str(update, bodhi_base_url=bc.base_url, width=self.options.wrap_width, wrap_bugs=self.options.wrap_bugs) if self.options.wrap_rpms: - print self.wrap_paragraphs_prefix(installed_rpms, first_prefix=" inst. RPMS: ", width=self.options.wrap_width) + print FEK_helper.wrap_paragraphs_prefix(installed_rpms, first_prefix=" inst. RPMS: ", width=self.options.wrap_width) else: print " inst. RPMS: %s\n" % ("\n" + " " * 11 + ": ").join(installed_rpms) if self.already_commented(update, self.options.fas_username): @@ -243,98 +376,6 @@ class FedoraEasyKarma(object): return False - def bodhi_update_str(self, update, bodhi_base_url="https://admin.fedoraproject.org/updates/", bugzilla_bug_url="https://bugzilla.redhat.com/", wrap_bugs=True, width=80): - - # copy update to avoid side effects - values = dict(update) - format_string = ( - "%(header_line)s\n" - "%(title)s\n" - "%(header_line)s\n" - "%(updateid)s" - " Release: %(release)s\n" - " Status: %(status)s\n" - " Type: %(type)s\n" - " Karma: %(karma)d\n" - "%(request)s" - "%(bugs)s" - "%(notes)s" - " Submitter: %(submitter)s\n" - " Submitted: %(date_submitted)s\n" - "%(comments)s" - "\n%(update_url)s" - ) - - values["header_line"] = "=" * width - values["title"] = "\n".join(wrap(update["title"].replace(",", ", "), width=width, initial_indent=" "*5, subsequent_indent=" "*5)) - - if update["updateid"]: - values["updateid"] = " Update ID: %s\n" % update["updateid"] - else: - values["updateid"] = "" - - values["release"] = update["release"]["long_name"] - - if update["request"]: - values["request"] = " Request: %s\n" % update["request"] - else: - values["request"] = "" - - if len(update["bugs"]): - bugs = [] - for bug in update["bugs"]: - bz_id = bug["bz_id"] - if bugzilla_bug_url: - bz_id = "%s%d" % ( bugzilla_bug_url, bz_id) - bz_title = bug["title"] - bugs.append("%s - %s" % (bz_id, bz_title)) - - if wrap_bugs: - values["bugs"] = "%s\n" % self.wrap_paragraphs_prefix(bugs, first_prefix=" Bugs: ", width=width, extra_newline=True) - else: - values["bugs"] = " Bugs: %s\n" % ("\n" + " " * 11 + ": ").join(bugs) - else: - values["bugs"] = "" - - if update["notes"]: - values["notes"] = "%s\n" % self.wrap_paragraphs_prefix(update["notes"].split("\r\n"), first_prefix=" Notes: ", width=width) - else: - values["notes"] = "" - - - if len(update["comments"]): - val = " Comments: " - comments = [] - for comment in update["comments"]: - - # copy comment to avoid side effects - comment = dict(comment) - - indent = " " * 13 - comment["indent"] = indent - - if comment["anonymous"]: - comment["author"] += " (unauthenticated)" - - comments.append("%(indent)s%(author)s - %(timestamp)s (karma %(karma)s)" % comment) - - if comment["text"]: - wrapped = wrap(comment["text"], initial_indent=indent, subsequent_indent=indent, width=width) - comments.append("\n".join(wrapped)) - val += "\n".join(comments).lstrip() + "\n" - values["comments"] = val - else: - values["comments"] = "" - - - if update["updateid"]: - url_path = "%s/%s" % (update["release"]["name"], update["updateid"]) - else: - url_path = update["title"] - - values["update_url"] = " %s%s\n" % (bodhi_base_url, url_path) - - return format_string % values def debug(self, message, update_timestamp=True): @@ -405,38 +446,6 @@ class FedoraEasyKarma(object): return (False, 'too many errors') - def wrap_paragraphs(self, paragraphs, width=67, subsequent_indent=(" "*11 + ": "), second_column_indent=0): - return ("\n%s" % subsequent_indent).join(map(lambda p: "\n".join(wrap(p, width=width, subsequent_indent=(subsequent_indent + " " * second_column_indent))), paragraphs)) - - - def wrap_paragraphs_prefix(self, paragraphs, first_prefix, width=80, extra_newline=False): - if isinstance(paragraphs, basestring): - paragraphs = paragraphs.split("\n") - - if first_prefix: - subsequent_indent = " " * (len(first_prefix) - 2) + ": " - else: - subsequent_indent = "" - - output = [] - first = True - wrapped = [] - - # remove trailing empty paragraphs - while paragraphs[-1] == "": - paragraphs.pop() - - for p in paragraphs: - if extra_newline and len(wrapped) > 1: - output.append("") - if first: - p = first_prefix + p - first = False - - wrapped = wrap(p, width=width, subsequent_indent=subsequent_indent) - output.append("\n".join(wrapped)) - - return ("\n%s" % subsequent_indent).join(output) if __name__ == "__main__": -- cgit