<commit_before>import structlog

from raiden.exceptions import ChannelIncorrectStateError
from raiden.messages import message_from_sendevent
from raiden.transfer.architecture import Event
from raiden.transfer.events import (
    ContractSendSecretReveal,
    ContractSendChannelClose,
    ContractSendChannelSettle,
    ContractSendChannelUpdateTransfer,
    ContractSendChannelBatchUnlock,
    EventTransferReceivedSuccess,
    EventTransferSentFailed,
    EventTransferSentSuccess,
    SendDirectTransfer,
    SendProcessed,
)
from raiden.transfer.mediated_transfer.events import (
    EventUnlockFailed,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
    SendBalanceProof,
    SendLockedTransfer,
    SendRefundTransfer,
    SendRevealSecret,
    SendSecretRequest,
)
from raiden.transfer.balance_proof import signing_update_data
from raiden.utils import pex
# type alias to avoid both circular dependencies and flake8 errors
RaidenService = 'RaidenService'

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name
UNEVENTFUL_EVENTS = (
    EventTransferReceivedSuccess,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
)

def handle_send_lockedtransfer(
        raiden: RaidenService,
        send_locked_transfer: SendLockedTransfer,
):
    mediated_transfer_message = message_from_sendevent(send_locked_transfer, raiden.address)
    raiden.sign(mediated_transfer_message)
    raiden.transport.send_async(
        mediated_transfer_message.recipient,
        send_locked_transfer.queue_name,
        mediated_transfer_message,
    )

def handle_send_directtransfer(
        raiden: RaidenService,
        send_direct_transfer: SendDirectTransfer,
):
    direct_transfer_message = message_from_sendevent(send_direct_transfer, raiden.address)
    raiden.sign(direct_transfer_message)
    raiden.transport.send_async(
        send_direct_transfer.recipient,
        send_direct_transfer.queue_name,
        direct_transfer_message,
    )

def handle_send_revealsecret(
        raiden: RaidenService,
        reveal_secret_event: SendRevealSecret,
):
    reveal_secret_message = message_from_sendevent(reveal_secret_event, raiden.address)
    raiden.sign(reveal_secret_message)
    raiden.transport.send_async(
        reveal_secret_event.recipient,
        reveal_secret_event.queue_name,
        reveal_secret_message,
    )

def handle_send_balanceproof(
        raiden: RaidenService,
        balance_proof_event: SendBalanceProof,
):
    secret_message = message_from_sendevent(balance_proof_event, raiden.address)
    raiden.sign(secret_message)
    raiden.transport.send_async(
        balance_proof_event.recipient,
        balance_proof_event.queue_name,
        secret_message,
    )

def handle_send_secretrequest(
        raiden: RaidenService,
        secret_request_event: SendSecretRequest,
):
    secret_request_message = message_from_sendevent(secret_request_event, raiden.address)
    raiden.sign(secret_request_message)
    raiden.transport.send_async(
        secret_request_event.recipient,
        secret_request_event.queue_name,
        secret_request_message,
    )

def handle_send_refundtransfer(
        raiden: RaidenService,
        refund_transfer_event: SendRefundTransfer,
):
    refund_transfer_message = message_from_sendevent(refund_transfer_event, raiden.address)
    raiden.sign(refund_transfer_message)
    raiden.transport.send_async(
        refund_transfer_event.recipient,
        refund_transfer_event.queue_name,
        refund_transfer_message,
    )

def handle_send_processed(
        raiden: RaidenService,
        processed_event: SendProcessed,
):
    processed_message = message_from_sendevent(processed_event, raiden.address)
    raiden.sign(processed_message)
    raiden.transport.send_async(
        processed_event.recipient,
        processed_event.queue_name,
        processed_message,
    )

def handle_transfersentsuccess(
        raiden: RaidenService,
        transfer_sent_success_event: EventTransferSentSuccess,
):
    for result in raiden.identifier_to_results[transfer_sent_success_event.identifier]:
        result.set(True)

    del raiden.identifier_to_results[transfer_sent_success_event.identifier]

def handle_transfersentfailed(
        raiden: RaidenService,
        transfer_sent_failed_event: EventTransferSentFailed,
):
    for result in raiden.identifier_to_results[transfer_sent_failed_event.identifier]:
        result.set(False)
    del raiden.identifier_to_results[transfer_sent_failed_event.identifier]

def handle_unlockfailed(
        raiden: RaidenService,
        unlock_failed_event: EventUnlockFailed,
):
    # pylint: disable=unused-argument
    log.error(
        'UnlockFailed!',
        secrethash=pex(unlock_failed_event.secrethash),
        reason=unlock_failed_event.reason,
    )

def handle_contract_send_secretreveal(
        raiden: RaidenService,
        channel_close_event: ContractSendSecretReveal,
):
    raiden.default_secret_registry.register_secret(channel_close_event.secret)

def handle_contract_send_channelclose(
        raiden: RaidenService,
        channel_close_event: ContractSendChannelClose,
):
    balance_proof = channel_close_event.balance_proof

    if balance_proof:
        nonce = balance_proof.nonce
        balance_hash = balance_proof.balance_hash
        signature = balance_proof.signature
        message_hash = balance_proof.message_hash

    else:
        nonce = 0
        balance_hash = b''
        signature = b''
        message_hash = b''

    channel = raiden.chain.payment_channel(
        token_network_address=channel_close_event.token_network_identifier,
        channel_id=channel_close_event.channel_identifier,
    )

    channel.close(
        nonce,
        balance_hash,
        message_hash,
        signature,
    )

def handle_contract_send_channelupdate(
        raiden: RaidenService,
        channel_update_event: ContractSendChannelUpdateTransfer,
):
    balance_proof = channel_update_event.balance_proof

    if balance_proof:
        channel = raiden.chain.payment_channel(
            token_network_address=channel_update_event.token_network_identifier,
            channel_id=channel_update_event.channel_identifier,
        )

        our_signature = signing_update_data(
            balance_proof,
            raiden.chain.network_id,
            raiden.privkey,
        )

        channel.update_transfer(
            balance_proof.nonce,
            balance_proof.balance_hash,
            balance_proof.message_hash,
            balance_proof.signature,
            our_signature,
        )

