Skip to content

Commit 3776bf3

Browse files
djmitchecrobinso
authored andcommitted
Factor out rc-handling and listify
This is in preparation for #75. Reviewed-by: Cole Robinson <crobinso@redhat.com>
1 parent cd955c6 commit 3776bf3

File tree

5 files changed

+104
-76
lines changed

5 files changed

+104
-76
lines changed

bugzilla/_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def get_default_url():
101101
"""
102102
Grab a default URL from bugzillarc [DEFAULT] url=X
103103
"""
104-
from bugzilla.base import _open_bugzillarc
105-
cfg = _open_bugzillarc()
104+
from bugzilla._rc import open_bugzillarc
105+
cfg = open_bugzillarc()
106106
if cfg:
107107
cfgurl = cfg.defaults().get("url", None)
108108
if cfgurl is not None:

bugzilla/_rc.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This program is free software; you can redistribute it and/or modify it
2+
# under the terms of the GNU General Public License as published by the
3+
# Free Software Foundation; either version 2 of the License, or (at your
4+
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
5+
# the full text of the license.
6+
7+
import os
8+
import sys
9+
from logging import getLogger
10+
11+
# pylint: disable=import-error,no-name-in-module,ungrouped-imports
12+
if sys.version_info[0] >= 3:
13+
from configparser import ConfigParser
14+
else:
15+
from ConfigParser import SafeConfigParser as ConfigParser
16+
# pylint: enable=import-error,no-name-in-module,ungrouped-imports
17+
18+
from ._util import listify
19+
20+
log = getLogger(__name__)
21+
22+
DEFAULT_CONFIGPATHS = [
23+
'/etc/bugzillarc',
24+
'~/.bugzillarc',
25+
'~/.config/python-bugzilla/bugzillarc',
26+
]
27+
28+
29+
def open_bugzillarc(configpaths=-1):
30+
if configpaths == -1:
31+
configpaths = DEFAULT_CONFIGPATHS[:]
32+
33+
# pylint: disable=protected-access
34+
configpaths = [os.path.expanduser(p) for p in
35+
listify(configpaths)]
36+
# pylint: enable=protected-access
37+
cfg = ConfigParser()
38+
read_files = cfg.read(configpaths)
39+
if not read_files:
40+
return
41+
42+
log.info("Found bugzillarc files: %s", read_files)
43+
return cfg

bugzilla/_util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This program is free software; you can redistribute it and/or modify it
2+
# under the terms of the GNU General Public License as published by the
3+
# Free Software Foundation; either version 2 of the License, or (at your
4+
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
5+
# the full text of the license.
6+
7+
8+
def listify(val):
9+
"""Ensure that value is either None or a list, converting single values
10+
into 1-element lists"""
11+
if val is None:
12+
return val
13+
if isinstance(val, list):
14+
return val
15+
return [val]

bugzilla/base.py

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
# pylint: enable=import-error,no-name-in-module,ungrouped-imports
3535

3636

37+
from ._util import listify
38+
from ._rc import DEFAULT_CONFIGPATHS, open_bugzillarc
3739
from .apiversion import __version__
3840
from .bug import Bug, User
3941
from .transport import BugzillaError, _BugzillaServerProxy, _RequestsTransport
@@ -105,30 +107,6 @@ def _build_cookiejar(cookiefile):
105107
cookiefile)
106108

107109

