Skip to content

Commit

Permalink
Made 'version' into a climesync_command
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncohen73 committed Nov 14, 2016
1 parent 3c57d3c commit 0b26cfd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
12 changes: 4 additions & 8 deletions climesync/climesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"gu - get users\n"
"du - delete user\n\n"
"us - update user settings\n\n"
"v - print version information\n"
"h - print this menu\n"
"q - exit\n")

Expand Down Expand Up @@ -114,6 +115,7 @@
("uu", "update-user", commands.update_user),
("gu", "get-users", commands.get_users),
("du", "delete-user", commands.delete_user),
("v", "version", commands.version),
]


Expand Down Expand Up @@ -166,12 +168,6 @@ def main(argv=None, test=False):
# Command line arguments
args = docopt(__doc__, argv=argv, options_first=True)

if args['-v']:
print "Climesync 0.1.1"
with open("LICENSE", "r") as f:
print f.read()
return

url = args['-c']
user = args['-u']
password = args['-p']
Expand Down Expand Up @@ -210,8 +206,8 @@ def main(argv=None, test=False):
config_dict=config_dict,
interactive=interactive)

if "error" in response or "pymesync error" in response or \
"climesync error" in response:
if ("error" in response or "pymesync error" in response or
"climesync error" in response) and command != "version":
util.print_json(response)

if command:
Expand Down
24 changes: 22 additions & 2 deletions climesync/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import util

version_string = "Climesync 0.1.1"

ts = None # pymesync.TimeSync object

autoupdate_config = True
Expand All @@ -14,12 +16,15 @@
# climesync_command decorator
class climesync_command():

def __init__(self, select_arg=None, optional_args=False):
def __init__(self, select_arg=None, optional_args=False,
authenticated=True):
self.select_arg = select_arg
self.optional_args = optional_args
self.authenticated = authenticated

def __call__(self, command):
def wrapped_command(argv=None):
# If the command is being run in scripting mode
if argv is not None:
args = docopt(command.__doc__, argv=argv)

Expand All @@ -43,7 +48,9 @@ def wrapped_command(argv=None):

return command(**command_kwargs)
else:
if util.check_token_expiration(ts):
# Bypass token check if the command doesn't require
# authentication
if util.check_token_expiration(ts) and self.authenticated:
return {"error": "You need to sign in."}

return command()
Expand Down Expand Up @@ -1241,3 +1248,16 @@ def delete_user(username=None):
return list()

return ts.delete_user(username=username)


@climesync_command(authenticated=False)
def version(post_data=None):
"""version
Usage: version
Examples:
climesync version
"""

return version_string
1 change: 0 additions & 1 deletion climesync/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def print_json(response):

print ""
else:
print "I don't know how to print that!"
print response


Expand Down

0 comments on commit 0b26cfd

Please sign in to comment.