Blame doc/rhbug.py

Packit 6f3914
# rhbug.py
Packit 6f3914
# rhbug Sphinx extension.
Packit 6f3914
#
Packit 6f3914
# Copyright (C) 2012-2016 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
Packit 6f3914
from docutils import nodes
Packit 6f3914
Packit 6f3914
try:
Packit 6f3914
    import bugzilla
Packit 6f3914
except ImportError:
Packit 6f3914
    bugzilla = None
Packit 6f3914
import json
Packit 6f3914
import os
Packit 6f3914
Packit 6f3914
class Summary(object):
Packit 6f3914
    def __init__(self, cache_fn):
Packit 6f3914
        self.cache_fn = cache_fn
Packit 6f3914
Packit 6f3914
    def __call__(self, bug_id):
Packit 6f3914
        bug_id = int(bug_id)
Packit 6f3914
        summary = self._from_cache(bug_id)
Packit 6f3914
        if summary is not None:
Packit 6f3914
            return summary
Packit 6f3914
        summary = self._from_bugzilla(bug_id)
Packit 6f3914
        self._store_in_cache(bug_id, summary)
Packit 6f3914
        return summary
Packit 6f3914
Packit 6f3914
    def _from_bugzilla(self, bug_id):
Packit 6f3914
        if bugzilla is None:
Packit 6f3914
            return ''
Packit 6f3914
        rhbz = bugzilla.RHBugzilla(url="https://bugzilla.redhat.com/xmlrpc.cgi")
Packit 6f3914
        query = rhbz.build_query(bug_id=bug_id)
Packit 6f3914
        bug = rhbz.query(query)[0]
Packit 6f3914
        return bug.summary
Packit 6f3914
Packit 6f3914
    def _from_cache(self, bug_id):
Packit 6f3914
        try:
Packit 6f3914
            with open(self.cache_fn, 'r') as json_file:
Packit 6f3914
                cache = json.load(json_file)
Packit 6f3914
                summary = [entry[1] for entry in cache if entry[0] == bug_id]
Packit 6f3914
                return summary[0]
Packit 6f3914
        except (IOError, IndexError):
Packit 6f3914
            return None
Packit 6f3914
Packit 6f3914
    def _store_in_cache(self, bug_id, summary):
Packit 6f3914
        if bugzilla is None:
Packit 6f3914
            return
Packit 6f3914
        try:
Packit 6f3914
            with open(self.cache_fn, 'r') as json_file:
Packit 6f3914
                cache = json.load(json_file)
Packit 6f3914
        except IOError:
Packit 6f3914
            cache = []
Packit 6f3914
        cache.append((bug_id, summary))
Packit 6f3914
        with open(self.cache_fn, 'w') as json_file:
Packit 6f3914
            json.dump(cache, json_file, indent=4)
Packit 6f3914
Packit 6f3914
def RhBug_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
Packit 6f3914
    source = inliner.document.settings._source
Packit 6f3914
    summaries_fn = '%s/summaries_cache' % os.path.dirname(source)
Packit 6f3914
    summary = Summary(summaries_fn)(text)
Packit 6f3914
    link_name = 'Bug %s - %s' % (text, summary)
Packit 6f3914
    url = 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' % text
Packit 6f3914
    node = nodes.reference(rawtext, link_name, refuri=url)
Packit 6f3914
    return [node], []
Packit 6f3914
Packit 6f3914
def setup(app):
Packit 6f3914
    app.add_role('rhbug', RhBug_role)