<commit_before>################################################################################
#  THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY  #
#  Read the zproject/README.md for information about making permanent changes. #
################################################################################

from __future__ import print_function
import os
import re
import sys

from pyczmq._cffi import ffi

try:
    # If LD_LIBRARY_PATH or your OSs equivalent is set, this is the only way to
    # load the library.  If we use find_library below, we get the wrong result.
    if os.name == 'posix':
        if sys.platform == 'darwin':
            libpath = 'libzyre.2.dylib'
        else:
            libpath = 'libzyre.so.2'
    elif os.name == 'nt':
        libpath = 'libzyre.dll'
    lib = ffi.dlopen(libpath)
except OSError:
    libpath = find_library("zyre")
    if not libpath:
        raise ImportError("Unable to find libzyre")
    lib = ffi.dlopen(libpath)

# Custom setup for zyre

cdefs = '''
typedef struct _zyre_t zyre_t;
typedef struct _zmsg_t zmsg_t;
typedef struct _zlist_t zlist_t;
typedef struct _zsock_t zsock_t;
typedef struct _zyre_event_t zyre_event_t;
typedef struct _zhash_t zhash_t;
// CLASS: zyre
// Constructor, creates a new Zyre node. Note that until you start the
// node it is silent and invisible to other nodes on the network.
// The node name is provided to other nodes during discovery. If you
// specify NULL, Zyre generates a randomized node name from the UUID.
zyre_t *
    zyre_new (const char *name);

// Destructor, destroys a Zyre node. When you destroy a node, any
// messages it is sending or receiving will be discarded.
void
    zyre_destroy (zyre_t **self_p);

// Return our node UUID string, after successful initialization
const char *
    zyre_uuid (zyre_t *self);

// Return our node name, after successful initialization. First 6
// characters of UUID by default.
const char *
    zyre_name (zyre_t *self);

// Set the public name of this node overriding the default. The name is
// provide during discovery and come in each ENTER message.
void
    zyre_set_name (zyre_t *self, const char *name);

// Set node header; these are provided to other nodes during discovery
// and come in each ENTER message.
void
    zyre_set_header (zyre_t *self, const char *name, const char *format, ...);

// Set verbose mode; this tells the node to log all traffic as well as
// all major events.
void
    zyre_set_verbose (zyre_t *self);

// Set UDP beacon discovery port; defaults to 5670, this call overrides
// that so you can create independent clusters on the same network, for
// e.g. development vs. production. Has no effect after zyre_start().
void
    zyre_set_port (zyre_t *self, int port_nbr);

// Set the peer evasiveness timeout, in milliseconds. Default is 5000.
// This can be tuned in order to deal with expected network conditions
// and the response time expected by the application. This is tied to
// the beacon interval and rate of messages received.
void
    zyre_set_evasive_timeout (zyre_t *self, int interval);

// Set the peer expiration timeout, in milliseconds. Default is 30000.
// This can be tuned in order to deal with expected network conditions
// and the response time expected by the application. This is tied to
// the beacon interval and rate of messages received.
void
    zyre_set_expired_timeout (zyre_t *self, int interval);

// Set UDP beacon discovery interval, in milliseconds. Default is instant
// beacon exploration followed by pinging every 1,000 msecs.
void
    zyre_set_interval (zyre_t *self, size_t interval);

// Set network interface for UDP beacons. If you do not set this, CZMQ will
// choose an interface for you. On boxes with several interfaces you should
// specify which one you want to use, or strange things can happen.
void
    zyre_set_interface (zyre_t *self, const char *value);

// By default, Zyre binds to an ephemeral TCP port and broadcasts the local
// host name using UDP beaconing. When you call this method, Zyre will use
// gossip discovery instead of UDP beaconing. You MUST set-up the gossip
// service separately using zyre_gossip_bind() and _connect(). Note that the
// endpoint MUST be valid for both bind and connect operations. You can use
// inproc://, ipc://, or tcp:// transports (for tcp://, use an IP address
// that is meaningful to remote as well as local nodes). Returns 0 if
// the bind was successful, else -1.
int
    zyre_set_endpoint (zyre_t *self, const char *format, ...);

// Set-up gossip discovery of other nodes. At least one node in the cluster
// must bind to a well-known gossip endpoint, so other nodes can connect to
// it. Note that gossip endpoints are completely distinct from Zyre node
// endpoints, and should not overlap (they can use the same transport).
void
    zyre_gossip_bind (zyre_t *self, const char *format, ...);

// Set-up gossip discovery of other nodes. A node may connect to multiple
// other nodes, for redundancy paths. For details of the gossip network
// design, see the CZMQ zgossip class.
void
    zyre_gossip_connect (zyre_t *self, const char *format, ...);

// Start node, after setting header values. When you start a node it
// begins discovery and connection. Returns 0 if OK, -1 if it wasn't
// possible to start the node.
int
    zyre_start (zyre_t *self);

// Stop node; this signals to other peers that this node will go away.
// This is polite; however you can also just destroy the node without
// stopping it.
void
    zyre_stop (zyre_t *self);

// Join a named group; after joining a group you can send messages to
// the group and all Zyre nodes in that group will receive them.
int
    zyre_join (zyre_t *self, const char *group);

// Leave a group
int
    zyre_leave (zyre_t *self, const char *group);

// Receive next message from network; the message may be a control
// message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
// Returns zmsg_t object, or NULL if interrupted
zmsg_t *
    zyre_recv (zyre_t *self);

// Send message to single peer, specified as a UUID string
// Destroys message after sending
int
    zyre_whisper (zyre_t *self, const char *peer, zmsg_t **msg_p);

// Send message to a named group
// Destroys message after sending
int
    zyre_shout (zyre_t *self, const char *group, zmsg_t **msg_p);

// Send formatted string to a single peer specified as UUID string
int
    zyre_whispers (zyre_t *self, const char *peer, const char *format, ...);

// Send formatted string to a named group
int
    zyre_shouts (zyre_t *self, const char *group, const char *format, ...);

// Return zlist of current peer ids.
zlist_t *
    zyre_peers (zyre_t *self);

// Return zlist of current peers of this group.
zlist_t *
    zyre_peers_by_group (zyre_t *self, const char *name);

// Return zlist of currently joined groups.
zlist_t *
    zyre_own_groups (zyre_t *self);

// Return zlist of groups known through connected peers.
zlist_t *
    zyre_peer_groups (zyre_t *self);

// Return the endpoint of a connected peer.
char *
    zyre_peer_address (zyre_t *self, const char *peer);

// Return the value of a header of a conected peer.
// Returns null if peer or key doesn't exits.
char *
    zyre_peer_header_value (zyre_t *self, const char *peer, const char *name);

// Return socket for talking to the Zyre node, for polling
zsock_t *
    zyre_socket (zyre_t *self);

// Print zyre node information to stdout
void
    zyre_print (zyre_t *self);

// Return the Zyre version for run-time API detection; returns
// major * 10000 + minor * 100 + patch, as a single integer.
uint64_t
    zyre_version (void);

// Self test of this class.
void
    zyre_test (bool verbose);

// CLASS: zyre event
// Constructor: receive an event from the zyre node, wraps zyre_recv.
// The event may be a control message (ENTER, EXIT, JOIN, LEAVE) or
// data (WHISPER, SHOUT).
zyre_event_t *
    zyre_event_new (zyre_t *node);

// Destructor; destroys an event instance
void
    zyre_event_destroy (zyre_event_t **self_p);

// Returns event type, as printable uppercase string. Choices are:
// "ENTER", "EXIT", "JOIN", "LEAVE", "EVASIVE", "WHISPER" and "SHOUT"
// and for the local node: "STOP"
const char *
    zyre_event_type (zyre_event_t *self);

// Return the sending peer's uuid as a string
const char *
    zyre_event_peer_uuid (zyre_event_t *self);

// Return the sending peer's public name as a string
const char *
    zyre_event_peer_name (zyre_event_t *self);

// Return the sending peer's ipaddress as a string
const char *
    zyre_event_peer_addr (zyre_event_t *self);

// Returns the event headers, or NULL if there are none
zhash_t *
    zyre_event_headers (zyre_event_t *self);

// Returns value of a header from the message headers
// obtained by ENTER. Return NULL if no value was found.
const char *
    zyre_event_header (zyre_event_t *self, const char *name);

// Returns the group name that a SHOUT event was sent to
const char *
    zyre_event_group (zyre_event_t *self);

// Returns the incoming message payload; the caller can modify the
// message but does not own it and should not destroy it.
zmsg_t *
    zyre_event_msg (zyre_event_t *self);

// Returns the incoming message payload, and pass ownership to the
// caller. The caller must destroy the message when finished with it.
// After called on the given event, further calls will return NULL.
zmsg_t *
    zyre_event_get_msg (zyre_event_t *self);

// Print event to zsys log
void
    zyre_event_print (zyre_event_t *self);

// Self test of this class.
void
    zyre_event_test (bool verbose);

'''
cdefs = re.sub(r';[^;]*\bva_list\b[^;]*;', ';', cdefs, flags=re.S) # we don't support anything with a va_list arg

ffi.cdef(cdefs)
<commit_msg>from ctypes.util import find_library<commit_after>################################################################################
#  THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY  #
#  Read the zproject/README.md for information about making permanent changes. #
################################################################################

from __future__ import print_function
import os
import re
import sys
from ctypes.util import find_library

from pyczmq._cffi import ffi

try:
    # If LD_LIBRARY_PATH or your OSs equivalent is set, this is the only way to
    # load the library.  If we use find_library below, we get the wrong result.
    if os.name == 'posix':
        if sys.platform == 'darwin':
            libpath = 'libzyre.2.dylib'
        else:
            libpath = 'libzyre.so.2'
    elif os.name == 'nt':
        libpath = 'libzyre.dll'
    lib = ffi.dlopen(libpath)
except OSError:
    libpath = find_library("zyre")
    if not libpath:
        raise ImportError("Unable to find libzyre")
    lib = ffi.dlopen(libpath)

