示例#1
0
文件: fluidprint.py 项目: djon3s/md
from __future__ import print_function
import __builtin__, sys, contextlib, StringIO
from md import fluid

__all__ = (
    'display', 'read', 'current_input_port', 'current_output_port',
    'output_to_string', 'input_from_string',
    'output_to_file', 'input_from_file'
)

## Default to None.  This let's the cells play more nicely with code
## that changes sys.stdout/sys.stderr directly (like doctest).
## Binding directly to sys.stdout / sys.stderr is more ideal.
CURRENT_OUTPUT_PORT = fluid.cell(None, type=fluid.acquired)
CURRENT_INPUT_PORT = fluid.cell(None, type=fluid.acquired)

def current_output_port():
    return CURRENT_OUTPUT_PORT.value or sys.stdout

def current_input_port():
    return CURRENT_INPUT_PORT.value or sys.stdin

def display(*args, **kwargs):
    kwargs.setdefault('file', current_output_port())
    return __builtin__.print(*args, **kwargs)

def read(*args):
    return current_input_port().read(*args)

@contextlib.contextmanager
def output_to_string(*args):
示例#2
0
    """zippers optionally accept a procedure that returns the email
    address of the current user.  This is the default."""

    return 'Anonymous <*****@*****.**>'


def now():
    """The default method for determining the time a commit is
    made."""

    return time.time()


## The commit message is dynamically parameterized.

MESSAGE = fluid.cell('', type=fluid.private)

message = fluid.accessor(MESSAGE)

## To maintain the logical key/value space, commits use manifests and
## checkpoints use changesets.  A manifest is a complete snapshot of
## the space; a changeset is a delta.


class RefMap(tree):
    @classmethod
    def __adapt__(cls, other):
        if isinstance(obj, Mapping):
            obj = obj.iteritems()
        if not isinstance(obj, basestring) and isinstance(obj, Iterable):
            values = cls.type
示例#3
0
def anonymous():
    """zippers optionally accept a procedure that returns the email
    address of the current user.  This is the default."""

    return 'Anonymous <*****@*****.**>'

def now():
    """The default method for determining the time a commit is
    made."""

    return time.time()

## The commit message is dynamically parameterized.

MESSAGE = fluid.cell('', type=fluid.private)

message = fluid.accessor(MESSAGE)

## To maintain the logical key/value space, commits use manifests and
## checkpoints use changesets.  A manifest is a complete snapshot of
## the space; a changeset is a delta.

class RefMap(tree):

    @classmethod
    def __adapt__(cls, other):
        if isinstance(obj, Mapping):
            obj = obj.iteritems()
        if not isinstance(obj, basestring) and isinstance(obj, Iterable):
            values = cls.type
示例#4
0
from md import fluid
from .. import avro, data

__all__ = (
    'RepoError', 'repository', 'repository_transaction', 'source', 'use',
    'branches', 'make_branch', 'open_branch', 'get_branch', 'save_branch',
    'remove_branch',
    'get', 'find', 'new', 'update', 'delete', 'delta'
)

RepoError = data.RepoError

## The current branch and repository are accessible in the dynamic
## context.  They can be set globally using the init() method.

REPOSITORY = fluid.cell(None, type=fluid.acquired)
repository = fluid.accessor(REPOSITORY)

SOURCE = fluid.cell(None, type=fluid.acquired)
source = fluid.accessor(SOURCE)

def init_api(zs, created=False):
    """Set the global branch; this should be done near the beginning
    of a program.  See load.init()."""

    REPOSITORY.set(zs.open())
    if created:
        with repository_transaction('Make initial branches.'):
            make_branch('live', publish='')
            make_branch('staging', publish='live')
示例#5
0
    if require and not user:
        raise ValueError('User does not exist: %r.' % name)
    return user

def valid_user(obj):
    if isinstance(obj, User):
        return obj
    return get_user(obj, True)

def is_admin(obj):
    return valid_user(obj).admin


### Dynamic Environment

CURRENT_USER = fluid.cell(fluid.UNDEFINED, validate=valid_user, type=fluid.acquired)
user = fluid.accessor(CURRENT_USER)

def set_user(user):
    CURRENT_USER.set(user)
    return user

CURRENT_AUTH = fluid.cell(None, type=fluid.acquired)
authenticator = fluid.accessor(CURRENT_AUTH)

def init_auth(auth=None, service=None, host=None):
    """Initialize the authentication service.  See load.init()."""

    auth = auth or LocalAuth(service, host)
    CURRENT_AUTH.set(auth)
    return auth
