<commit_before>from django.contrib.sites.shortcuts import get_current_site

from comicsite.models import send_templated_email

def send_participation_request_notification_email(request, obj):
    """ When a user requests to become a participant, let this know to all admins

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = 'New participation request for {0}'.format(obj.project.short_name)
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'site': mainportal,
              'project': obj.project}
    for admin in obj.project.get_admins():
        kwargs["admin"] = admin
        send_templated_email(
            title,
            "emails/participation_request_notification_email.html",
            kwargs,
            [admin.email],
            request=request,
        )

def send_participation_request_accepted_email(request, obj):
    """ When a users requests to become a participant is accepted, let the user know

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = obj.project.short_name + ' participation request accepted'
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'adder': request.user,
              'site': mainportal,
              'project': obj.project}

    send_templated_email(
        title,
        "emails/participation_request_accepted_email.html",
        kwargs,
        [obj.user.email],
        request=request,
    )

def send_participation_request_rejected_email(request, obj):
    """ When a users requests to become a participant is rejected, let the user know

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = obj.project.short_name + 'participation request rejected'
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'adder': request.user,
              'site': mainportal,
              'project': obj.project}

    send_templated_email(
        title,
        "emails/participation_request_rejected_email.html",
        kwargs,
        [obj.user.email],
        request=request,
    )
<commit_msg>TRAC#7506 Missing space<commit_after>from django.contrib.sites.shortcuts import get_current_site

from comicsite.models import send_templated_email

def send_participation_request_notification_email(request, obj):
    """ When a user requests to become a participant, let this know to all admins

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = 'New participation request for {0}'.format(obj.project.short_name)
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'site': mainportal,
              'project': obj.project}
    for admin in obj.project.get_admins():
        kwargs["admin"] = admin
        send_templated_email(
            title,
            "emails/participation_request_notification_email.html",
            kwargs,
            [admin.email],
            request=request,
        )

