# Path: tiddlywebplugins/tiddlyspace/web.py
# def determine_host(environ):
#     """
#     Extract the current HTTP host from the environment.
#     Return that plus the server_host from config. This is
#     used to help calculate what space we are in when HTTP
#     requests are made.
#     """
#     server_host = environ['tiddlyweb.config']['server_host']
#     port = int(server_host['port'])
#     if port == 80 or port == 443:
#         host_url = server_host['host']
#     else:
#         host_url = '%s:%s' % (server_host['host'], port)
# 
#     http_host = environ.get('HTTP_HOST', host_url)
#     if ':' in http_host:
#         for port in [':80', ':443']:
#             if http_host.endswith(port):
#                 http_host = http_host.replace(port, '')
#                 break
#     return http_host, host_url
# 
# def determine_space(environ, http_host):
#     """
#     Calculate the space associated with a subdomain.
#     """
#     server_host = environ['tiddlyweb.config']['server_host']['host']
#     if '.%s' % server_host in http_host:
#         return http_host.rsplit('.', server_host.count('.') + 1)[0]
#     else:
#         if ':' in http_host:
#             http_host = http_host.split(':', 1)[0]
#         store = environ['tiddlyweb.store']
#         tiddler = Tiddler(http_host, 'MAPSPACE')
#         try:
#             tiddler = store.get(tiddler)
#             return tiddler.fields['mapped_space']
#         except (KeyError, StoreError):
#             pass
#     return None
# 
# def determine_space_recipe(environ, space_name):
#     """
#     Given a space name, check if the current user is a member of that
#     named space. If so, use the private recipe.
#     """
#     store = environ['tiddlyweb.store']
#     usersign = environ['tiddlyweb.usersign']
#     try:
#         space = Space(space_name)
#         recipe = Recipe(space.public_recipe())
#         recipe = store.get(recipe)
#     except (ValueError, StoreError), exc:
#         raise HTTP404('Space for %s does not exist: %s' % (space_name, exc))
# 
#     try:
#         recipe.policy.allows(usersign, 'manage')
#         space_type = 'private'
#     except PermissionsError:
#         space_type = 'public'
# 
#     recipe_name_method = getattr(space, '%s_recipe' % space_type)
#     recipe_name = recipe_name_method()
#     return recipe_name


import logging
from tiddlyweb.util import read_utf8_file
from tiddlywebwiki.serialization import Serialization as WikiSerialization
from tiddlywebplugins.tiddlyspace.web import (determine_host,
        determine_space, determine_space_recipe)
    """
    Create the configuration key which will be used to locate
    the base tiddlywiki file.
    """
    base = 'base_tiddlywiki'
    if external:
        base += '_external'
    if beta:
        base += '_beta'
    return base


class Serialization(WikiSerialization):
    """
    Subclass of the standard TiddlyWiki serialization to allow
    choosing beta or externalized versions of the base empty.html
    in which the tiddlers will be servered.

    Also, if the TiddlyWiki is not being downloaded, add
    the UniversalBackstage by injecting a script tag.
    """

    def list_tiddlers(self, tiddlers):
        """
        Override tiddlers.link so the location in noscript is to
        /tiddlers.
        """
        http_host, _ = determine_host(self.environ)
        space_name = determine_space(self.environ, http_host)
        if space_name:

# Path: tiddlywebplugins/tiddlyspace/web.py
# def determine_host(environ):
#     """
#     Extract the current HTTP host from the environment.
#     Return that plus the server_host from config. This is
#     used to help calculate what space we are in when HTTP
#     requests are made.
#     """
#     server_host = environ['tiddlyweb.config']['server_host']
#     port = int(server_host['port'])
#     if port == 80 or port == 443:
#         host_url = server_host['host']
#     else:
#         host_url = '%s:%s' % (server_host['host'], port)
# 
#     http_host = environ.get('HTTP_HOST', host_url)
#     if ':' in http_host:
#         for port in [':80', ':443']:
#             if http_host.endswith(port):
#                 http_host = http_host.replace(port, '')
#                 break
#     return http_host, host_url
# 
# def determine_space(environ, http_host):
#     """
#     Calculate the space associated with a subdomain.
#     """
#     server_host = environ['tiddlyweb.config']['server_host']['host']
#     if '.%s' % server_host in http_host:
#         return http_host.rsplit('.', server_host.count('.') + 1)[0]
#     else:
#         if ':' in http_host:
#             http_host = http_host.split(':', 1)[0]
#         store = environ['tiddlyweb.store']
#         tiddler = Tiddler(http_host, 'MAPSPACE')
#         try:
#             tiddler = store.get(tiddler)
#             return tiddler.fields['mapped_space']
#         except (KeyError, StoreError):
#             pass
#     return None
# 
# def determine_space_recipe(environ, space_name):
#     """
#     Given a space name, check if the current user is a member of that
#     named space. If so, use the private recipe.
#     """
#     store = environ['tiddlyweb.store']
#     usersign = environ['tiddlyweb.usersign']
#     try:
#         space = Space(space_name)
#         recipe = Recipe(space.public_recipe())
#         recipe = store.get(recipe)
#     except (ValueError, StoreError), exc:
#         raise HTTP404('Space for %s does not exist: %s' % (space_name, exc))
# 
#     try:
#         recipe.policy.allows(usersign, 'manage')
#         space_type = 'private'
#     except PermissionsError:
#         space_type = 'public'
# 
#     recipe_name_method = getattr(space, '%s_recipe' % space_type)
#     recipe_name = recipe_name_method()
#     return recipe_name


# Path: tiddlywebplugins/tiddlyspace/betaserialization.py
import logging
from tiddlyweb.util import read_utf8_file
from tiddlywebwiki.serialization import Serialization as WikiSerialization
from tiddlywebplugins.tiddlyspace.web import (determine_host,
        determine_space, determine_space_recipe)

    """
    Create the configuration key which will be used to locate
    the base tiddlywiki file.
    """
    base = 'base_tiddlywiki'
    if external:
        base += '_external'
    if beta:
        base += '_beta'
    return base


class Serialization(WikiSerialization):
    """
    Subclass of the standard TiddlyWiki serialization to allow
    choosing beta or externalized versions of the base empty.html
    in which the tiddlers will be servered.

    Also, if the TiddlyWiki is not being downloaded, add
    the UniversalBackstage by injecting a script tag.
    """

    def list_tiddlers(self, tiddlers):
        """
        Override tiddlers.link so the location in noscript is to
        /tiddlers.
        """
        http_host, _ = determine_host(self.environ)
        space_name = determine_space(self.environ, http_host)
        if space_name:

            recipe_name = determine_space_recipe(self.environ, space_name)