<commit_before>"""
Routine related to web handling of space
listing, creation, subscription, etc.
"""

import re
import urllib
import simplejson

from tiddlyweb.model.bag import Bag
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.user import User
from tiddlyweb.model.policy import Policy
from tiddlyweb.store import NoRecipeError, NoBagError, NoUserError
from tiddlyweb.web.http import HTTP403, HTTP404, HTTP409

from tiddlywebplugins.utils import require_any_user


SPACE_NAME_PATTERN = re.compile(r"^[a-z][0-9a-z\-]*[0-9a-z]$")

try:
    JSONDecodeError = simplejson.decoder.JSONDecodeError
except AttributeError: # Python 2.6+
    JSONDecodeError = ValueError


def add_spaces_routes(selector):
    """
    Set up the routes and handlers used by spaces.
    """
    selector.add('/spaces', # list all spaces
            GET=list_spaces)
    selector.add('/spaces/{space_name:segment}',
            GET=confirm_space, # confirm space exists
            PUT=create_space,  # create a new space
            POST=subscribe_space, # subscribe a space to this space
            )
    selector.add('/spaces/{space_name:segment}/members', # list space members
            GET=list_space_members)
    selector.add('/spaces/{space_name:segment}/members', # list space members
            GET=list_space_members)
    selector.add('/spaces/{space_name:segment}/members/{user_name:segment}',
            PUT=add_space_member, # add member to space
            DELETE=delete_space_member) # delete from from space


def add_space_member(environ, start_response):
    """
    Add a member to a space if they are not already a member.
    If they are already a member, nothing happens. If the username
    given does not exist, raise 409. If the space does not exist
    raise 404.
    """
    store = environ['tiddlyweb.store']
    space_name = environ['wsgiorg.routing_args'][1]['space_name']
    user_name = environ['wsgiorg.routing_args'][1]['user_name']
    current_user = environ['tiddlyweb.usersign']

    try:
        change_space_member(store, space_name, add=user_name,
                current_user=current_user)<commit_msg>Remove duplicate selector rule.<commit_after>"""
Routine related to web handling of space
listing, creation, subscription, etc.
"""

import re
import urllib
import simplejson

from tiddlyweb.model.bag import Bag
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.user import User
from tiddlyweb.model.policy import Policy
from tiddlyweb.store import NoRecipeError, NoBagError, NoUserError
from tiddlyweb.web.http import HTTP403, HTTP404, HTTP409

from tiddlywebplugins.utils import require_any_user


SPACE_NAME_PATTERN = re.compile(r"^[a-z][0-9a-z\-]*[0-9a-z]$")

try:
    JSONDecodeError = simplejson.decoder.JSONDecodeError
except AttributeError: # Python 2.6+
    JSONDecodeError = ValueError


def add_spaces_routes(selector):
    """
    Set up the routes and handlers used by spaces.
    """
    selector.add('/spaces', # list all spaces
            GET=list_spaces)
    selector.add('/spaces/{space_name:segment}',
            GET=confirm_space, # confirm space exists
            PUT=create_space,  # create a new space
            POST=subscribe_space, # subscribe a space to this space
            )
    selector.add('/spaces/{space_name:segment}/members', # list space members
            GET=list_space_members)
    selector.add('/spaces/{space_name:segment}/members/{user_name:segment}',
            PUT=add_space_member, # add member to space
            DELETE=delete_space_member) # delete from from space


def add_space_member(environ, start_response):
    """
    Add a member to a space if they are not already a member.
    If they are already a member, nothing happens. If the username
    given does not exist, raise 409. If the space does not exist
    raise 404.
    """
    store = environ['tiddlyweb.store']
    space_name = environ['wsgiorg.routing_args'][1]['space_name']
    user_name = environ['wsgiorg.routing_args'][1]['user_name']
    current_user = environ['tiddlyweb.usersign']

    try:
        change_space_member(store, space_name, add=user_name,
                current_user=current_user)
{'fraction_non_alphanumeric': 0.0964576352321685, 'fraction_numerical': 0.011010052656773576, 'mean_word_length': 3.5572519083969465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '511261', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1246, 'n_words': 335}