示例#6
0
def leaf(obj):
    """Is this object a leaf object?"""

    try:
        return obj.__leaf__()
    except AttributeError:
        ## This is a little weird, but the default value is True
        ## because being a non-leaf node is opt-in.
        return True


### Dynamic Context

# The original input sequence
COLLECTION = fluid.cell(())
collection = fluid.accessor(COLLECTION)

# The currently focused item
FOCUS = fluid.cell()
focus = fluid.accessor(FOCUS)

# The index of the currently focused item.
INDEX = fluid.cell()
index = fluid.accessor(INDEX)


### Top Level

def Path(exprs):
    if isinstance(exprs, Sequence):
示例#7
0

def valid_user(obj):
    if isinstance(obj, User):
        return obj
    return get_user(obj, True)


def is_admin(obj):
    return valid_user(obj).admin


### Dynamic Environment

CURRENT_USER = fluid.cell(fluid.UNDEFINED,
                          validate=valid_user,
                          type=fluid.acquired)
user = fluid.accessor(CURRENT_USER)


def set_user(user):
    CURRENT_USER.set(user)
    return user


CURRENT_AUTH = fluid.cell(None, type=fluid.acquired)
authenticator = fluid.accessor(CURRENT_AUTH)


def init_auth(auth=None, service=None, host=None):
    """Initialize the authentication service.  See load.init()."""
示例#8
0
文件: transaction.py 项目: djon3s/md
def changed():
    return (c.cursor for c in current_journal().changed())


### Journal

class acquire_memory(fluid.acquired):
    def localize(self, loc):
        if isinstance(loc.value, Journal):
            return self.make_location(find_memory(loc.value))
        else:
            return super(acquire_memory, self).localize(loc)

def find_memory(journal):
    while journal.source:
        journal = journal.source
    return journal

JOURNAL = fluid.cell(type=acquire_memory)

current_journal = fluid.accessor(JOURNAL, name='current_journal')

def current_memory():
    return find_memory(current_journal())

def initialize(mem=None):
    journal = JOURNAL.value
    if isinstance(journal, Journal) and not isinstance(journal, Memory):
        raise RuntimeError('Cannot uninitialize a transaction', journal)
    JOURNAL.value = mem or memory()
示例#9
0
"""tests -- unit tests

Copyright (c) 2009, Coptix, Inc.  All rights reserved.
See the LICENSE file for license terms and warranty disclaimer.
"""

from __future__ import absolute_import
import unittest
from md import fluid
from . import *

## Fluid parameters representing configuration values or request
## parameters.
USER = fluid.cell()
PASS = fluid.cell()
SERV = fluid.cell('test-service')
HOST = fluid.cell('example.net')

## User database and authentication.
USERS = {'*****@*****.**': 'baz'}

AUTH = SimpleAuth(DigestMD5Password, USERS, USER.get, PASS.get, SERV.get,
                  HOST.get)


class TestRFC(unittest.TestCase):
    """Test the RFC parser."""
    def setUp(self):
        from . import rfc

        self.headers = rfc.items(min=1,
示例#10
0
from md import fluid

CURRENT_REQUEST = fluid.cell(None, type=fluid.private)
current_request = fluid.accessor(CURRENT_REQUEST, 'request')

class RequestMiddleware(object):
    def process_request(self, request):
	CURRENT_REQUEST.value = request


示例#11
0
def leaf(obj):
    """Is this object a leaf object?"""

    try:
        return obj.__leaf__()
    except AttributeError:
        ## This is a little weird, but the default value is True
        ## because being a non-leaf node is opt-in.
        return True


### Dynamic Context

# The original input sequence
COLLECTION = fluid.cell(())
collection = fluid.accessor(COLLECTION)

# The currently focused item
FOCUS = fluid.cell()
focus = fluid.accessor(FOCUS)

# The index of the currently focused item.
INDEX = fluid.cell()
index = fluid.accessor(INDEX)

### Top Level


def Path(exprs):
    if isinstance(exprs, Sequence):
"""tests -- unit tests

Copyright (c) 2009, Coptix, Inc.  All rights reserved.
See the LICENSE file for license terms and warranty disclaimer.
"""

from __future__ import absolute_import
import unittest
from md import fluid
from . import *

## Fluid parameters representing configuration values or request
## parameters.
USER = fluid.cell()
PASS = fluid.cell()
SERV = fluid.cell('test-service')
HOST = fluid.cell('example.net')

## User database and authentication.
USERS = { '*****@*****.**': 'baz' }

AUTH = SimpleAuth(
    DigestMD5Password, USERS, USER.get, PASS.get, SERV.get, HOST.get
)

class TestRFC(unittest.TestCase):
    """Test the RFC parser."""

    def setUp(self):
        from . import rfc