def send_participation_request_accepted_email(request, obj):
    """ When a users requests to become a participant is accepted, let the user know

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = obj.project.short_name + ' participation request accepted'
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'adder': request.user,
              'site': mainportal,
              'project': obj.project}

    send_templated_email(
        title,
        "emails/participation_request_accepted_email.html",
        kwargs,
        [obj.user.email],
        request=request,
    )

def send_participation_request_rejected_email(request, obj):
    """ When a users requests to become a participant is rejected, let the user know

    request:     HTTPRequest containing the current admin posting this
    obj:         ParticipationRequest object containing info on which user requested
                 participation for which project

    """

    title = obj.project.short_name + ' participation request rejected'
    mainportal = get_current_site(request)
    kwargs = {'user': obj.user,
              'adder': request.user,
              'site': mainportal,
              'project': obj.project}

    send_templated_email(
        title,
        "emails/participation_request_rejected_email.html",
        kwargs,
        [obj.user.email],
        request=request,
    )
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext as _
from taggit.managers import TaggableManager
from taggit.models import TaggedItem

from scout.models  import Place, PostalAddress
from projects.models import Project

from django.db.models.signals import post_save
from django.dispatch.dispatcher import receiver

class MakerScienceProjectTaggedItem (TaggedItem):
    PROJECT_TAG_TYPE_CHOICES = (
        ('th', 'Thématique'),
        ('tg', 'Cibles visées'),
        ('fm', 'Formats'),
        ('nd', 'Besoins'),
    )
    tag_type = models.CharField(max_length=2, choices=PROJECT_TAG_TYPE_CHOICES)

class MakerScienceResourceTaggedItem (TaggedItem):
    RESOURCE_TAG_TYPE_CHOICES = (
        ('th', 'Thématique'),
        ('tg', 'Cibles visées'),
        ('fm', 'Formats'),
        ('rs', 'Ressources nécessaires'),
    )
    tag_type = models.CharField(max_length=2, choices=RESOURCE_TAG_TYPE_CHOICES)

class MakerScienceProject(models.Model):
    class Meta:
        permissions = (
            ('view_makerscienceproject', _("View MakerScienceProject")),
        )

    parent = models.ForeignKey(Project)
    tags = TaggableManager(through=MakerScienceProjectTaggedItem, blank=True)
    modified = models.DateTimeField()

    linked_resources = models.ManyToManyField("MakerScienceResource", null=True, blank=True)

    featured = models.BooleanField(default=False)

    def __unicode__(self):
        return self.parent.title

    class Meta :
        ordering = ['parent__created_on',]

@receiver(post_save, sender=MakerScienceProject)
def assign_place_to_project(sender, created, instance, **kwargs):
    if instance.parent.location is None:
        instance.parent.location = Place.objects.create(address=PostalAddress.objects.create())
        instance.parent.save()

class MakerScienceResource(models.Model):

    class Meta:
        permissions = (
            ('view_makerscienceresource', _("View MakerScienceResource")),
        )

    LEVEL_CHOICES = (
        ("1", "Facile"),
        ("2", "Moyen"),
        ("3", "Difficile")
    )

    parent = models.ForeignKey(Project)
    tags = TaggableManager(through=MakerScienceResourceTaggedItem, blank=True)
    modified = models.DateTimeField()

    level = models.CharField(max_length=1, choices=LEVEL_CHOICES)
    duration = models.CharField(max_length=30)
    cost = models.CharField(max_length=30)

    linked_resources = models.ManyToManyField("MakerScienceResource", null=True, blank=True)

    featured = models.BooleanField(default=False)

    class Meta :
        ordering = ['parent__created_on',]
<commit_msg>Remove obsolete fields<commit_after># -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext as _
from taggit.managers import TaggableManager
from taggit.models import TaggedItem

from scout.models  import Place, PostalAddress
from projects.models import Project

class MakerScienceProjectTaggedItem (TaggedItem):
    PROJECT_TAG_TYPE_CHOICES = (
        ('th', 'Thématique'),
        ('tg', 'Cibles visées'),
        ('fm', 'Formats'),
        ('nd', 'Besoins'),
    )
    tag_type = models.CharField(max_length=2, choices=PROJECT_TAG_TYPE_CHOICES)

class MakerScienceResourceTaggedItem (TaggedItem):
    RESOURCE_TAG_TYPE_CHOICES = (
        ('th', 'Thématique'),
        ('tg', 'Cibles visées'),
        ('fm', 'Formats'),
        ('rs', 'Ressources nécessaires'),
    )
    tag_type = models.CharField(max_length=2, choices=RESOURCE_TAG_TYPE_CHOICES)

class MakerScienceProject(models.Model):
    parent = models.ForeignKey(Project)
    tags = TaggableManager(through=MakerScienceProjectTaggedItem, blank=True)
    modified = models.DateTimeField()

    linked_resources = models.ManyToManyField("MakerScienceResource", null=True, blank=True)

    featured = models.BooleanField(default=False)

    def __unicode__(self):
        return self.parent.title

    class Meta :
        ordering = ['parent__created_on',]

@receiver(post_save, sender=MakerScienceProject)
def assign_place_to_project(sender, created, instance, **kwargs):
    if instance.parent.location is None:
        instance.parent.location = Place.objects.create(address=PostalAddress.objects.create())
        instance.parent.save()

class MakerScienceResource(models.Model):
    parent = models.ForeignKey(Project)
    tags = TaggableManager(through=MakerScienceResourceTaggedItem, blank=True)
    modified = models.DateTimeField()

    duration = models.CharField(max_length=30)

    linked_resources = models.ManyToManyField("MakerScienceResource", null=True, blank=True)

    featured = models.BooleanField(default=False)

    class Meta :
        ordering = ['parent__created_on',]
<|endoftext|>


<commit_before><commit_msg>Fix (?) IndexError when warning about argument names<commit_after><|endoftext|>


<commit_before>from __future__ import with_statement
from fabric.api import env, local, cd, roles
from fabric.contrib.files import *
from fabric.contrib.project import rsync_project
from fabfile.target import _needs_targetenv
from fabfile.app import _deploy_war
from fabfile.app import _deploy_war_norestart
from fabfile.util import venv
from os import path as p

env.java_opts = 'JAVA_OPTS="-Xms512m -Xmx1024m"'
# env.demodata_dir = "/opt/work/rinfo/demodata"
env.demodata_dir = "/tmp/rinfo-work/rinfo/demodata"
env.demodata_tools = p.join(env.projectroot, "tools", "demodata")

lagen_nu_datasets = ('sfs', 'dv')
riksdagen_se_datasets = ('prop', 'sou', 'ds')

exempel_datasets = [{'emfs': ['Forfattningar/EMFS/2011','exempelmyndigheten/exempelmyndigheten_source_feed.atom']}]
exempel_datasets_keys = ('emfs')

def _can_handle_dataset(dataset):
    if not any(dataset in ds for ds in (lagen_nu_datasets, riksdagen_se_datasets, exempel_datasets_keys)):
        raise ValueError("Undefined dataset %r" % dataset)

@task
def download(dataset, force="1"):
    """Downloads a demo dataset from its source."""
    _can_handle_dataset(dataset)
    if not int(force) and p.isdir("%(demodata_dir)s/%(dataset)s-raw" % venv()):
        return
    _mkdir_keep_prev("%(demodata_dir)s/%(dataset)s-raw" % venv())
    with cd("%(demodata_dir)s/%(dataset)s-raw" % venv()):
        if dataset in lagen_nu_datasets:
            _download_lagen_nu_data(dataset)
        elif dataset in riksdagen_se_datasets:
            _download_riksdagen_data(dataset)
        elif dataset in exempel_datasets_keys:
            _copy_local_repo(dataset)

@task
def create_depot(dataset):
    """Transforms the downloaded demo data to a depot."""
    _can_handle_dataset(dataset)
    _mkdir_keep_prev("%(demodata_dir)s/%(dataset)s" % venv())
    if dataset in lagen_nu_datasets:
        _transform_lagen_nu_data(dataset)
    elif dataset in riksdagen_se_datasets:
        _transform_riksdagen_data(dataset)

@task
@roles('demosource')
def upload(dataset):
    """Upload the transformed demo data depot to the demo server."""
    _can_handle_dataset(dataset)
    _needs_targetenv()
    if not exists(env.demo_data_root):
       sudo("mkdir -p %(demo_data_root)s" % env)
       sudo("chown %(user)s %(demo_data_root)s" % env)
    rsync_project(env.demo_data_root, "%(demodata_dir)s/%(dataset)s" % venv(), exclude=".*", delete=True)

#@task
def build_dataset_war(dataset):
    """Build a webapp capable of serving an uploaded demo data depot."""
    local("cd %(java_packages)s/demodata-supply && "
            "mvn -Ddataset=%(dataset)s -Ddemodata-root=%(demo_data_root)s clean package" % venv(), capture=False)

#@task
@roles('demosource')
def deploy_dataset_war(dataset, restart=True):
    """Deploy a demo webapp for the given uploaded dataset."""
    _can_handle_dataset(dataset)
    if not exists(env.dist_dir):
        run("mkdir %(dist_dir)s"%env)
    if restart:
        _deploy_war("%(java_packages)s/demodata-supply/target/%(dataset)s-demodata-supply.war" % venv(),
            "%(dataset)s-demodata-supply" % venv())
    else:
        _deploy_war_norestart("%(java_packages)s/demodata-supply/target/%(dataset)s-demodata-supply.war" % venv(),
            "%(dataset)s-demodata-supply" % venv())

@task
@roles('demosource')
def dataset_war(dataset):
    """Package and deploy a demo webapp for the given uploaded dataset."""
    build_dataset_war(dataset)
    deploy_dataset_war(dataset)

@task
def refresh(dataset, force="0"):
    """
    Download, transform, upload, build and deploy a webapp for serving a demo
    dataset.
    """
    _can_handle_dataset(dataset)
    download(dataset, force)
    create_depot(dataset)
    deploy_dataset(dataset)

@task
def deploy_dataset(dataset):
    upload(dataset)
    dataset_war(dataset)

#def full_demo_deploy():
#    from itertools import chain
#    for dataset in chain(lagen_nu_datasets, riksdagen_se_datasets):
#        # TODO:? env.role = dataset
#        demo_refresh(dataset)
#    from fabfile.app import admin
#    admin.all()

@task
def deploy_testfeed(dataset):
    for lookup in dataset.keys():
        key = lookup
    create_depot(key)
    _copy_local_repo(dataset[key])
    build_dataset_war(key)
    upload(key)
    deploy_dataset_war(key, restart=False)

@task
def deploy_all_testfeeds():
    for dataset in exempel_datasets:
        deploy_testfeed(dataset)

def _mkdir_keep_prev(dir_path):
    if p.isdir("%s-prev"%dir_path):
        local("rm -rf %s-prev"%dir_path)
    if p.isdir("%s"%dir_path):
        local("mv %s %s-prev"%(dir_path, dir_path))
    local("mkdir -p %s"%dir_path)

def _download_lagen_nu_data(dataset):
    local("curl https://lagen.nu/%(dataset)s/parsed/rdf.nt -o %(demodata_dir)s/%(dataset)s-raw/lagennu-%(dataset)s.nt" % venv())

def _transform_lagen_nu_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/lagen_nu/n3dump_to_depot.groovy "
            " %(demodata_dir)s/%(dataset)s-raw/lagennu-%(dataset)s.nt %(demodata_dir)s/%(dataset)s" % venv())

def _download_riksdagen_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/data_riksdagen_se/fetch_data_riksdagen_se.groovy "
            " %(demodata_dir)s/%(dataset)s-raw %(dataset)s -f" % venv())

def _transform_riksdagen_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/data_riksdagen_se/depot_from_data_riksdagen_se.groovy "
            " %(demodata_dir)s/%(dataset)s-raw %(demodata_dir)s/%(dataset)s" % venv())

def _copy_local_repo(dataset_value):
    rdf_example_path = dataset_value[0]
    atom_example_subpath_and_file = dataset_value[1]
    local('cp -r ../../documentation/exempel/documents/publ/' + rdf_example_path + '/* %(demodata_dir)s/%(dataset)s' % venv())
    local('cp -r ../../documentation/exempel/feeds/sources/' + atom_example_subpath_and_file + ' %(demodata_dir)s/%(dataset)s/feed.atom' % venv())

<commit_msg>Prepare for more testfeeds<commit_after>from __future__ import with_statement
from fabric.api import env, local, cd, roles
from fabric.contrib.files import *
from fabric.contrib.project import rsync_project
from fabfile.target import _needs_targetenv
from fabfile.app import _deploy_war
from fabfile.app import _deploy_war_norestart
from fabfile.util import venv
from os import path as p

env.java_opts = 'JAVA_OPTS="-Xms512m -Xmx1024m"'
# env.demodata_dir = "/opt/work/rinfo/demodata"
env.demodata_dir = "/tmp/rinfo-work/rinfo/demodata"
env.demodata_tools = p.join(env.projectroot, "tools", "demodata")

lagen_nu_datasets = ('sfs', 'dv')
riksdagen_se_datasets = ('prop', 'sou', 'ds')

exempel_datasets = [{'emfs': ['Forfattningar/EMFS/2011','exempelmyndigheten/exempelmyndigheten_source_feed.atom']}]
exempel_datasets_keys = ('emfs')

def _can_handle_dataset(dataset):
    if not any(dataset in ds for ds in (lagen_nu_datasets, riksdagen_se_datasets, exempel_datasets_keys)):
        raise ValueError("Undefined dataset %r" % dataset)

@task
def download(dataset, force="1"):
    """Downloads a demo dataset from its source."""
    _can_handle_dataset(dataset)
    if not int(force) and p.isdir("%(demodata_dir)s/%(dataset)s-raw" % venv()):
        return
    _mkdir_keep_prev("%(demodata_dir)s/%(dataset)s-raw" % venv())
    with cd("%(demodata_dir)s/%(dataset)s-raw" % venv()):
        if dataset in lagen_nu_datasets:
            _download_lagen_nu_data(dataset)
        elif dataset in riksdagen_se_datasets:
            _download_riksdagen_data(dataset)
        elif dataset in exempel_datasets_keys:
            _copy_local_repo(dataset)

@task
def create_depot(dataset):
    """Transforms the downloaded demo data to a depot."""
    _can_handle_dataset(dataset)
    _mkdir_keep_prev("%(demodata_dir)s/%(dataset)s" % venv())
    if dataset in lagen_nu_datasets:
        _transform_lagen_nu_data(dataset)
    elif dataset in riksdagen_se_datasets:
        _transform_riksdagen_data(dataset)

@task
@roles('demosource')
def upload(dataset):
    """Upload the transformed demo data depot to the demo server."""
    _can_handle_dataset(dataset)
    _needs_targetenv()
    if not exists(env.demo_data_root):
       sudo("mkdir -p %(demo_data_root)s" % env)
       sudo("chown %(user)s %(demo_data_root)s" % env)
    rsync_project(env.demo_data_root, "%(demodata_dir)s/%(dataset)s" % venv(), exclude=".*", delete=True)

#@task
def build_dataset_war(dataset):
    """Build a webapp capable of serving an uploaded demo data depot."""
    local("cd %(java_packages)s/demodata-supply && "
            "mvn -Ddataset=%(dataset)s -Ddemodata-root=%(demo_data_root)s clean package" % venv(), capture=False)

#@task
@roles('demosource')
def deploy_dataset_war(dataset, restart=True):
    """Deploy a demo webapp for the given uploaded dataset."""
    _can_handle_dataset(dataset)
    if not exists(env.dist_dir):
        run("mkdir %(dist_dir)s"%env)
    if restart:
        _deploy_war("%(java_packages)s/demodata-supply/target/%(dataset)s-demodata-supply.war" % venv(),
            "%(dataset)s-demodata-supply" % venv())
    else:
        _deploy_war_norestart("%(java_packages)s/demodata-supply/target/%(dataset)s-demodata-supply.war" % venv(),
            "%(dataset)s-demodata-supply" % venv())

@task
@roles('demosource')
def dataset_war(dataset):
    """Package and deploy a demo webapp for the given uploaded dataset."""
    build_dataset_war(dataset)
    deploy_dataset_war(dataset)

@task
def refresh(dataset, force="0"):
    """
    Download, transform, upload, build and deploy a webapp for serving a demo
    dataset.
    """
    _can_handle_dataset(dataset)
    download(dataset, force)
    create_depot(dataset)
    deploy_dataset(dataset)

@task
def deploy_dataset(dataset):
    upload(dataset)
    dataset_war(dataset)

#def full_demo_deploy():
#    from itertools import chain
#    for dataset in chain(lagen_nu_datasets, riksdagen_se_datasets):
#        # TODO:? env.role = dataset
#        demo_refresh(dataset)
#    from fabfile.app import admin
#    admin.all()

@task
def deploy_testfeed(dataset):
    for lookup in dataset.keys():
        key = lookup
    create_depot(key)
    _copy_local_repo(key, dataset[key])
    build_dataset_war(key)
    upload(key)
    deploy_dataset_war(key, restart=False)

@task
def deploy_all_testfeeds():
    for dataset in exempel_datasets:
        deploy_testfeed(dataset)

def _mkdir_keep_prev(dir_path):
    if p.isdir("%s-prev"%dir_path):
        local("rm -rf %s-prev"%dir_path)
    if p.isdir("%s"%dir_path):
        local("mv %s %s-prev"%(dir_path, dir_path))
    local("mkdir -p %s"%dir_path)

def _download_lagen_nu_data(dataset):
    local("curl https://lagen.nu/%(dataset)s/parsed/rdf.nt -o %(demodata_dir)s/%(dataset)s-raw/lagennu-%(dataset)s.nt" % venv())

def _transform_lagen_nu_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/lagen_nu/n3dump_to_depot.groovy "
            " %(demodata_dir)s/%(dataset)s-raw/lagennu-%(dataset)s.nt %(demodata_dir)s/%(dataset)s" % venv())

def _download_riksdagen_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/data_riksdagen_se/fetch_data_riksdagen_se.groovy "
            " %(demodata_dir)s/%(dataset)s-raw %(dataset)s -f" % venv())

def _transform_riksdagen_data(dataset):
    local("%(java_opts)s groovy %(demodata_tools)s/data_riksdagen_se/depot_from_data_riksdagen_se.groovy "
            " %(demodata_dir)s/%(dataset)s-raw %(demodata_dir)s/%(dataset)s" % venv())

def _copy_local_repo(dataset_key, dataset_value):
    rdf_example_path = dataset_value[0]
    atom_example_subpath_and_file = dataset_value[1]
    local('cp -r ../../documentation/exempel/documents/publ/' + rdf_example_path + '/* %(demodata_dir)s/%(dataset_key)s' % venv())
    local('cp -r ../../documentation/exempel/feeds/sources/' + atom_example_subpath_and_file + ' %(demodata_dir)s/%(dataset_key)s/feed.atom' % venv())

<|endoftext|>


<commit_before># jsb/socklib/rest/server.py
#
#

## jsb imports

from jsb.utils.exception import handle_exception, exceptionmsg
from jsb.utils.trace import calledfrom
from jsb.lib.persiststate import ObjectState
from jsb.lib.threads import start_new_thread
from jsb.lib.version import version

## basic imports

from SocketServer import BaseServer, ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urllib import unquote_plus
from asyncore import dispatcher
from cgi import escape
import time
import sys
import select
import types
import socket
import logging

## RestServerBase class

class RestServerBase(HTTPServer):

    """ REST web server """

    allow_reuse_address = True
    daemon_thread = True

    def start(self):
        """ start the REST server. """
        self.name = calledfrom(sys._getframe(0))
        self.stop = False
        self.running = False
        self.handlers = {}
        self.webmods = {}
        self.state = ObjectState()
        self.state.define('whitelistenable', 0)
        self.state.define('whitelist', [])
        self.state.define('blacklist', [])
        self.state.define('disable', [])
        self.poll = select.poll()
        self.poll.register(self)
        start_new_thread(self.serve, ())

    def shutdown(self):
        """ shutdown the REST server. """
        try:
            self.stop = True
            time.sleep(0.2)
            self.server_close()
        except Exception, ex: handle_exception()

    def serve(self):
        """ serving loop. """
        logging.warn('rest.server - starting')
        time.sleep(1)
        while not self.stop:
            self.running = True
            try: got = self.poll.poll(100)
            except Exception, ex: handle_exception()
            if got and not self.stop:
                try: self.handle_request()
                except Exception, ex: handle_exception()
            time.sleep(0.01)
        self.running = False
        logging.warn('rest.server - stopping')

    def entrypoint(self, request):
        """ check lists whether request should be allowed. """
        ip = request.ip
        if not self.whitelistenable() and ip in self.blacklist():
            logging.warn('rest.server - denied %s' % ip)
            request.send_error(401)
            return False
        if  self.whitelistenable() and ip not in self.whitelist():
            logging.warn('rest.server - denied %s' % ip)
            request.send_error(401)
            return False
        return True

    def whitelistenable(self):
        """ enable whitelist? """
        return self.state['whitelistenable']

    def whitelist(self):
        """ return the whitelist. """
        return self.state['whitelist']

    def blacklist(self):
        """ return the black list. """
        return self.state['blacklist']

    def addhandler(self, path, type, handler):
        """ add a web handler """
        path = unquote_plus(path)
        splitted = []
        for i in path.split('/'):
            if i: splitted.append(i)
        splitted = tuple(splitted)
        if not self.handlers.has_key(splitted): self.handlers[splitted[0]] = {}
        self.handlers[splitted[0]][type] = handler
        logging.info('rest.server - %s %s handler added' % (splitted[0], type))

    def enable(self, what):
        """ enable an path. """
        try:
            self.state['disable'].remove(what)
            logging.info('rest.server - enabled %s' % str(what))
        except ValueError: pass

    def disable(self, what):
        """ disable an path. """
        self.state['disable'].append(what)
        logging.info('rest.server - disabled %s' % str(what))

    def do(self, request):
        """ do a request """
        path = unquote_plus(request.path.strip())
        path = path.split('?')[0]
        if path.endswith('/'): path = path[:-1]
        splitted = []
        for i in path.split('/'):
            if i: splitted.append(i)
        splitted = tuple(splitted)
        logging.warn("rest.server - incoming - %s" % str(splitted))
        for i in self.state['disable']:
            if i in splitted:
                logging.warn('rest.server - %s - denied disabled %s' % (request.ip, i))
                request.send_error(404)
                return
        request.splitted = splitted
        request.value = None
        type = request.command
        try: func = self.handlers[splitted[0]][type]
        except (KeyError, ValueError):
            try:
                func = self.handlers[splitted[0]][type]
                request.value = splitted[-1]
            except (KeyError, ValueError):
                logging.error("rest.server - no handler found for %s" % str(splitted[:-1]))
                request.send_error(404)
                return
        result = func(self, request)
        logging.info('rest.server - %s - result: %s' % (request.ip, str(result)))
        return result

    def handle_error(self, request, addr):
        """ log the error """
        ip = request.ip
        exctype, excvalue, tb = sys.exc_info()
        if exctype == socket.timeout:
            logging.warn('rest.server - %s - socket timeout' % (ip, ))
            return
        if exctype == socket.error:
            logging.warn('rest.server - %s - socket error: %s' % (ip, excvalue))
            return
        exceptstr = exceptionmsg()
        logging.warn('rest.server - %s - error %s %s => %s' % (ip, exctype, excvalue, exceptstr))

## Mixin classes

class RestServer(ThreadingMixIn, RestServerBase):

    pass

class RestServerAsync(RestServerBase, dispatcher):

    pass

## RestReqeustHandler class

class RestRequestHandler(BaseHTTPRequestHandler):

    """ timeserver request handler class """

    def setup(self):
        """ called on each incoming request. """
        BaseHTTPRequestHandler.setup(self)
        self.ip = self.client_address[0]
        self.name = self.ip
        self.size = 0

    def writeheader(self, type='text/plain'):
        """ write headers to the client. """
        self.send_response(200)
        self.send_header('Content-type', '%s; charset=%s ' % (type,sys.getdefaultencoding()))
        self.send_header('Server', version)
        self.end_headers()

    def sendresult(self):
        """ dispatch a call. """
        try:
            result = self.server.do(self)
            if not result: return
            self.size = len(result)
        except Exception, ex:
            handle_exception()
            self.send_error(501)
            return
        self.writeheader()
        self.wfile.write(result)
        self.wfile.close()

    def handle_request(self):
        """ handle a REST request. """
        if not self.server.entrypoint(self): return
        self.sendresult()

    do_DELETE = do_PUT = do_GET = do_POST = handle_request

    def log_request(self, code):
        """ log the request """
        try: ua = self.headers['user-agent']
        except: ua = "-"
        try: rf = self.headers['referer']
        except: rf = "-"
        if hasattr(self, 'path'):
            logging.debug('rest.server - %s "%s %s %s" %s %s "%s" "%s"' % (self.address_string(), self.command, self.path, self.request_version, code, self.size, rf, ua))
        else:
            logging.debug('rest.server - %s "%s %s %s" %s %s "%s" "%s"' % (self.address_string(), self.command, "none", self.request_version, code, self.size, rf, ua))

## secure classes .. not working yet

class SecureRestServer(RestServer):

    def __init__(self, server_address, HandlerClass, keyfile, certfile):
        from OpenSSL import SSL
        BaseServer.__init__(self, server_address, HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.set_options(SSL.OP_NO_SSLv2)
        logging.warn("rest.server - loading private key from %s" % keyfile)
        ctx.use_privatekey_file (keyfile)
        logging.warn('rest.server - loading certificate from %s' % certfile)
        ctx.use_certificate_file(certfile)
        logging.info('rest.server - creating SSL socket on %s' % str(server_address))
        self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
                                                        self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureAuthRestServer(SecureRestServer):

    def __init__(self, server_address, HandlerClass, chain, serverkey, servercert):
        from OpenSSL import SSL
        BaseServer.__init__(self, server_address, HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        logging.warn("rest.server - loading private key from %s" % serverkey)
        ctx.use_privatekey_file (serverkey)
        logging.warn('rest.server - loading certificate from %s' % servercert)
        ctx.use_certificate_file(servercert)
        logging.warn('rest.server - loading chain of certifications from %s' % chain)
        ctx.set_verify_depth(2)
        ctx.load_client_ca(chain)
        #ctx.load_verify_locations(chain)
        logging.info('rest.server - creating SSL socket on %s' % str(server_address))
        callback = lambda conn,cert,errno,depth,retcode: retcode
        ctx.set_verify(SSL.VERIFY_FAIL_IF_NO_PEER_CERT | SSL.VERIFY_PEER, callback)
        ctx.set_session_id('jsb')
        self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
                                                        self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureRequestHandler(RestRequestHandler):

    def setup(self):
        self.connection = self.request._sock
        self.request._sock.setblocking(1)
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.rbufsize)
<commit_msg>make REST server work properly when no path is given<commit_after># jsb/socklib/rest/server.py
#
#

## jsb imports

from jsb.utils.exception import handle_exception, exceptionmsg
from jsb.utils.trace import calledfrom
from jsb.lib.persiststate import ObjectState
from jsb.lib.threads import start_new_thread
from jsb.lib.version import version

## basic imports

from SocketServer import BaseServer, ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urllib import unquote_plus
from asyncore import dispatcher
from cgi import escape
import time
import sys
import select
import types
import socket
import logging

## RestServerBase class

class RestServerBase(HTTPServer):

    """ REST web server """

    allow_reuse_address = True
    daemon_thread = True

    def start(self):
        """ start the REST server. """
        self.name = calledfrom(sys._getframe(0))
        self.stop = False
        self.running = False
        self.handlers = {}
        self.webmods = {}
        self.state = ObjectState()
        self.state.define('whitelistenable', 0)
        self.state.define('whitelist', [])
        self.state.define('blacklist', [])
        self.state.define('disable', [])
        self.poll = select.poll()
        self.poll.register(self)
        start_new_thread(self.serve, ())

    def shutdown(self):
        """ shutdown the REST server. """
        try:
            self.stop = True
            time.sleep(0.2)
            self.server_close()
        except Exception, ex: handle_exception()

    def serve(self):
        """ serving loop. """
        logging.warn('rest.server - starting')
        time.sleep(1)
        while not self.stop:
            self.running = True
            try: got = self.poll.poll(100)
            except Exception, ex: handle_exception()
            if got and not self.stop:
                try: self.handle_request()
                except Exception, ex: handle_exception()
            time.sleep(0.01)
        self.running = False
        logging.warn('rest.server - stopping')

    def entrypoint(self, request):
        """ check lists whether request should be allowed. """
        ip = request.ip
        if not self.whitelistenable() and ip in self.blacklist():
            logging.warn('rest.server - denied %s' % ip)
            request.send_error(401)
            return False
        if  self.whitelistenable() and ip not in self.whitelist():
            logging.warn('rest.server - denied %s' % ip)
            request.send_error(401)
            return False
        return True

    def whitelistenable(self):
        """ enable whitelist? """
        return self.state['whitelistenable']

    def whitelist(self):
        """ return the whitelist. """
        return self.state['whitelist']

    def blacklist(self):
        """ return the black list. """
        return self.state['blacklist']

    def addhandler(self, path, type, handler):
        """ add a web handler """
        path = unquote_plus(path)
        splitted = []
        for i in path.split('/'):
            if i: splitted.append(i)
        else: splitted.append("/")
        splitted = tuple(splitted)
        if not self.handlers.has_key(splitted): self.handlers[splitted[0]] = {}
        self.handlers[splitted[0]][type] = handler
        logging.info('rest.server - %s %s handler added' % (splitted[0], type))

    def enable(self, what):
        """ enable an path. """
        try:
            self.state['disable'].remove(what)
            logging.info('rest.server - enabled %s' % str(what))
        except ValueError: pass

    def disable(self, what):
        """ disable an path. """
        self.state['disable'].append(what)
        logging.info('rest.server - disabled %s' % str(what))

    def do(self, request):
        """ do a request """
        path = unquote_plus(request.path.strip())
        path = path.split('?')[0]
        if path.endswith('/'): path = path[:-1]
        splitted = []
        for i in path.split('/'):
            if i: splitted.append(i)
        else: splitted.append("/")
        splitted = tuple(splitted)
        logging.warn("rest.server - incoming - %s" % str(splitted))
        for i in self.state['disable']:
            if i in splitted:
                logging.warn('rest.server - %s - denied disabled %s' % (request.ip, i))
                request.send_error(404)
                return
        request.splitted = splitted
        request.value = None
        type = request.command
        try: func = self.handlers[splitted[0]][type]
        except (KeyError, ValueError):
            try:
                func = self.handlers[splitted[0]][type]
                request.value = splitted[-1]
            except (KeyError, ValueError):
                logging.error("rest.server - no handler found for %s" % str(splitted[:-1]))
                request.send_error(404)
                return
        result = func(self, request)
        logging.info('rest.server - %s - result: %s' % (request.ip, str(result)))
        return result

    def handle_error(self, request, addr):
        """ log the error """
        ip = request.ip
        exctype, excvalue, tb = sys.exc_info()
        if exctype == socket.timeout:
            logging.warn('rest.server - %s - socket timeout' % (ip, ))
            return
        if exctype == socket.error:
            logging.warn('rest.server - %s - socket error: %s' % (ip, excvalue))
            return
        exceptstr = exceptionmsg()
        logging.warn('rest.server - %s - error %s %s => %s' % (ip, exctype, excvalue, exceptstr))

## Mixin classes

class RestServer(ThreadingMixIn, RestServerBase):

    pass

class RestServerAsync(RestServerBase, dispatcher):

    pass

## RestReqeustHandler class

class RestRequestHandler(BaseHTTPRequestHandler):

    """ timeserver request handler class """

    def setup(self):
        """ called on each incoming request. """
        BaseHTTPRequestHandler.setup(self)
        self.ip = self.client_address[0]
        self.name = self.ip
        self.size = 0

    def writeheader(self, type='text/plain'):
        """ write headers to the client. """
        self.send_response(200)
        self.send_header('Content-type', '%s; charset=%s ' % (type,sys.getdefaultencoding()))
        self.send_header('Server', version)
        self.end_headers()

    def sendresult(self):
        """ dispatch a call. """
        try:
            result = self.server.do(self)
            if not result: return
            self.size = len(result)
        except Exception, ex:
            handle_exception()
            self.send_error(501)
            return
        self.writeheader()
        self.wfile.write(result)
        self.wfile.close()

    def handle_request(self):
        """ handle a REST request. """
        if not self.server.entrypoint(self): return
        self.sendresult()

    do_DELETE = do_PUT = do_GET = do_POST = handle_request

    def log_request(self, code):
        """ log the request """
        try: ua = self.headers['user-agent']
        except: ua = "-"
        try: rf = self.headers['referer']
        except: rf = "-"
        if hasattr(self, 'path'):
            logging.debug('rest.server - %s "%s %s %s" %s %s "%s" "%s"' % (self.address_string(), self.command, self.path, self.request_version, code, self.size, rf, ua))
        else:
            logging.debug('rest.server - %s "%s %s %s" %s %s "%s" "%s"' % (self.address_string(), self.command, "none", self.request_version, code, self.size, rf, ua))

## secure classes .. not working yet

class SecureRestServer(RestServer):

    def __init__(self, server_address, HandlerClass, keyfile, certfile):
        from OpenSSL import SSL
        BaseServer.__init__(self, server_address, HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.set_options(SSL.OP_NO_SSLv2)
        logging.warn("rest.server - loading private key from %s" % keyfile)
        ctx.use_privatekey_file (keyfile)
        logging.warn('rest.server - loading certificate from %s' % certfile)
        ctx.use_certificate_file(certfile)
        logging.info('rest.server - creating SSL socket on %s' % str(server_address))
        self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
                                                        self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureAuthRestServer(SecureRestServer):

    def __init__(self, server_address, HandlerClass, chain, serverkey, servercert):
        from OpenSSL import SSL
        BaseServer.__init__(self, server_address, HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        logging.warn("rest.server - loading private key from %s" % serverkey)
        ctx.use_privatekey_file (serverkey)
        logging.warn('rest.server - loading certificate from %s' % servercert)
        ctx.use_certificate_file(servercert)
        logging.warn('rest.server - loading chain of certifications from %s' % chain)
        ctx.set_verify_depth(2)
        ctx.load_client_ca(chain)
        #ctx.load_verify_locations(chain)
        logging.info('rest.server - creating SSL socket on %s' % str(server_address))
        callback = lambda conn,cert,errno,depth,retcode: retcode
        ctx.set_verify(SSL.VERIFY_FAIL_IF_NO_PEER_CERT | SSL.VERIFY_PEER, callback)
        ctx.set_session_id('jsb')
        self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
                                                        self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureRequestHandler(RestRequestHandler):

    def setup(self):
        self.connection = self.request._sock
        self.request._sock.setblocking(1)
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.rbufsize)
<|endoftext|>


<commit_before><commit_msg>added context_id argument to context_id<commit_after><|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os
import csv

import yaml

"""
Facades for accessing the configuration data.
"""

__author__ = 'Bernardo Martínez Garrido'
__license__ = 'MIT'
__status__ = 'Development'

class _FileReader(object):
    """
    Offers methods to read the data files.
    """

    # Singleton control object
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

    def __path(self):
        """
        Returns the path to the folder in which this class is contained.

        As this class is to be on the same folder as the data files, this will be the data files folder.

        :return: path to the data files folder.
        """
        return os.path.dirname(__file__)

    def read_csv_file(self, file_name):
        """
        Parses a CSV file into a list.

        :param file_name: name of the CSV file
        :return: a list with the file's contents
        """
        result = []
        with open(os.path.join(self.__path(), os.path.basename(file_name)), 'rt') as csvfile:
            headers_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
            for type_row in headers_reader:
                for t in type_row:
                    result.append(t)
        return result

    def read_yaml_file(self, file_name):
        """
        Parses a YAML file into a matrix.

        :param file_name: name of the YAML file
        :return: a matrix with the file's contents
        """
        with open(os.path.join(self.__path(), os.path.basename(file_name)), 'rt') as yamlfile:
            return yaml.load(yamlfile)

class CWRConfiguration(object):
    """
    Offers methods to access the CWR configuration data.
    """

    # Reader for the files
    _reader = _FileReader()

    # Files containing the CWR info
    _file_record_config = 'record_config.yml'
    _file_defaults = 'default_values.yml'
    _file_field_config_table = 'field_config_table.yml'

    # CWR configuration information
    _record_config = None
    _cwr_defaults = None
    _field_config_table = None

    # Singleton control object
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

    def _load_record_config(self):
        """
        Loads the CWR configuration file, creating a matrix from it, and then returns this data.

        The file will only be loaded once.

        :return: the CWR configuration matrix
        """
        if self._record_config is None:
            self._record_config = self._reader.read_yaml_file(self._file_record_config)

        return self._record_config

    def _load_cwr_defaults(self):
        """
        Loads the CWR default values file, creating a matrix from it, and then returns this data.

        The file will only be loaded once.

        :return: the CWR default values matrix
        """
        if self._cwr_defaults is None:
            self._cwr_defaults = self._reader.read_yaml_file(self._file_defaults)

        return self._cwr_defaults

    def load_field_config_table(self):
        """
        Loads the table fields configuration.

        The file will only be loaded once.

        :return: the table fields configuration
        """
        if self._field_config_table is None:
            self._field_config_table = self._reader.read_yaml_file(self._file_field_config_table)

        return self._field_config_table

    def default_version(self):
        """
        The current version of the CWR standard.

        :return: the current version of the CWR standard
        """
        return self._load_cwr_defaults()['default_version']

    def field_size(self, record, field):
        """
        Returns the expected size for a record's field.

        The record and field are the internal name used to identify a record type.

        :param record: the id for the record type
        :param field: the id for the field
        :return: the expected size for the field on the record
        """
        return self._load_record_config()[record][field]['size']

    def field_value(self, record, field):
        """
        Returns the expected value for a record's field.

        The record and field are the internal name used to identify a record type.

        :param record: the id for the record type
        :param field: the id for the field
        :return: the expected value for the field on the record
        """
        return self._load_record_config()[record][field]['value']

    def record_type(self, record):
        """
        Returns the expected record type for the received record.

        The record is the internal name used to identify a record type.

        :param record: the id for the record type
        :return: the expected record type on the record prefix
        """
        return self._load_record_config()[record]['type']

class CWRTables(object):
    def __init__(self):
        self._file_values = {}
        # Reader for the files
        self._reader = _FileReader()

    def get_data(self, id):
        if not id in self._file_values:
            file = 'cwr_%s.csv' % id
            self._file_values[id] = self._reader.read_csv_file(file)

        return self._file_values[id]<commit_msg>Tables accessor is now a singleton.<commit_after># -*- coding: utf-8 -*-

import os
import csv

import yaml

"""
Facades for accessing the configuration data.
"""

__author__ = 'Bernardo Martínez Garrido'
__license__ = 'MIT'
__status__ = 'Development'

class _FileReader(object):
    """
    Offers methods to read the data files.
    """

    # Singleton control object
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

    def __path(self):
        """
        Returns the path to the folder in which this class is contained.

        As this class is to be on the same folder as the data files, this will be the data files folder.

        :return: path to the data files folder.
        """
        return os.path.dirname(__file__)

    def read_csv_file(self, file_name):
        """
        Parses a CSV file into a list.

        :param file_name: name of the CSV file
        :return: a list with the file's contents
        """
        result = []
        with open(os.path.join(self.__path(), os.path.basename(file_name)), 'rt') as csvfile:
            headers_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
            for type_row in headers_reader:
                for t in type_row:
                    result.append(t)
        return result

    def read_yaml_file(self, file_name):
        """
        Parses a YAML file into a matrix.

        :param file_name: name of the YAML file
        :return: a matrix with the file's contents
        """
        with open(os.path.join(self.__path(), os.path.basename(file_name)), 'rt') as yamlfile:
            return yaml.load(yamlfile)

class CWRConfiguration(object):
    """
    Offers methods to access the CWR configuration data.
    """

    # Reader for the files
    _reader = _FileReader()

    # Files containing the CWR info
    _file_record_config = 'record_config.yml'
    _file_defaults = 'default_values.yml'
    _file_field_config_table = 'field_config_table.yml'

    # CWR configuration information
    _record_config = None
    _cwr_defaults = None
    _field_config_table = None

    # Singleton control object
    __shared_state = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

    def _load_record_config(self):
        """
        Loads the CWR configuration file, creating a matrix from it, and then returns this data.

        The file will only be loaded once.

        :return: the CWR configuration matrix
        """
        if self._record_config is None:
            self._record_config = self._reader.read_yaml_file(self._file_record_config)

        return self._record_config

    def _load_cwr_defaults(self):
        """
        Loads the CWR default values file, creating a matrix from it, and then returns this data.

        The file will only be loaded once.

        :return: the CWR default values matrix
        """
        if self._cwr_defaults is None:
            self._cwr_defaults = self._reader.read_yaml_file(self._file_defaults)

        return self._cwr_defaults

    def load_field_config_table(self):
        """
        Loads the table fields configuration.

        The file will only be loaded once.

        :return: the table fields configuration
        """
        if self._field_config_table is None:
            self._field_config_table = self._reader.read_yaml_file(self._file_field_config_table)

        return self._field_config_table

    def default_version(self):
        """
        The current version of the CWR standard.

        :return: the current version of the CWR standard
        """
        return self._load_cwr_defaults()['default_version']

    def field_size(self, record, field):
        """
        Returns the expected size for a record's field.

        The record and field are the internal name used to identify a record type.

        :param record: the id for the record type
        :param field: the id for the field
        :return: the expected size for the field on the record
        """
        return self._load_record_config()[record][field]['size']

    def field_value(self, record, field):
        """
        Returns the expected value for a record's field.

        The record and field are the internal name used to identify a record type.

        :param record: the id for the record type
        :param field: the id for the field
        :return: the expected value for the field on the record
        """
        return self._load_record_config()[record][field]['value']

    def record_type(self, record):
        """
        Returns the expected record type for the received record.

        The record is the internal name used to identify a record type.

        :param record: the id for the record type
        :return: the expected record type on the record prefix
        """
        return self._load_record_config()[record]['type']

class CWRTables(object):
    """
    Accesses the data inside CWR table files.

    This is used on the Lookup fields, to know which values are valid.

    The files are read only once, and then the data is stored to be returned each time it is required.
    """

    _instance = None

    def __init__(self):
        self._file_values = {}
        # Reader for the files
        self._reader = _FileReader()

    def __new__(self, *args, **kwargs):
        if not self._instance:
            self._instance = super(CWRTables, self).__new__(
                self, *args, **kwargs)
        return self._instance

    def get_data(self, id):
        """
        Acquires the data from the table identified by the id.

        The file is read only once, consecutive calls to this method will return the sale collection.

        :param id: identifier for the table
        :return: all the values from the table
        """
        if not id in self._file_values:
            file = 'cwr_%s.csv' % id
            self._file_values[id] = self._reader.read_csv_file(file)

        return self._file_values[id]<|endoftext|>


<commit_before>__all__ = ['writer']

import docutils.core as dc
import docutils.writers
from docutils import nodes

from docutils.writers.latex2e import (Writer, LaTeXTranslator,
                                      PreambleCmds)

import code_block

from options import options

PreambleCmds.float_settings = '''
\\usepackage[font={small,it},labelfont=bf]{caption}
\\usepackage{float}
'''

class Translator(LaTeXTranslator):
    def __init__(self, *args, **kwargs):
        LaTeXTranslator.__init__(self, *args, **kwargs)

    # Handle author declarations

    current_field = ''

    author_names = []
    author_institutions = []
    author_emails = []
    paper_title = ''
    table_caption = []

    def visit_docinfo(self, node):
        pass

    def depart_docinfo(self, node):
        pass

    def visit_author(self, node):
        self.author_names.append(self.encode(node.astext()))
        raise nodes.SkipNode

    def depart_author(self, node):
        pass

    def visit_classifier(self, node):
        pass

    def depart_classifier(self, node):
        pass

    def visit_field_name(self, node):
        self.current_field = node.astext()
        raise nodes.SkipNode

    def visit_field_body(self, node):
        text = self.encode(node.astext())

        if self.current_field == 'email':
            self.author_emails.append(text)
        elif self.current_field == 'institution':
            self.author_institutions.append(text)

        self.current_field = ''

        raise nodes.SkipNode

    def depart_field_body(self, node):
        raise nodes.SkipNode

    def depart_document(self, node):
        LaTeXTranslator.depart_document(self, node)

        title = self.paper_title
        authors = ', '.join(self.author_names)

        author_notes = ['''
The corresponding author is with %s, e-mail: \protect\href{%s}{%s}.
''' % (self.author_institutions[0], self.author_emails[0],
       self.author_emails[0])]

        author_notes = ''.join('\\thanks{%s}' % n for n in author_notes)

        title_template = '\\title{%s}\\author{%s%s}\\maketitle'
        title_template = title_template % (title,
                                           authors,
                                           author_notes)

        marks = r'''
        \renewcommand{\leftmark}{%s}
        \renewcommand{\rightmark}{%s}
        ''' % (options['proceedings_title'], title.upper())
        title_template += marks

        self.body_pre_docinfo = [title_template]

    def visit_title(self, node):
        if self.section_level == 1:
            if self.paper_title:
                import warnings
                warnings.warn(RuntimeWarning("Title set twice--ignored. "
                                             "Could be due to ReST"
                                             "error.)"))
            else:
                self.paper_title = self.encode(node.astext())
            raise nodes.SkipNode

        elif node.astext() == 'References':
            raise nodes.SkipNode

        LaTeXTranslator.visit_title(self, node)

    def visit_paragraph(self, node):
        if 'abstract' in node['classes']:
            self.out.append('\\begin{abstract}')

        elif 'keywords' in node['classes']:
            self.out.append('\\begin{IEEEkeywords}')

        else:
            self.out.append('\n\n')

    def depart_paragraph(self, node):
        if 'abstract' in node['classes']:
            self.out.append('\\end{abstract}')

        if 'keywords' in node['classes']:
            self.out.append('\\end{IEEEkeywords}')

    def visit_image(self, node):
        attrs = node.attributes
        node.attributes['width'] = '\columnwidth'
        node.attributes['align'] = 'left'

        LaTeXTranslator.visit_image(self, node)

    def visit_footnote(self, node):
        # Work-around for a bug in docutils where a
        # "%" is prepended to footnote text.
        LaTeXTranslator.visit_footnote(self, node)
        self.out[-1] = self.out[-1].strip('%')

    def visit_table(self, node):
        self.out.append(r'\begin{table}')
        LaTeXTranslator.visit_table(self, node)

    def depart_table(self, node):
        LaTeXTranslator.depart_table(self, node)

        self.out.append(r'\caption{%s}' % ''.join(self.table_caption))
        self.table_caption = []

        self.out.append(r'\end{table}')
        self.active_table.set('preamble written', 1)

    def visit_thead(self, node):
        # Store table caption locally and then remove it
        # from the table so that docutils doesn't render it
        # (in the wrong place)
        self.table_caption = self.active_table.caption
        self.active_table.caption = []

        LaTeXTranslator.visit_thead(self, node)

    def depart_thead(self, node):
        LaTeXTranslator.depart_thead(self, node)

    def visit_literal_block(self, node):
        if 'language' in node.attributes:
            # do highlighting
            from pygments import highlight
            from pygments.lexers import PythonLexer, get_lexer_by_name
            from pygments.formatters import LatexFormatter

            linenos = node.attributes.get('linenos', False)
            lexer = get_lexer_by_name(node.attributes['language'])
            tex = highlight(node.astext(), lexer,
                            LatexFormatter(linenos=linenos,
                                           verboptions='fontsize=\small'))

            self.out.append(tex)
            raise nodes.SkipNode
        else:
            LaTeXTranslator.visit_literal_block(self, node)

    def depart_literal_block(self, node):
        pass

writer = Writer()
writer.translator_class = Translator
<commit_msg>Match font sizes of code-block and verbatim.<commit_after>__all__ = ['writer']

import docutils.core as dc
import docutils.writers
from docutils import nodes

from docutils.writers.latex2e import (Writer, LaTeXTranslator,
                                      PreambleCmds)

import code_block

from options import options

PreambleCmds.float_settings = '''
\\usepackage[font={small,it},labelfont=bf]{caption}
\\usepackage{float}
'''

class Translator(LaTeXTranslator):
    def __init__(self, *args, **kwargs):
        LaTeXTranslator.__init__(self, *args, **kwargs)

    # Handle author declarations

    current_field = ''

    author_names = []
    author_institutions = []
    author_emails = []
    paper_title = ''
    table_caption = []

    def visit_docinfo(self, node):
        pass

    def depart_docinfo(self, node):
        pass

    def visit_author(self, node):
        self.author_names.append(self.encode(node.astext()))
        raise nodes.SkipNode

    def depart_author(self, node):
        pass

    def visit_classifier(self, node):
        pass

    def depart_classifier(self, node):
        pass

    def visit_field_name(self, node):
        self.current_field = node.astext()
        raise nodes.SkipNode

    def visit_field_body(self, node):
        text = self.encode(node.astext())

        if self.current_field == 'email':
            self.author_emails.append(text)
        elif self.current_field == 'institution':
            self.author_institutions.append(text)

        self.current_field = ''

        raise nodes.SkipNode

    def depart_field_body(self, node):
        raise nodes.SkipNode

    def depart_document(self, node):
        LaTeXTranslator.depart_document(self, node)

        title = self.paper_title
        authors = ', '.join(self.author_names)

        author_notes = ['''
The corresponding author is with %s, e-mail: \protect\href{%s}{%s}.
''' % (self.author_institutions[0], self.author_emails[0],
       self.author_emails[0])]

        author_notes = ''.join('\\thanks{%s}' % n for n in author_notes)

        title_template = '\\title{%s}\\author{%s%s}\\maketitle'
        title_template = title_template % (title,
                                           authors,
                                           author_notes)

        marks = r'''
        \renewcommand{\leftmark}{%s}
        \renewcommand{\rightmark}{%s}
        ''' % (options['proceedings_title'], title.upper())
        title_template += marks

        self.body_pre_docinfo = [title_template]

    def visit_title(self, node):
        if self.section_level == 1:
            if self.paper_title:
                import warnings
                warnings.warn(RuntimeWarning("Title set twice--ignored. "
                                             "Could be due to ReST"
                                             "error.)"))
            else:
                self.paper_title = self.encode(node.astext())
            raise nodes.SkipNode

        elif node.astext() == 'References':
            raise nodes.SkipNode

        LaTeXTranslator.visit_title(self, node)

    def visit_paragraph(self, node):
        if 'abstract' in node['classes']:
            self.out.append('\\begin{abstract}')

        elif 'keywords' in node['classes']:
            self.out.append('\\begin{IEEEkeywords}')

        else:
            self.out.append('\n\n')

    def depart_paragraph(self, node):
        if 'abstract' in node['classes']:
            self.out.append('\\end{abstract}')

        if 'keywords' in node['classes']:
            self.out.append('\\end{IEEEkeywords}')

    def visit_image(self, node):
        attrs = node.attributes
        node.attributes['width'] = '\columnwidth'
        node.attributes['align'] = 'left'

        LaTeXTranslator.visit_image(self, node)

    def visit_footnote(self, node):
        # Work-around for a bug in docutils where a
        # "%" is prepended to footnote text.
        LaTeXTranslator.visit_footnote(self, node)
        self.out[-1] = self.out[-1].strip('%')

    def visit_table(self, node):
        self.out.append(r'\begin{table}')
        LaTeXTranslator.visit_table(self, node)

    def depart_table(self, node):
        LaTeXTranslator.depart_table(self, node)

        self.out.append(r'\caption{%s}' % ''.join(self.table_caption))
        self.table_caption = []

        self.out.append(r'\end{table}')
        self.active_table.set('preamble written', 1)

    def visit_thead(self, node):
        # Store table caption locally and then remove it
        # from the table so that docutils doesn't render it
        # (in the wrong place)
        self.table_caption = self.active_table.caption
        self.active_table.caption = []

        LaTeXTranslator.visit_thead(self, node)

    def depart_thead(self, node):
        LaTeXTranslator.depart_thead(self, node)

    def visit_literal_block(self, node):
        if 'language' in node.attributes:
            # do highlighting
            from pygments import highlight
            from pygments.lexers import PythonLexer, get_lexer_by_name
            from pygments.formatters import LatexFormatter

            linenos = node.attributes.get('linenos', False)
            lexer = get_lexer_by_name(node.attributes['language'])
            tex = highlight(node.astext(), lexer,
                            LatexFormatter(linenos=linenos,
                                           verboptions='fontsize=\\footnotesize'))

            self.out.append(tex)
            raise nodes.SkipNode
        else:
            LaTeXTranslator.visit_literal_block(self, node)

    def depart_literal_block(self, node):
        pass

writer = Writer()
writer.translator_class = Translator
<|endoftext|>


<commit_before># vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

from threading import Lock
from couchdb import http

from couchdb.design import ViewDefinition
from couchdb.http import ResourceNotFound
import couchdb.client

import settings
from documents import DocumentBase
from datetime import datetime
from mangrove.utils import dates
from mangrove.utils.types import is_empty, is_sequence
from mangrove.errors.MangroveException import NoDocumentError, DataObjectNotFound, FailedToSaveDataObject

_dbms = {}
_dbms_lock = Lock()

def get_db_manager(server=None, database=None, credentials=settings.COUCHDB_CREDENTIALS):
    global _dbms
    assert _dbms is not None

    # use defaults if not passed
    srv = (server if server is not None else settings.SERVER)
    db = (database if database is not None else settings.DATABASE)

    k = (srv, db)
    # check if already created and in dict
    if k not in _dbms or _dbms[k] is None:
        with _dbms_lock:
            if k not in _dbms or _dbms[k] is None:
                # nope, create it
                _dbms[k] = DatabaseManager(credentials, server, database)

    return _dbms[k]

def remove_db_manager(dbm):
    global _dbms
    assert isinstance(dbm, DatabaseManager)
    assert dbm in _dbms.values()

    with _dbms_lock:
        try:
            del _dbms[(dbm.url, dbm.database_name)]
        except KeyError, ex:
            print ex
            assert False
            pass  # must have been already deleted

def _delete_db_and_remove_db_manager(dbm):
    """This is really only used for testing purposes."""
    remove_db_manager(dbm)
    if dbm.database_name in dbm.server:
        del dbm.server[dbm.database_name]

class DataObject(object):
    """
    Superclass for all objects that are essentially wrappers of DB
    data.

    There isn't much API contract that needs to be implemented by
    subclasses:

    - They must have a class level member called __document_class__
      that points to the python-couchdb document class that they use
      internally.

    - A constructor where only 'dbm' is required (for initial
      construction)

    - _set_document call which is used to initialize with a new
      python-couchdb document

    - a member self._doc which is used to hold the python-couchdb
      document
    """

    __document_class__ = None

    @classmethod
    def new_from_doc(cls, dbm, doc):
        assert isinstance(doc, cls.__document_class__)
        me = cls(dbm)
        me._set_document(doc)
        return me

    @classmethod
    def get(cls, dbm, id, get_or_create=False):
        return dbm.get(id, cls, get_or_create)

    def __init__(self, dbm):
        self._doc = None
        self._dbm = dbm

    def _set_document(self, document):
        assert isinstance(document, self.__document_class__)
        self._doc = document

    def save(self):
        if self._doc is None:
            raise NoDocumentError('No document to save')
        return self._dbm._save_document(self._doc)

    def delete(self):
        if self.id is not None:
            self._dbm.delete(self)

    def void(self):
        if self._doc is not None:
            self._doc.void = True
            self.save()

    def is_void(self):
        return self._doc.void

    @property
    def id(self):
        return self._doc.id if self._doc is not None else None

    @property
    def created(self):
        return self._doc.created

class View(object):
    def __init__(self, database):
        self.database = database

    def _load_all_rows_in_view(self, **values):
        full_view_name = self.name + '/' + self.name
        print '[DEBUG] loading view: %s' % full_view_name
        start = datetime.now()
        rows = self.database.view(full_view_name, **values).rows
        end = datetime.now()
        delta_t = (end - start)
        print "[DEBUG] --- took %s.%s seconds (%s rows)" %\
              (delta_t.seconds, delta_t.microseconds, len(rows))
        return rows

    def _execute(self, **values):
        return self._load_all_rows_in_view(**values)

    def __getattr__(self, name):
        self.name = name
        return self._execute

class DatabaseManager(object):
    def __init__(self, credentials, server=None, database=None  ):
        """
        Connect to the CouchDB server. If no database name is given,
        use the name provided in the settings
        """

        self.url = (server if server is not None else settings.SERVER)
        self.database_name = database or settings.DATABASE
        self.server = couchdb.client.Server(self.url, session=http.Session(retry_delays=[5, 30]))
        self.server.resource.credentials = credentials
        try:
            self.database = self.server[self.database_name]
        except ResourceNotFound:
            self.database = self.server.create(self.database_name)

        self.view = View(self.database)

    def __unicode__(self):
        return u"Connected on %s - working on %s" % (self.url, self.database_name)

    def __str__(self):
        return unicode(self)

    def __repr__(self):
        return repr(self.database)

    def load_all_rows_in_view(self, view_name, **values):
        full_view_name = view_name + '/' + view_name
        print '[DEBUG] loading view: %s' % full_view_name
        start = datetime.now()
        rows = self.database.view(full_view_name, **values).rows
        end = datetime.now()
        delta_t = (end - start)
        print "[DEBUG] --- took %s.%s seconds (%s rows)" %\
              (delta_t.seconds, delta_t.microseconds, len(rows))
        return rows

    def create_view(self, view_name, map, reduce):
        view_document = view_name # views get their own design doc for the time being
        view = ViewDefinition(view_document, view_name, map, reduce)
        view.sync(self.database)

    def test_views(self):
        self._delete_design_docs()
        self.create_default_views()

    def _get_design_docs(self):
        return [self._load_document(row['id'])
                for row in self.database.view('_all_docs', startkey='_design', endkey='_design0')]

    def _delete_design_docs(self):
        for doc in self._get_design_docs(): del self.database[doc.id]

    def _save_document(self, document, modified=None):
        u"""'Returns document ID''"""
        # TODO: Throw exception if an error
        result = self._save_documents([document], modified)[0]
        # first item is success/failure
        if not result[0]:
            raise FailedToSaveDataObject(str(result))

        # second item is doc ID
        document.post_update(self)
        return result[1]

    def _save_documents(self, documents, modified=None):
        assert is_sequence(documents)
        assert modified is None or isinstance(modified, datetime)
        for doc in documents:
            assert isinstance(doc, DocumentBase)
            doc.modified = (modified if modified is not None else dates.utcnow())

        # TODO: what should we return here? this is what is available from db.update()...
        # The return value of this method is a list containing a tuple for every
        # element in the `documents` sequence. Each tuple is of the form
        # ``(success, docid, rev_or_exc)``, where ``success`` is a boolean
        # indicating whether the update succeeded, ``docid`` is the ID of the
        # document, and ``rev_or_exc`` is either the new document revision, or
        # an exception instance (e.g. `ResourceConflict`) if the update failed.

        # Fix up rev, 'cause bulk update seems not to do that
        results = self.database.update(documents)
        for x in range(len(results)):
            if results[x][0]:
                documents[x]._data['_rev'] = results[x][2]
        return results

    def invalidate(self, uid):
        doc = self._load_document(uid)
        doc.void = True
        self._save_document(doc)

    def _delete_document(self, document):
        self.database.delete(document)

    def _load_document(self, id, document_class=DocumentBase):
        """
        Load a document from the DB into an in memory document object.

        Low level interface does not create wrapping DataObject

        """
        if is_empty(id):
            return None
        else:
            return document_class.load(self.database, id=id)

    def get_many(self, ids, object_class):
        """
        Get many data objects at once.

        Returns a (possibly empty) list of retrieved objects

        """
        assert issubclass(object_class, DataObject)
        assert is_sequence(ids)

        objs = []
        rows = self.database.view('_all_docs', keys=ids, include_docs=True)
        for row in rows:
            if 'error' in row:
                continue
            obj = object_class.new_from_doc(self, object_class.__document_class__.wrap(row.get('doc')))
            objs.append(obj)
        return objs

    def get(self, id, object_class, get_or_create=False):
        """
        Return the object from the database with the given
        id. object_class must be given so we know how to wrap the data
        retrieved from the database. If get_or_create is True and
        there is no object in the database with this id then the
        object will be created.
        """
        assert issubclass(object_class, DataObject)

        many = self.get_many([id], object_class)
        if get_or_create and len(many) == 0:
            # create one 'cause none exists
            doc = object_class.__document_class__(id=id)
            doc.store(self.database)
            many.append(object_class.new_from_doc(self, doc))

        if not len(many):
            raise DataObjectNotFound(dataobject_name=object_class.__name__, param="id", value=id)

        return many[0]

    def delete(self, d_obj):
        """
        Deletes the document associated with the data_obj from the database.

        """
        assert isinstance(d_obj, DataObject)

        if d_obj._doc is None:
            raise NoDocumentError

        id = d_obj._doc.id
        self.database.delete(d_obj._doc)
