Skip to content

Commit 5cd48c7

Browse files
committed
examples: Add bug_autorefresh.py example
Demonstrates the bug_autorefresh behavior, and explains a bit more about the default change and what you need to do.
1 parent 312c23a commit 5cd48c7

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

examples/bug_autorefresh.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
#
3+
# This program is free software; you can redistribute it and/or modify it
4+
# under the terms of the GNU General Public License as published by the
5+
# Free Software Foundation; either version 2 of the License, or (at your
6+
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
7+
# the full text of the license.
8+
9+
# bug_autorefresh.py: Show what bug_autorefresh is all about, and explain
10+
# how to handle the default change via python-bugzilla in 2016
11+
12+
from __future__ import print_function
13+
14+
import bugzilla
15+
16+
# public test instance of bugzilla.redhat.com. It's okay to make changes
17+
URL = "partner-bugzilla.redhat.com"
18+
bzapi = bugzilla.Bugzilla(URL)
19+
20+
# The Bugzilla.bug_autorefresh setting controls whether bugs will
21+
# automatically go out and try to update their cached contents when code
22+
# tries to access a bug attribute that isn't already cached.
23+
#
24+
# Note this is likely only relevant if some part of your code is using
25+
# include_fields, or exclude_fields, or you are depending on access
26+
# to bugzilla.redhat.com 'extra_fields' type data like 'attachments'
27+
# without explicitly asking the API for them. If you aren't using any
28+
# of those bits, you can ignore this.
29+
#
30+
# Though if you aren't using include_fields and you are running regular
31+
# queries in a script, check examples/query.py for a simple usecase that
32+
# shows how much include_fields usage can speed up your scripts.
33+
34+
# The default as of mid 2016 is bug_autorefresh=off, so set it True here
35+
# to demonstrate
36+
bzapi.bug_autorefresh = True
37+
bug = bzapi.getbug(427301, include_fields=["id", "summary"])
38+
39+
# The limited include_fields here means that only "id" and "summary" fields
40+
# of the bug are cached in the bug object. What happens when we try to
41+
# get component for example?
42+
print("Bug component=%s" % bug.component)
43+
44+
# Because bug_autorefresh is True, the bug object basically did a
45+
# a bug.refresh() for us, grabbed all its data, and now the component field
46+
# is there. Let's try it again, but this time without bug_autorefresh
47+
bzapi.bug_autorefresh = False
48+
bug = bzapi.getbug(427301, include_fields=["id", "summary"])
49+
try:
50+
print("Shouldn't see this! bug component=%s" % bug.component)
51+
except AttributeError:
52+
print("With bug_autorefresh=False, we received AttributeError as expected")
53+
54+
# Why does this matter? Some scripts are implicitly depending on this
55+
# auto-refresh behavior, because their include_fields specification doesn't
56+
# cover all attributes they actually use. Your script will work, sure, but
57+
# it's likely doing many more XML-RPC calls than needed, possibly 1 per bug.
58+
# So if after upgrading python-bugzilla you start hitting issues, the
59+
# recommendation is to fix your include_fields.

0 commit comments

Comments
 (0)