|
@@ -0,0 +1,71 @@
|
|
|
|
+import getpass
|
|
|
|
+import inspect
|
|
|
|
+import sys
|
|
|
|
+import client
|
|
|
|
+import connection
|
|
|
|
+from client import allowed_commands
|
|
|
|
+from connection import client_request
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def login(username=None, password=None):
|
|
|
|
+ if connection.session_id is not None:
|
|
|
|
+ client.fake_loading_bar('Signing out', delay=0.025)
|
|
|
|
+ connection.session_id = None
|
|
|
|
+
|
|
|
|
+ if username is None:
|
|
|
|
+ username = input('Username: ')
|
|
|
|
+
|
|
|
|
+ if password is None:
|
|
|
|
+ if sys.stdin.isatty():
|
|
|
|
+ password = getpass.getpass('Password: ')
|
|
|
|
+ else:
|
|
|
|
+ password = input('Password: ')
|
|
|
|
+
|
|
|
|
+ response = client_request('login', {"username": username, "password": password})
|
|
|
|
+ success = 'session_id' in response
|
|
|
|
+ if success:
|
|
|
|
+ connection.session_id = response['session_id']
|
|
|
|
+ print('Login successful.')
|
|
|
|
+ else:
|
|
|
|
+ if 'error_message' in response:
|
|
|
|
+ print('Login failed with message:', response['error_message'])
|
|
|
|
+ else:
|
|
|
|
+ print('Login failed.')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def register(username, password=None):
|
|
|
|
+ if connection.session_id is not None:
|
|
|
|
+ client.fake_loading_bar('Signing out', delay=0.025)
|
|
|
|
+ connection.session_id = None
|
|
|
|
+
|
|
|
|
+ if username is None:
|
|
|
|
+ username = input('Username: ')
|
|
|
|
+
|
|
|
|
+ if password is None:
|
|
|
|
+ if sys.stdin.isatty():
|
|
|
|
+ password = getpass.getpass('Password: ')
|
|
|
|
+ else:
|
|
|
|
+ password = input('Password: ')
|
|
|
|
+
|
|
|
|
+ response = client_request('register', {"username": username, "password": password})
|
|
|
|
+ success = 'session_id' in response
|
|
|
|
+ if success:
|
|
|
|
+ connection.session_id = response['session_id']
|
|
|
|
+ print('Registration successful.')
|
|
|
|
+ else:
|
|
|
|
+ if 'error_message' in response:
|
|
|
|
+ print('Registration failed with message:', response['error_message'])
|
|
|
|
+ else:
|
|
|
|
+ print('Registration failed.')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def help():
|
|
|
|
+ print('Allowed commands:')
|
|
|
|
+ for cmd in allowed_commands:
|
|
|
|
+ this_module = sys.modules[__name__]
|
|
|
|
+ method = getattr(this_module, cmd[0])
|
|
|
|
+ params = inspect.signature(method).parameters
|
|
|
|
+ num_args = len(params)
|
|
|
|
+ print(cmd, 'takes the following', num_args, 'arguments:')
|
|
|
|
+ for p in params:
|
|
|
|
+ print(' ', p) # TODO print default value
|