<commit_msg>Dileep | Adding optional post process option on project update for optimizing processing time<commit_after># vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

from threading import Lock
from couchdb import http

from couchdb.design import ViewDefinition
from couchdb.http import ResourceNotFound
import couchdb.client

import settings
from documents import DocumentBase
from datetime import datetime
from mangrove.utils import dates
from mangrove.utils.types import is_empty, is_sequence
from mangrove.errors.MangroveException import NoDocumentError, DataObjectNotFound, FailedToSaveDataObject

_dbms = {}
_dbms_lock = Lock()

def get_db_manager(server=None, database=None, credentials=settings.COUCHDB_CREDENTIALS):
    global _dbms
    assert _dbms is not None

    # use defaults if not passed
    srv = (server if server is not None else settings.SERVER)
    db = (database if database is not None else settings.DATABASE)

    k = (srv, db)
    # check if already created and in dict
    if k not in _dbms or _dbms[k] is None:
        with _dbms_lock:
            if k not in _dbms or _dbms[k] is None:
                # nope, create it
                _dbms[k] = DatabaseManager(credentials, server, database)

    return _dbms[k]

def remove_db_manager(dbm):
    global _dbms
    assert isinstance(dbm, DatabaseManager)
    assert dbm in _dbms.values()

    with _dbms_lock:
        try:
            del _dbms[(dbm.url, dbm.database_name)]
        except KeyError, ex:
            print ex
            assert False
            pass  # must have been already deleted

def _delete_db_and_remove_db_manager(dbm):
    """This is really only used for testing purposes."""
    remove_db_manager(dbm)
    if dbm.database_name in dbm.server:
        del dbm.server[dbm.database_name]

class DataObject(object):
    """
    Superclass for all objects that are essentially wrappers of DB
    data.

    There isn't much API contract that needs to be implemented by
    subclasses:

    - They must have a class level member called __document_class__
      that points to the python-couchdb document class that they use
      internally.

    - A constructor where only 'dbm' is required (for initial
      construction)

    - _set_document call which is used to initialize with a new
      python-couchdb document

    - a member self._doc which is used to hold the python-couchdb
      document
    """

    __document_class__ = None

    @classmethod
    def new_from_doc(cls, dbm, doc):
        assert isinstance(doc, cls.__document_class__)
        me = cls(dbm)
        me._set_document(doc)
        return me

    @classmethod
    def get(cls, dbm, id, get_or_create=False):
        return dbm.get(id, cls, get_or_create)

    def __init__(self, dbm):
        self._doc = None
        self._dbm = dbm

    def _set_document(self, document):
        assert isinstance(document, self.__document_class__)
        self._doc = document

    def save(self):
        if self._doc is None:
            raise NoDocumentError('No document to save')
        return self._dbm._save_document(self._doc)

    def delete(self):
        if self.id is not None:
            self._dbm.delete(self)

    def void(self):
        if self._doc is not None:
            self._doc.void = True
            self.save()

    def is_void(self):
        return self._doc.void

    @property
    def id(self):
        return self._doc.id if self._doc is not None else None

    @property
    def created(self):
        return self._doc.created

class View(object):
    def __init__(self, database):
        self.database = database

    def _load_all_rows_in_view(self, **values):
        full_view_name = self.name + '/' + self.name
        print '[DEBUG] loading view: %s' % full_view_name
        start = datetime.now()
        rows = self.database.view(full_view_name, **values).rows
        end = datetime.now()
        delta_t = (end - start)
        print "[DEBUG] --- took %s.%s seconds (%s rows)" %\
              (delta_t.seconds, delta_t.microseconds, len(rows))
        return rows

    def _execute(self, **values):
        return self._load_all_rows_in_view(**values)

    def __getattr__(self, name):
        self.name = name
        return self._execute

class DatabaseManager(object):
    def __init__(self, credentials, server=None, database=None  ):
        """
        Connect to the CouchDB server. If no database name is given,
        use the name provided in the settings
        """

        self.url = (server if server is not None else settings.SERVER)
        self.database_name = database or settings.DATABASE
        self.server = couchdb.client.Server(self.url, session=http.Session(retry_delays=[5, 30]))
        self.server.resource.credentials = credentials
        try:
            self.database = self.server[self.database_name]
        except ResourceNotFound:
            self.database = self.server.create(self.database_name)

        self.view = View(self.database)

    def __unicode__(self):
        return u"Connected on %s - working on %s" % (self.url, self.database_name)

    def __str__(self):
        return unicode(self)

    def __repr__(self):
        return repr(self.database)

    def load_all_rows_in_view(self, view_name, **values):
        full_view_name = view_name + '/' + view_name
        print '[DEBUG] loading view: %s' % full_view_name
        start = datetime.now()
        rows = self.database.view(full_view_name, **values).rows
        end = datetime.now()
        delta_t = (end - start)
        print "[DEBUG] --- took %s.%s seconds (%s rows)" %\
              (delta_t.seconds, delta_t.microseconds, len(rows))
        return rows

    def create_view(self, view_name, map, reduce):
        view_document = view_name # views get their own design doc for the time being
        view = ViewDefinition(view_document, view_name, map, reduce)
        view.sync(self.database)

    def test_views(self):
        self._delete_design_docs()
        self.create_default_views()

    def _get_design_docs(self):
        return [self._load_document(row['id'])
                for row in self.database.view('_all_docs', startkey='_design', endkey='_design0')]

    def _delete_design_docs(self):
        for doc in self._get_design_docs(): del self.database[doc.id]

    def _save_document(self, document, modified=None, process_post_update=True):
        u"""'Returns document ID''"""
        # TODO: Throw exception if an error
        result = self._save_documents([document], modified)[0]
        # first item is success/failure
        if not result[0]:
            raise FailedToSaveDataObject(str(result))
        if process_post_update:
            document.post_update(self)
        # second item is doc ID
        return result[1]

    def _save_documents(self, documents, modified=None):
        assert is_sequence(documents)
        assert modified is None or isinstance(modified, datetime)
        for doc in documents:
            assert isinstance(doc, DocumentBase)
            doc.modified = (modified if modified is not None else dates.utcnow())

        # TODO: what should we return here? this is what is available from db.update()...
        # The return value of this method is a list containing a tuple for every
        # element in the `documents` sequence. Each tuple is of the form
        # ``(success, docid, rev_or_exc)``, where ``success`` is a boolean
        # indicating whether the update succeeded, ``docid`` is the ID of the
        # document, and ``rev_or_exc`` is either the new document revision, or
        # an exception instance (e.g. `ResourceConflict`) if the update failed.

        # Fix up rev, 'cause bulk update seems not to do that
        results = self.database.update(documents)
        for x in range(len(results)):
            if results[x][0]:
                documents[x]._data['_rev'] = results[x][2]
        return results

    def invalidate(self, uid):
        doc = self._load_document(uid)
        doc.void = True
        self._save_document(doc)

    def _delete_document(self, document):
        self.database.delete(document)

    def _load_document(self, id, document_class=DocumentBase):
        """
        Load a document from the DB into an in memory document object.

        Low level interface does not create wrapping DataObject

        """
        if is_empty(id):
            return None
        else:
            return document_class.load(self.database, id=id)

    def get_many(self, ids, object_class):
        """
        Get many data objects at once.

        Returns a (possibly empty) list of retrieved objects

        """
        assert issubclass(object_class, DataObject)
        assert is_sequence(ids)

        objs = []
        rows = self.database.view('_all_docs', keys=ids, include_docs=True)
        for row in rows:
            if 'error' in row:
                continue
            obj = object_class.new_from_doc(self, object_class.__document_class__.wrap(row.get('doc')))
            objs.append(obj)
        return objs

    def get(self, id, object_class, get_or_create=False):
        """
        Return the object from the database with the given
        id. object_class must be given so we know how to wrap the data
        retrieved from the database. If get_or_create is True and
        there is no object in the database with this id then the
        object will be created.
        """
        assert issubclass(object_class, DataObject)

        many = self.get_many([id], object_class)
        if get_or_create and len(many) == 0:
            # create one 'cause none exists
            doc = object_class.__document_class__(id=id)
            doc.store(self.database)
            many.append(object_class.new_from_doc(self, doc))

        if not len(many):
            raise DataObjectNotFound(dataobject_name=object_class.__name__, param="id", value=id)

        return many[0]

    def delete(self, d_obj):
        """
        Deletes the document associated with the data_obj from the database.

        """
        assert isinstance(d_obj, DataObject)

        if d_obj._doc is None:
            raise NoDocumentError

        id = d_obj._doc.id
        self.database.delete(d_obj._doc)
<|endoftext|>