Skip to content

Commit 77e9697

Browse files
committed
bugzilla: Move class detection out of __init__
And rework the __class__ overriding so that we don't need to run the subclass __init__, which simplifies a lot of things
1 parent 740adb2 commit 77e9697

3 files changed

Lines changed: 46 additions & 66 deletions

File tree

bugzilla/__init__.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,15 @@
99
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
1010
# the full text of the license.
1111

12-
from logging import getLogger
13-
import sys
14-
15-
if hasattr(sys.version_info, "major") and sys.version_info.major >= 3:
16-
# pylint: disable=F0401
17-
from xmlrpc.client import Fault, ServerProxy
18-
else:
19-
from xmlrpclib import Fault, ServerProxy
20-
2112
from .apiversion import version, __version__
22-
from .base import BugzillaBase
23-
from .transport import BugzillaError, _RequestsTransport
13+
from .base import BugzillaBase as Bugzilla
14+
from .transport import BugzillaError
2415
from .rhbugzilla import RHBugzilla
2516
from .oldclasses import (Bugzilla3, Bugzilla32, Bugzilla34, Bugzilla36,
2617
Bugzilla4, Bugzilla42, Bugzilla44,
2718
NovellBugzilla, RHBugzilla3, RHBugzilla4)
2819

2920

30-
class Bugzilla(BugzillaBase):
31-
'''
32-
Magical Bugzilla class that figures out which Bugzilla implementation
33-
to use and uses that.
34-
'''
35-
def _init_class_from_url(self, url, sslverify):
36-
if url is None:
37-
raise TypeError("You must pass a valid bugzilla URL")
38-
url = RHBugzilla.fix_url(url)
39-
log.debug("Detecting subclass for %s", url)
40-
41-
if "bugzilla.redhat.com" in url:
42-
log.info("Using RHBugzilla for URL containing bugzilla.redhat.com")
43-
c = RHBugzilla
44-
else:
45-
# Check for a Red Hat extension
46-
s = ServerProxy(url, _RequestsTransport(url, sslverify=sslverify))
47-
try:
48-
extensions = s.Bugzilla.extensions()
49-
if extensions.get('extensions', {}).get('RedHat', False):
50-
log.debug("Found RedHat bugzilla extension")
51-
c = RHBugzilla
52-
except Fault:
53-
pass
54-
55-
if not c:
56-
return
57-
58-
self.__class__ = c
59-
log.info("Found subclass %s", c.__name__)
60-
return True
61-
62-
6321
# This is the public API. If you are explicitly instantiating any other
6422
# class, using some function, or poking into internal files, don't complain
6523
# if things break on you.

bugzilla/base.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
from configparser import SafeConfigParser
2424
from http.cookiejar import LoadError, LWPCookieJar, MozillaCookieJar
2525
from urllib.parse import urlparse, parse_qsl
26-
from xmlrpc.client import Binary, Fault
26+
from xmlrpc.client import Binary, Fault, ServerProxy
2727
else:
2828
from ConfigParser import SafeConfigParser
2929
from cookielib import LoadError, LWPCookieJar, MozillaCookieJar
3030
from urlparse import urlparse, parse_qsl
31-
from xmlrpclib import Binary, Fault
31+
from xmlrpclib import Binary, Fault, ServerProxy
3232

3333

3434
from .apiversion import __version__
@@ -242,15 +242,7 @@ def __init__(self, url=None, user=None, password=None, cookiefile=-1,
242242
False to disable SSL verification, but it can also be a path
243243
to file or directory for custom certs.
244244
"""
245-
# Hook to allow Bugzilla autodetection without weirdly overriding
246-
# __init__
247-
if self._init_class_from_url(url, sslverify):
248-
kwargs = locals().copy()
249-
del(kwargs["self"])
250-
251-
# pylint: disable=non-parent-init-called
252-
self.__class__.__init__(self, **kwargs)
253-
return
245+
self._init_class_from_url(url, sslverify)
254246

255247
# Settings the user might want to tweak
256248
self.user = user or ''
@@ -278,12 +270,45 @@ def __init__(self, url=None, user=None, password=None, cookiefile=-1,
278270
if url:
279271
self.connect(url)
280272

273+
self._init_class_state()
274+
281275
def _init_class_from_url(self, url, sslverify):
282276
"""
283-
Hook used by the Bugzilla() autodetect class to work its magic
277+
Detect if we should use RHBugzilla class, and if so, set it
278+
"""
279+
from bugzilla import RHBugzilla
280+
if url is None:
281+
return
282+
283+
url = self.fix_url(url)
284+
log.debug("Detecting subclass for %s", url)
285+
286+
c = None
287+
if "bugzilla.redhat.com" in url:
288+
log.info("Using RHBugzilla for URL containing bugzilla.redhat.com")
289+
c = RHBugzilla
290+
else:
291+
# Check for a Red Hat extension
292+
s = ServerProxy(url, _RequestsTransport(url, sslverify=sslverify))
293+
try:
294+
extensions = s.Bugzilla.extensions()
295+
if extensions.get('extensions', {}).get('RedHat', False):
296+
log.debug("Found RedHat bugzilla extension")
297+
c = RHBugzilla
298+
except Fault:
299+
pass
300+
301+
if not c:
302+
return
303+
304+
self.__class__ = c
305+
log.info("Found subclass %s", c.__name__)
306+
307+
def _init_class_state(self):
308+
"""
309+
Hook for subclasses to do any __init__ time setup
284310
"""
285-
ignore = url
286-
ignore = sslverify
311+
pass
287312

288313
def _init_field_aliases(self):
289314
# List of field aliases. Maps old style RHBZ parameter

bugzilla/rhbugzilla.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
from logging import getLogger
1313

14-
from .base import BugzillaBase as _parent
14+
from .base import BugzillaBase
1515

1616
log = getLogger(__name__)
1717

1818

19-
class RHBugzilla(_parent):
19+
class RHBugzilla(BugzillaBase):
2020
'''
2121
Bugzilla class for connecting Red Hat's forked bugzilla instance,
2222
bugzilla.redhat.com
@@ -31,10 +31,7 @@ class RHBugzilla(_parent):
3131
This class was written using bugzilla.redhat.com's API docs:
3232
https://bugzilla.redhat.com/docs/en/html/api/
3333
'''
34-
35-
def __init__(self, *args, **kwargs):
36-
_parent.__init__(self, *args, **kwargs)
37-
34+
def _init_class_state(self):
3835
def _add_both_alias(newname, origname):
3936
self._add_field_alias(newname, origname, is_api=False)
4037
self._add_field_alias(origname, newname, is_bug=False)
@@ -109,7 +106,7 @@ def get_alias():
109106
get_sub_component()
110107
get_alias()
111108

112-
vals = _parent.build_update(self, **kwargs)
109+
vals = BugzillaBase.build_update(self, **kwargs)
113110
vals.update(adddict)
114111

115112
return vals
@@ -480,7 +477,7 @@ def make_bool_str(prefix):
480477
if extra_fields:
481478
query["extra_fields"] = extra_fields
482479

483-
newquery = _parent.build_query(self, **kwargs)
480+
newquery = BugzillaBase.build_query(self, **kwargs)
484481
query.update(newquery)
485482
self.pre_translation(query)
486483
return query

0 commit comments

Comments
 (0)