# Custom setup for zyre

cdefs = '''
typedef struct _zyre_t zyre_t;
typedef struct _zmsg_t zmsg_t;
typedef struct _zlist_t zlist_t;
typedef struct _zsock_t zsock_t;
typedef struct _zyre_event_t zyre_event_t;
typedef struct _zhash_t zhash_t;
// CLASS: zyre
// Constructor, creates a new Zyre node. Note that until you start the
// node it is silent and invisible to other nodes on the network.
// The node name is provided to other nodes during discovery. If you
// specify NULL, Zyre generates a randomized node name from the UUID.
zyre_t *
    zyre_new (const char *name);

// Destructor, destroys a Zyre node. When you destroy a node, any
// messages it is sending or receiving will be discarded.
void
    zyre_destroy (zyre_t **self_p);

// Return our node UUID string, after successful initialization
const char *
    zyre_uuid (zyre_t *self);

// Return our node name, after successful initialization. First 6
// characters of UUID by default.
const char *
    zyre_name (zyre_t *self);

// Set the public name of this node overriding the default. The name is
// provide during discovery and come in each ENTER message.
void
    zyre_set_name (zyre_t *self, const char *name);

// Set node header; these are provided to other nodes during discovery
// and come in each ENTER message.
void
    zyre_set_header (zyre_t *self, const char *name, const char *format, ...);

// Set verbose mode; this tells the node to log all traffic as well as
// all major events.
void
    zyre_set_verbose (zyre_t *self);

// Set UDP beacon discovery port; defaults to 5670, this call overrides
// that so you can create independent clusters on the same network, for
// e.g. development vs. production. Has no effect after zyre_start().
void
    zyre_set_port (zyre_t *self, int port_nbr);

// Set the peer evasiveness timeout, in milliseconds. Default is 5000.
// This can be tuned in order to deal with expected network conditions
// and the response time expected by the application. This is tied to
// the beacon interval and rate of messages received.
void
    zyre_set_evasive_timeout (zyre_t *self, int interval);

// Set the peer expiration timeout, in milliseconds. Default is 30000.
// This can be tuned in order to deal with expected network conditions
// and the response time expected by the application. This is tied to
// the beacon interval and rate of messages received.
void
    zyre_set_expired_timeout (zyre_t *self, int interval);

// Set UDP beacon discovery interval, in milliseconds. Default is instant
// beacon exploration followed by pinging every 1,000 msecs.
void
    zyre_set_interval (zyre_t *self, size_t interval);

// Set network interface for UDP beacons. If you do not set this, CZMQ will
// choose an interface for you. On boxes with several interfaces you should
// specify which one you want to use, or strange things can happen.
void
    zyre_set_interface (zyre_t *self, const char *value);

// By default, Zyre binds to an ephemeral TCP port and broadcasts the local
// host name using UDP beaconing. When you call this method, Zyre will use
// gossip discovery instead of UDP beaconing. You MUST set-up the gossip
// service separately using zyre_gossip_bind() and _connect(). Note that the
// endpoint MUST be valid for both bind and connect operations. You can use
// inproc://, ipc://, or tcp:// transports (for tcp://, use an IP address
// that is meaningful to remote as well as local nodes). Returns 0 if
// the bind was successful, else -1.
int
    zyre_set_endpoint (zyre_t *self, const char *format, ...);

// Set-up gossip discovery of other nodes. At least one node in the cluster
// must bind to a well-known gossip endpoint, so other nodes can connect to
// it. Note that gossip endpoints are completely distinct from Zyre node
// endpoints, and should not overlap (they can use the same transport).
void
    zyre_gossip_bind (zyre_t *self, const char *format, ...);

// Set-up gossip discovery of other nodes. A node may connect to multiple
// other nodes, for redundancy paths. For details of the gossip network
// design, see the CZMQ zgossip class.
void
    zyre_gossip_connect (zyre_t *self, const char *format, ...);

// Start node, after setting header values. When you start a node it
// begins discovery and connection. Returns 0 if OK, -1 if it wasn't
// possible to start the node.
int
    zyre_start (zyre_t *self);

// Stop node; this signals to other peers that this node will go away.
// This is polite; however you can also just destroy the node without
// stopping it.
void
    zyre_stop (zyre_t *self);

// Join a named group; after joining a group you can send messages to
// the group and all Zyre nodes in that group will receive them.
int
    zyre_join (zyre_t *self, const char *group);

// Leave a group
int
    zyre_leave (zyre_t *self, const char *group);

// Receive next message from network; the message may be a control
// message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
// Returns zmsg_t object, or NULL if interrupted
zmsg_t *
    zyre_recv (zyre_t *self);

// Send message to single peer, specified as a UUID string
// Destroys message after sending
int
    zyre_whisper (zyre_t *self, const char *peer, zmsg_t **msg_p);

// Send message to a named group
// Destroys message after sending
int
    zyre_shout (zyre_t *self, const char *group, zmsg_t **msg_p);

// Send formatted string to a single peer specified as UUID string
int
    zyre_whispers (zyre_t *self, const char *peer, const char *format, ...);

// Send formatted string to a named group
int
    zyre_shouts (zyre_t *self, const char *group, const char *format, ...);

// Return zlist of current peer ids.
zlist_t *
    zyre_peers (zyre_t *self);

// Return zlist of current peers of this group.
zlist_t *
    zyre_peers_by_group (zyre_t *self, const char *name);

// Return zlist of currently joined groups.
zlist_t *
    zyre_own_groups (zyre_t *self);

// Return zlist of groups known through connected peers.
zlist_t *
    zyre_peer_groups (zyre_t *self);

// Return the endpoint of a connected peer.
char *
    zyre_peer_address (zyre_t *self, const char *peer);

// Return the value of a header of a conected peer.
// Returns null if peer or key doesn't exits.
char *
    zyre_peer_header_value (zyre_t *self, const char *peer, const char *name);

// Return socket for talking to the Zyre node, for polling
zsock_t *
    zyre_socket (zyre_t *self);

// Print zyre node information to stdout
void
    zyre_print (zyre_t *self);

// Return the Zyre version for run-time API detection; returns
// major * 10000 + minor * 100 + patch, as a single integer.
uint64_t
    zyre_version (void);

// Self test of this class.
void
    zyre_test (bool verbose);

// CLASS: zyre event
// Constructor: receive an event from the zyre node, wraps zyre_recv.
// The event may be a control message (ENTER, EXIT, JOIN, LEAVE) or
// data (WHISPER, SHOUT).
zyre_event_t *
    zyre_event_new (zyre_t *node);

// Destructor; destroys an event instance
void
    zyre_event_destroy (zyre_event_t **self_p);

// Returns event type, as printable uppercase string. Choices are:
// "ENTER", "EXIT", "JOIN", "LEAVE", "EVASIVE", "WHISPER" and "SHOUT"
// and for the local node: "STOP"
const char *
    zyre_event_type (zyre_event_t *self);

// Return the sending peer's uuid as a string
const char *
    zyre_event_peer_uuid (zyre_event_t *self);

// Return the sending peer's public name as a string
const char *
    zyre_event_peer_name (zyre_event_t *self);

// Return the sending peer's ipaddress as a string
const char *
    zyre_event_peer_addr (zyre_event_t *self);

// Returns the event headers, or NULL if there are none
zhash_t *
    zyre_event_headers (zyre_event_t *self);

// Returns value of a header from the message headers
// obtained by ENTER. Return NULL if no value was found.
const char *
    zyre_event_header (zyre_event_t *self, const char *name);

// Returns the group name that a SHOUT event was sent to
const char *
    zyre_event_group (zyre_event_t *self);

// Returns the incoming message payload; the caller can modify the
// message but does not own it and should not destroy it.
zmsg_t *
    zyre_event_msg (zyre_event_t *self);

// Returns the incoming message payload, and pass ownership to the
// caller. The caller must destroy the message when finished with it.
// After called on the given event, further calls will return NULL.
zmsg_t *
    zyre_event_get_msg (zyre_event_t *self);

// Print event to zsys log
void
    zyre_event_print (zyre_event_t *self);

// Self test of this class.
void
    zyre_event_test (bool verbose);

'''
cdefs = re.sub(r';[^;]*\bva_list\b[^;]*;', ';', cdefs, flags=re.S) # we don't support anything with a va_list arg

ffi.cdef(cdefs)
<|endoftext|>


<commit_before>#     Copyright 2017, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" Code generation for standard CPython/API calls.

