Implement lexicon-create

This commit is contained in:
Tim Van Baak 2020-01-17 14:43:15 -08:00
parent 01c5df1e5d
commit 3122379da1
2 changed files with 63 additions and 14 deletions

View File

@ -1,16 +1,27 @@
from cli.helpers import add_argument, no_argument, requires from cli.helpers import (
add_argument, no_argument, requires,
config_get, config_set, CONFIG_GET_ROOT_VALUE)
# #
# CRUD commands # CRUD commands
# #
@no_argument @add_argument("--name", required=True, help="Name of the lexicon")
@requires("lexicon") @add_argument("--editor", required=True, help="Name of the user who will be editor")
def command_create(args): def command_create(args):
""" """
Create a lexicon Create a lexicon
A newly created lexicon is not open for joining and requires additional
configuration before it is playable. The editor should ensure that all
settings are as desired before opening the lexicon for player joins.
""" """
raise NotImplementedError() from lexicon.manage import create_lexicon
import user
# TODO verify args
uid = user.uid_from_username(args.editor)
u = user.user_from_uid(uid)
create_lexicon(args.name, u)
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -18,14 +29,14 @@ def command_delete(args):
""" """
Delete a lexicon and all its data Delete a lexicon and all its data
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
def command_list(args): def command_list(args):
""" """
List all lexicons and their statuses List all lexicons and their statuses
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -33,7 +44,7 @@ def command_config(args):
""" """
Interact with a lexicon's config Interact with a lexicon's config
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
# #
# Player/character commands # Player/character commands
@ -45,7 +56,7 @@ def command_player_add(args):
""" """
Add a player to a lexicon Add a player to a lexicon
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -56,7 +67,7 @@ def command_player_remove(args):
Removing a player dissociates them from any characters Removing a player dissociates them from any characters
they control but does not delete any character data. they control but does not delete any character data.
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -64,7 +75,7 @@ def command_player_list(args):
""" """
List all players in a lexicon List all players in a lexicon
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -72,7 +83,7 @@ def command_char_create(args):
""" """
Create a character for a lexicon Create a character for a lexicon
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -83,7 +94,7 @@ def command_char_delete(args):
Deleting a character dissociates them from any content Deleting a character dissociates them from any content
they have contributed rather than deleting it. they have contributed rather than deleting it.
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
@no_argument @no_argument
@requires("lexicon") @requires("lexicon")
@ -91,7 +102,7 @@ def command_char_list(args):
""" """
List all characters in a lexicon List all characters in a lexicon
""" """
raise NotImplementedError() raise NotImplementedError() # TODO
# #
# Procedural commands # Procedural commands
@ -106,4 +117,4 @@ def command_publish_turn(args):
Ability to publish is checked against the lexicon's Ability to publish is checked against the lexicon's
turn publish policy unless --force is specified. turn publish policy unless --force is specified.
""" """
raise NotImplementedError() raise NotImplementedError() # TODO

View File

@ -0,0 +1,38 @@
import os
import time
import uuid
import config
import lexicon
import resources
def create_lexicon(name, editor):
"""
"""
# Create the lexicon directory and initialize it with a blank lexicon
lid = uuid.uuid4().hex
lex_dir = config.prepend("lexicon", lid)
os.mkdir(lex_dir)
with resources.get_stream("lexicon.json") as s:
with open(config.prepend(lex_dir, 'config.json'), 'wb') as f:
f.write(s.read())
# Fill out the new lexicon
with config.json_rw(lex_dir, 'config.json') as cfg:
cfg['lid'] = lid
cfg['name'] = name
cfg['editor'] = editor.uid
cfg['time']['created'] = int(time.time())
# Update the index with the new lexicon
with config.json_rw('lexicon', 'index.json') as index:
index[name] = lid
# Load the Lexicon and log creation
l = lexicon.LexiconModel(lid)
l.log("Lexicon created")
config.logger.info("Created Lexicon {0.name}, ed. {1.displayname} ({0.id})".format(
l, editor))
return l