import shelve
import os
import sys
from wsgiref.simple_server import make_server

import pyramid.httpexceptions as exc
from pyramid.config import Configurator
from pyramid.response import Response
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

DATABASE_URL = os.environ['QUEST_DATABASE_URL']

with shelve.open(DATABASE_URL) as db:
    db['p1'] = 1000
    db['p2'] = 2000


def home(request):
    with shelve.open(DATABASE_URL) as db:
        for key in db:
            p1_score = key.upper() + " " + str(db[key])

    return Response(str(p1_score))


def players(request):
    content = """
    <form action="" method="POST">
        <input id="player-name" type="text" name="player-name">
        <input id="submit" type="submit" value="Create">
    </form>
    """

    return Response(content) 


def create_player(request):
    name = request.POST['player-name']

    with shelve.open(DATABASE_URL) as db:
        db[name] = 1000

    raise exc.HTTPFound(request.route_url('player', player=name))


def view_player(request):
    name = request.matchdict['player']

    with shelve.open(DATABASE_URL) as db:
        player_score = name.upper() + ' ' + str(db[name])

    return Response(player_score)


config = Configurator()
config.add_route('home', '/')
config.add_view(home, route_name='home')

config.add_route('players', '/players')
config.add_view(players, route_name='players', request_method='GET')
config.add_view(create_player, route_name='players', request_method='POST')

config.add_route('player', '/players/{player}')
config.add_view(view_player, route_name='player')

app = config.make_wsgi_app()


if __name__ == '__main__':
    logger.info('ENVIRONMENT=%s',
                {key: os.environ[key] for key in os.environ
                 if key.startswith('QUEST_')})
    sys.stderr.write("started\n")
    sys.stderr.flush()

    server = make_server('127.0.0.1', 8080, app)
    server.serve_forever()

@@ -333,16 +333,129 @@
 _URL'%5D%0A%0A
+%0Aclass PlayerRepository(object):%0A    def __init__(self, database_url):%0A        self._url = database_url%0A%0A        
 with she
@@ -459,36 +459,33 @@
 shelve.open(
-DATABASE_URL
+self._url
 ) as db:%0A   
@@ -477,24 +477,32 @@
 url) as db:%0A
+        
     db%5B'p1'%5D
@@ -509,16 +509,24 @@
  = 1000%0A
+        
     db%5B'
@@ -542,28 +542,231 @@
 00%0A%0A
-%0Adef home(request):%0A
+    def scores(self):%0A        with shelve.open(self._url) as db:%0A            _scores = %5Bkey.upper() + ' ' + str(db%5Bkey%5D) + '%5Cn'%0A                       for key in db%5D%0A%0A        return _scores%0A%0A    def create(self, name):%0A    
     
@@ -778,36 +778,33 @@
 shelve.open(
-DATABASE_URL
+self._url
 ) as db:%0A   
@@ -808,26 +808,105 @@
         
-for key in
+    db%5Bname%5D = 1000%0A%0A    def score(self, name):%0A        with shelve.open(DATABASE_URL) as
  db:%0A   
@@ -915,17 +915,21 @@
        p
-1
+layer
 _score =
@@ -929,19 +929,20 @@
 score = 
-key
+name
 .upper()
@@ -948,11 +948,11 @@
 ) + 
-%22 %22
+' '
  + s
@@ -961,14 +961,150 @@
 (db%5B
-key%5D)%0A
+name%5D)%0A%0A        return player_score%0A%0A%0A%0Aplayer_repo = PlayerRepository(DATABASE_URL)%0A%0A%0Adef home(request):%0A    score_list = player_repo.scores()
 %0A   
@@ -1128,16 +1128,18 @@
 str(
-p1_
 score
+_list
 ))%0A%0A
@@ -1459,75 +1459,36 @@
 e'%5D%0A
-%0A
     
-with shelve.open(DATABASE_URL) as db:%0A        db%5Bname%5D = 1000%0A
+player_repo.create(name)
 %0A   
@@ -1621,109 +1621,50 @@
 r'%5D%0A
-%0A    with shelve.open(DATABASE_URL) as db:%0A        player_score = name.upper() + ' ' + str(db%5B
+    player_score = player_repo.score(
 name
-%5D)%0A
+)
 %0A   

Move database player retrieval to separate class