This is generic stuff.
"""

from .ErrorCodes import (
    getErrorExitBoolCode,
    getErrorExitCode,
    getReleaseCode,
    getReleaseCodes
)
from .Helpers import generateExpressionCode

def generateCAPIObjectCodeCommon(to_name, capi, arg_desc, may_raise, ref_count,
                                 source_ref, emit, context, none_null = False):
    arg_names = []

    for arg_name, arg_expression in arg_desc:
        if arg_expression is None and none_null:
            arg_names.append("NULL")
        else:
            arg_name = context.allocateTempName(arg_name)

            generateExpressionCode(
                to_name    = arg_name,
                expression = arg_expression,
                emit       = emit,
                context    = context
            )

            arg_names.append(arg_name)

    context.setCurrentSourceCodeReference(source_ref)

    getCAPIObjectCode(
        to_name   = to_name,
        capi      = capi,
        arg_names = arg_names,
        may_raise = may_raise,
        ref_count = ref_count,
        emit      = emit,
        context   = context
    )

def generateCAPIObjectCode(to_name, capi, arg_desc, may_raise, source_ref, emit,
                           context, none_null = False):
    generateCAPIObjectCodeCommon(
        to_name    = to_name,
        capi       = capi,
        arg_desc   = arg_desc,
        may_raise  = may_raise,
        ref_count  = 1,
        source_ref = source_ref,
        emit       = emit,
        context    = context,
        none_null  = none_null
    )

def generateCAPIObjectCode0(to_name, capi, arg_desc, may_raise, source_ref,
                            emit, context, none_null = False):
    generateCAPIObjectCodeCommon(
        to_name    = to_name,
        capi       = capi,
        arg_desc   = arg_desc,
        may_raise  = may_raise,
        ref_count  = 0,
        source_ref = source_ref,
        emit       = emit,
        context    = context,
        none_null  = none_null
    )

def getCAPIObjectCode(to_name, capi, arg_names, may_raise, ref_count, emit,
                      context):
    emit(
        "%s = %s( %s );" % (
            to_name,
            capi,
            ", ".join(arg_names)
        )
    )

    getReleaseCodes(
        release_names = (
            arg_name
            for arg_name in
            arg_names
            if arg_name != "NULL"
        ),
        emit          = emit,
        context       = context
    )

    getErrorExitCode(
        check_name  = to_name,
        needs_check = may_raise,
        emit        = emit,
        context     = context
    )

    if ref_count:
        context.addCleanupTempName(to_name)

def getCAPIIntCode(res_name, capi, args, emit, context):
    emit(
        "%s = %s( %s );" % (
            res_name,
            capi,
            ", ".join(args)
        )
    )

    # TODO: Order, potentially
    for arg in args:
        getReleaseCode(
            release_name = arg,
            emit         = emit,
            context      = context
        )

    getErrorExitBoolCode(
        condition = "%s == -1" % res_name,
        emit      = emit,
        context   = context
    )

def getReferenceExportCode(base_name, context):
    if context.needsCleanup(base_name):
        return base_name
    else:
        return "INCREASE_REFCOUNT( %s )" % base_name

def getReferenceExportCode2(base_name, emit, context):
    if not context.needsCleanup(base_name):
        emit("Py_INCREF( %s );" % base_name)
<commit_msg>Cleanup, remove unused code.<commit_after>#     Copyright 2017, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" Code generation for standard CPython/API calls.

This is generic stuff.
"""

from .ErrorCodes import getErrorExitCode, getReleaseCodes
from .Helpers import generateExpressionCode

def generateCAPIObjectCodeCommon(to_name, capi, arg_desc, may_raise, ref_count,
                                 source_ref, emit, context, none_null = False):
    arg_names = []

    for arg_name, arg_expression in arg_desc:
        if arg_expression is None and none_null:
            arg_names.append("NULL")
        else:
            arg_name = context.allocateTempName(arg_name)

            generateExpressionCode(
                to_name    = arg_name,
                expression = arg_expression,
                emit       = emit,
                context    = context
            )

            arg_names.append(arg_name)

    context.setCurrentSourceCodeReference(source_ref)

    getCAPIObjectCode(
        to_name   = to_name,
        capi      = capi,
        arg_names = arg_names,
        may_raise = may_raise,
        ref_count = ref_count,
        emit      = emit,
        context   = context
    )

def generateCAPIObjectCode(to_name, capi, arg_desc, may_raise, source_ref, emit,
                           context, none_null = False):
    generateCAPIObjectCodeCommon(
        to_name    = to_name,
        capi       = capi,
        arg_desc   = arg_desc,
        may_raise  = may_raise,
        ref_count  = 1,
        source_ref = source_ref,
        emit       = emit,
        context    = context,
        none_null  = none_null
    )

def generateCAPIObjectCode0(to_name, capi, arg_desc, may_raise, source_ref,
                            emit, context, none_null = False):
    generateCAPIObjectCodeCommon(
        to_name    = to_name,
        capi       = capi,
        arg_desc   = arg_desc,
        may_raise  = may_raise,
        ref_count  = 0,
        source_ref = source_ref,
        emit       = emit,
        context    = context,
        none_null  = none_null
    )

def getCAPIObjectCode(to_name, capi, arg_names, may_raise, ref_count, emit,
                      context):
    emit(
        "%s = %s( %s );" % (
            to_name,
            capi,
            ", ".join(arg_names)
        )
    )

    getReleaseCodes(
        release_names = (
            arg_name
            for arg_name in
            arg_names
            if arg_name != "NULL"
        ),
        emit          = emit,
        context       = context
    )

    getErrorExitCode(
        check_name  = to_name,
        needs_check = may_raise,
        emit        = emit,
        context     = context
    )

    if ref_count:
        context.addCleanupTempName(to_name)

def getReferenceExportCode(base_name, context):
    if context.needsCleanup(base_name):
        return base_name
    else:
        return "INCREASE_REFCOUNT( %s )" % base_name

def getReferenceExportCode2(base_name, emit, context):
    if not context.needsCleanup(base_name):
        emit("Py_INCREF( %s );" % base_name)
<|endoftext|>


<commit_before>#! /usr/bin/env python3
# vim:ts=4:sw=4:ai:et:si:sts=4

import argparse
import logging
import json
from flask_jsonrpc.proxy import ServiceProxy
import os
import sys
import re
import time
import configparser

FORMAT = "%(asctime)s: %(name)s:%(lineno)d (%(threadName)s) - %(levelname)s - %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger(None).setLevel(logging.INFO)
logging.captureWarnings(True)
logger = logging.getLogger(__name__)

progname = os.path.basename(sys.argv[0])
if progname == "rpcclient.py" or progname == "common" or len(progname) < 7:
    logger.error("This must be run via a symlink")
    sys.exit(1)

# Strip the video_ off the beginning
progname = progname[6:]

# Common config file
configFile = os.path.expanduser("~/.video.cfg")
configData = configparser.ConfigParser()
configData.optionxform = str

try:
    configData.read(configFile)
except Exception:
    pass

config = {}
try:
    config.update({k: configData.get("default", k) for k in configData.options("default")})
except Exception:
    pass

try:
    config.update({k: configData.get("progname", k) for k in configData.options("progname")})
except Exception:
    pass

envVars = {
    "VID_PROJECT": "project",
    "VID_SERVERIP": "serverIP",
}

config.update({v: os.environ.get(k, None) for (k, v) in envVars.items() if k in os.environ})

nonProjectMethods = ["poll", "list_outstanding"]
parameters = {
    "common": {
        "arguments": [
            {
                "args": ["--debug", "-d"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use debug mode",
                }
            },
            {
                "args": ["--verbose", "-v"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use verbose mode",
                }
            },
            {
                "args": ["--project", "-p"],
                "kwargs": {
                    "action": "store",
                    "required": progname not in nonProjectMethods,
                    "help": "Video project to upload",
                }
            },
            {
                "args": ["--serverIP", "-i"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Specify the server's IP",
                }
            },
            {
                "args": ["--remoteIP", "-I"],
                "kwargs": {
                    "action": "store",
                    "default": "",
                    "help": "Override the client IP",
                }
            },
            {
                "args": ['--nopoll'],
                "kwargs": {
                    "action": "store_false",
                    "dest": "poll",
                    "help": "Disable the poll for completion",
                }
            },
        ],
    },
    "upload_inputs": {
        "description": "Upload input video files to server for processing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "args": ["--force", "-f"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Force deletion of old files on target",
                }
            }
        ]
    },
    "convert_inputs": {
        "description": "Convert videos to editable and proxy versions",
        "params": ["project", "files", "factor"],
        "arguments" : [
            {
                "args": ["--file", "-f"],
                "kwargs": {
                    "action": "append",
                    "dest": "files",
                    "help": "Choose specific files to run (one per --file)",
                }
            },
            {
                "args": ["--factor", "-F"],
                "kwargs": {
                    "action": "store",
                    "type": float,
                    "default": 0.5,
                    "help": "Set the shrink factor for proxy files (default %(default)s)",
                }
            }
        ]
    },
    "download_editables": {
        "description": "Download editable video files from server for editing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "include": "upload_inputs"
            }
        ]
    },
    "download_proxies": {
        "description": "Download proxy video files from server for editing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "include": "upload_inputs"
            }
        ]
    },
    "upload_edl": {
        "description": "Upload the EDL to the server",
        "params": ["project", "remoteIP", "edlfile"],
        "arguments": [
            {
                "args": ["--edlfile", '-e'],
                "kwargs": {
                    "required": True,
                    "action": "store",
                    "help": "The EDL File to send",
                }
            }
        ]
    },
    "upload_proxy_edl": {
        "description": "Upload the proxy EDL to the server",
        "params": ["project", "remoteIP", "edlfile"],
        "arguments": [
            {
                "include": "upload_edl"
            }
        ]
    },
    "render_edl": {
        "description": "Render the EDL file on the server",
        "params": ["project", "outfile", "edlfile", "proxy", "mode"],
        "arguments": [
            {
                "include": "upload_edl"
            },
            {
                "args": ["--outfile", '-o'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the output filename",
                }
            },
            {
                "args": ["--proxy", '-P'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Render using proxy files",
                }
            },
            {
                "args": ["--mode", '-m'],
                "kwargs": {
                    "action": "store",
                    "choices": ["cinelerra", "pitivi"],
                    "default": "pitivi",
                    "help": "Editing mode (default %(default)s)",
                },
            },
        ]
    },
    "upload_to_youtube": {
        "description": "Upload the output video to YouTube",
        "params": ["project", "outfile", "title", "description", "category",
                   "keywords"],
        "arguments": [
            {
                "args": ["--outfile", '-o'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the output filename",
                }
            },
            {
                "args": ["--title", "-t"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Title for the video",
                }
            },
            {
                "args": ["--description", "-D"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Description for the video",
                }
            },
            {
                "args": ["--category", "-c"],
                "kwargs": {
                    "action": "store",
                    "default": 28,
                    "type": int,
                    "help": "Category for the video (default %(default)s)",
                }
            },
            {
                "args": ["--keywords", "-k"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Keywords for the video (comma separated)",
                }
            },
        ]
    },
    "archive_to_s3": {
        "description": "Archive a project to S3",
        "params": ["project", "skip", "inputs", "delete", "accelerate"],
        "arguments": [
            {
                "args": ["--skip", '-s'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Skip uploading",
                }
            },
            {
                "args": ["--inputs"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Archive inputs too",
                }
            },
            {
                "args": ["--delete", '-D'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Delete project locally after upload",
                }
            },
            {
                "args": ["--accelerate", '-a'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use S3 Transfer Acceleration",
                }
            },
        ],
    },
    "poll": {
        "description": "Poll for completion of a task",
        "params": ["id"],
        "arguments": [
            {
                "args": ["--id", '-u'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the id to poll for",
                }
            }
        ]
    },
    "list_outstanding": {
        "description": "List outstanding tasks",
        "params": [],
        "arguments": [
        ]
    }
}

