Skip to content

Instantly share code, notes, and snippets.

@bhearsum
Created May 12, 2015 13:32
Show Gist options
  • Save bhearsum/e094b83bfa295b7a5ebc to your computer and use it in GitHub Desktop.
Save bhearsum/e094b83bfa295b7a5ebc to your computer and use it in GitHub Desktop.
➜ tmp cat test.py
#!/usr/bin/env python
class BugzillaClient(object):
def configure(self, bzurl, username=None, password=None, apikey=None):
if apikey:
self.apikey = apikey
self.username = None
self.password = None
if username or password:
print ValueError("Cannot use apikey along with user-based login")
elif username and password:
self.apikey = None
self.username = username
self.password = password
else:
print ValueError("One of apikey or username & password must be supplied")
print self.apikey
print self.username
print self.password
class BugzillaClient2(object):
def configure(self, bzurl, **kwargs):
if not set(kwargs.keys()).issubset(set(["password", "username", "apikey"])):
print ValueError("Invalid arguments passed to BugzillaClient.configure")
if "apikey" in kwargs:
self.apikey = kwargs["apikey"]
self.username = None
self.password = None
if kwargs.get("username", None) or kwargs.get("password", None):
print ValueError("Cannot use apikey along with user-based login")
elif "username" in kwargs and "password" in kwargs:
self.apikey = None
self.username = kwargs["username"]
self.password = kwargs["password"]
else:
print ValueError("One of apikey or username & password must be supplied")
print self.apikey
print self.username
print self.password
before = BugzillaClient()
after = BugzillaClient2()
print "Before, apikey:"
before.configure("bz", apikey="foo")
before = BugzillaClient()
print
print "Before, username/password:"
before = BugzillaClient()
before.configure("bz", username="foo", password="bar")
print
print "Before, no auth:"
try:
before = BugzillaClient()
before.configure("bz")
except AttributeError:
pass
print
print "After, apikey:"
after.configure("bz", apikey="foo")
print
print "After, username/password:"
after = BugzillaClient2()
after.configure("bz", username="foo", password="bar")
print
print "Aefore, no auth:"
after = BugzillaClient2()
try:
after.configure("bz")
except AttributeError:
pass
➜ tmp python test.py
Before, apikey:
foo
None
None
Before, username/password:
None
foo
bar
Before, no auth:
One of apikey or username & password must be supplied
After, apikey:
foo
None
None
After, username/password:
None
foo
bar
Aefore, no auth:
One of apikey or username & password must be supplied
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment