# Path: radarpost/plugins.py
# ENTRY_POINT = 'radarpost_plugins'
# def get(slot):
# def _get(slot):
# def register(thing, slot):
# def plugin(slot):
#     def dd(thing):
# def _search():
# 
# Path: radarpost/plugins.py
# def plugin(slot):
#     """
#     decorator to declare a plugin
#     """
#     def dd(thing):
#         register(thing, slot)
#         return thing
#     return dd
# 
# Path: radarpost/mailbox.py
# def iter_mailboxes(couchdb, prefix=None): 
#     """
#     iterate through mailboxes in the current context.
#     if prefix is specified, only databases with names
#     that start with the string specified are returned.
#     """
#     for db_name in couchdb:
#         db = couchdb[db_name]
#         if prefix is not None and not db_name.startswith(prefix):
#             continue
#         if is_mailbox(db):
#             yield db
# 
# Path: radarpost/user.py
# class User(Document):
#     """
#     This is a representation of a 'standard' couchdb user
#     document.  Creating one of these in the couchdb users
#     database (usually _users) adds a database user.  They 
#     can be created elsewhere if database level access is 
#     not desirable.
#     
#     See http://wiki.apache.org/couchdb/Security_Features_Overview
#     
#     """
#     def __init__(self, username=None, password=None):
#         """
#         User may be initialized with the special keyword arguments
#         username and password for creating a new User:
#         
#         u = User(username=joe, password="bl0w")
#         """
#         Document.__init__(self)
# 
#         if 'username' is not None: 
#             self.id = 'org.couchdb.user:%s' % username
#             self['name'] = username
#     
#         if password is not None:
#             self.set_password(password)
#         else: 
#             self.lock_password()
# 
#     @classmethod
#     def get_by_username(cls, db, username):
#         natid = 'org.couchdb.user:%s' % username
#         return cls.load(db, natid)
# 
#     @property
#     def username(self):
#         return self['name']
# 
#     def get_public_id(self):
#         # same for now.
#         return self.username
# 
#     type = TextField(default="user")
#     roles = ListField(TextField)
#     password_sha = TextField()
#     salt = TextField()
# 
#     def set_password(self, plaintext_password):
#         self.salt = _generate_salt()
#         self.password_sha = _password_hash(plaintext_password, self.salt)
# 
#     def lock_password(self):
#         self.password_sha = LOCKED_PASSWORD
#         
#     def check_password(self, plaintext_password):
#         if self.password_sha == LOCKED_PASSWORD:
#             return False
#         hashed_pw = _password_hash(plaintext_password, self.salt)
#         return self.password_sha == hashed_pw
# 
#     def is_anonymous(self):
#         return False
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
# 
#     def has_perm(self, perm, obj=None):
#         if self.has_role(ROLE_ADMIN):
#             return True
#         if perm == PERM_READ:
#             return True
#         return False
# 
# class AnonymousUser(object):
# 
#     def __init__(self):
#         self.roles = ['anonymous']
# 
#     def is_anonymous(self):
#         return True
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
#         
#     def has_perm(self, perm, obj=None):
#         if perm == PERM_READ:
#             return True
#         return False
# 
# Path: radarpost/user.py
# PERM_CREATE = 'radar:perm:create'
# 
# PERM_READ   = 'radar:perm:read'
# 
# PERM_UPDATE = 'radar:perm:update'
# 
# PERM_DELETE = 'radar:perm:delete'
# 
# Path: radarpost/user.py
# PERM_CREATE_MAILBOX = 'radar:perm:create:mailbox'


import base64
import logging
import mimetypes
import os
import routes
import sys
import traceback
from couchdb import Server, ResourceNotFound
from hashlib import md5
from jinja2 import Environment
from jinja2.loaders import ChoiceLoader, PackageLoader
from pytz import timezone, utc
from routes.route import Route
from webob import Response as HttpResponse
from webob.etag import ETagMatcher
from radarpost import plugins
from radarpost.plugins import plugin
from radarpost.mailbox import iter_mailboxes as _iter_mailboxes
from radarpost.user import User, AnonymousUser
from radarpost.user import PERM_CREATE, PERM_READ, PERM_UPDATE, PERM_DELETE
from radarpost.user import PERM_CREATE_MAILBOX
#    return 'foo-' + x + '-foo'
#
# allows you to say: 
#
# {{ someval|fooify }}

TEMPLATE_CONTEXT_PROCESSORS = 'radarpost.template_context_processors'
# this plugin represents functions that modify 
# the template context for each request, eg expose
# some standard variables or otherwise 
# sanitize things. Registered functions should 
# accept a webob Request and a dict representing the 
# template context and modify the context in place.
#
# eg: 
# @plugin(TEMPLATE_CONTEXT_PROCESSORS)
# def include_method(request, ctx): 
#    # always expose the http 'method' into the context
#    ctx['method'] = request.method
#
# allows you to say: 
# {{method}} 
# in any template to utilize the http method for the 
# current request.


class TemplateContext(dict):
    def __init__(self, request, ctx):
        self.update(ctx)
        self.request = request

# Path: radarpost/plugins.py
# ENTRY_POINT = 'radarpost_plugins'
# def get(slot):
# def _get(slot):
# def register(thing, slot):
# def plugin(slot):
#     def dd(thing):
# def _search():
# 
# Path: radarpost/plugins.py
# def plugin(slot):
#     """
#     decorator to declare a plugin
#     """
#     def dd(thing):
#         register(thing, slot)
#         return thing
#     return dd
# 
# Path: radarpost/mailbox.py
# def iter_mailboxes(couchdb, prefix=None): 
#     """
#     iterate through mailboxes in the current context.
#     if prefix is specified, only databases with names
#     that start with the string specified are returned.
#     """
#     for db_name in couchdb:
#         db = couchdb[db_name]
#         if prefix is not None and not db_name.startswith(prefix):
#             continue
#         if is_mailbox(db):
#             yield db
# 
# Path: radarpost/user.py
# class User(Document):
#     """
#     This is a representation of a 'standard' couchdb user
#     document.  Creating one of these in the couchdb users
#     database (usually _users) adds a database user.  They 
#     can be created elsewhere if database level access is 
#     not desirable.
#     
#     See http://wiki.apache.org/couchdb/Security_Features_Overview
#     
#     """
#     def __init__(self, username=None, password=None):
#         """
#         User may be initialized with the special keyword arguments
#         username and password for creating a new User:
#         
#         u = User(username=joe, password="bl0w")
#         """
#         Document.__init__(self)
# 
#         if 'username' is not None: 
#             self.id = 'org.couchdb.user:%s' % username
#             self['name'] = username
#     
#         if password is not None:
#             self.set_password(password)
#         else: 
#             self.lock_password()
# 
#     @classmethod
#     def get_by_username(cls, db, username):
#         natid = 'org.couchdb.user:%s' % username
#         return cls.load(db, natid)
# 
#     @property
#     def username(self):
#         return self['name']
# 
#     def get_public_id(self):
#         # same for now.
#         return self.username
# 
#     type = TextField(default="user")
#     roles = ListField(TextField)
#     password_sha = TextField()
#     salt = TextField()
# 
#     def set_password(self, plaintext_password):
#         self.salt = _generate_salt()
#         self.password_sha = _password_hash(plaintext_password, self.salt)
# 
#     def lock_password(self):
#         self.password_sha = LOCKED_PASSWORD
#         
#     def check_password(self, plaintext_password):
#         if self.password_sha == LOCKED_PASSWORD:
#             return False
#         hashed_pw = _password_hash(plaintext_password, self.salt)
#         return self.password_sha == hashed_pw
# 
#     def is_anonymous(self):
#         return False
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
# 
#     def has_perm(self, perm, obj=None):
#         if self.has_role(ROLE_ADMIN):
#             return True
#         if perm == PERM_READ:
#             return True
#         return False
# 
# class AnonymousUser(object):
# 
#     def __init__(self):
#         self.roles = ['anonymous']
# 
#     def is_anonymous(self):
#         return True
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
#         
#     def has_perm(self, perm, obj=None):
#         if perm == PERM_READ:
#             return True
#         return False
# 
# Path: radarpost/user.py
# PERM_CREATE = 'radar:perm:create'
# 
# PERM_READ   = 'radar:perm:read'
# 
# PERM_UPDATE = 'radar:perm:update'
# 
# PERM_DELETE = 'radar:perm:delete'
# 
# Path: radarpost/user.py
# PERM_CREATE_MAILBOX = 'radar:perm:create:mailbox'


# Path: radarpost/web/context.py
import base64
import logging
import mimetypes
import os
import routes
import sys
import traceback
from couchdb import Server, ResourceNotFound
from hashlib import md5
from jinja2 import Environment
from jinja2.loaders import ChoiceLoader, PackageLoader
from pytz import timezone, utc
from routes.route import Route
from webob import Response as HttpResponse
from webob.etag import ETagMatcher
from radarpost import plugins
from radarpost.plugins import plugin
from radarpost.mailbox import iter_mailboxes as _iter_mailboxes
from radarpost.user import User, AnonymousUser
from radarpost.user import PERM_CREATE, PERM_READ, PERM_UPDATE, PERM_DELETE
from radarpost.user import PERM_CREATE_MAILBOX

#    return 'foo-' + x + '-foo'
#
# allows you to say: 
#
# {{ someval|fooify }}

TEMPLATE_CONTEXT_PROCESSORS = 'radarpost.template_context_processors'
# this plugin represents functions that modify 
# the template context for each request, eg expose
# some standard variables or otherwise 
# sanitize things. Registered functions should 
# accept a webob Request and a dict representing the 
# template context and modify the context in place.
#
# eg: 
# @plugin(TEMPLATE_CONTEXT_PROCESSORS)
# def include_method(request, ctx): 
#    # always expose the http 'method' into the context
#    ctx['method'] = request.method
#
# allows you to say: 
# {{method}} 
# in any template to utilize the http method for the 
# current request.


class TemplateContext(dict):
    def __init__(self, request, ctx):
        self.update(ctx)
        self.request = request

        for proc in plugins.get(TEMPLATE_CONTEXT_PROCESSORS):