-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate username save/restore into API #20
Comments
I think part of the appeal of Qhue is its simplicity, so I'd like to keep the core classes pretty minimal, and many people might want to store the credentials in different ways. But we could certainly add a subclass in another file called, say, AuthorizedBridge ? |
It would just be another function as wrapper. My current solution looks like this: def read_username_from_config(ip, filepath='~/.config/qhue/username.conf', retries=3, newuser=False):
# Check for a credential file
username = None
if newuser or not os.path.exists(filepath):
for x in range(retries):
try:
username = create_new_username(ip)
break
except QhueException as err:
print("Error occurred while creating a new username: {}".format(err))
if not username:
raise QhueException("Failed to create new user ({} attempts).\n".format(retries))
else:
# Create non existing config directory
directory = os.path.dirname(filepath)
if not os.path.exists(directory):
os.makedirs(directory)
# Store the username in a credential file
with open(filepath, "w") as cred_file:
cred_file.write(username)
# Read existing credential file
else:
with open(filepath, "r") as cred_file:
username = cred_file.read()
return username
def main():
try:
username = read_username_from_config(BRIDGE_IP)
except QhueException as err:
print("Error occurred while reading the username: {}".format(err))
sys.exit(1)
# create the bridge resource, passing the captured username
bridge = Bridge(BRIDGE_IP, username) It should be made a static function of course, as also suggested in #21 One thing I want to add is the option to select between newuser=None,True,False. So you can say it should create a new user(true), should not (false) or just create one if no config was found (none). This is not yet implemented. |
It would be nice if the code from the example can be moved directly into the API. The reason for that is, that it saves a lot of overhead. I recommend to have a default storage location for the username somewhere in the home directory. Optional a different config file could be passed instead. The username can then be omitted of course.
I am working on some code... will update.
The text was updated successfully, but these errors were encountered: