Skip to content

Commit

Permalink
Clean up context modules. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Feb 9, 2015
1 parent 93db11b commit e59e603
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 63 deletions.
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Context / Registry
.. automodule:: libres.context.registry
:members:

.. automodule:: libres.context.context
.. automodule:: libres.context.core
:members:

.. automodule:: libres.context.session
Expand Down
4 changes: 2 additions & 2 deletions libres/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import unicode_literals

from libres.context import setup_registry
from libres.context.registry import create_default_registry
from libres.db import new_scheduler

registry = setup_registry()
registry = create_default_registry()

__all__ = ['new_scheduler', 'registry']
46 changes: 0 additions & 46 deletions libres/context/__init__.py
Original file line number Diff line number Diff line change
@@ -1,46 +0,0 @@
def setup_registry():

import re

from libres.context.registry import Registry
from libres.context.session import SessionProvider
from libres.context.settings import set_default_settings
from libres.context.exposure import Exposure

from uuid import uuid5 as new_namespace_uuid

registry = Registry()

def session_provider(context):
return SessionProvider(context.get_setting('dsn'))

def email_validator_factory(context):
# A very simple and stupid email validator. It's way too simple, but
# it can be extended to do more powerful checks.
def is_valid_email(email):
return re.match(r'[^@]+@[^@]+\.[^@]+', email)

return is_valid_email

def exposure_factory(context):
return Exposure()

def uuid_generator_factory(context):
def uuid_generator(name):
return new_namespace_uuid(
context.get_setting('uuid_namespace'),
'/'.join((context.name, name))
)
return uuid_generator

master = registry.master_context
master.set_service('email_validator', email_validator_factory)
master.set_service('session_provider', session_provider, cache=True)
master.set_service('exposure', exposure_factory)
master.set_service('uuid_generator', uuid_generator_factory)

set_default_settings(master)

master.lock()

return registry
File renamed without changes.
55 changes: 52 additions & 3 deletions libres/context/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,56 @@
from contextlib import contextmanager

from libres.modules import errors
from libres.context.context import Context
from libres.context.core import Context


def create_default_registry():
""" Creates the default registry for libres. """

import re

from libres.context.registry import Registry
from libres.context.session import SessionProvider
from libres.context.settings import set_default_settings
from libres.context.exposure import Exposure

from uuid import uuid5 as new_namespace_uuid

registry = Registry()

def session_provider(context):
return SessionProvider(context.get_setting('dsn'))

def email_validator_factory(context):
# A very simple and stupid email validator. It's way too simple, but
# it can be extended to do more powerful checks.
def is_valid_email(email):
return re.match(r'[^@]+@[^@]+\.[^@]+', email)

return is_valid_email

def exposure_factory(context):
return Exposure()

def uuid_generator_factory(context):
def uuid_generator(name):
return new_namespace_uuid(
context.get_setting('uuid_namespace'),
'/'.join((context.name, name))
)
return uuid_generator

master = registry.master_context
master.set_service('email_validator', email_validator_factory)
master.set_service('session_provider', session_provider, cache=True)
master.set_service('exposure', exposure_factory)
master.set_service('uuid_generator', uuid_generator_factory)

set_default_settings(master)

master.lock()

return registry


class Registry(object):
Expand All @@ -17,8 +66,8 @@ class Registry(object):
Though if global state is something you need to avoid, you can create
your own version of the registry::
from libres.context import setup_registry
registry = setup_registry()
from libres.context.registry import create_default_registry
registry = create_default_registry()
"""

Expand Down
2 changes: 1 addition & 1 deletion libres/context/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import event

from libres.context.context import StoppableService
from libres.context.core import StoppableService
from libres.modules import errors

SERIALIZABLE = 'SERIALIZABLE'
Expand Down
2 changes: 1 addition & 1 deletion libres/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import timedelta
from itertools import groupby
from libres.context.context import ContextServicesMixin
from libres.context.core import ContextServicesMixin
from libres.context.session import serialized, Serializable
from libres.db.models import Allocation, Reservation, ReservedSlot
from libres.modules import errors, events, calendar
Expand Down
4 changes: 2 additions & 2 deletions libres/db/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta

from libres.context.context import ContextServicesMixin
from libres.context.core import ContextServicesMixin
from libres.context.session import serialized, Serializable
from libres.db.models import ORMBase, Allocation, ReservedSlot, Reservation
from libres.db.queries import Queries
Expand Down Expand Up @@ -29,7 +29,7 @@ def __init__(self, context, name, timezone):
""" Initializeds a new Scheduler instance.
:context:
The :class:`libres.context.context.Context` this scheduler should
The :class:`libres.context.core.Context` this scheduler should
operate on. Acquire a context by using
:func:`libres.context.registry.Registry.register_context`.
Expand Down
14 changes: 7 additions & 7 deletions libres/modules/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __call__(self, *args, **kwargs):
""" Called when an allocation is added, with the following arguments:
:context:
The :class:`libres.context.context.Context` used when adding the
The :class:`libres.context.core.Context` used when adding the
allocations.
:allocations:
Expand All @@ -49,7 +49,7 @@ def __call__(self, *args, **kwargs):
""" Called when a reservation is made, with the following arguments:
:context:
The :class:`libres.context.context.Context` used when adding the
The :class:`libres.context.core.Context` used when adding the
reservation.
:reservations:
Expand All @@ -65,7 +65,7 @@ def __call__(self, *args, **kwargs):
the following arguments:
:context:
The :class:`libres.context.context.Context` used when confirming the
The :class:`libres.context.core.Context` used when confirming the
reservation.
:reservations:
Expand All @@ -80,7 +80,7 @@ def __call__(self, *args, **kwargs):
""" Called when a reservation is approved, with the following arguments:
:context:
The :class:`libres.context.context.Context` used when approving the
The :class:`libres.context.core.Context` used when approving the
reservation.
:reservations:
Expand All @@ -93,7 +93,7 @@ def __call__(self, *args, **kwargs):
""" Called when a reservation is denied, with the following arguments:
:context:
The :class:`libres.context.context.Context` used when denying the
The :class:`libres.context.core.Context` used when denying the
reservation.
:reservations:
Expand All @@ -106,7 +106,7 @@ def __call__(self, *args, **kwargs):
""" Called when a reservation is removed, with the following arguments:
:context:
The :class:`libres.context.context.Context` used when removing the
The :class:`libres.context.core.Context` used when removing the
reservation.
:reservations:
Expand All @@ -119,7 +119,7 @@ def __call__(self, *args, **kwargs):
""" Called when a reservation's time changes , with the following arguments:
:context:
The :class:`libres.context.context.Context` used when changing the
The :class:`libres.context.core.Context` used when changing the
reservation time.
:reservation:
Expand Down

0 comments on commit e59e603

Please sign in to comment.