Remove more dead code

This commit is contained in:
Tim Van Baak 2021-10-10 15:08:02 -07:00
parent c14959e6e6
commit 23d9b1b221
6 changed files with 0 additions and 312 deletions

View File

@ -1,32 +0,0 @@
from .admin import (
valid_name,
create_lexicon,
load_all_lexicons)
from .gameloop import (
get_player_characters,
get_player_drafts,
get_draft,
title_constraint_analysis,
content_constraint_analysis,
sort_by_index_spec,
attempt_publish)
from .setup import (
player_can_join_lexicon,
add_player_to_lexicon,
create_character_in_lexicon)
__all__ = [member.__name__ for member in [
valid_name,
create_lexicon,
load_all_lexicons,
get_player_characters,
get_player_drafts,
get_draft,
title_constraint_analysis,
content_constraint_analysis,
sort_by_index_spec,
attempt_publish,
player_can_join_lexicon,
add_player_to_lexicon,
create_character_in_lexicon,
]]

View File

@ -99,54 +99,6 @@ class HtmlRenderer(RenderableVisitor):
return f'<a href="{link}"{link_class}>{"".join(span.recurse(self))}</a>' return f'<a href="{link}"{link_class}>{"".join(span.recurse(self))}</a>'
def get_player_characters(
lexicon: LexiconModel,
uid: str) -> Iterable[ReadOnlyOrderedDict]:
"""
Returns each character in the lexicon owned by the given player
"""
for character in lexicon.cfg.character.values():
if character.player == uid:
yield character
def get_player_drafts(
lexicon: LexiconModel,
uid: str) -> Iterable[ReadOnlyOrderedDict]:
"""
Returns each draft in the lexicon by a character owned by the
given player.
"""
characters = list(get_player_characters(lexicon, uid))
drafts: List[Any] = []
for filename in lexicon.ctx.draft.ls():
for character in characters:
if filename.startswith(character.cid):
drafts.append(filename)
break
for i in range(len(drafts)):
with lexicon.ctx.draft.read(drafts[i]) as draft:
drafts[i] = draft
return drafts
def get_draft(
lexicon: LexiconModel,
aid: str) -> Optional[ReadOnlyOrderedDict]:
"""
Loads an article from its id
"""
article_fn = None
for filename in lexicon.ctx.draft.ls():
if filename.endswith(f'{aid}.json'):
article_fn = filename
break
if not article_fn:
return None
with lexicon.ctx.draft.read(article_fn) as article:
return article
def title_constraint_analysis( def title_constraint_analysis(
lexicon: LexiconModel, lexicon: LexiconModel,
player: UserModel, player: UserModel,

View File

@ -1,99 +0,0 @@
# """
# Functions for managing lexicons, primarily within the context of the
# Amanuensis config directory.
# """
# import json
# import os
# import re
# import shutil
# import time
# import uuid
# from amanuensis.config import prepend, json_rw, json_ro, logger
# from amanuensis.config.loader import AttrOrderedDict
# from amanuensis.errors import ArgumentError
# from amanuensis.lexicon import LexiconModel
# from amanuensis.parser import parse_raw_markdown, filesafe_title, titlesort
# from amanuensis.resources import get_stream
# def delete_lexicon(lex, purge=False):
# """
# Deletes the given lexicon from the internal configuration
# Does not delete the lexicon from the data folder unless purge=True.
# """
# # Verify arguments
# if lex is None:
# raise ArgumentError("Invalid lexicon: '{}'".format(lex))
# # Delete the lexicon from the index
# with json_rw('lexicon', 'index.json') as index:
# if lex.name in index:
# del index[lex.name]
# # Delete the lexicon data folder if purging
# if purge:
# raise NotImplementedError()
# # Delete the lexicon config
# lex_path = prepend('lexicon', lex.id)
# shutil.rmtree(lex_path)
# def get_user_lexicons(user):
# """
# Loads each lexicon that the given user is a player in
# """
# return [
# lexicon
# for lexicon in get_all_lexicons()
# if user.in_lexicon(lexicon)]
# def remove_player(lex, player):
# """
# Remove a player from a lexicon
# """
# # Verify arguments
# if lex is None:
# raise ArgumentError("Invalid lexicon: '{}'".format(lex))
# if player is None:
# raise ArgumentError("Invalid player: '{}'".format(player))
# if lex.editor == player.id:
# raise ArgumentError(
# "Can't remove the editor '{}' from lexicon '{}'".format(
# player.username, lex.name))
# # Idempotently remove player
# with json_rw(lex.config_path) as cfg:
# if player.id in cfg.join.joined:
# cfg.join.joined.remove(player.id)
# # TODO Reassign the player's characters to the editor
# def delete_character(lex, charname):
# """
# Delete a character from a lexicon
# """
# # Verify arguments
# if lex is None:
# raise ArgumentError("Invalid lexicon: '{}'".format(lex))
# if charname is None:
# raise ArgumentError("Invalid character name: '{}'".format(charname))
# # Find character in this lexicon
# matches = [
# char for cid, char in lex.character.items()
# if char.name == charname]
# if len(matches) != 1:
# raise ArgumentError(matches)
# char = matches[0]
# # Remove character from character list
# with json_rw(lex.config_path) as cfg:
# del cfg.character[char.cid]

View File

@ -1,81 +0,0 @@
"""
Submodule of functions for managing lexicon games during the setup and
joining part of the game lifecycle.
"""
import json
import uuid
from amanuensis.config import AttrOrderedDict
from amanuensis.errors import ArgumentError
from amanuensis.models import LexiconModel, UserModel
from amanuensis.resources import get_stream
def player_can_create_character(
player: UserModel,
lexicon: LexiconModel,
name: str) -> bool:
"""
Checks whether a player can create a character with the given name
"""
# Trivial failures
if not player or not lexicon or not name:
return False
# User needs to be a player
if player.uid not in lexicon.cfg.join.joined:
return False
# Character can't be a dupe
if any([
char.name for char in lexicon.cfg.character.values()
if char.name == name]):
return False
# Player can't add more characters than the limit
if len([
char for char in lexicon.cfg.character.values()
if char.player == player.uid]) > lexicon.cfg.join.chars_per_player:
return False
# Players can't add characters after the game has started
if lexicon.cfg.turn.current:
return False
return True
def create_character_in_lexicon(
player: UserModel,
lexicon: LexiconModel,
name: str) -> str:
"""
Unconditionally creates a character for a player
"""
# Verify arguments
if lexicon is None:
raise ArgumentError(f'Invalid lexicon: {lexicon}')
if player is None:
raise ArgumentError(f'Invalid player: {player}')
if player.uid not in lexicon.cfg.join.joined:
raise ArgumentError(f'Player {player} not in lexicon {lexicon}')
if not name:
raise ArgumentError(f'Invalid character name: "{name}"')
if any([
char.name for char in lexicon.cfg.character.values()
if char.name == name]):
raise ArgumentError(f'Duplicate character name: "{name}"')
# Load the character template
with get_stream('character.json') as template:
character = json.load(template, object_pairs_hook=AttrOrderedDict)
# Fill out the character's information
character.cid = uuid.uuid4().hex
character.name = name
character.player = player.uid
character.signature = "~" + character.name
# Add the character to the lexicon
with lexicon.ctx.edit_config() as cfg:
cfg.character.new(character.cid, character)
# Log addition
lexicon.log(f'Character "{name}" created ({character.cid})')
return character.cid

View File

@ -1,33 +0,0 @@
from flask import current_app
from wtforms.validators import ValidationError
from amanuensis.config import RootConfigDirectoryContext
# Custom validators
def User(should_exist: bool = True):
template: str = 'User "{{}}" {}'.format(
"not found" if should_exist else "already exists")
should_exist_copy: bool = bool(should_exist)
def validate_user(form, field):
root: RootConfigDirectoryContext = current_app.config['root']
with root.user.read_index() as index:
if (field.data in index.keys()) != should_exist_copy:
raise ValidationError(template.format(field.data))
return validate_user
def Lexicon(should_exist: bool = True):
template: str = 'Lexicon "{{}}" {}'.format(
"not found" if should_exist else "already exists")
should_exist_copy: bool = bool(should_exist)
def validate_lexicon(form, field):
root: RootConfigDirectoryContext = current_app.config['root']
with root.lexicon.read_index() as index:
if (field.data in index.keys()) != should_exist_copy:
raise ValidationError(template.format(field.data))
return validate_lexicon

View File

@ -6,25 +6,6 @@ from wtforms.validators import DataRequired
from .settings import ConfigFormBase from .settings import ConfigFormBase
class LexiconCharacterForm(FlaskForm):
"""/lexicon/<name>/session/character/"""
characterName = StringField(
'Character name',
validators=[DataRequired()])
defaultSignature = TextAreaField('Default signature')
submit = SubmitField('Submit')
def for_new(self):
self.characterName.data = ""
self.defaultSignature.data = "~"
return self
def for_character(self, character):
self.characterName.data = character.name
self.defaultSignature.data = character.signature
return self
class LexiconReviewForm(FlaskForm): class LexiconReviewForm(FlaskForm):
"""/lexicon/<name>/session/review/""" """/lexicon/<name>/session/review/"""
APPROVED = 'Y' APPROVED = 'Y'