108-
_default_configpaths = [
109-
'/etc/bugzillarc',
110-
'~/.bugzillarc',
111-
'~/.config/python-bugzilla/bugzillarc',
112-
]
113-
114-
115-
def _open_bugzillarc(configpaths=-1):
116-
if configpaths == -1:
117-
configpaths = _default_configpaths[:]
118-
119-
# pylint: disable=protected-access
120-
configpaths = [os.path.expanduser(p) for p in
121-
Bugzilla._listify(configpaths)]
122-
# pylint: enable=protected-access
123-
cfg = ConfigParser()
124-
read_files = cfg.read(configpaths)
125-
if not read_files:
126-
return
127-
128-
log.info("Found bugzillarc files: %s", read_files)
129-
return cfg
130-
131-
132110
def _save_api_key(url, api_key):
133111
"""
134112
Save the API_KEY in the config file.
@@ -275,15 +253,6 @@ def fix_url(url):
275253
log.debug("Generated fixed URL: %s", newurl)
276254
return newurl
277255

278-
@staticmethod
279-
def _listify(val):
280-
if val is None:
281-
return val
282-
if isinstance(val, list):
283-
return val
284-
return [val]
285-
286-
287256
def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
288257
sslverify=True, tokenfile=-1, use_creds=True, api_key=None,
289258
cert=None, configpaths=-1, basic_auth=False):
@@ -348,7 +317,7 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
348317
if tokenfile == -1:
349318
tokenfile = _default_cache_location("bugzillatoken")
350319
if configpaths == -1:
351-
configpaths = _default_configpaths[:]
320+
configpaths = DEFAULT_CONFIGPATHS[:]
352321

353322
log.debug("Using tokenfile=%s", tokenfile)
354323
self.cookiefile = cookiefile
@@ -512,7 +481,7 @@ def readconfig(self, configpath=None, overwrite=True):
512481
:param overwrite: If True, bugzillarc will clobber any already
513482
set self.user/password/api_key/cert value.
514483
"""
515-
cfg = _open_bugzillarc(configpath or self.configpath)
484+
cfg = open_bugzillarc(configpath or self.configpath)
516485
if not cfg:
517486
return
518487

