Blame doc/rhbug.py

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