Created
May 12, 2015 13:32
-
-
Save bhearsum/e094b83bfa295b7a5ebc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ 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 "Before, username/password:" | |
before = BugzillaClient() | |
before.configure("bz", username="foo", password="bar") | |
print "Before, no auth:" | |
try: | |
before = BugzillaClient() | |
before.configure("bz") | |
except AttributeError: | |
pass | |
print "After, apikey:" | |
after.configure("bz", apikey="foo") | |
print "After, username/password:" | |
after = BugzillaClient2() | |
after.configure("bz", username="foo", password="bar") | |
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