def add_parser_args(parser, progname):
    arguments = parameters.get(progname, {}).get("arguments", [])
    for arg in arguments:
        if "include" in arg:
            add_parser_args(parser, arg['include'])
            continue

        args = arg.get('args', [])
        kwargs = arg.get('kwargs', {})

        if kwargs.get('required', False):
            dests = [item.lstrip("-") for item in args]
            for item in dests:
                value = config.get(item, None)
                if value is not None:
                    kwargs["default"] = value
                    kwargs.pop("required", None)
                    break

        parser.add_argument(*args, **kwargs)

def print_response(response):
    global verbose
    if not verbose:
        result = response.get('result', None)
        if result is not None:
            if isinstance(result, dict):
                result = result.get('result', "")
                if result:
                    print(result)
                return 0

    print(json.dumps(response, indent=2))

    if "errors" in response:
        return 1
    return 0

if progname not in parameters:
    logger.error("RPC service %s is not defined" % progname)
    sys.exit(1)

parser = argparse.ArgumentParser(prog=progname,
            description=parameters[progname].get("description", None))
add_parser_args(parser, 'common')
add_parser_args(parser, progname)
args = parser.parse_args()

if progname in nonProjectMethods:
    args.poll = False

if args.debug:
    logging.getLogger(None).setLevel(logging.DEBUG)

if hasattr(args, "files") and not args.files:
    args.files = []

verbose = args.verbose

config.update(args.__dict__)

logger.info("Config: %s" % config)

apiurl = "http://%s:5005/api" % config.get("serverIP", None)
logger.info("Using service at %s" % apiurl)
proxy = ServiceProxy(apiurl)
apifunc = getattr(proxy.App, progname)

params = parameters[progname].get('params', [])
apiparams = {param: config.get(param, None) for param in params}

if progname != "poll":
    response = apifunc(**apiparams)

    retCode = print_response(response)

    if not config.get("poll", False):
        sys.exit(retCode)

    uuid = response['id']
else:
    uuid = config.get("id", None)

sleepTime = 0
while True:
    logger.info("Sleeping for %ss" % sleepTime)
    time.sleep(sleepTime)
    sleepTime = max(min(sleepTime * 2, 300), 10)

    response = proxy.App.poll(id=uuid)
    retCode = print_response(response)
    if retCode:
        output = None
        break

    output = response.get("result", {})
    if output.get("status", "complete") == "complete":
        break

sys.exit(retCode)
<commit_msg>Change the polling timing to start at 1s, go up to 256s max<commit_after>#! /usr/bin/env python3
# vim:ts=4:sw=4:ai:et:si:sts=4

import argparse
import logging
import json
from flask_jsonrpc.proxy import ServiceProxy
import os
import sys
import re
import time
import configparser

FORMAT = "%(asctime)s: %(name)s:%(lineno)d (%(threadName)s) - %(levelname)s - %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger(None).setLevel(logging.INFO)
logging.captureWarnings(True)
logger = logging.getLogger(__name__)

progname = os.path.basename(sys.argv[0])
if progname == "rpcclient.py" or progname == "common" or len(progname) < 7:
    logger.error("This must be run via a symlink")
    sys.exit(1)

# Strip the video_ off the beginning
progname = progname[6:]

# Common config file
configFile = os.path.expanduser("~/.video.cfg")
configData = configparser.ConfigParser()
configData.optionxform = str

try:
    configData.read(configFile)
except Exception:
    pass

config = {}
try:
    config.update({k: configData.get("default", k) for k in configData.options("default")})
except Exception:
    pass

try:
    config.update({k: configData.get("progname", k) for k in configData.options("progname")})
except Exception:
    pass

envVars = {
    "VID_PROJECT": "project",
    "VID_SERVERIP": "serverIP",
}

config.update({v: os.environ.get(k, None) for (k, v) in envVars.items() if k in os.environ})

nonProjectMethods = ["poll", "list_outstanding"]
parameters = {
    "common": {
        "arguments": [
            {
                "args": ["--debug", "-d"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use debug mode",
                }
            },
            {
                "args": ["--verbose", "-v"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use verbose mode",
                }
            },
            {
                "args": ["--project", "-p"],
                "kwargs": {
                    "action": "store",
                    "required": progname not in nonProjectMethods,
                    "help": "Video project to upload",
                }
            },
            {
                "args": ["--serverIP", "-i"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Specify the server's IP",
                }
            },
            {
                "args": ["--remoteIP", "-I"],
                "kwargs": {
                    "action": "store",
                    "default": "",
                    "help": "Override the client IP",
                }
            },
            {
                "args": ['--nopoll'],
                "kwargs": {
                    "action": "store_false",
                    "dest": "poll",
                    "help": "Disable the poll for completion",
                }
            },
        ],
    },
    "upload_inputs": {
        "description": "Upload input video files to server for processing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "args": ["--force", "-f"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Force deletion of old files on target",
                }
            }
        ]
    },
    "convert_inputs": {
        "description": "Convert videos to editable and proxy versions",
        "params": ["project", "files", "factor"],
        "arguments" : [
            {
                "args": ["--file", "-f"],
                "kwargs": {
                    "action": "append",
                    "dest": "files",
                    "help": "Choose specific files to run (one per --file)",
                }
            },
            {
                "args": ["--factor", "-F"],
                "kwargs": {
                    "action": "store",
                    "type": float,
                    "default": 0.5,
                    "help": "Set the shrink factor for proxy files (default %(default)s)",
                }
            }
        ]
    },
    "download_editables": {
        "description": "Download editable video files from server for editing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "include": "upload_inputs"
            }
        ]
    },
    "download_proxies": {
        "description": "Download proxy video files from server for editing",
        "params": ["project", "remoteIP", "force"],
        "arguments": [
            {
                "include": "upload_inputs"
            }
        ]
    },
    "upload_edl": {
        "description": "Upload the EDL to the server",
        "params": ["project", "remoteIP", "edlfile"],
        "arguments": [
            {
                "args": ["--edlfile", '-e'],
                "kwargs": {
                    "required": True,
                    "action": "store",
                    "help": "The EDL File to send",
                }
            }
        ]
    },
    "upload_proxy_edl": {
        "description": "Upload the proxy EDL to the server",
        "params": ["project", "remoteIP", "edlfile"],
        "arguments": [
            {
                "include": "upload_edl"
            }
        ]
    },
    "render_edl": {
        "description": "Render the EDL file on the server",
        "params": ["project", "outfile", "edlfile", "proxy", "mode"],
        "arguments": [
            {
                "include": "upload_edl"
            },
            {
                "args": ["--outfile", '-o'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the output filename",
                }
            },
            {
                "args": ["--proxy", '-P'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Render using proxy files",
                }
            },
            {
                "args": ["--mode", '-m'],
                "kwargs": {
                    "action": "store",
                    "choices": ["cinelerra", "pitivi"],
                    "default": "pitivi",
                    "help": "Editing mode (default %(default)s)",
                },
            },
        ]
    },
    "upload_to_youtube": {
        "description": "Upload the output video to YouTube",
        "params": ["project", "outfile", "title", "description", "category",
                   "keywords"],
        "arguments": [
            {
                "args": ["--outfile", '-o'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the output filename",
                }
            },
            {
                "args": ["--title", "-t"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Title for the video",
                }
            },
            {
                "args": ["--description", "-D"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Description for the video",
                }
            },
            {
                "args": ["--category", "-c"],
                "kwargs": {
                    "action": "store",
                    "default": 28,
                    "type": int,
                    "help": "Category for the video (default %(default)s)",
                }
            },
            {
                "args": ["--keywords", "-k"],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Keywords for the video (comma separated)",
                }
            },
        ]
    },
    "archive_to_s3": {
        "description": "Archive a project to S3",
        "params": ["project", "skip", "inputs", "delete", "accelerate"],
        "arguments": [
            {
                "args": ["--skip", '-s'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Skip uploading",
                }
            },
            {
                "args": ["--inputs"],
                "kwargs": {
                    "action": "store_true",
                    "help": "Archive inputs too",
                }
            },
            {
                "args": ["--delete", '-D'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Delete project locally after upload",
                }
            },
            {
                "args": ["--accelerate", '-a'],
                "kwargs": {
                    "action": "store_true",
                    "help": "Use S3 Transfer Acceleration",
                }
            },
        ],
    },
    "poll": {
        "description": "Poll for completion of a task",
        "params": ["id"],
        "arguments": [
            {
                "args": ["--id", '-u'],
                "kwargs": {
                    "action": "store",
                    "required": True,
                    "help": "Set the id to poll for",
                }
            }
        ]
    },
    "list_outstanding": {
        "description": "List outstanding tasks",
        "params": [],
        "arguments": [
        ]
    }
}

def add_parser_args(parser, progname):
    arguments = parameters.get(progname, {}).get("arguments", [])
    for arg in arguments:
        if "include" in arg:
            add_parser_args(parser, arg['include'])
            continue

        args = arg.get('args', [])
        kwargs = arg.get('kwargs', {})

        if kwargs.get('required', False):
            dests = [item.lstrip("-") for item in args]
            for item in dests:
                value = config.get(item, None)
                if value is not None:
                    kwargs["default"] = value
                    kwargs.pop("required", None)
                    break

        parser.add_argument(*args, **kwargs)