def handle_contract_send_channelunlock(
        raiden: RaidenService,
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.payment_channel(
        channel_unlock_event.token_network_identifier,
        channel_unlock_event.channel_identifier,
    )
    channel.unlock(channel_unlock_event.merkle_treee_leaves)

def handle_contract_send_channelsettle(
        raiden: RaidenService,
        channel_settle_event: ContractSendChannelSettle,
):
    channel = raiden.chain.payment_channel(
        token_network_address=channel_settle_event.token_network_identifier,
        channel_id=channel_settle_event.channel_identifier,
    )
    our_balance_proof = channel_settle_event.our_balance_proof
    partner_balance_proof = channel_settle_event.partner_balance_proof

    if our_balance_proof:
        our_transferred_amount = our_balance_proof.transferred_amount
        our_locked_amount = our_balance_proof.locked_amount
        our_locksroot = our_balance_proof.locksroot
    else:
        our_transferred_amount = 0
        our_locked_amount = 0
        our_locksroot = b''

    if partner_balance_proof:
        partner_transferred_amount = partner_balance_proof.transferred_amount
        partner_locked_amount = partner_balance_proof.locked_amount
        partner_locksroot = partner_balance_proof.locksroot
    else:
        partner_transferred_amount = 0
        partner_locked_amount = 0
        partner_locksroot = b''

    # The smart contract requires the first balance proof to be the one with
    # the smaller transferred_amount. This is used to simplify overflow checks
    # in the smart contract.
    if our_transferred_amount < partner_transferred_amount:
        first_transferred_amount = partner_transferred_amount
        first_locked_amount = partner_locked_amount
        first_locksroot = partner_locksroot
        second_transferred_amount = our_transferred_amount
        second_locked_amount = our_locked_amount
        second_locksroot = our_locksroot
    else:
        first_transferred_amount = our_transferred_amount
        first_locked_amount = our_locked_amount
        first_locksroot = our_locksroot
        second_transferred_amount = partner_transferred_amount
        second_locked_amount = partner_locked_amount
        second_locksroot = partner_locksroot

    try:
        channel.settle(
            first_transferred_amount,
            first_locked_amount,
            first_locksroot,
            second_transferred_amount,
            second_locked_amount,
            second_locksroot,
        )
    except ChannelIncorrectStateError:
        # Ignoring the exception as there might
        # be a race condition when both nodes try to settle
        # at the same time.
        pass

def on_raiden_event(raiden: RaidenService, event: Event):
    # pylint: disable=too-many-branches

    if type(event) == SendLockedTransfer:
        handle_send_lockedtransfer(raiden, event)
    elif type(event) == SendDirectTransfer:
        handle_send_directtransfer(raiden, event)
    elif type(event) == SendRevealSecret:
        handle_send_revealsecret(raiden, event)
    elif type(event) == SendBalanceProof:
        handle_send_balanceproof(raiden, event)
    elif type(event) == SendSecretRequest:
        handle_send_secretrequest(raiden, event)
    elif type(event) == SendRefundTransfer:
        handle_send_refundtransfer(raiden, event)
    elif type(event) == SendProcessed:
        handle_send_processed(raiden, event)
    elif type(event) == EventTransferSentSuccess:
        handle_transfersentsuccess(raiden, event)
    elif type(event) == EventTransferSentFailed:
        handle_transfersentfailed(raiden, event)
    elif type(event) == EventUnlockFailed:
        handle_unlockfailed(raiden, event)
    elif type(event) == ContractSendSecretReveal:
        handle_contract_send_secretreveal(raiden, event)
    elif type(event) == ContractSendChannelClose:
        handle_contract_send_channelclose(raiden, event)
    elif type(event) == ContractSendChannelUpdateTransfer:
        handle_contract_send_channelupdate(raiden, event)
    elif type(event) == ContractSendChannelBatchUnlock:
        handle_contract_send_channelunlock(raiden, event)
    elif type(event) == ContractSendChannelSettle:
        handle_contract_send_channelsettle(raiden, event)
    elif type(event) in UNEVENTFUL_EVENTS:
        pass
    else:
        log.error('Unknown event {}'.format(type(event)))
<commit_msg>Fixed the call to settle<commit_after>import structlog

from raiden.exceptions import ChannelIncorrectStateError
from raiden.messages import message_from_sendevent
from raiden.transfer.architecture import Event
from raiden.transfer.events import (
    ContractSendSecretReveal,
    ContractSendChannelClose,
    ContractSendChannelSettle,
    ContractSendChannelUpdateTransfer,
    ContractSendChannelBatchUnlock,
    EventTransferReceivedSuccess,
    EventTransferSentFailed,
    EventTransferSentSuccess,
    SendDirectTransfer,
    SendProcessed,
)
from raiden.transfer.mediated_transfer.events import (
    EventUnlockFailed,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
    SendBalanceProof,
    SendLockedTransfer,
    SendRefundTransfer,
    SendRevealSecret,
    SendSecretRequest,
)
from raiden.transfer.balance_proof import signing_update_data
from raiden.utils import pex
# type alias to avoid both circular dependencies and flake8 errors
RaidenService = 'RaidenService'

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name
UNEVENTFUL_EVENTS = (
    EventTransferReceivedSuccess,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
)

def handle_send_lockedtransfer(
        raiden: RaidenService,
        send_locked_transfer: SendLockedTransfer,
):
    mediated_transfer_message = message_from_sendevent(send_locked_transfer, raiden.address)
    raiden.sign(mediated_transfer_message)
    raiden.transport.send_async(
        mediated_transfer_message.recipient,
        send_locked_transfer.queue_name,
        mediated_transfer_message,
    )

def handle_send_directtransfer(
        raiden: RaidenService,
        send_direct_transfer: SendDirectTransfer,
):
    direct_transfer_message = message_from_sendevent(send_direct_transfer, raiden.address)
    raiden.sign(direct_transfer_message)
    raiden.transport.send_async(
        send_direct_transfer.recipient,
        send_direct_transfer.queue_name,
        direct_transfer_message,
    )

def handle_send_revealsecret(
        raiden: RaidenService,
        reveal_secret_event: SendRevealSecret,
):
    reveal_secret_message = message_from_sendevent(reveal_secret_event, raiden.address)
    raiden.sign(reveal_secret_message)
    raiden.transport.send_async(
        reveal_secret_event.recipient,
        reveal_secret_event.queue_name,
        reveal_secret_message,
    )

def handle_send_balanceproof(
        raiden: RaidenService,
        balance_proof_event: SendBalanceProof,
):
    secret_message = message_from_sendevent(balance_proof_event, raiden.address)
    raiden.sign(secret_message)
    raiden.transport.send_async(
        balance_proof_event.recipient,
        balance_proof_event.queue_name,
        secret_message,
    )

def handle_send_secretrequest(
        raiden: RaidenService,
        secret_request_event: SendSecretRequest,
):
    secret_request_message = message_from_sendevent(secret_request_event, raiden.address)
    raiden.sign(secret_request_message)
    raiden.transport.send_async(
        secret_request_event.recipient,
        secret_request_event.queue_name,
        secret_request_message,
    )

def handle_send_refundtransfer(
        raiden: RaidenService,
        refund_transfer_event: SendRefundTransfer,
):
    refund_transfer_message = message_from_sendevent(refund_transfer_event, raiden.address)
    raiden.sign(refund_transfer_message)
    raiden.transport.send_async(
        refund_transfer_event.recipient,
        refund_transfer_event.queue_name,
        refund_transfer_message,
    )

def handle_send_processed(
        raiden: RaidenService,
        processed_event: SendProcessed,
):
    processed_message = message_from_sendevent(processed_event, raiden.address)
    raiden.sign(processed_message)
    raiden.transport.send_async(
        processed_event.recipient,
        processed_event.queue_name,
        processed_message,
    )

def handle_transfersentsuccess(
        raiden: RaidenService,
        transfer_sent_success_event: EventTransferSentSuccess,
):
    for result in raiden.identifier_to_results[transfer_sent_success_event.identifier]:
        result.set(True)

    del raiden.identifier_to_results[transfer_sent_success_event.identifier]

def handle_transfersentfailed(
        raiden: RaidenService,
        transfer_sent_failed_event: EventTransferSentFailed,
):
    for result in raiden.identifier_to_results[transfer_sent_failed_event.identifier]:
        result.set(False)
    del raiden.identifier_to_results[transfer_sent_failed_event.identifier]

def handle_unlockfailed(
        raiden: RaidenService,
        unlock_failed_event: EventUnlockFailed,
):
    # pylint: disable=unused-argument
    log.error(
        'UnlockFailed!',
        secrethash=pex(unlock_failed_event.secrethash),
        reason=unlock_failed_event.reason,
    )

def handle_contract_send_secretreveal(
        raiden: RaidenService,
        channel_close_event: ContractSendSecretReveal,
):
    raiden.default_secret_registry.register_secret(channel_close_event.secret)

def handle_contract_send_channelclose(
        raiden: RaidenService,
        channel_close_event: ContractSendChannelClose,
):
    balance_proof = channel_close_event.balance_proof

    if balance_proof:
        nonce = balance_proof.nonce
        balance_hash = balance_proof.balance_hash
        signature = balance_proof.signature
        message_hash = balance_proof.message_hash

    else:
        nonce = 0
        balance_hash = b''
        signature = b''
        message_hash = b''

    channel = raiden.chain.payment_channel(
        token_network_address=channel_close_event.token_network_identifier,
        channel_id=channel_close_event.channel_identifier,
    )

    channel.close(
        nonce,
        balance_hash,
        message_hash,
        signature,
    )

def handle_contract_send_channelupdate(
        raiden: RaidenService,
        channel_update_event: ContractSendChannelUpdateTransfer,
):
    balance_proof = channel_update_event.balance_proof

    if balance_proof:
        channel = raiden.chain.payment_channel(
            token_network_address=channel_update_event.token_network_identifier,
            channel_id=channel_update_event.channel_identifier,
        )

        our_signature = signing_update_data(
            balance_proof,
            raiden.chain.network_id,
            raiden.privkey,
        )

        channel.update_transfer(
            balance_proof.nonce,
            balance_proof.balance_hash,
            balance_proof.message_hash,
            balance_proof.signature,
            our_signature,
        )

def handle_contract_send_channelunlock(
        raiden: RaidenService,
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.payment_channel(
        channel_unlock_event.token_network_identifier,
        channel_unlock_event.channel_identifier,
    )
    channel.unlock(channel_unlock_event.merkle_treee_leaves)

def handle_contract_send_channelsettle(
        raiden: RaidenService,
        channel_settle_event: ContractSendChannelSettle,
):
    channel = raiden.chain.payment_channel(
        token_network_address=channel_settle_event.token_network_identifier,
        channel_id=channel_settle_event.channel_identifier,
    )
    our_balance_proof = channel_settle_event.our_balance_proof
    partner_balance_proof = channel_settle_event.partner_balance_proof

    if our_balance_proof:
        our_transferred_amount = our_balance_proof.transferred_amount
        our_locked_amount = our_balance_proof.locked_amount
        our_locksroot = our_balance_proof.locksroot
    else:
        our_transferred_amount = 0
        our_locked_amount = 0
        our_locksroot = b''

    if partner_balance_proof:
        partner_transferred_amount = partner_balance_proof.transferred_amount
        partner_locked_amount = partner_balance_proof.locked_amount
        partner_locksroot = partner_balance_proof.locksroot
    else:
        partner_transferred_amount = 0
        partner_locked_amount = 0
        partner_locksroot = b''

    our_max_transferred = our_transferred_amount + our_locked_amount
    partner_max_transferred = partner_transferred_amount + partner_locked_amount

    # The smart contract requires the max transferred of the /first/ balance
    # proof to be /smaller/.
    if our_max_transferred < partner_max_transferred:
        first_transferred_amount = our_transferred_amount
        first_locked_amount = our_locked_amount
        first_locksroot = our_locksroot
        second_transferred_amount = partner_transferred_amount
        second_locked_amount = partner_locked_amount
        second_locksroot = partner_locksroot
    else:
        first_transferred_amount = partner_transferred_amount
        first_locked_amount = partner_locked_amount
        first_locksroot = partner_locksroot
        second_transferred_amount = our_transferred_amount
        second_locked_amount = our_locked_amount
        second_locksroot = our_locksroot

    try:
        channel.settle(
            first_transferred_amount,
            first_locked_amount,
            first_locksroot,
            second_transferred_amount,
            second_locked_amount,
            second_locksroot,
        )
    except ChannelIncorrectStateError:
        # Ignoring the exception as there might
        # be a race condition when both nodes try to settle
        # at the same time.
        pass

def on_raiden_event(raiden: RaidenService, event: Event):
    # pylint: disable=too-many-branches

    if type(event) == SendLockedTransfer:
        handle_send_lockedtransfer(raiden, event)
    elif type(event) == SendDirectTransfer:
        handle_send_directtransfer(raiden, event)
    elif type(event) == SendRevealSecret:
        handle_send_revealsecret(raiden, event)
    elif type(event) == SendBalanceProof:
        handle_send_balanceproof(raiden, event)
    elif type(event) == SendSecretRequest:
        handle_send_secretrequest(raiden, event)
    elif type(event) == SendRefundTransfer:
        handle_send_refundtransfer(raiden, event)
    elif type(event) == SendProcessed:
        handle_send_processed(raiden, event)
    elif type(event) == EventTransferSentSuccess:
        handle_transfersentsuccess(raiden, event)
    elif type(event) == EventTransferSentFailed:
        handle_transfersentfailed(raiden, event)
    elif type(event) == EventUnlockFailed:
        handle_unlockfailed(raiden, event)
    elif type(event) == ContractSendSecretReveal:
        handle_contract_send_secretreveal(raiden, event)
    elif type(event) == ContractSendChannelClose:
        handle_contract_send_channelclose(raiden, event)
    elif type(event) == ContractSendChannelUpdateTransfer:
        handle_contract_send_channelupdate(raiden, event)
    elif type(event) == ContractSendChannelBatchUnlock:
        handle_contract_send_channelunlock(raiden, event)
    elif type(event) == ContractSendChannelSettle:
        handle_contract_send_channelsettle(raiden, event)
    elif type(event) in UNEVENTFUL_EVENTS:
        pass
    else:
        log.error('Unknown event {}'.format(type(event)))
<|endoftext|>


<commit_before>from nd2reader.model import Image
import numpy as np

class ImageStack(object):
    """
    This is a model that combines multiple ND2s into one logical image set. Other file types may be used if they are in
    an object that exposes the same interface as nd2reader.

    """
    def __init__(self):
        self._image_sets = {}
        self._image_lookup_table = {}
        self._groups = {}
        self._registration_corrector = None
        self._rotation_corrector = None

    def set_offsets(self, registration=None, rotation=None):
        self._registration_corrector = registration
        self._rotation_corrector = rotation

    def add(self, image_set):
        """
        Adds a set of images to the virtual image stack. Image sets are typically a single ND2 file.
        The order that sets are added determines the order of the final stack!

        """
        # ensure the image set is valid
        if not (hasattr(image_set, "__getitem__") and hasattr(image_set, "__len__")):
            raise TypeError("You tried to add an image set to the ImageStack,"
                            " but it doesn't provide the right interface (__getitem__ and __len__)")
        # we need a new slot in our _image_sets dictionary for the new image set we've just been given
        # since we want the last index plus one, we can just use the length of _image_sets
        image_set_index = len(self._image_sets)
        # we need to know how many total images we already have, so when we index the new images in the new image set,
        # we can start from the next available index
        image_count = len(self._image_lookup_table)
        # We will keep track of groups of images. At each field of view, images with unique combinations of channel and
        # focus are taken together. Then we move on to another field of view, which is considered another group.
        # If we only have one field of view, a new group starts when an image has the same channel and focus as one in
        # the current group.
        for n in range(len(image_set)):
            # for each image, we create a new global index number, and map that to the particular image set it came
            # from and its local index number
            self._image_lookup_table[image_count + n] = (image_set_index, n)
        # here we just store the image set so we can access the images from it
        self._image_sets[image_set_index] = image_set

    @property
    def frame_count(self):
        return sum([len(image_set.frames) for image_set in self._image_sets.values()])

    @property
    def field_of_view_count(self):
        return len(self._image_sets[0].fields_of_view)

    def __len__(self) -> int:
        """ The number of total images there are in all the image sets. """
        return len(self._image_lookup_table)

    def _correct(self, image: Image) -> np.ndarray:
        # the correctors return numpy arrays so we need to capture this information before any transformations are done
        field_of_view = image.field_of_view
        frame_number = image.frame_number
        if self._registration_corrector is not None:
            image = self._registration_corrector.align(image, field_of_view, frame_number)
        if self._rotation_corrector is not None:
            image = self._rotation_corrector.rotate(image, field_of_view)
        return image

    def __getitem__(self, index):
        if not isinstance(index, int):
            raise TypeError("ImageStack uses integer indexes only.")
        if not 0 <= index < len(self):
            raise IndexError("Out of bounds index access for ImageStack")
        # We have several sets of images, each indexed from 0 to some arbitrary number. We need to figure out which
        # image set the given index maps to which file. Once we know the image set, we also need to know which image
        # we should use.
        image_set_index, image_index = self._image_lookup_table[index]
        return self._correct(self._image_sets[image_set_index][image_index])

    def select(self, fields_of_view: int=None, z_levels: int=None, channels: str=None):
        """ Returns an iterator over images that meet the given criteria. """
        for _, image_set in sorted(self._image_sets.items()):
            for image in image_set.select(fields_of_view=fields_of_view, z_levels=z_levels, channels=channels):
                yield self._correct(image)
<commit_msg>ImageStack always returns an Image object now, and never an ndarray<commit_after>from nd2reader.model import Image

class ImageStack(object):
    """
    This is a model that combines multiple ND2s into one logical image set. Other file types may be used if they are in
    an object that exposes the same interface as nd2reader.

    """
    def __init__(self):
        self._image_sets = {}
        self._image_lookup_table = {}
        self._groups = {}
        self._registration_corrector = None
        self._rotation_corrector = None

    def set_offsets(self, registration=None, rotation=None):
        self._registration_corrector = registration
        self._rotation_corrector = rotation

    def add(self, image_set):
        """
        Adds a set of images to the virtual image stack. Image sets are typically a single ND2 file.
        The order that sets are added determines the order of the final stack!

        """
        # ensure the image set is valid
        if not (hasattr(image_set, "__getitem__") and hasattr(image_set, "__len__")):
            raise TypeError("You tried to add an image set to the ImageStack,"
                            " but it doesn't provide the right interface (__getitem__ and __len__)")
        # we need a new slot in our _image_sets dictionary for the new image set we've just been given
        # since we want the last index plus one, we can just use the length of _image_sets
        image_set_index = len(self._image_sets)
        # we need to know how many total images we already have, so when we index the new images in the new image set,
        # we can start from the next available index
        image_count = len(self._image_lookup_table)
        # We will keep track of groups of images. At each field of view, images with unique combinations of channel and
        # focus are taken together. Then we move on to another field of view, which is considered another group.
        # If we only have one field of view, a new group starts when an image has the same channel and focus as one in
        # the current group.
        for n in range(len(image_set)):
            # for each image, we create a new global index number, and map that to the particular image set it came
            # from and its local index number
            self._image_lookup_table[image_count + n] = (image_set_index, n)
        # here we just store the image set so we can access the images from it
        self._image_sets[image_set_index] = image_set

    @property
    def frame_count(self):
        return sum([len(image_set.frames) for image_set in self._image_sets.values()])

    @property
    def field_of_view_count(self):
        return len(self._image_sets[0].fields_of_view)

    def __len__(self) -> int:
        """ The number of total images there are in all the image sets. """
        return len(self._image_lookup_table)

    def _correct(self, image: Image) -> Image:
        """ Translates and rotates an image, if necessary. """
        if self._registration_corrector is None and self._rotation_corrector is None:
            return image
        # the correctors return numpy arrays so we need to capture this information before any transformations are done
        field_of_view = image.field_of_view
        frame_number = image.frame_number
        timestamp = image.timestamp
        index = image.index
        channel = image.channel
        z_level = image.z_level

        # make the corrections
        if self._registration_corrector is not None:
            image = self._registration_corrector.align(image, field_of_view, frame_number)
        if self._rotation_corrector is not None:
            image = self._rotation_corrector.rotate(image, field_of_view)
        corrected_image = Image(image)
        # add the metadata back
        corrected_image.add_params(index, timestamp, frame_number, field_of_view, channel, z_level)
        return corrected_image

    def __getitem__(self, index):
        if not isinstance(index, int):
            raise TypeError("ImageStack uses integer indexes only.")
        if not 0 <= index < len(self):
            raise IndexError("Out of bounds index access for ImageStack")
        # We have several sets of images, each indexed from 0 to some arbitrary number. We need to figure out which
        # image set the given index maps to which file. Once we know the image set, we also need to know which image
        # we should use.
        image_set_index, image_index = self._image_lookup_table[index]
        return self._correct(self._image_sets[image_set_index][image_index])

    def select(self, fields_of_view: int=None, z_levels: int=None, channels: str=None):
        """ Returns an iterator over images that meet the given criteria. """
        for _, image_set in sorted(self._image_sets.items()):
            for image in image_set.select(fields_of_view=fields_of_view, z_levels=z_levels, channels=channels):
                yield self._correct(image)
<|endoftext|>


<commit_before>import structlog

from raiden.messages import (
    message_from_sendevent,
    Lock,
)
from raiden.transfer.architecture import Event
from raiden.transfer.events import (
    ContractSendSecretReveal,
    ContractSendChannelClose,
    ContractSendChannelSettle,
    ContractSendChannelUpdateTransfer,
    ContractSendChannelBatchUnlock,
    EventTransferReceivedSuccess,
    EventTransferSentFailed,
    EventTransferSentSuccess,
    SendDirectTransfer,
    SendProcessed,
)
from raiden.transfer.mediated_transfer.events import (
    EventUnlockFailed,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
    SendBalanceProof,
    SendLockedTransfer,
    SendRefundTransfer,
    SendRevealSecret,
    SendSecretRequest,
)
from raiden.utils import pex
# type alias to avoid both circular dependencies and flake8 errors
RaidenService = 'RaidenService'

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name
UNEVENTFUL_EVENTS = (
    EventTransferReceivedSuccess,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
)

def handle_send_lockedtransfer(
        raiden: RaidenService,
        send_locked_transfer: SendLockedTransfer,
):
    mediated_transfer_message = message_from_sendevent(send_locked_transfer, raiden.address)
    raiden.sign(mediated_transfer_message)
    raiden.transport.send_async(
        mediated_transfer_message.recipient,
        send_locked_transfer.queue_name,
        mediated_transfer_message,
    )

def handle_send_directtransfer(
        raiden: RaidenService,
        send_direct_transfer: SendDirectTransfer,
):
    direct_transfer_message = message_from_sendevent(send_direct_transfer, raiden.address)
    raiden.sign(direct_transfer_message)
    raiden.transport.send_async(
        send_direct_transfer.recipient,
        send_direct_transfer.queue_name,
        direct_transfer_message,
    )

def handle_send_revealsecret(
        raiden: RaidenService,
        reveal_secret_event: SendRevealSecret,
):
    reveal_secret_message = message_from_sendevent(reveal_secret_event, raiden.address)
    raiden.sign(reveal_secret_message)
    raiden.transport.send_async(
        reveal_secret_event.recipient,
        reveal_secret_event.queue_name,
        reveal_secret_message,
    )

def handle_send_balanceproof(
        raiden: RaidenService,
        balance_proof_event: SendBalanceProof,
):
    secret_message = message_from_sendevent(balance_proof_event, raiden.address)
    raiden.sign(secret_message)
    raiden.transport.send_async(
        balance_proof_event.recipient,
        balance_proof_event.queue_name,
        secret_message,
    )

def handle_send_secretrequest(
        raiden: RaidenService,
        secret_request_event: SendSecretRequest,
):
    secret_request_message = message_from_sendevent(secret_request_event, raiden.address)
    raiden.sign(secret_request_message)
    raiden.transport.send_async(
        secret_request_event.recipient,
        secret_request_event.queue_name,
        secret_request_message,
    )

def handle_send_refundtransfer(
        raiden: RaidenService,
        refund_transfer_event: SendRefundTransfer,
):
    refund_transfer_message = message_from_sendevent(refund_transfer_event, raiden.address)
    raiden.sign(refund_transfer_message)
    raiden.transport.send_async(
        refund_transfer_event.recipient,
        refund_transfer_event.queue_name,
        refund_transfer_message,
    )

def handle_send_processed(
        raiden: RaidenService,
        processed_event: SendProcessed,
):
    processed_message = message_from_sendevent(processed_event, raiden.address)
    raiden.sign(processed_message)
    raiden.transport.send_async(
        processed_event.recipient,
        processed_event.queue_name,
        processed_message,
    )

def handle_transfersentsuccess(
        raiden: RaidenService,
        transfer_sent_success_event: EventTransferSentSuccess,
):
    for result in raiden.identifier_to_results[transfer_sent_success_event.identifier]:
        result.set(True)

    del raiden.identifier_to_results[transfer_sent_success_event.identifier]

def handle_transfersentfailed(
        raiden: RaidenService,
        transfer_sent_failed_event: EventTransferSentFailed,
):
    for result in raiden.identifier_to_results[transfer_sent_failed_event.identifier]:
        result.set(False)
    del raiden.identifier_to_results[transfer_sent_failed_event.identifier]

def handle_unlockfailed(
        raiden: RaidenService,
        unlock_failed_event: EventUnlockFailed,
):
    # pylint: disable=unused-argument
    log.error(
        'UnlockFailed!',
        secrethash=pex(unlock_failed_event.secrethash),
        reason=unlock_failed_event.reason,
    )

def handle_contract_send_secretreveal(
        raiden: 'RaidenService',
        channel_close_event: ContractSendSecretReveal,
):
    raiden.default_secret_registry.register_secret(channel_close_event.secret)

def handle_contract_send_channelclose(
        raiden: RaidenService,
        channel_close_event: ContractSendChannelClose,
):
    balance_proof = channel_close_event.balance_proof

    if balance_proof:
        nonce = balance_proof.nonce
        transferred_amount = balance_proof.transferred_amount
        locked_amount = balance_proof.locked_amount
        locksroot = balance_proof.locksroot
        signature = balance_proof.signature
        message_hash = balance_proof.message_hash

    else:
        nonce = 0
        transferred_amount = 0
        locked_amount = 0
        locksroot = b''
        signature = b''
        message_hash = b''

    channel = raiden.chain.netting_channel(channel_close_event.channel_identifier)

    channel.close(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        message_hash,
        signature,
    )

def handle_contract_send_channelupdate(
        raiden: RaidenService,
        channel_update_event: ContractSendChannelUpdateTransfer,
):
    balance_proof = channel_update_event.balance_proof

    if balance_proof:
        channel = raiden.chain.netting_channel(channel_update_event.channel_identifier)
        channel.update_transfer(
            balance_proof.nonce,
            balance_proof.transferred_amount,
            balance_proof.locked_amount,
            balance_proof.locksroot,
            balance_proof.message_hash,
            balance_proof.signature,
        )

def handle_contract_send_channelunlock(
        raiden: RaidenService,
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.netting_channel(channel_unlock_event.channel_identifier)
    block_number = raiden.get_block_number()

    for unlock_proof in channel_unlock_event.unlock_proofs:
        lock = Lock.from_bytes(unlock_proof.lock_encoded)

        if lock.expiration < block_number:
            log.error('Lock has expired!', lock=lock)
        else:
            channel.unlock(unlock_proof)

def handle_contract_send_channelunlock2(
        raiden: 'RaidenService',
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.netting_channel(channel_unlock_event.channel_identifier)

    channel.unlock(channel_unlock_event.unlock_proofs)

def handle_contract_send_channelsettle(
        raiden: RaidenService,
        channel_settle_event: ContractSendChannelSettle,
):
    channel = raiden.chain.netting_channel(channel_settle_event.channel_identifier)
    channel.settle()

def on_raiden_event(raiden: RaidenService, event: Event):
    # pylint: disable=too-many-branches

    if type(event) == SendLockedTransfer:
        handle_send_lockedtransfer(raiden, event)
    elif type(event) == SendDirectTransfer:
        handle_send_directtransfer(raiden, event)
    elif type(event) == SendRevealSecret:
        handle_send_revealsecret(raiden, event)
    elif type(event) == SendBalanceProof:
        handle_send_balanceproof(raiden, event)
    elif type(event) == SendSecretRequest:
        handle_send_secretrequest(raiden, event)
    elif type(event) == SendRefundTransfer:
        handle_send_refundtransfer(raiden, event)
    elif type(event) == SendProcessed:
        handle_send_processed(raiden, event)
    elif type(event) == EventTransferSentSuccess:
        handle_transfersentsuccess(raiden, event)
    elif type(event) == EventTransferSentFailed:
        handle_transfersentfailed(raiden, event)
    elif type(event) == EventUnlockFailed:
        handle_unlockfailed(raiden, event)
    elif type(event) == ContractSendSecretReveal:
        handle_contract_send_secretreveal(raiden, event)
    elif type(event) == ContractSendChannelClose:
        handle_contract_send_channelclose(raiden, event)
    elif type(event) == ContractSendChannelUpdateTransfer:
        handle_contract_send_channelupdate(raiden, event)
    elif type(event) == ContractSendChannelBatchUnlock:
        handle_contract_send_channelunlock(raiden, event)
    elif type(event) == ContractSendChannelSettle:
        handle_contract_send_channelsettle(raiden, event)
    elif type(event) in UNEVENTFUL_EVENTS:
        pass
    else:
        log.error('Unknown event {}'.format(type(event)))
<commit_msg>New handle close channel - handle_contract_send_channelclose2<commit_after>import structlog

from raiden.messages import (
    message_from_sendevent,
    Lock,
)
from raiden.transfer.architecture import Event
from raiden.transfer.events import (
    ContractSendSecretReveal,
    ContractSendChannelClose,
    ContractSendChannelSettle,
    ContractSendChannelUpdateTransfer,
    ContractSendChannelBatchUnlock,
    EventTransferReceivedSuccess,
    EventTransferSentFailed,
    EventTransferSentSuccess,
    SendDirectTransfer,
    SendProcessed,
)
from raiden.transfer.mediated_transfer.events import (
    EventUnlockFailed,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
    SendBalanceProof,
    SendLockedTransfer,
    SendRefundTransfer,
    SendRevealSecret,
    SendSecretRequest,
)
from raiden.transfer.balance_proof import hash_balance_data
from raiden.utils import pex
# type alias to avoid both circular dependencies and flake8 errors
RaidenService = 'RaidenService'

log = structlog.get_logger(__name__)  # pylint: disable=invalid-name
UNEVENTFUL_EVENTS = (
    EventTransferReceivedSuccess,
    EventUnlockSuccess,
    EventUnlockClaimFailed,
    EventUnlockClaimSuccess,
)

def handle_send_lockedtransfer(
        raiden: RaidenService,
        send_locked_transfer: SendLockedTransfer,
):
    mediated_transfer_message = message_from_sendevent(send_locked_transfer, raiden.address)
    raiden.sign(mediated_transfer_message)
    raiden.transport.send_async(
        mediated_transfer_message.recipient,
        send_locked_transfer.queue_name,
        mediated_transfer_message,
    )

def handle_send_directtransfer(
        raiden: RaidenService,
        send_direct_transfer: SendDirectTransfer,
):
    direct_transfer_message = message_from_sendevent(send_direct_transfer, raiden.address)
    raiden.sign(direct_transfer_message)
    raiden.transport.send_async(
        send_direct_transfer.recipient,
        send_direct_transfer.queue_name,
        direct_transfer_message,
    )

def handle_send_revealsecret(
        raiden: RaidenService,
        reveal_secret_event: SendRevealSecret,
):
    reveal_secret_message = message_from_sendevent(reveal_secret_event, raiden.address)
    raiden.sign(reveal_secret_message)
    raiden.transport.send_async(
        reveal_secret_event.recipient,
        reveal_secret_event.queue_name,
        reveal_secret_message,
    )

def handle_send_balanceproof(
        raiden: RaidenService,
        balance_proof_event: SendBalanceProof,
):
    secret_message = message_from_sendevent(balance_proof_event, raiden.address)
    raiden.sign(secret_message)
    raiden.transport.send_async(
        balance_proof_event.recipient,
        balance_proof_event.queue_name,
        secret_message,
    )

def handle_send_secretrequest(
        raiden: RaidenService,
        secret_request_event: SendSecretRequest,
):
    secret_request_message = message_from_sendevent(secret_request_event, raiden.address)
    raiden.sign(secret_request_message)
    raiden.transport.send_async(
        secret_request_event.recipient,
        secret_request_event.queue_name,
        secret_request_message,
    )

def handle_send_refundtransfer(
        raiden: RaidenService,
        refund_transfer_event: SendRefundTransfer,
):
    refund_transfer_message = message_from_sendevent(refund_transfer_event, raiden.address)
    raiden.sign(refund_transfer_message)
    raiden.transport.send_async(
        refund_transfer_event.recipient,
        refund_transfer_event.queue_name,
        refund_transfer_message,
    )

def handle_send_processed(
        raiden: RaidenService,
        processed_event: SendProcessed,
):
    processed_message = message_from_sendevent(processed_event, raiden.address)
    raiden.sign(processed_message)
    raiden.transport.send_async(
        processed_event.recipient,
        processed_event.queue_name,
        processed_message,
    )

def handle_transfersentsuccess(
        raiden: RaidenService,
        transfer_sent_success_event: EventTransferSentSuccess,
):
    for result in raiden.identifier_to_results[transfer_sent_success_event.identifier]:
        result.set(True)

    del raiden.identifier_to_results[transfer_sent_success_event.identifier]

def handle_transfersentfailed(
        raiden: RaidenService,
        transfer_sent_failed_event: EventTransferSentFailed,
):
    for result in raiden.identifier_to_results[transfer_sent_failed_event.identifier]:
        result.set(False)
    del raiden.identifier_to_results[transfer_sent_failed_event.identifier]

def handle_unlockfailed(
        raiden: RaidenService,
        unlock_failed_event: EventUnlockFailed,
):
    # pylint: disable=unused-argument
    log.error(
        'UnlockFailed!',
        secrethash=pex(unlock_failed_event.secrethash),
        reason=unlock_failed_event.reason,
    )

def handle_contract_send_secretreveal(
        raiden: 'RaidenService',
        channel_close_event: ContractSendSecretReveal,
):
    raiden.default_secret_registry.register_secret(channel_close_event.secret)

def handle_contract_send_channelclose(
        raiden: RaidenService,
        channel_close_event: ContractSendChannelClose,
):
    balance_proof = channel_close_event.balance_proof

    if balance_proof:
        nonce = balance_proof.nonce
        transferred_amount = balance_proof.transferred_amount
        locked_amount = balance_proof.locked_amount
        locksroot = balance_proof.locksroot
        signature = balance_proof.signature
        message_hash = balance_proof.message_hash

    else:
        nonce = 0
        transferred_amount = 0
        locked_amount = 0
        locksroot = b''
        signature = b''
        message_hash = b''

    channel = raiden.chain.netting_channel(channel_close_event.channel_identifier)

    channel.close(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        message_hash,
        signature,
    )

def handle_contract_send_channelclose2(
        raiden: RaidenService,
        channel_close_event: ContractSendChannelClose,
):
    balance_proof = channel_close_event.balance_proof

    if balance_proof:
        nonce = balance_proof.nonce
        balance_hash = hash_balance_data(
            balance_proof.transferred_amount,
            balance_proof.locked_amount,
            balance_proof.locksroot,
        )
        signature = balance_proof.signature
        message_hash = balance_proof.message_hash

    else:
        nonce = 0
        balance_hash = b''
        signature = b''
        message_hash = b''

    channel = raiden.chain.payment_channel(channel_close_event.channel_identifier)

    channel.close(
        nonce,
        balance_hash,
        message_hash,
        signature,
    )

def handle_contract_send_channelupdate(
        raiden: RaidenService,
        channel_update_event: ContractSendChannelUpdateTransfer,
):
    balance_proof = channel_update_event.balance_proof

    if balance_proof:
        channel = raiden.chain.netting_channel(channel_update_event.channel_identifier)
        channel.update_transfer(
            balance_proof.nonce,
            balance_proof.transferred_amount,
            balance_proof.locked_amount,
            balance_proof.locksroot,
            balance_proof.message_hash,
            balance_proof.signature,
        )

def handle_contract_send_channelunlock(
        raiden: RaidenService,
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.netting_channel(channel_unlock_event.channel_identifier)
    block_number = raiden.get_block_number()

    for unlock_proof in channel_unlock_event.unlock_proofs:
        lock = Lock.from_bytes(unlock_proof.lock_encoded)

        if lock.expiration < block_number:
            log.error('Lock has expired!', lock=lock)
        else:
            channel.unlock(unlock_proof)

def handle_contract_send_channelunlock2(
        raiden: 'RaidenService',
        channel_unlock_event: ContractSendChannelBatchUnlock,
):
    channel = raiden.chain.netting_channel(channel_unlock_event.channel_identifier)

    channel.unlock(channel_unlock_event.unlock_proofs)

def handle_contract_send_channelsettle(
        raiden: RaidenService,
        channel_settle_event: ContractSendChannelSettle,
):
    channel = raiden.chain.netting_channel(channel_settle_event.channel_identifier)
    channel.settle()

def on_raiden_event(raiden: RaidenService, event: Event):
    # pylint: disable=too-many-branches

    if type(event) == SendLockedTransfer:
        handle_send_lockedtransfer(raiden, event)
    elif type(event) == SendDirectTransfer:
        handle_send_directtransfer(raiden, event)
    elif type(event) == SendRevealSecret:
        handle_send_revealsecret(raiden, event)
    elif type(event) == SendBalanceProof:
        handle_send_balanceproof(raiden, event)
    elif type(event) == SendSecretRequest:
        handle_send_secretrequest(raiden, event)
    elif type(event) == SendRefundTransfer:
        handle_send_refundtransfer(raiden, event)
    elif type(event) == SendProcessed:
        handle_send_processed(raiden, event)
    elif type(event) == EventTransferSentSuccess:
        handle_transfersentsuccess(raiden, event)
    elif type(event) == EventTransferSentFailed:
        handle_transfersentfailed(raiden, event)
    elif type(event) == EventUnlockFailed:
        handle_unlockfailed(raiden, event)
    elif type(event) == ContractSendSecretReveal:
        handle_contract_send_secretreveal(raiden, event)
    elif type(event) == ContractSendChannelClose:
        handle_contract_send_channelclose(raiden, event)
        # handle_contract_send_channelclose2(raiden, event)
    elif type(event) == ContractSendChannelUpdateTransfer:
        handle_contract_send_channelupdate(raiden, event)
    elif type(event) == ContractSendChannelBatchUnlock:
        handle_contract_send_channelunlock(raiden, event)
    elif type(event) == ContractSendChannelSettle:
        handle_contract_send_channelsettle(raiden, event)
    elif type(event) in UNEVENTFUL_EVENTS:
        pass
    else:
        log.error('Unknown event {}'.format(type(event)))
<|endoftext|>


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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import sys

logger = logging.getLogger(__name__)

def transform_entity_synonyms(synonyms, known_synonyms=None):
    """Transforms the entity synonyms into a text->value dictionary"""
    entity_synonyms = known_synonyms if known_synonyms else {}
    for s in synonyms:
        if "value" in s and "synonyms" in s:
            for synonym in s["synonyms"]:
                entity_synonyms[synonym] = s["value"]
    return entity_synonyms

def check_duplicate_synonym(entity_synonyms, text, syn, context_str=""):
    if text in entity_synonyms and entity_synonyms[text] != syn:
        logger.warning("Found inconsistent entity synonyms while {0}, overwriting {1}->{2}"
                       "with {1}->{2} during merge".format(context_str, text, entity_synonyms[text], syn))

def generate_lookup_regex(file_path, print_data_size=False):
    # takes a lookup filename of a comma-separated list and creates a giant regex out of the contents.
    lookup_elements = []
    with open(file_path, 'r') as f:
        for l in f.readlines():
            new_elemnts = [e.strip() for e in l.split(',')]
            lookup_elements += new_elemnts
    regex_string = '(?i)(' + '|'.join(lookup_elements) + ')'
    if print_data_size:
        num_words = len(lookup_elements)
        regex_size = sys.getsizeof(regex_string)
        print("found {} words in lookup table '{}' with a size of {:.2e} bytes".format(num_words,file_path,regex_size))
    return regex_string
<commit_msg>replaced print() by logger.info()<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import sys

logger = logging.getLogger(__name__)

def transform_entity_synonyms(synonyms, known_synonyms=None):
    """Transforms the entity synonyms into a text->value dictionary"""
    entity_synonyms = known_synonyms if known_synonyms else {}
    for s in synonyms:
        if "value" in s and "synonyms" in s:
            for synonym in s["synonyms"]:
                entity_synonyms[synonym] = s["value"]
    return entity_synonyms

def check_duplicate_synonym(entity_synonyms, text, syn, context_str=""):
    if text in entity_synonyms and entity_synonyms[text] != syn:
        logger.warning("Found inconsistent entity synonyms while {0}, overwriting {1}->{2}"
                       "with {1}->{2} during merge".format(context_str, text, entity_synonyms[text], syn))

def generate_lookup_regex(file_path, print_data_size=True):
    # takes a lookup filename of a comma-separated list and creates a giant regex out of the contents.
    lookup_elements = []
    with open(file_path, 'r') as f:
        for l in f.readlines():
            new_elemnts = [e.strip() for e in l.split(',')]
            lookup_elements += new_elemnts
    regex_string = '(?i)(' + '|'.join(lookup_elements) + ')'
    if print_data_size:
        num_words = len(lookup_elements)
        regex_size = sys.getsizeof(regex_string)
        logger.info("found {} words in lookup table '{}' with a size of {:.2e} bytes".format(num_words,file_path,regex_size))
    return regex_string
<|endoftext|>


<commit_before>"""
raven.contrib.django.client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

import time
import logging

from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.http import HttpRequest
from django.template import TemplateSyntaxError

try:
    # support Django 1.9
    from django.template.base import Origin
except ImportError:
    # backward compatibility
    from django.template.loader import LoaderOrigin as Origin

from raven.base import Client
from raven.contrib.django.utils import get_data_from_template, get_host
from raven.contrib.django.middleware import SentryLogMiddleware
from raven.utils.wsgi import get_headers, get_environ
from raven.utils import once
from raven import breadcrumbs

__all__ = ('DjangoClient',)

@once
def install_sql_hook():
    """If installed this causes Django's queries to be captured."""
    try:
        from django.db.backends.utils import CursorWrapper
    except ImportError:
        from django.db.backends.util import CursorWrapper

    def record_sql(start, sql, params):
        breadcrumbs.record_breadcrumb('query', {
            'query': sql,
            'params': params,
            'duration': time.time() - start,
            'classifier': 'django.db'
        })

    real_execute = CursorWrapper.execute
    real_executemany = CursorWrapper.executemany

    def execute(self, sql, params=None):
        start = time.time()
        try:
            return real_execute(self, sql, params)
        finally:
            record_sql(start, sql, params)

    def executemany(self, sql, param_list):
        start = time.time()
        try:
            return real_executemany(self, sql, param_list)
        finally:
            record_sql(start, sql, param_list)

    CursorWrapper.execute = execute
    CursorWrapper.executemany = executemany
    breadcrumbs.ignore_logger('django.db.backends')

class DjangoClient(Client):
    logger = logging.getLogger('sentry.errors.client.django')

    def __init__(self, *args, **kwargs):
        install_sql_hook = kwargs.pop('install_sql_hook', True)
        Client.__init__(self, *args, **kwargs)
        if install_sql_hook:
            self.install_sql_hook()

    def install_sql_hook(self):
        install_sql_hook()

    def get_user_info(self, user):
        if hasattr(user, 'is_authenticated') and \
           not user.is_authenticated():
            return None

        user_info = {
            'id': user.pk,
        }

        if hasattr(user, 'email'):
            user_info['email'] = user.email

        if hasattr(user, 'get_username'):
            user_info['username'] = user.get_username()
        elif hasattr(user, 'username'):
            user_info['username'] = user.username

        return user_info

    def get_data_from_request(self, request):
        result = {}

        user = getattr(request, 'user', None)
        if user is not None:
            user_info = self.get_user_info(user)
            if user_info:
                result['user'] = user_info

        try:
            uri = request.build_absolute_uri()
        except SuspiciousOperation:
            # attempt to build a URL for reporting as Django won't allow us to
            # use get_host()
            if request.is_secure():
                scheme = 'https'
            else:
                scheme = 'http'
            host = get_host(request)
            uri = '%s://%s%s' % (scheme, host, request.path)

        if request.method not in ('GET', 'HEAD'):
            try:
                data = request.body
            except Exception:
                try:
                    data = request.raw_post_data
                except Exception:
                    # assume we had a partial read.
                    try:
                        data = request.POST or '<unavailable>'
                    except Exception:
                        data = '<unavailable>'
        else:
            data = None

        environ = request.META

        result.update({
            'request': {
                'method': request.method,
                'url': uri,
                'query_string': request.META.get('QUERY_STRING'),
                'data': data,
                'cookies': dict(request.COOKIES),
                'headers': dict(get_headers(environ)),
                'env': dict(get_environ(environ)),
            }
        })

        return result

    def build_msg(self, *args, **kwargs):
        data = super(DjangoClient, self).build_msg(*args, **kwargs)

        for frame in self._iter_frames(data):
            module = frame.get('module')
            if not module:
                continue

            if module.startswith('django.'):
                frame['in_app'] = False

        if not self.site and 'django.contrib.sites' in settings.INSTALLED_APPS:
            try:
                from django.contrib.sites.models import Site
                site = Site.objects.get_current()
                site_name = site.name or site.domain
                data['tags'].setdefault('site', site_name)
            except Exception:
                # Database error? Fallback to the id
                try:
                    data['tags'].setdefault('site', settings.SITE_ID)
                except AttributeError:
                    # SITE_ID wasn't set, so just ignore
                    pass

        return data

    def capture(self, event_type, request=None, **kwargs):
        if 'data' not in kwargs:
            kwargs['data'] = data = {}
        else:
            data = kwargs['data']

        if request is None:
            request = getattr(SentryLogMiddleware.thread, 'request', None)

        is_http_request = isinstance(request, HttpRequest)
        if is_http_request:
            data.update(self.get_data_from_request(request))

        if kwargs.get('exc_info'):
            exc_value = kwargs['exc_info'][1]
            # As of r16833 (Django) all exceptions may contain a
            # ``django_template_source`` attribute (rather than the legacy
            # ``TemplateSyntaxError.source`` check) which describes
            # template information.  As of Django 1.9 or so the new
            # template debug thing showed up.
            if hasattr(exc_value, 'django_template_source') or \
               ((isinstance(exc_value, TemplateSyntaxError) and
                isinstance(getattr(exc_value, 'source', None),
                           (tuple, list)) and
                isinstance(exc_value.source[0], Origin))) or \
               hasattr(exc_value, 'template_debug'):
                source = getattr(exc_value, 'django_template_source',
                                 getattr(exc_value, 'source', None))
                debug = getattr(exc_value, 'template_debug', None)
                if source is None:
                    self.logger.info('Unable to get template source from exception')
                data.update(get_data_from_template(source, debug))

        result = super(DjangoClient, self).capture(event_type, **kwargs)

        if is_http_request and result:
            # attach the sentry object to the request
            request.sentry = {
                'project_id': data.get('project', self.remote.project),
                'id': self.get_ident(result),
            }

        return result
<commit_msg>Fix breakage for older django versions<commit_after>"""
raven.contrib.django.client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

import time
import logging

from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.http import HttpRequest
from django.template import TemplateSyntaxError

try:
    # support Django 1.9
    from django.template.base import Origin
except ImportError:
    # backward compatibility
    from django.template.loader import LoaderOrigin as Origin

from raven.base import Client
from raven.contrib.django.utils import get_data_from_template, get_host
from raven.contrib.django.middleware import SentryLogMiddleware
from raven.utils.wsgi import get_headers, get_environ
from raven.utils import once
from raven import breadcrumbs

__all__ = ('DjangoClient',)

@once
def install_sql_hook():
    """If installed this causes Django's queries to be captured."""
    try:
        from django.db.backends.utils import CursorWrapper
    except ImportError:
        from django.db.backends.util import CursorWrapper

    try:
        real_execute = CursorWrapper.execute
        real_executemany = CursorWrapper.executemany
    except AttributeError:
        # XXX(mitsuhiko): On some very old django versions (<1.6) this
        # trickery would have to look different but I can't be bothered.
        return

    def record_sql(start, sql, params):
        breadcrumbs.record_breadcrumb('query', {
            'query': sql,
            'params': params,
            'duration': time.time() - start,
            'classifier': 'django.db'
        })

    def execute(self, sql, params=None):
        start = time.time()
        try:
            return real_execute(self, sql, params)
        finally:
            record_sql(start, sql, params)

    def executemany(self, sql, param_list):
        start = time.time()
        try:
            return real_executemany(self, sql, param_list)
        finally:
            record_sql(start, sql, param_list)

    CursorWrapper.execute = execute
    CursorWrapper.executemany = executemany
    breadcrumbs.ignore_logger('django.db.backends')

class DjangoClient(Client):
    logger = logging.getLogger('sentry.errors.client.django')

    def __init__(self, *args, **kwargs):
        install_sql_hook = kwargs.pop('install_sql_hook', True)
        Client.__init__(self, *args, **kwargs)
        if install_sql_hook:
            self.install_sql_hook()

    def install_sql_hook(self):
        install_sql_hook()

    def get_user_info(self, user):
        if hasattr(user, 'is_authenticated') and \
           not user.is_authenticated():
            return None

        user_info = {
            'id': user.pk,
        }

        if hasattr(user, 'email'):
            user_info['email'] = user.email

        if hasattr(user, 'get_username'):
            user_info['username'] = user.get_username()
        elif hasattr(user, 'username'):
            user_info['username'] = user.username

        return user_info

    def get_data_from_request(self, request):
        result = {}

        user = getattr(request, 'user', None)
        if user is not None:
            user_info = self.get_user_info(user)
            if user_info:
                result['user'] = user_info

        try:
            uri = request.build_absolute_uri()
        except SuspiciousOperation:
            # attempt to build a URL for reporting as Django won't allow us to
            # use get_host()
            if request.is_secure():
                scheme = 'https'
            else:
                scheme = 'http'
            host = get_host(request)
            uri = '%s://%s%s' % (scheme, host, request.path)

        if request.method not in ('GET', 'HEAD'):
            try:
                data = request.body
            except Exception:
                try:
                    data = request.raw_post_data
                except Exception:
                    # assume we had a partial read.
                    try:
                        data = request.POST or '<unavailable>'
                    except Exception:
                        data = '<unavailable>'
        else:
            data = None

        environ = request.META

        result.update({
            'request': {
                'method': request.method,
                'url': uri,
                'query_string': request.META.get('QUERY_STRING'),
                'data': data,
                'cookies': dict(request.COOKIES),
                'headers': dict(get_headers(environ)),
                'env': dict(get_environ(environ)),
            }
        })

        return result

    def build_msg(self, *args, **kwargs):
        data = super(DjangoClient, self).build_msg(*args, **kwargs)

        for frame in self._iter_frames(data):
            module = frame.get('module')
            if not module:
                continue

            if module.startswith('django.'):
                frame['in_app'] = False

        if not self.site and 'django.contrib.sites' in settings.INSTALLED_APPS:
            try:
                from django.contrib.sites.models import Site
                site = Site.objects.get_current()
                site_name = site.name or site.domain
                data['tags'].setdefault('site', site_name)
            except Exception:
                # Database error? Fallback to the id
                try:
                    data['tags'].setdefault('site', settings.SITE_ID)
                except AttributeError:
                    # SITE_ID wasn't set, so just ignore
                    pass

        return data

    def capture(self, event_type, request=None, **kwargs):
        if 'data' not in kwargs:
            kwargs['data'] = data = {}
        else:
            data = kwargs['data']

        if request is None:
            request = getattr(SentryLogMiddleware.thread, 'request', None)

        is_http_request = isinstance(request, HttpRequest)
        if is_http_request:
            data.update(self.get_data_from_request(request))

        if kwargs.get('exc_info'):
            exc_value = kwargs['exc_info'][1]
            # As of r16833 (Django) all exceptions may contain a
            # ``django_template_source`` attribute (rather than the legacy
            # ``TemplateSyntaxError.source`` check) which describes
            # template information.  As of Django 1.9 or so the new
            # template debug thing showed up.
            if hasattr(exc_value, 'django_template_source') or \
               ((isinstance(exc_value, TemplateSyntaxError) and
                isinstance(getattr(exc_value, 'source', None),
                           (tuple, list)) and
                isinstance(exc_value.source[0], Origin))) or \
               hasattr(exc_value, 'template_debug'):
                source = getattr(exc_value, 'django_template_source',
                                 getattr(exc_value, 'source', None))
                debug = getattr(exc_value, 'template_debug', None)
                if source is None:
                    self.logger.info('Unable to get template source from exception')
                data.update(get_data_from_template(source, debug))

        result = super(DjangoClient, self).capture(event_type, **kwargs)

        if is_http_request and result:
            # attach the sentry object to the request
            request.sentry = {
                'project_id': data.get('project', self.remote.project),
                'id': self.get_ident(result),
            }

        return result
<|endoftext|>


<commit_before>from raven.handlers.logging import SentryHandler
from ZConfig.components.logger.factory import Factory
from inspect import getouterframes, currentframe, getinnerframes
from raven.utils.stacks import iter_stack_frames

import logging

class ZopeSentryHandlerFactory(Factory):
    def getLevel(self):
        return logging.WARNING

    def create(self):
        return ZopeSentryHandler(**self.section.__dict__)

    def __init__(self, section):
        Factory.__init__(self)
        self.section = section

class ZopeSentryHandler(SentryHandler):
    def __init__(self, *args, **kw):
        super(ZopeSentryHandler, self).__init__(*args, **kw)
        level = kw.get('level', logging.ERROR)
        self.setLevel(level)

    def emit(self, record):
        if record.levelno <= logging.ERROR:
            request = None
            exc_info = None
            for frame_info in getouterframes(currentframe()):
                frame = frame_info[0]
                request = frame.f_locals.get('request', None)
                exc_info = frame.f_locals.get('exc_info', None)
                if request and exc_info:
                    break
            if exc_info:
                record.exc_info = exc_info
                record.stack = iter_stack_frames(getinnerframes(exc_info[2]))
            if request:
                try:
                    body_pos = request.stdin.tell()
                    request.stdin.seek(0)
                    body = request.stdin.read()
                    request.stdin.seek(body_pos)
                    data = dict(headers=request.environ,
                                url=request.getURL(),
                                method=request.method,
                                host=request.environ.get('REMOTE_ADDR', ''),
                                data=body)
                    if not hasattr(record, 'data'):
                        record.data = {}
                    record.data['sentry.interfaces.Http'] = data
                    user = request.AUTHENTICATED_USER
                    user_dict = dict(id=user.getId(),
                                     is_authenticated=user.has_role('Authenticated'),
                                     email='todo')
                    record.data['sentry.interfaces.User'] = user_dict
                except (AttributeError, KeyError):
                    pass
        return super(ZopeSentryHandler, self).emit(record)

<commit_msg>Formatting and pathetic excuses<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

from inspect import getouterframes, currentframe, getinnerframes
from raven.handlers.logging import SentryHandler
from ZConfig.components.logger.factory import Factory
import logging

from raven.utils.stacks import iter_stack_frames

class ZopeSentryHandlerFactory(Factory):

    def getLevel(self):
        return self.section.level

    def create(self):
        return ZopeSentryHandler(**self.section.__dict__)

    def __init__(self, section):
        Factory.__init__(self)
        self.section = section

class ZopeSentryHandler(SentryHandler):
    """
    Zope unfortunately eats the stack trace information.
    To get the stack trace information and other useful information
    from the request object, this class looks into the different stack
    frames when the emit method is invoked.
    """

    def __init__(self, *args, **kw):
        super(ZopeSentryHandler, self).__init__(*args, **kw)
        level = kw.get('level', logging.ERROR)
        self.setLevel(level)

    def emit(self, record):
        if record.levelno <= logging.ERROR:
            request = None
            exc_info = None
            for frame_info in getouterframes(currentframe()):
                frame = frame_info[0]
                request = frame.f_locals.get('request', None)
                exc_info = frame.f_locals.get('exc_info', None)
                if request and exc_info:
                    break
            if exc_info:
                record.exc_info = exc_info
                record.stack = \
                    iter_stack_frames(getinnerframes(exc_info[2]))
            if request:
                try:
                    body_pos = request.stdin.tell()
                    request.stdin.seek(0)
                    body = request.stdin.read()
                    request.stdin.seek(body_pos)
                    data = dict(headers=request.environ,
                                url=request.getURL(),
                                method=request.method,
                                host=request.environ.get('REMOTE_ADDR',
                                ''), data=body)
                    if not hasattr(record, 'data'):
                        record.data = {}
                    record.data['sentry.interfaces.Http'] = data
                    user = request.AUTHENTICATED_USER
                    user_dict = dict(id=user.getId(),
                            is_authenticated=user.has_role('Authenticated'
                            ), email='todo')
                    record.data['sentry.interfaces.User'] = user_dict
                except (AttributeError, KeyError):
                    pass
        return super(ZopeSentryHandler, self).emit(record)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
from .base import ModelBase
from .way import Way, WayType
from .locations import Coordinates, Location, Stop, POI, Address
from .ride import RideSegment
from .line import LineTypes
from .tickets import TicketList
from datetime import timedelta

class Trip(ModelBase):
    def __init__(self):
        super().__init__()
        self.parts = []
        self.walk_speed = 'normal'
        self.tickets = None

    @classmethod
    def _validate(cls):
        return {
            'parts': ((RideSegment, Way), ),
            'tickets': (None, TicketList)
        }

    def _serialize(self, depth):
        data = {}
        data['parts'] = [p.serialize(depth, True) for p in self.parts]
        self._serial_get(data, 'walk_speed')
        if self.tickets:
            data['tickets'] = self.tickets.serialize()
        return data

    def _unserialize(self, data):
        self.parts = [self._unserialize_typed(part, (RideSegment, Way))
                      for part in data['parts']]
        self._serial_add(data, 'walk_speed')
        if 'tickets' in data:
            self.tickets = TicketList.unserialize(data['tickets'])

    class Request(ModelBase.Request):
        def __init__(self):
            super().__init__()
            self.parts = []
            self.walk_speed = 'normal'
            self.origin = None
            self.via = []
            self.destination = None
            self.departure = None
            self.arrival = None
            self.linetypes = LineTypes()
            self.max_changes = None

            self.with_bike = False
            self.wheelchair = False
            self.low_floor_only = False
            self.allow_solid_stairs = True
            self.allow_escalators = True
            self.allow_elevators = True

            self.waytype_origin = WayType('walk')
            self.waytype_via = WayType('walk')
            self.waytype_destination = WayType('walk')

            self.wayduration_origin = timedelta(minutes=10)
            self.wayduration_via = timedelta(minutes=10)
            self.wayduration_destination = timedelta(minutes=10)

        def _load(self, data):
            super()._load(data)
            self._serial_get(data, 'walk_speed')
            self._serial_get(data, 'parts')
            self.parts = [ModelBase.unserialize(part) for part in self.parts]

        def _serialize(self, ids):
            data = {}
            parts = [part.serialize() for part in self.parts]
            self._serial_add(data, 'parts', ids, val=parts)
            self._serial_add(data, 'walk_speed', ids)
            return data

        def _matches(self, obj):
            if self.origin != obj.origin or self.destination != obj.destination:
                return False

            if not obj.wayonly:
                if self.departure is not None and self.departure < obj.departure:
                    return False

                if self.arrival is not None and self.arrival > obj.arrival:
                    return False

            for i, part in enumerate(obj):
                if isinstance(part, RideSegment):
                    if part.line.linetype not in self.linetypes:
                        return False
                else:
                    if part.waytype == WayType('walk'):
                        continue
                    if i == 0:
                        if self.waytype_origin != part.waytype:
                            return False
                    elif i + 1 == len(obj):
                        if self.waytype_destination != part.waytype:
                            return False
                    else:
                        if self.waytype_via != part.waytype:
                            return False

            return True

    class Results(ModelBase.Results):
        def __init__(self, *args):
            super().__init__(*args)
            self.origin = None
            self.via = None
            self.destination = None

        @classmethod
        def _validate(cls):
            return {
                'origin': Location,
                'via': (None, Location),
                'destination': Location
            }

        def _load(self, data):
            super()._load(data)
            types = (Location, Stop, POI, Address, Coordinates)
            self.origin = self._unserialize_typed(data['origin'], types)
            self.destination = self._unserialize_typed(data['destination'], types)

        def _serialize(self, depth):
            data = {}
            data['origin'] = self.origin.serialize(depth, True)
            data['destination'] = self.destination.serialize(depth, True)
            return data

    @property
    def origin(self):
        return self.parts[0].origin

    @property
    def destination(self):
        return self.parts[-1].destination

    @property
    def departure(self):
        delta = timedelta(0)
        for part in self.parts:
            if isinstance(part, RideSegment):
                return (part.departure - delta) if part.departure else None
            elif part.duration is None:
                return None
            else:
                delta += part.duration

    @property
    def arrival(self):
        delta = timedelta(0)
        for part in reversed(self.parts):
            if isinstance(part, RideSegment):
                return (part.arrival + delta) if part.arrival else None
            elif part.duration is None:
                return None
            else:
                delta += part.duration

    @property
    def wayonly(self):
        for part in self:
            if isinstance(part, RideSegment):
                return False
        return True

    @property
    def linetypes(self):
        types = LineTypes(False)
        for part in self.parts:
            linetype = part.line.linetype
            if linetype is not None:
                types.add(linetype)
        return types

    @property
    def changes(self):
        changes = -1
        for part in self.parts:
            if isinstance(part, RideSegment):
                changes += 1
        return max(0, changes)

    @property
    def bike_friendly(self):
        for part in self.parts:
            if not isinstance(part, RideSegment):
                continue
            if part.bike_friendly is None:
                return None
            elif part.bike_friendly is False:
                return False
        return True

    def to_request(self):
        r = Trip.Request()
        r.walk_speed = self.walk_speed
        r.origin = self.origin
        r.destination = self.destination
        r.departure = self.departure
        r.arrival = self.arrival
        r.linetypes = self.linetypes
        r.max_changtes = self.max_changes
        r.bike_friendly = self.bike_friendly
        return r

    def __iter__(self):
        yield from self.parts
<commit_msg>fix Trip.linetypes<commit_after>#!/usr/bin/env python3
from .base import ModelBase
from .way import Way, WayType
from .locations import Coordinates, Location, Stop, POI, Address
from .ride import RideSegment
from .line import LineTypes
from .tickets import TicketList
from datetime import timedelta

class Trip(ModelBase):
    def __init__(self):
        super().__init__()
        self.parts = []
        self.walk_speed = 'normal'
        self.tickets = None

    @classmethod
    def _validate(cls):
        return {
            'parts': ((RideSegment, Way), ),
            'tickets': (None, TicketList)
        }

    def _serialize(self, depth):
        data = {}
        data['parts'] = [p.serialize(depth, True) for p in self.parts]
        self._serial_get(data, 'walk_speed')
        if self.tickets:
            data['tickets'] = self.tickets.serialize()
        return data

    def _unserialize(self, data):
        self.parts = [self._unserialize_typed(part, (RideSegment, Way))
                      for part in data['parts']]
        self._serial_add(data, 'walk_speed')
        if 'tickets' in data:
            self.tickets = TicketList.unserialize(data['tickets'])

    class Request(ModelBase.Request):
        def __init__(self):
            super().__init__()
            self.parts = []
            self.walk_speed = 'normal'
            self.origin = None
            self.via = []
            self.destination = None
            self.departure = None
            self.arrival = None
            self.linetypes = LineTypes()
            self.max_changes = None

            self.with_bike = False
            self.wheelchair = False
            self.low_floor_only = False
            self.allow_solid_stairs = True
            self.allow_escalators = True
            self.allow_elevators = True

            self.waytype_origin = WayType('walk')
            self.waytype_via = WayType('walk')
            self.waytype_destination = WayType('walk')

            self.wayduration_origin = timedelta(minutes=10)
            self.wayduration_via = timedelta(minutes=10)
            self.wayduration_destination = timedelta(minutes=10)

        def _load(self, data):
            super()._load(data)
            self._serial_get(data, 'walk_speed')
            self._serial_get(data, 'parts')
            self.parts = [ModelBase.unserialize(part) for part in self.parts]

        def _serialize(self, ids):
            data = {}
            parts = [part.serialize() for part in self.parts]
            self._serial_add(data, 'parts', ids, val=parts)
            self._serial_add(data, 'walk_speed', ids)
            return data

        def _matches(self, obj):
            if self.origin != obj.origin or self.destination != obj.destination:
                return False

            if not obj.wayonly:
                if self.departure is not None and self.departure < obj.departure:
                    return False

                if self.arrival is not None and self.arrival > obj.arrival:
                    return False

            for i, part in enumerate(obj):
                if isinstance(part, RideSegment):
                    if part.line.linetype not in self.linetypes:
                        return False
                else:
                    if part.waytype == WayType('walk'):
                        continue
                    if i == 0:
                        if self.waytype_origin != part.waytype:
                            return False
                    elif i + 1 == len(obj):
                        if self.waytype_destination != part.waytype:
                            return False
                    else:
                        if self.waytype_via != part.waytype:
                            return False

            return True

    class Results(ModelBase.Results):
        def __init__(self, *args):
            super().__init__(*args)
            self.origin = None
            self.via = None
            self.destination = None

        @classmethod
        def _validate(cls):
            return {
                'origin': Location,
                'via': (None, Location),
                'destination': Location
            }

        def _load(self, data):
            super()._load(data)
            types = (Location, Stop, POI, Address, Coordinates)
            self.origin = self._unserialize_typed(data['origin'], types)
            self.destination = self._unserialize_typed(data['destination'], types)

        def _serialize(self, depth):
            data = {}
            data['origin'] = self.origin.serialize(depth, True)
            data['destination'] = self.destination.serialize(depth, True)
            return data

    @property
    def origin(self):
        return self.parts[0].origin

    @property
    def destination(self):
        return self.parts[-1].destination

    @property
    def departure(self):
        delta = timedelta(0)
        for part in self.parts:
            if isinstance(part, RideSegment):
                return (part.departure - delta) if part.departure else None
            elif part.duration is None:
                return None
            else:
                delta += part.duration

    @property
    def arrival(self):
        delta = timedelta(0)
        for part in reversed(self.parts):
            if isinstance(part, RideSegment):
                return (part.arrival + delta) if part.arrival else None
            elif part.duration is None:
                return None
            else:
                delta += part.duration

    @property
    def wayonly(self):
        for part in self:
            if isinstance(part, RideSegment):
                return False
        return True

    @property
    def linetypes(self):
        types = LineTypes(())
        for part in self.parts:
            if isinstance(part, RideSegment):
                types.include(part.line.linetype)
        return types

    @property
    def changes(self):
        changes = -1
        for part in self.parts:
            if isinstance(part, RideSegment):
                changes += 1
        return max(0, changes)

    @property
    def bike_friendly(self):
        for part in self.parts:
            if not isinstance(part, RideSegment):
                continue
            if part.bike_friendly is None:
                return None
            elif part.bike_friendly is False:
                return False
        return True

    def to_request(self):
        r = Trip.Request()
        r.walk_speed = self.walk_speed
        r.origin = self.origin
        r.destination = self.destination
        r.departure = self.departure
        r.arrival = self.arrival
        r.linetypes = self.linetypes
        r.max_changtes = self.max_changes
        r.bike_friendly = self.bike_friendly
        return r

    def __iter__(self):
        yield from self.parts
<|endoftext|>


<commit_before>"""
Because fedpkg/pyrpkg sucks horribly, this entire file full of hacks is
needed to interface with it. Disgusting.
"""
import ConfigParser
import datetime
import logging
import sys
import time
import os

from rdopkg import exception
from rdopkg.utils.log import log
from rdopkg import guess

KOJI_AVAILABLE = False
try:
    import koji
    KOJI_AVAILABLE = True
except ImportError:
    pass

FEDPKG_AVAILABLE = False
try:
    import fedpkg
    FEDPKG_AVAILABLE = True
except ImportError:
    pass

KOJI_HUB_URL = 'http://koji.fedoraproject.org/kojihub'
KOJI_BASE_URL = 'http://kojipkgs.fedoraproject.org/work/'

def modules_check():
    if not KOJI_AVAILABLE:
        raise exception.ModuleNotAvailable(module='koji')
    if not FEDPKG_AVAILABLE:
        raise exception.ModuleNotAvailable(module='fedpkg')

def setup_fedpkg_logger():
    flog = logging.getLogger('rpkg')
    fmtr = logging.Formatter('%(message)s')
    for h in list(flog.handlers):
        flog.removeHandler(h)
    stdouth = logging.StreamHandler(sys.stdout)
    stdouth.setFormatter(fmtr)
    flog.addHandler(stdouth)
    flog.setLevel(logging.INFO)
    return flog

def get_fedpkg_config():
    fedpkg_conf = '/etc/rpkg/fedpkg.conf'
    config = ConfigParser.SafeConfigParser()
    config.read(fedpkg_conf)
    return config

def get_fedpkg_commands():
    config = get_fedpkg_config()
    items = dict(config.items('fedpkg', raw=True))
    # I don't even...
    cmd = fedpkg.Commands(os.getcwd(),
                          items['lookaside'],
                          items['lookasidehash'],
                          items['lookaside_cgi'],
                          items['gitbaseurl'],
                          items['anongiturl'],
                          items['branchre'],
                          items['kojiconfig'],
                          items['build_client'],
                          )
    return cmd

class FedpkgArgsStub(object):
    """
    fedpkg sucks, this is one of many results
    """

    def __init__(self):
        self.q = False

def get_fedpkg_cli(config=None, flog=None):
    if not config:
        config = get_fedpkg_config()
    if not flog:
        flog = logging.getLogger('rpkg')
    cli = fedpkg.cli.cliClient(config)
    cli.log = flog
    cli.args = cli.args = FedpkgArgsStub()
    return cli

def new_build(watch=True):
    modules_check()
    flog = setup_fedpkg_logger()
    fcmd = get_fedpkg_commands()
    target = fcmd.target
    task_id = fcmd.build()

    if watch:
        print('')
        try:
            cli = get_fedpkg_cli()
            r = cli._watch_koji_tasks(fcmd.kojisession, [task_id])
        except ConfigParser.NoSectionError:
            # <C-C> causes this for some reason
            print('')
        except Exception as ex:
            log.warn(str(type(ex).__name__))
            log.warn("Failed to get build status: %s" % ex)

    return fcmd.nvr
<commit_msg>linting cleanup in kojibuild.py - F841<commit_after>"""
Because fedpkg/pyrpkg sucks horribly, this entire file full of hacks is
needed to interface with it. Disgusting.
"""
import ConfigParser
import datetime
import logging
import sys
import time
import os

from rdopkg import exception
from rdopkg.utils.log import log
from rdopkg import guess

KOJI_AVAILABLE = False
try:
    import koji
    KOJI_AVAILABLE = True
except ImportError:
    pass

FEDPKG_AVAILABLE = False
try:
    import fedpkg
    FEDPKG_AVAILABLE = True
except ImportError:
    pass

KOJI_HUB_URL = 'http://koji.fedoraproject.org/kojihub'
KOJI_BASE_URL = 'http://kojipkgs.fedoraproject.org/work/'

def modules_check():
    if not KOJI_AVAILABLE:
        raise exception.ModuleNotAvailable(module='koji')
    if not FEDPKG_AVAILABLE:
        raise exception.ModuleNotAvailable(module='fedpkg')

def setup_fedpkg_logger():
    flog = logging.getLogger('rpkg')
    fmtr = logging.Formatter('%(message)s')
    for h in list(flog.handlers):
        flog.removeHandler(h)
    stdouth = logging.StreamHandler(sys.stdout)
    stdouth.setFormatter(fmtr)
    flog.addHandler(stdouth)
    flog.setLevel(logging.INFO)
    return flog

def get_fedpkg_config():
    fedpkg_conf = '/etc/rpkg/fedpkg.conf'
    config = ConfigParser.SafeConfigParser()
    config.read(fedpkg_conf)
    return config

def get_fedpkg_commands():
    config = get_fedpkg_config()
    items = dict(config.items('fedpkg', raw=True))
    # I don't even...
    cmd = fedpkg.Commands(os.getcwd(),
                          items['lookaside'],
                          items['lookasidehash'],
                          items['lookaside_cgi'],
                          items['gitbaseurl'],
                          items['anongiturl'],
                          items['branchre'],
                          items['kojiconfig'],
                          items['build_client'],
                          )
    return cmd

class FedpkgArgsStub(object):
    """
    fedpkg sucks, this is one of many results
    """

    def __init__(self):
        self.q = False

def get_fedpkg_cli(config=None, flog=None):
    if not config:
        config = get_fedpkg_config()
    if not flog:
        flog = logging.getLogger('rpkg')
    cli = fedpkg.cli.cliClient(config)
    cli.log = flog
    cli.args = cli.args = FedpkgArgsStub()
    return cli

def new_build(watch=True):
    modules_check()
    flog = setup_fedpkg_logger()  # NOQA
    fcmd = get_fedpkg_commands()
    task_id = fcmd.build()

    if watch:
        print('')
        try:
            cli = get_fedpkg_cli()
            # TODO: might be good to push this return data back up
            #       or check the status of the return
            r = cli._watch_koji_tasks(fcmd.kojisession, [task_id])
        except ConfigParser.NoSectionError:
            # <C-C> causes this for some reason
            print('')
        except Exception as ex:
            log.warn(str(type(ex).__name__))
            log.warn("Failed to get build status: %s" % ex)

    return fcmd.nvr
<|endoftext|>


<commit_before>from django.conf import settings
from django.core.urlresolvers import reverse

def redirect_filename(project, filename=None):
    """
    Return a url for a page. Always use http for now,
    to avoid content warnings.
    """
    protocol = "http"
    version = project.get_default_version()
    lang = project.language
    use_subdomain = getattr(settings, 'USE_SUBDOMAIN', False)
    if use_subdomain:
        if project.single_version:
            return "%s://%s/%s" % (
                protocol,
                project.subdomain,
                filename,
            )
        else:
            return "%s://%s/%s/%s/%s" % (
                protocol,
                project.subdomain,
                lang,
                version,
                filename,
            )
    else:
        if project.single_version:
            return reverse('docs_detail', kwargs={
                'project_slug': project.slug,
                'filename': filename,
            })
        else:
            return reverse('docs_detail', kwargs={
                'project_slug': project.slug,
                'lang_slug': lang,
                'version_slug': version,
                'filename': filename,
            })
<commit_msg>Handle explicit full-path redirects. Fixes #964.<commit_after>from django.conf import settings
from django.core.urlresolvers import reverse

def redirect_filename(project, filename=None):
    """
    Return a url for a page. Always use http for now,
    to avoid content warnings.
    """
    protocol = "http"
    # Handle explicit http redirects
    if filename.startswith(protocol):
        return filename
    version = project.get_default_version()
    lang = project.language
    use_subdomain = getattr(settings, 'USE_SUBDOMAIN', False)
    if use_subdomain:
        if project.single_version:
            return "%s://%s/%s" % (
                protocol,
                project.subdomain,
                filename,
            )
        else:
            return "%s://%s/%s/%s/%s" % (
                protocol,
                project.subdomain,
                lang,
                version,
                filename,
            )
    else:
        if project.single_version:
            return reverse('docs_detail', kwargs={
                'project_slug': project.slug,
                'filename': filename,
            })
        else:
            return reverse('docs_detail', kwargs={
                'project_slug': project.slug,
                'lang_slug': lang,
                'version_slug': version,
                'filename': filename,
            })
<|endoftext|>