diff --git a/amanuensis/cli.py b/amanuensis/cli.py index 6fb571f..69f0a74 100644 --- a/amanuensis/cli.py +++ b/amanuensis/cli.py @@ -121,6 +121,8 @@ def command_run(args): @add_argument("--email", help="User's email") def command_user_add(args): """Creates a user""" + import json + import user import config @@ -139,6 +141,24 @@ def command_user_add(args): return -1 # Create user new_user = user.create_user(args.username, args.displayname, args.email) + with config.json_ro(new_user.config) as js: + print(json.dumps(js, indent=2)) + +@no_argument +def command_user_list(args): + """Lists users""" + import os + + import config + + user_dirs = os.listdir(config.prepend('user')) + users = [] + for uid in user_dirs: + with config.json_ro('user', uid, 'config.json') as user: + users.append(user) + users.sort(key=lambda u: u['username']) + for user in users: + print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username'])) @add_argument("--foo", action="store_true") def command_dump(args): diff --git a/amanuensis/config/__init__.py b/amanuensis/config/__init__.py index 0fd3abf..72a2a18 100644 --- a/amanuensis/config/__init__.py +++ b/amanuensis/config/__init__.py @@ -40,8 +40,8 @@ def init_config(args): def get(key): return GLOBAL_CONFIG[key] -def prepend(path): - return os.path.join(CONFIG_DIR, path) +def prepend(*path): + return os.path.join(CONFIG_DIR, *path) def open_sh(path, mode): return config.loader.open_sh(prepend(path), mode) @@ -49,14 +49,14 @@ def open_sh(path, mode): def open_ex(path, mode): return config.loader.open_ex(prepend(path), mode) -def json_ro(path): - return config.loader.json_ro(prepend(path)) +def json_ro(*path): + return config.loader.json_ro(prepend(*path)) -def json_rw(path): - return config.loader.json_rw(prepend(path)) - -def json(*args, mode='r'): - if not args[-1].endswith(".json"): - args[-1] = args[-1] + ".json" - path = os.path.join(CONFIG_DIR, *args) +def json_rw(*path): + return config.loader.json_rw(prepend(*path)) +def new_user(user_json): + user_dir = prepend("user", user_json['uid']) + os.mkdir(user_dir) + with config.loader.open_ex(os.path.join(user_dir, "config.json"), 'w') as f: + json.dump(user_json, f, allow_nan=False, indent='\t') diff --git a/amanuensis/user.py b/amanuensis/user.py index a682674..3e88df5 100644 --- a/amanuensis/user.py +++ b/amanuensis/user.py @@ -1,7 +1,16 @@ +import os import re +import time +import uuid + +from werkzeug.security import generate_password_hash, check_password_hash + +import config class User(): - pass + def __init__(self, uid): + self.uid = uid + self.config = os.path.join('user', uid, 'config.json') def valid_username(username): return re.match(r"^[A-Za-z0-9-_]{3,}$", username) is not None @@ -14,4 +23,15 @@ def valid_email(email): return re.match(addrspec, email) def create_user(username, displayname, email): - pass + uid = uuid.uuid4().hex + now = int(time.time()) + user_json = { + 'uid': uid, + 'username': username, + 'displayname': displayname, + 'email': email, + 'password': None, + 'created': now, + } + config.new_user(user_json) + return User(uid)