def print_response(response):
    global verbose
    if not verbose:
        result = response.get('result', None)
        if result is not None:
            if isinstance(result, dict):
                result = result.get('result', "")
                if result:
                    print(result)
                return 0

    print(json.dumps(response, indent=2))

    if "errors" in response:
        return 1
    return 0

if progname not in parameters:
    logger.error("RPC service %s is not defined" % progname)
    sys.exit(1)

parser = argparse.ArgumentParser(prog=progname,
            description=parameters[progname].get("description", None))
add_parser_args(parser, 'common')
add_parser_args(parser, progname)
args = parser.parse_args()

if progname in nonProjectMethods:
    args.poll = False

if args.debug:
    logging.getLogger(None).setLevel(logging.DEBUG)

if hasattr(args, "files") and not args.files:
    args.files = []

verbose = args.verbose

config.update(args.__dict__)

logger.info("Config: %s" % config)

apiurl = "http://%s:5005/api" % config.get("serverIP", None)
logger.info("Using service at %s" % apiurl)
proxy = ServiceProxy(apiurl)
apifunc = getattr(proxy.App, progname)

params = parameters[progname].get('params', [])
apiparams = {param: config.get(param, None) for param in params}

if progname != "poll":
    response = apifunc(**apiparams)

    retCode = print_response(response)

    if not config.get("poll", False):
        sys.exit(retCode)

    uuid = response['id']
else:
    uuid = config.get("id", None)

sleepTime = 0
while True:
    sleepTime = max(min(sleepTime * 2, 256), 1)
    logger.info("Sleeping for %ss" % sleepTime)
    time.sleep(sleepTime)

    response = proxy.App.poll(id=uuid)
    retCode = print_response(response)
    if retCode:
        output = None
        break

    output = response.get("result", {})
    if output.get("status", "complete") == "complete":
        break

sys.exit(retCode)
<|endoftext|>


<commit_before>"""Unit tests for MAU101."""
from __future__ import absolute_import

from .check import Check
from proselint.checks.garner import a_vs_an as chk

class TestCheck(Check):

    """Test garner.a_vs_n."""

    __test__ = True

    @property
    def this_check(self):
        """Boilerplate."""
        return chk

    def test(self):
        """Ensure the test works correctly."""
        assert self.passes("""An apple a day keeps the doctor away.""")
        assert self.passes("""The Epicurean garden.""")
        assert not self.passes("""A apple a day keeps the doctor away.""")
        assert not self.passes("""An apple an day keeps the doctor away.""")
        assert not self.passes("""An apple an\nday keeps the doctor away.""")
<commit_msg>Remove a vs. n test<commit_after><|endoftext|>


<commit_before>import pytest

import python_jsonschema_objects as pjo

@pytest.fixture
def base_schema():
    return {
        'title': 'example',
        'type': 'object',
        "additionalProperties": False,
        "properties": {
          "dictLike": {
            "additionalProperties": {
              "type": "integer"
            },
            "type": "object"
          }
        }
    }

def test_wrong_exception_protocolbase_getitem(base_schema):
    """
    to declare a dict like object in json-schema, we are supposed
    to declare it as an object of additional properties.
    When trying to use it as dict, for instance testing if a key is inside
    the dictionary, methods like __contains__ in the ProtocolBase expect
    __getitem__ to raise a KeyError. getitem calls __getattr__ without any
    exception handling, which raises an AttributeError (necessary for proper
    behaviour of getattr, for instance).
    Solution found is to handle AttributeError in getitem and to raise KeyError
    """
    builder = pjo.ObjectBuilder(base_schema)
    ns = builder.build_classes()

    t = ns.Example(dictLike={'a': 0, 'b': 1})
    t.validate()
    assert 'a' in t.dictLike
    assert not 'c' in t.dictLike
    assert getattr(t, 'not_present', None) is None

if __name__ == '__main__':
    test_wrong_exception_protocolbase_getitem(base_schema())
<commit_msg>correct code formatting<commit_after>import pytest

import python_jsonschema_objects as pjo

@pytest.fixture
def base_schema():
    return {
        'title': 'example',
        'type': 'object',
        "additionalProperties": False,
        "properties": {
          "dictLike": {
            "additionalProperties": {
              "type": "integer"
            },
            "type": "object"
          }
        }
    }

def test_wrong_exception_protocolbase_getitem(base_schema):
    """
    to declare a dict like object in json-schema, we are supposed
    to declare it as an object of additional properties.
    When trying to use it as dict, for instance testing if a key is inside
    the dictionary, methods like __contains__ in the ProtocolBase expect
    __getitem__ to raise a KeyError. getitem calls __getattr__ without any
    exception handling, which raises an AttributeError (necessary for proper
    behaviour of getattr, for instance).
    Solution found is to handle AttributeError in getitem and to raise KeyError
    """
    builder = pjo.ObjectBuilder(base_schema)
    ns = builder.build_classes()

    t = ns.Example(dictLike={'a': 0, 'b': 1})
    t.validate()
    assert 'a' in t.dictLike
    assert 'c' not in t.dictLike
    assert getattr(t, 'not_present', None) is None

if __name__ == '__main__':
    test_wrong_exception_protocolbase_getitem(base_schema())
<|endoftext|>


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

# Copyright (c) 2010 LE GOFF Vincent
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""Fichier contenant le masque <nombre>."""

from primaires.interpreteur.masque.masque import Masque
from primaires.interpreteur.masque.fonctions import *
from primaires.interpreteur.masque.exceptions.erreur_validation \
        import ErreurValidation

class Nombre(Masque):

    """Masque <nombre>.
    On attend un nombre en paramètre.

    """

    nom = "nombre"
    nom_complet = "nombre"

    def init(self):
        """Initialisation des attributs"""
        self.nombre = None

    def valider(self, personnage, dic_masques, commande):
        """Validation du masque"""
        Masque.valider(self, personnage, dic_masques, commande)
        str_nombre = liste_vers_chaine(commande).lstrip()
        str_nombre = str_nombre.split(" ")[0]

        if not str_nombre:
            raise ErreurValidation( \
                "Précisez un nombre.")

        try:
            nombre = int(str_nombre)
        except ValueError:
            raise ErreurValidation( \
                "Ce nombre est invalide.")

        print(str_nombre, nombre)
        self.nombre = nombre

        commande[:] = commande[len(str_nombre) + 1:]
        return True
<commit_msg>Ajout d'une vérification pour empêcher l'entrée de nombre négatif : Cette limitation est contrôlable par des propriétés. Par défaut, on ne peut rentrer de nombre <= 0.<commit_after># -*-coding:Utf-8 -*

# Copyright (c) 2010 LE GOFF Vincent
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""Fichier contenant le masque <nombre>."""

from primaires.interpreteur.masque.masque import Masque
from primaires.interpreteur.masque.fonctions import *
from primaires.interpreteur.masque.exceptions.erreur_validation \
        import ErreurValidation

class Nombre(Masque):

    """Masque <nombre>.
    On attend un nombre en paramètre.

    """

    nom = "nombre"
    nom_complet = "nombre"

    def init(self):
        """Initialisation des attributs"""
        self.nombre = None
        self.limite_inf = 1
        self.limite_sup = None

    def valider(self, personnage, dic_masques, commande):
        """Validation du masque"""
        Masque.valider(self, personnage, dic_masques, commande)
        str_nombre = liste_vers_chaine(commande).lstrip()
        str_nombre = str_nombre.split(" ")[0]

        if not str_nombre:
            raise ErreurValidation( \
                "Précisez un nombre.")

        try:
            nombre = int(str_nombre)
            if self.limite_inf is not None:
                assert nombre >= self.limite_inf
            if self.limite_sup is not None:
                assert nombre <= self.limite_sup
        except (ValueError, AssertionError):
            raise ErreurValidation( \
                "Ce nombre est invalide.")

        print(str_nombre, nombre)
        self.nombre = nombre

        commande[:] = commande[len(str_nombre) + 1:]
        return True
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from .metrics import _AutoscalingMetric

class Autoscaling(object):
    """
    Represents autoscaling configuration for Endpoint, to be passed to
    :meth:`Endpoint.update() <verta.endpoint._endpoint.Endpoint.update>`.

    The JSON equivalent for this is:

    .. code-block:: json

        {
            "autoscaling": {
                "quantities": {"min_replicas": 2, "max_replicas": 7, "min_scale": 0.2, "max_scale": 0.7},
                "metrics": []
            }
        }

    Parameters
    ----------
    min_replicas : int
        Minimum number of replicas to scale down to.
    max_replicas : int
        Maximum number of replicas to scale up to.
    min_scale : float in (0, 1]
        Minimum growth factor for scaling.
    max_scale : float in (0, 1]
        Maximum growth factor for scaling.

    """
    def __init__(self, min_replicas=None, max_replicas=None, min_scale=None, max_scale=None):
        self._min_replicas = min_replicas
        self._max_replicas = max_replicas
        self._min_scale = min_scale
        self._max_scale = max_scale
        self._metrics = []

    def _as_dict(self):
        return {
            "quantities": {
                "min_replicas": self._min_replicas,
                "max_replicas": self._max_replicas,
                "min_scale": self._min_scale,
                "max_scale": self._max_scale
            },
            "metrics": list(map(lambda metric: metric._as_dict(), self._metrics))
        }

    @classmethod
    def _from_dict(cls, autoscaling_dict):
        return cls(
            autoscaling_dict.get("min_replicas"),
            autoscaling_dict.get("max_replicas"),
            autoscaling_dict.get("min_scale"),
            autoscaling_dict.get("max_scale"),
        )

    def add_metric(self, metric):
        """
        Adds a metric.

        Parameters
        ----------
        metric : :ref:`autoscaling metric <autoscaling-metrics>`
            Metric to add.

        """
        if not isinstance(metric, _AutoscalingMetric):
            raise TypeError("`metric` must be an object from verta.endpoint.autoscaling.metrics")

        self._metrics.append(metric)
<commit_msg>VR-6720 Update client docs not restrict autoscaling *_scale to (0, 1] (#1556)<commit_after># -*- coding: utf-8 -*-
from .metrics import _AutoscalingMetric

class Autoscaling(object):
    """
    Represents autoscaling configuration for Endpoint, to be passed to
    :meth:`Endpoint.update() <verta.endpoint._endpoint.Endpoint.update>`.

    The JSON equivalent for this is:

    .. code-block:: json

        {
            "autoscaling": {
                "quantities": {"min_replicas": 2, "max_replicas": 7, "min_scale": 0.2, "max_scale": 0.7},
                "metrics": []
            }
        }

    Parameters
    ----------
    min_replicas : int
        Minimum number of replicas to scale down to.
    max_replicas : int
        Maximum number of replicas to scale up to.
    min_scale : float in (0, 1)
        Minimum growth factor for scaling.
    max_scale : float > 1
        Maximum growth factor for scaling.

    """
    def __init__(self, min_replicas=None, max_replicas=None, min_scale=None, max_scale=None):
        self._min_replicas = min_replicas
        self._max_replicas = max_replicas
        self._min_scale = min_scale
        self._max_scale = max_scale
        self._metrics = []

    def _as_dict(self):
        return {
            "quantities": {
                "min_replicas": self._min_replicas,
                "max_replicas": self._max_replicas,
                "min_scale": self._min_scale,
                "max_scale": self._max_scale
            },
            "metrics": list(map(lambda metric: metric._as_dict(), self._metrics))
        }

    @classmethod
    def _from_dict(cls, autoscaling_dict):
        return cls(
            autoscaling_dict.get("min_replicas"),
            autoscaling_dict.get("max_replicas"),
            autoscaling_dict.get("min_scale"),
            autoscaling_dict.get("max_scale"),
        )

    def add_metric(self, metric):
        """
        Adds a metric.

        Parameters
        ----------
        metric : :ref:`autoscaling metric <autoscaling-metrics>`
            Metric to add.

        """
        if not isinstance(metric, _AutoscalingMetric):
            raise TypeError("`metric` must be an object from verta.endpoint.autoscaling.metrics")

        self._metrics.append(metric)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#coding=utf8
'''
Headmouse!
'''

import serial
import time
import threading
import sys

import cv2

import hmCam
import hmFilterData as filter
import arduinoSerial

CAMERA_ID = 0
ARDUINO_PORT = 'COM7'
INPUT_VISUALIZER = True

def main():
    # output driver setup
    arduino = arduinoSerial.get_serial_link(ARDUINO_PORT, 115200, timeout=1, async=False, slices=8)

    # input driver setup
    hmCam.displayWindow = INPUT_VISUALIZER
    hmCam.bind(CAMERA_ID)

    def input_source():
    	while True:
    		yield hmCam.popAndAnalyze()

    # signal proc chain setup
    velocity_gen = filter.relative_movement()
    sub_pix_gen = filter.sub_pix_trunc()
    stateful_smooth_gen = filter.stateful_smoother()
    input_smoother_gen = filter.ema_smoother(.90)
    slow_smoother_gen = filter.slow_smoother(.6)
    acceleration_gen = filter.accelerate_exp(p=2, accel=1.4, sensitivity=6.5)

    # main loop setup
    startTime = time.time()
    timeC = 0
    loops = 0

    # main loop
    for coords in input_source():
        loops += 1
        timeC += time.time() - startTime
        if loops == 100:
            loops = 0
            print "fps is around:", 100. / timeC
            timeC = 0
        #print "time took:", time.time() - startTime
        startTime = time.time()
        # Capture frame-by-frame

        ### Filter Section ###
        #Take absolute position return relative position
        v = velocity_gen.send(coords)
        v = filter.killOutliers(v, 20)

        v = slow_smoother_gen.send((v, 6))
        v = input_smoother_gen.send(v)
        v = acceleration_gen.send(v)
        #v = filter.accelerate(v)

        v = sub_pix_gen.send(v)

        #Mirror image on x-axis
        x = -v[0]
        y = v[1]

        #Duplicate in Arduino
        #print "coords are:" + x + ", " + y

        arduino.move_mouse(x,y)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # input driver cleanup
    # When everything done, release the capture
    hmCam.cleanup()

    return 0

if __name__ == "__main__":
    sys.exit(main())

<commit_msg>Settings modified<commit_after>#!/usr/bin/env python
#coding=utf8
'''
Headmouse!
'''

import serial
import time
import threading
import sys

import cv2

import hmCam
import hmFilterData as filter
import arduinoSerial

CAMERA_ID = 0
ARDUINO_PORT = 'COM7'
INPUT_VISUALIZER = True

def main():
    # output driver setup
    arduino = arduinoSerial.get_serial_link(ARDUINO_PORT, 115200, timeout=1, async=False, slices=8)

    # input driver setup
    hmCam.displayWindow = INPUT_VISUALIZER
    hmCam.bind(CAMERA_ID)

    def input_source():
    	while True:
    		yield hmCam.popAndAnalyze()

    # signal proc chain setup
    velocity_gen = filter.relative_movement()
    sub_pix_gen = filter.sub_pix_trunc()
    stateful_smooth_gen = filter.stateful_smoother()
    input_smoother_gen = filter.ema_smoother(.90)
    #slow_smoother_gen = filter.slow_smoother(.6)
    acceleration_gen = filter.accelerate_exp(p=2, accel=2.3, sensitivity=2)

    # main loop setup
    startTime = time.time()
    timeC = 0
    loops = 0

    # main loop
    for coords in input_source():
        loops += 1
        timeC += time.time() - startTime
        if loops == 100:
            loops = 0
            print "fps is around:", 100. / timeC
            timeC = 0
        #print "time took:", time.time() - startTime
        startTime = time.time()
        # Capture frame-by-frame

        ### Filter Section ###
        #Take absolute position return relative position
        v = velocity_gen.send(coords)
        v = filter.killOutliers(v, 20)

        #v = slow_smoother_gen.send((v, 6))
        v = input_smoother_gen.send(v)
        v = acceleration_gen.send(v)
        #v = filter.accelerate(v)

        v = sub_pix_gen.send(v)

        #Mirror image on x-axis
        x = -v[0]
        y = v[1]

        #Duplicate in Arduino
        #print "coords are:" + x + ", " + y

        arduino.move_mouse(x,y)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # input driver cleanup
    # When everything done, release the capture
    hmCam.cleanup()

    return 0

if __name__ == "__main__":
    sys.exit(main())

<|endoftext|>


<commit_before>#!/usr/bin/python
#
# Copyright 2011 Friday Film Club. All Rights Reserved.
"""Handles the Movie Auto Complete UI."""

__author__ = 'adamjmcgrath@gmail.com (Adam McGrath)'

import logging
import os
import re

import webapp2
from google.appengine.api import memcache
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers

from mapreduce import control, mapreduce_pipeline
from mapreduce.model import MapreduceState

import baserequesthandler
import forms
import map_reduce
import models
import settings

class HomePage(baserequesthandler.RequestHandler):
  """Shows the homepage."""

  def get(self):
    return self.render_template('admin/index.html', {})

class AddFilms(baserequesthandler.RequestHandler):
  """Form for uploading films as a CSV file as '{Year}, {Title}'."""

  def get(self):
    upload_url = blobstore.create_upload_url('/admin/addfilmshandler')

    return self.render_template('admin/addfilms.html', {
        'upload_url': upload_url
    })

class AddEditQuestion(baserequesthandler.RequestHandler):
  """Adds a question to the datastore."""

  def get(self, key=None):
    if key:
      question_entity = ndb.Key('Question', key).get()

      form = forms.Question(obj=question_entity)
    else:
      question_entity = None
      form = forms.Question()

    return self.render_template('admin/addquestion.html', {
        'form': form,
        'question': question_entity,
    })

  def post(self, key=None):
    form = forms.Question(formdata=self.request.POST)

    if key:
      question_entity = ndb.Key('Question', key).get()
    else:
      question_entity = models.Question()

    logging.info(key)
    logging.info(question_entity)
    logging.info(models.Question())

    if form.validate():
      # TODO(adamjmcgrath): only put once.
      logging.info('1')
      logging.info(question_entity)
      question_entity.put()
      form.populate_obj(question_entity)
      logging.info('2')
      logging.info(question_entity)
      question_entity.put()
      return webapp2.redirect('/admin/questions')
    else:
      return self.render_template('admin/addquestion.html', {
          'form': form,
          'question': question_entity,
          'debug': self.request.get('debug')
      })

class Questions(baserequesthandler.RequestHandler):
  """Adds a question to the datastore."""

  def get(self):
    return self.render_template('admin/questions.html', {
      'questions': models.Question.query(),
    })

class AddFilmsHandler(blobstore_handlers.BlobstoreUploadHandler):
  """Processes the uploaded films and create's a blob to upoad and index."""

  def post(self):
    upload_files = self.get_uploads('file')
    blob_info = upload_files[0]

    batch = 1
    last_add = models.Film.query().order(-models.Film.batch).get()
    if last_add:
      batch += last_add.batch

    logging.info('Uploading film batch: %d.' % batch)

    mapreduce_parameters = {
      'blob_key': str(blob_info.key()),
      'done_callback': '/admin/addfilmsdone',
    }

    mapper_parameters = {
      'blob_keys': str(blob_info.key()),
      'batch': batch
    }

    map_reduce_id = control.start_map(
        'Add films to datastore.', # Name
        'map_reduce.add_film_map', # handler_spec
        'mapreduce.input_readers.BlobstoreLineInputReader', # reader_spec
        mapper_parameters, # mapper_parameters
        mapreduce_parameters=mapreduce_parameters)

    return webapp2.redirect('/admin')

class AddFilmsDone(baserequesthandler.RequestHandler):
  """Delete the blob once the films have been added to the datastore."""

  def post(self):
    logging.info('Add Movies Complete.')
    mr_id = self.request.headers['Mapreduce-Id']
    mr_state = MapreduceState.get_by_key_name(mr_id)
    mr_spec = mr_state.mapreduce_spec
    json_spec = mr_spec.to_json()
    json_params = json_spec['params']
    blob_key = json_params['blob_key']
    blob_info = blobstore.BlobInfo.get(blob_key)
    blob_info.delete()
    logging.info('Temp blob deleted for mapreduce: %s' % mr_id)

class IndexFilms(baserequesthandler.RequestHandler):
  """Index's the films."""

  def post(self):
    logging.info('Start indexing films.')
    pipeline = map_reduce.IndexerPipeline()
    pipeline.start()

    return webapp2.redirect('/admin')
<commit_msg>Remove spurious logging<commit_after>#!/usr/bin/python
#
# Copyright 2011 Friday Film Club. All Rights Reserved.
"""Handles the Movie Auto Complete UI."""

__author__ = 'adamjmcgrath@gmail.com (Adam McGrath)'

import logging
import os
import re

import webapp2
from google.appengine.api import memcache
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers

from mapreduce import control, mapreduce_pipeline
from mapreduce.model import MapreduceState

import baserequesthandler
import forms
import map_reduce
import models
import settings

class HomePage(baserequesthandler.RequestHandler):
  """Shows the homepage."""

  def get(self):
    return self.render_template('admin/index.html', {})

class AddFilms(baserequesthandler.RequestHandler):
  """Form for uploading films as a CSV file as '{Year}, {Title}'."""

  def get(self):
    upload_url = blobstore.create_upload_url('/admin/addfilmshandler')

    return self.render_template('admin/addfilms.html', {
        'upload_url': upload_url
    })

class AddEditQuestion(baserequesthandler.RequestHandler):
  """Adds a question to the datastore."""

  def get(self, key=None):
    if key:
      question_entity = ndb.Key('Question', key).get()

      form = forms.Question(obj=question_entity)
    else:
      question_entity = None
      form = forms.Question()

    return self.render_template('admin/addquestion.html', {
        'form': form,
        'question': question_entity,
    })

  def post(self, key=None):
    form = forms.Question(formdata=self.request.POST)

    if key:
      question_entity = ndb.Key('Question', key).get()
    else:
      question_entity = models.Question()

    if form.validate():
      # TODO(adamjmcgrath): only put once.
      question_entity.put()
      form.populate_obj(question_entity)
      question_entity.put()
      return webapp2.redirect('/admin/questions')
    else:
      return self.render_template('admin/addquestion.html', {
          'form': form,
          'question': question_entity,
          'debug': self.request.get('debug')
      })

class Questions(baserequesthandler.RequestHandler):
  """Adds a question to the datastore."""

  def get(self):
    return self.render_template('admin/questions.html', {
      'questions': models.Question.query(),
    })

class AddFilmsHandler(blobstore_handlers.BlobstoreUploadHandler):
  """Processes the uploaded films and create's a blob to upoad and index."""

  def post(self):
    upload_files = self.get_uploads('file')
    blob_info = upload_files[0]

    batch = 1
    last_add = models.Film.query().order(-models.Film.batch).get()
    if last_add:
      batch += last_add.batch

    logging.info('Uploading film batch: %d.' % batch)

    mapreduce_parameters = {
      'blob_key': str(blob_info.key()),
      'done_callback': '/admin/addfilmsdone',
    }

    mapper_parameters = {
      'blob_keys': str(blob_info.key()),
      'batch': batch
    }

    map_reduce_id = control.start_map(
        'Add films to datastore.', # Name
        'map_reduce.add_film_map', # handler_spec
        'mapreduce.input_readers.BlobstoreLineInputReader', # reader_spec
        mapper_parameters, # mapper_parameters
        mapreduce_parameters=mapreduce_parameters)

    return webapp2.redirect('/admin')

class AddFilmsDone(baserequesthandler.RequestHandler):
  """Delete the blob once the films have been added to the datastore."""

  def post(self):
    logging.info('Add Movies Complete.')
    mr_id = self.request.headers['Mapreduce-Id']
    mr_state = MapreduceState.get_by_key_name(mr_id)
    mr_spec = mr_state.mapreduce_spec
    json_spec = mr_spec.to_json()
    json_params = json_spec['params']
    blob_key = json_params['blob_key']
    blob_info = blobstore.BlobInfo.get(blob_key)
    blob_info.delete()
    logging.info('Temp blob deleted for mapreduce: %s' % mr_id)

class IndexFilms(baserequesthandler.RequestHandler):
  """Index's the films."""

  def post(self):
    logging.info('Start indexing films.')
    pipeline = map_reduce.IndexerPipeline()
    pipeline.start()

    return webapp2.redirect('/admin')
<|endoftext|>


<commit_before>import os, tempfile

import pytest

from .. import exceptions

def test_open_error():
    with pytest.raises(exceptions.OpenError):
        open('/not-a-file')

def test_permission_error():
    with tempfile.NamedTemporaryFile() as tmp:
        tmp.write('')
    os.chmod(tmp.name, 000)
    with pytest.raises(exceptions.PermissionError):
        open(tmp.name, 'w').write('stuff')
    os.remomve(tmp.name)

def test_delete_error():
    with pytest.raises(exceptions.DeleteError):
        os.remove('/not-a-file')

def test_file_exists_error():
    with pytest.raises(exceptions.FileExistsError):
        os.mkdir('/')

def test_out_of_space(exception):
    import warnings
    warnings.warn('out_of_space is not tested.')
<commit_msg>test_exceptions<commit_after>import os, tempfile

import pytest

from .. import exceptions

def test_open_error():
    with pytest.raises(exceptions.OpenError):
        open('/not-a-file')

def test_permission_error():
    tmp = tempfile.NamedTemporaryFile()
    os.chmod(tmp.name, 000)
    with pytest.raises(exceptions.PermissionError):
        open(tmp.name, 'w').write('stuff')
    os.remove(tmp.name)

def test_delete_error():
    with pytest.raises(exceptions.DeleteError):
        os.remove('/not-a-file')

def test_file_exists_error():
    with pytest.raises(exceptions.FileExistsError):
        os.mkdir('/')

@pytest.mark.skipif(True, reason = 'Running out of space in a test is annoying.')
def test_out_of_space():
    raise NotImplementedError
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import mako
import os
import requests
import subprocess
from girder import constants, events
from girder.constants import SettingKey
from girder.utility.model_importer import ModelImporter
from .imagefeatures_rest import ImageFeatures
from .imagepivot_rest import ImagePivot
from .imagesearch_rest import ImageSearch
from .imageprefix_rest import ImagePrefix
from .settings import ImageSpaceSetting

imagespaceSetting = ImageSpaceSetting()

class CustomAppRoot(object):
    """
    This serves the main index HTML file of the custom app from /
    """
    exposed = True

    indexHtml = None

    vars = {
        'apiRoot': 'api/v1',
        'staticRoot': 'static',
        'title': 'ImageSpace',
        'versionInfo': {
            'niceName': 'SUG v3.0',
            'sha': subprocess.check_output(
                ['git', 'rev-parse', '--short', 'HEAD'],
                cwd=os.path.dirname(os.path.realpath(__file__))
            ).strip()
        }
    }

    template = r"""
    <!DOCTYPE html>

        <title>${title}</title>
        <link rel="stylesheet"
              href="//fonts.googleapis.com/css?family=Droid+Sans:400,700">
        <link rel="stylesheet"
              href="${staticRoot}/lib/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet"
              href="${staticRoot}/lib/fontello/css/fontello.css">
        <link rel="stylesheet"
              href="${staticRoot}/lib/fontello/css/animation.css">
        <link rel="stylesheet"
              href="${staticRoot}/built/app.min.css">
        % for plugin in pluginCss:
            % if plugin != 'imagespace':
        <link rel="stylesheet"
              href="${staticRoot}/built/plugins/${plugin}/plugin.min.css">
            % endif
        % endfor
        <link rel="stylesheet"
              href="${staticRoot}/built/plugins/imagespace/imagespace.min.css">
        <link rel="icon"
              type="image/png"
              href="${staticRoot}/img/Girder_Favicon.png">

${apiRoot}

${staticRoot}

         % for plugin in pluginJs:
           % if plugin != 'imagespace':

           % endif
        % endfor

    """

    def GET(self):
        self.vars['pluginCss'] = []
        self.vars['pluginJs'] = []
        builtDir = os.path.join(constants.STATIC_ROOT_DIR, 'clients', 'web',
                                'static', 'built', 'plugins')
        for plugin in ModelImporter.model('setting').get(
                SettingKey.PLUGINS_ENABLED):
            if os.path.exists(os.path.join(builtDir, plugin, 'plugin.min.css')):
                self.vars['pluginCss'].append(plugin)
            if os.path.exists(os.path.join(builtDir, plugin, 'plugin.min.js')):
                self.vars['pluginJs'].append(plugin)

        if self.indexHtml is None:
            self.indexHtml = mako.template.Template(self.template).render(
                **self.vars)

        return self.indexHtml

def load(info):
    for setting in ImageSpaceSetting.requiredSettings:
        imagespaceSetting.get(setting)

    # Absolute path to a directory of images to serve statically at /basename
    image_dir = imagespaceSetting.get('IMAGE_SPACE_IMAGE_DIR')

    if image_dir:
        info['config']['/images'] = {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': image_dir
        }

    # Bind our REST resources
    info['apiRoot'].imagesearch = ImageSearch()
    info['apiRoot'].imagefeatures = ImageFeatures()
    info['apiRoot'].imagepivot = ImagePivot()
    info['apiRoot'].imageprefix = ImagePrefix()

    # Move girder app to /girder, serve our custom app from /
    info['serverRoot'], info['serverRoot'].girder = (CustomAppRoot(),
                                                     info['serverRoot'])
    info['serverRoot'].api = info['serverRoot'].girder.api

def solr_documents_from_field(field, values, classifications=None):
    """Given a field, and a list of values, return list of relevant solr documents.

    Additionally it can take an iterable of classifications which will be
    searched for through Solr.

    :param paths: List of solr paths corresponding to the Solr id attribute
    :param classifications: List of classifications to search by
    :returns: List of solr documents
    """
    def paged_request(params):
        """
        Takes a params dictionary and manages paging.

        Uses POST so very large request bodies can be sent to Solr.

        Returns a list of all documents.
        """
        documents = []

        # Adjust paging params
        params['start'] = 0
        params['rows'] = 1000

        numFound = None
        numRetrieved = None
        while numRetrieved is None or numRetrieved < numFound:
            r = requests.post(imagespaceSetting.get('IMAGE_SPACE_SOLR') + '/select',
                              data=params,
                              verify=False).json()

            numFound = r['response']['numFound']
            numRetrieved = len(r['response']['docs']) if numRetrieved is None \
                           else numRetrieved + len(r['response']['docs'])
            documents += r['response']['docs']

            # Setup offset for next request
            params['start'] = numRetrieved

        return documents

    event = events.trigger('imagespace.solr_documents_from_field', info={
        'field': field,
        'values': values
    })
    for response in event.responses:
        field = response['field']
        values = response['values']

    if classifications:
        q = ' OR '.join(['%s:[.7 TO *]' % key
                         for key in classifications])
    else:
        q = '*:*'

    qparams = {
        'wt': 'json',
        'q': q
    }

    # Give plugins a chance to adjust the Solr query parameters
    event = events.trigger('imagespace.imagesearch.qparams', qparams)
    for response in event.responses:
        qparams = response

    # Filter by field
    qparams['fq'] = qparams['fq'] if 'fq' in qparams else []
    qparams['fq'].append('%(field)s:(%(value)s)' % {
        'field': field,
        'value': ' '.join(values)
    })

    return paged_request(qparams)
<commit_msg>Changing title to VideoSpace<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import mako
import os
import requests
import subprocess
from girder import constants, events
from girder.constants import SettingKey
from girder.utility.model_importer import ModelImporter
from .imagefeatures_rest import ImageFeatures
from .imagepivot_rest import ImagePivot
from .imagesearch_rest import ImageSearch
from .imageprefix_rest import ImagePrefix
from .settings import ImageSpaceSetting

imagespaceSetting = ImageSpaceSetting()

class CustomAppRoot(object):
    """
    This serves the main index HTML file of the custom app from /
    """
    exposed = True

    indexHtml = None

    vars = {
        'apiRoot': 'api/v1',
        'staticRoot': 'static',
        'title': 'VideoSpace',
        'versionInfo': {
            'niceName': 'SUG v3.0',
            'sha': subprocess.check_output(
                ['git', 'rev-parse', '--short', 'HEAD'],
                cwd=os.path.dirname(os.path.realpath(__file__))
            ).strip()
        }
    }

    template = r"""
    <!DOCTYPE html>

        <title>${title}</title>
        <link rel="stylesheet"
              href="//fonts.googleapis.com/css?family=Droid+Sans:400,700">
        <link rel="stylesheet"
              href="${staticRoot}/lib/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet"
              href="${staticRoot}/lib/fontello/css/fontello.css">
        <link rel="stylesheet"
              href="${staticRoot}/lib/fontello/css/animation.css">
        <link rel="stylesheet"
              href="${staticRoot}/built/app.min.css">
        % for plugin in pluginCss:
            % if plugin != 'imagespace':
        <link rel="stylesheet"
              href="${staticRoot}/built/plugins/${plugin}/plugin.min.css">
            % endif
        % endfor
        <link rel="stylesheet"
              href="${staticRoot}/built/plugins/imagespace/imagespace.min.css">
        <link rel="icon"
              type="image/png"
              href="${staticRoot}/img/Girder_Favicon.png">

${apiRoot}

${staticRoot}

         % for plugin in pluginJs:
           % if plugin != 'imagespace':

           % endif
        % endfor

    """

    def GET(self):
        self.vars['pluginCss'] = []
        self.vars['pluginJs'] = []
        builtDir = os.path.join(constants.STATIC_ROOT_DIR, 'clients', 'web',
                                'static', 'built', 'plugins')
        for plugin in ModelImporter.model('setting').get(
                SettingKey.PLUGINS_ENABLED):
            if os.path.exists(os.path.join(builtDir, plugin, 'plugin.min.css')):
                self.vars['pluginCss'].append(plugin)
            if os.path.exists(os.path.join(builtDir, plugin, 'plugin.min.js')):
                self.vars['pluginJs'].append(plugin)

        if self.indexHtml is None:
            self.indexHtml = mako.template.Template(self.template).render(
                **self.vars)

        return self.indexHtml

def load(info):
    for setting in ImageSpaceSetting.requiredSettings:
        imagespaceSetting.get(setting)

    # Absolute path to a directory of images to serve statically at /basename
    image_dir = imagespaceSetting.get('IMAGE_SPACE_IMAGE_DIR')

    if image_dir:
        info['config']['/images'] = {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': image_dir
        }

    # Bind our REST resources
    info['apiRoot'].imagesearch = ImageSearch()
    info['apiRoot'].imagefeatures = ImageFeatures()
    info['apiRoot'].imagepivot = ImagePivot()
    info['apiRoot'].imageprefix = ImagePrefix()

    # Move girder app to /girder, serve our custom app from /
    info['serverRoot'], info['serverRoot'].girder = (CustomAppRoot(),
                                                     info['serverRoot'])
    info['serverRoot'].api = info['serverRoot'].girder.api

def solr_documents_from_field(field, values, classifications=None):
    """Given a field, and a list of values, return list of relevant solr documents.

    Additionally it can take an iterable of classifications which will be
    searched for through Solr.

    :param paths: List of solr paths corresponding to the Solr id attribute
    :param classifications: List of classifications to search by
    :returns: List of solr documents
    """
    def paged_request(params):
        """
        Takes a params dictionary and manages paging.

        Uses POST so very large request bodies can be sent to Solr.

        Returns a list of all documents.
        """
        documents = []

        # Adjust paging params
        params['start'] = 0
        params['rows'] = 1000

        numFound = None
        numRetrieved = None
        while numRetrieved is None or numRetrieved < numFound:
            r = requests.post(imagespaceSetting.get('IMAGE_SPACE_SOLR') + '/select',
                              data=params,
                              verify=False).json()

            numFound = r['response']['numFound']
            numRetrieved = len(r['response']['docs']) if numRetrieved is None \
                           else numRetrieved + len(r['response']['docs'])
            documents += r['response']['docs']

            # Setup offset for next request
            params['start'] = numRetrieved

        return documents

    event = events.trigger('imagespace.solr_documents_from_field', info={
        'field': field,
        'values': values
    })
    for response in event.responses:
        field = response['field']
        values = response['values']

    if classifications:
        q = ' OR '.join(['%s:[.7 TO *]' % key
                         for key in classifications])
    else:
        q = '*:*'

    qparams = {
        'wt': 'json',
        'q': q
    }

    # Give plugins a chance to adjust the Solr query parameters
    event = events.trigger('imagespace.imagesearch.qparams', qparams)
    for response in event.responses:
        qparams = response

    # Filter by field
    qparams['fq'] = qparams['fq'] if 'fq' in qparams else []
    qparams['fq'].append('%(field)s:(%(value)s)' % {
        'field': field,
        'value': ' '.join(values)
    })

    return paged_request(qparams)
<|endoftext|>


<commit_before>import downloader_base
import base64
import httplib
import socket

class downloader(downloader_base.downloader):
    def __init__(self):
        downloader_base.downloader.__init__(self)
        self.port_default = 80

    def requestAsString(self):
        output = {'code' : 0}
        auth = base64.standard_b64encode("%s:%s" % (self.username, self.password))
        timeout = 60
        con = httplib.HTTPConnection(self.server, self.port, True, timeout)
        headers = {"User-Agent": "vmcatcher"}
        if (self.username != None) and (self.password != None):
            auth = base64.standard_b64encode("%s:%s" % (self.username, self.password))
            headers["Authorization"] = "Basic %s" % (auth)
        try:
            con.request("GET" , self.path, headers=headers)
        except socket.gaierror as exp:
            output['error'] = exp.strerror
            output['code'] = 404
            return output
        except socket.error as exp:
            output['error'] = exp.strerror
            output['code'] = 404
            return output
        responce =  con.getresponse()
        httpstatus = responce.status
        if httpstatus == 200:
            output['responce'] = responce.read()
        else:
            output['error'] = responce.reason
            output['code'] = httpstatus
        return output

<commit_msg>report errors better<commit_after>import downloader_base
import base64
import httplib
import socket

log = logging.getLogger(__name__)

class downloader(downloader_base.downloader):
    def __init__(self):
        downloader_base.downloader.__init__(self)
        self.port_default = 80

    def requestAsString(self):
        output = {'code' : 0}
        auth = base64.standard_b64encode("%s:%s" % (self.username, self.password))
        timeout = 60
        con = httplib.HTTPConnection(self.server, self.port, True, timeout)
        headers = {"User-Agent": "vmcatcher"}
        if (self.username != None) and (self.password != None):
            auth = base64.standard_b64encode("%s:%s" % (self.username, self.password))
            headers["Authorization"] = "Basic %s" % (auth)
        try:
            con.request("GET" , self.path, headers=headers)
        except socket.gaierror as exp:
            output['error'] = exp.strerror
            output['code'] = 404
            log.error("Failed to connect to %s:%s error %s:%s" % (self.server,self.port, output['code'], output['code']))
            return output
        except socket.error as exp:
            output['error'] = exp.strerror
            output['code'] = 404
            log.error("Failed to connect to %s:%s error %s:%s" % (self.server,self.port, output['code'], output['code']))
            return output
        responce =  con.getresponse()
        httpstatus = responce.status
        if httpstatus == 200:
            output['responce'] = responce.read()
        else:
            output['error'] = responce.reason
            output['code'] = httpstatus
        return output

<|endoftext|>