@@ -826,9 +795,9 @@ def product_get(self, ids=None, names=None,
826795

827796
kwargs = {}
828797
if ids:
829-
kwargs["ids"] = self._listify(ids)
798+
kwargs["ids"] = listify(ids)
830799
if names:
831-
kwargs["names"] = self._listify(names)
800+
kwargs["names"] = listify(names)
832801
if include_fields:
833802
kwargs["include_fields"] = include_fields
834803
if exclude_fields:
@@ -1097,7 +1066,7 @@ def _getbugs(self, idlist, permissive,
10971066
# String aliases can be passed as well
10981067
idlist.append(i)
10991068

1100-
extra_fields = self._listify(extra_fields or [])
1069+
extra_fields = listify(extra_fields or [])
11011070
extra_fields += self._getbug_extra_fields
11021071

11031072
getbugdata = {"ids": idlist}
@@ -1122,7 +1091,7 @@ def _getbugs(self, idlist, permissive,
11221091
else:
11231092
# Need to map an alias
11241093
for valdict in bugdict.values():
1125-
if i in self._listify(valdict.get("alias", None) or []):
1094+
if i in listify(valdict.get("alias", None) or []):
11261095
found = valdict
11271096
break
11281097

@@ -1243,8 +1212,8 @@ def build_query(self,
12431212

12441213
query = {
12451214
"alias": alias,
1246-
"product": self._listify(product),
1247-
"component": self._listify(component),
1215+
"product": listify(product),
1216+
"component": listify(component),
12481217
"version": version,
12491218
"id": bug_id,
12501219
"short_desc": short_desc,
@@ -1253,17 +1222,17 @@ def build_query(self,
12531222
"priority": priority,
12541223
"target_release": target_release,
12551224
"target_milestone": target_milestone,
1256-
"tag": self._listify(tags),
1225+
"tag": listify(tags),
12571226
"quicksearch": quicksearch,
12581227
"savedsearch": savedsearch,
12591228
"sharer_id": savedsearch_sharer_id,
12601229

12611230
# RH extensions... don't add any more. See comment below
1262-
"sub_components": self._listify(sub_component),
1231+
"sub_components": listify(sub_component),
12631232
}
12641233

12651234
def add_bool(bzkey, value, bool_id, booltype=None):
1266-
value = self._listify(value)
1235+
value = listify(value)
12671236
if value is None:
12681237
return bool_id
12691238

@@ -1394,7 +1363,7 @@ def update_bugs(self, ids, updates):
13941363
build_update(), otherwise we cannot guarantee back compatibility.
13951364
"""
13961365
tmp = updates.copy()
1397-
tmp["ids"] = self._listify(ids)
1366+
tmp["ids"] = listify(ids)
13981367

13991368
return self._proxy.Bug.update(tmp)
14001369

@@ -1404,12 +1373,12 @@ def update_tags(self, idlist, tags_add=None, tags_remove=None):
14041373
"""
14051374
tags = {}
14061375
if tags_add:
1407-
tags["add"] = self._listify(tags_add)
1376+
tags["add"] = listify(tags_add)
14081377
if tags_remove:
1409-
tags["remove"] = self._listify(tags_remove)
1378+
tags["remove"] = listify(tags_remove)
14101379

14111380
d = {
1412-
"ids": self._listify(idlist),
1381+
"ids": listify(idlist),
14131382
"tags": tags,
14141383
}
14151384

@@ -1506,7 +1475,7 @@ def add_dict(key, add, remove, _set=None, convert=None):
15061475
return
15071476

15081477
def c(val):
1509-
val = self._listify(val)
1478+
val = listify(val)
15101479
if convert:
15111480
val = [convert(v) for v in val]
15121481
return val
@@ -1548,7 +1517,7 @@ def c(val):
15481517
s("whiteboard", whiteboard)
15491518
s("work_time", work_time, float)
15501519
s("flags", flags)
1551-
s("comment_tags", comment_tags, self._listify)
1520+
s("comment_tags", comment_tags, listify)
15521521

15531522
add_dict("blocks", blocks_add, blocks_remove, blocks_set,
15541523
convert=int)
@@ -1629,7 +1598,7 @@ def attachfile(self, idlist, attachfile, description, **kwargs):
16291598
data = data.encode(locale.getpreferredencoding())
16301599
kwargs['data'] = Binary(data)
16311600

1632-
kwargs['ids'] = self._listify(idlist)
1601+
kwargs['ids'] = listify(idlist)
16331602

16341603
if 'file_name' not in kwargs and hasattr(f, "name"):
16351604
kwargs['file_name'] = os.path.basename(f.name)
@@ -1697,13 +1666,13 @@ def get_attachments(self, ids, attachment_ids,
16971666
https://bugzilla.readthedocs.io/en/latest/api/core/v1/attachment.html#get-attachment
16981667
"""
16991668
params = {
1700-
"ids": self._listify(ids) or [],
1701-
"attachment_ids": self._listify(attachment_ids) or [],
1669+
"ids": listify(ids) or [],
1670+
"attachment_ids": listify(attachment_ids) or [],
17021671
}
17031672
if include_fields:
1704-
params["include_fields"] = self._listify(include_fields)
1673+
params["include_fields"] = listify(include_fields)
17051674
if exclude_fields:
1706-
params["exclude_fields"] = self._listify(exclude_fields)
1675+
params["exclude_fields"] = listify(exclude_fields)
17071676

17081677
return self._proxy.Bug.attachments(params)
17091678

@@ -1751,15 +1720,15 @@ def build_createbug(self,
17511720

17521721
localdict = {}
17531722
if blocks:
1754-
localdict["blocks"] = self._listify(blocks)
1723+
localdict["blocks"] = listify(blocks)
17551724
if cc:
1756-
localdict["cc"] = self._listify(cc)
1725+
localdict["cc"] = listify(cc)
17571726
if depends_on:
1758-
localdict["depends_on"] = self._listify(depends_on)
1727+
localdict["depends_on"] = listify(depends_on)
17591728
if groups:
1760-
localdict["groups"] = self._listify(groups)
1729+
localdict["groups"] = listify(groups)
17611730
if keywords:
1762-
localdict["keywords"] = self._listify(keywords)
1731+
localdict["keywords"] = listify(keywords)
17631732
if description:
17641733
localdict["description"] = description
17651734
if comment_private:
@@ -1845,11 +1814,11 @@ def _getusers(self, ids=None, names=None, match=None):
18451814
"""
18461815
params = {}
18471816
if ids:
1848-
params['ids'] = self._listify(ids)
1817+
params['ids'] = listify(ids)
18491818
if names:
1850-
params['names'] = self._listify(names)
1819+
params['names'] = listify(names)
18511820
if match:
1852-
params['match'] = self._listify(match)
1821+
params['match'] = listify(match)
18531822
if not params:
18541823
raise BugzillaError('_get() needs one of ids, '
18551824
' names, or match kwarg.')
@@ -1925,14 +1894,14 @@ def updateperms(self, user, action, groups):
19251894
:arg action: add, remove, or set
19261895
:arg groups: list of groups to be added to (i.e. ['fedora_contrib'])
19271896
"""
1928-
groups = self._listify(groups)
1897+
groups = listify(groups)
19291898
if action == "rem":
19301899
action = "remove"
19311900
if action not in ["add", "remove", "set"]:
19321901
raise BugzillaError("Unknown user permission action '%s'" % action)
19331902

19341903
update = {
1935-
"names": self._listify(user),
1904+
"names": listify(user),
19361905
"groups": {
19371906
action: groups,
19381907
}

0 commit comments

Comments
 (0)