<commit_before># ------------------------------------------------------------------------------
#  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.
# ------------------------------------------------------------------------------
"""
TODO module description
"""

import logging
import logging.handlers
import sys
import traceback

from station.interfaces import ILed
from station.interfaces import IPushButton
from station.interfaces import IVibrationMotor

# ------------------------------------------------------------------------------
class Led(ILed):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 name,
                 outputPin):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        self.Name = name + " (" + outputPin +")"
        logger.debug('Constructing LED %s', self.Name)

    # --------------------------------------------------------------------------
    def __enter__(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Entering LED %s', self.Name)
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting LED %s', self.Name)
        turnOff(self)

    # --------------------------------------------------------------------------
    def turnOff(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Turning off LED %s', self.Name)

    # --------------------------------------------------------------------------
    def turnOn(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Turning on LED %s', self.Name)

    # --------------------------------------------------------------------------
    def setFlashing(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Set LED %s flashing', self.Name)

# ------------------------------------------------------------------------------
class PushButton(IPushButton):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 buttonPressHandler,
                 config):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Constructing push button')
        self._listening = False
        self._timeToExit = False
        self._handler = buttonPressHandler
        self._thread = Thread(target = self.run)
        self._thread.daemon = True
        self._thread.start()

    # --------------------------------------------------------------------------
    def __enter__(self):
        logger.debug('Entering push button')
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting push button')
        stopListening(self)
        self._timeToExit = True
        self._thread.join()

    # --------------------------------------------------------------------------
    def run(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        with NonBlockingConsole() as nbc:
            logger.debug('Starting key press thread for push button')

            while not self._timeToExit:
                try:
                    if self._listening:

                        keypress = nbc.get_data()

                        if keypress == ' ':
                            logger.debug('Received key press event for <SPACE>')
                            self._handler()

                        # ignore all other key presses
                        else:
                            pass

                    sleep(1)
                except Exception, e:
                    # JIA TODO BEGIN change
                    #logger.critical("Exception occurred: " + e.args[0])
                    # JIA TODO UPDATE change
                    exType, ex, tb = sys.exc_info()
                    logger.critical("Exception occurred of type " + exType.__name_)
                    logger.critical(str(e))
                    traceback.print_tb(tb)
                    # JIA TODO END change

    # --------------------------------------------------------------------------
    def startListening(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Starting listening for push button')
        # TODO
        self._listening = True

    # --------------------------------------------------------------------------
    def stopListening(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Stopping listening for push button')
        self._listening = False

# ------------------------------------------------------------------------------
class VibrationMotor(IVibrationMotor):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 name,
                 outputPin):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        self.Name = name + " (" + outputPin +")"

    # --------------------------------------------------------------------------
    def __enter__(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Entering vibration motor %s', self.Name)
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting vibration motor %s', self.Name)
        stop(self)

    # --------------------------------------------------------------------------
    def start(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Started vibration motor \"%s\".', self.Name)

    # --------------------------------------------------------------------------
    def stop(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Stopped vibration motor \"%s\".', self.Name)

# ------------------------------------------------------------------------------
# Module Initialization
# ------------------------------------------------------------------------------
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # TODO - delete
handler = logging.handlers.SysLogHandler(address = '/dev/log')
logger.addHandler(handler)

<commit_msg>Updated Console to new Led Interface<commit_after># ------------------------------------------------------------------------------
#  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.
# ------------------------------------------------------------------------------
"""
TODO module description
"""

import logging
import logging.handlers
import sys
import traceback

from station.interfaces import ILed
from station.interfaces import IPushButton
from station.interfaces import IVibrationMotor

# ------------------------------------------------------------------------------
class Led(ILed):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 name,
                 config):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        self.Name = name + " (" + config.OutputPin +")"
        logger.debug('Constructing LED %s', self.Name)

    # --------------------------------------------------------------------------
    def __enter__(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Entering LED %s', self.Name)
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting LED %s', self.Name)
        turnOff(self)

    # --------------------------------------------------------------------------
    def turnOff(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Turning off LED %s', self.Name)

    # --------------------------------------------------------------------------
    def turnOn(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Turning on LED %s', self.Name)

    # --------------------------------------------------------------------------
    def setFlashing(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Set LED %s flashing', self.Name)

# ------------------------------------------------------------------------------
class PushButton(IPushButton):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 buttonPressHandler,
                 config):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Constructing push button')
        self._listening = False
        self._timeToExit = False
        self._handler = buttonPressHandler
        self._thread = Thread(target = self.run)
        self._thread.daemon = True
        self._thread.start()

    # --------------------------------------------------------------------------
    def __enter__(self):
        logger.debug('Entering push button')
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting push button')
        stopListening(self)
        self._timeToExit = True
        self._thread.join()

    # --------------------------------------------------------------------------
    def run(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        with NonBlockingConsole() as nbc:
            logger.debug('Starting key press thread for push button')

            while not self._timeToExit:
                try:
                    if self._listening:

                        keypress = nbc.get_data()

                        if keypress == ' ':
                            logger.debug('Received key press event for <SPACE>')
                            self._handler()

                        # ignore all other key presses
                        else:
                            pass

                    sleep(1)
                except Exception, e:
                    # JIA TODO BEGIN change
                    #logger.critical("Exception occurred: " + e.args[0])
                    # JIA TODO UPDATE change
                    exType, ex, tb = sys.exc_info()
                    logger.critical("Exception occurred of type " + exType.__name_)
                    logger.critical(str(e))
                    traceback.print_tb(tb)
                    # JIA TODO END change

    # --------------------------------------------------------------------------
    def startListening(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Starting listening for push button')
        # TODO
        self._listening = True

    # --------------------------------------------------------------------------
    def stopListening(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Stopping listening for push button')
        self._listening = False

# ------------------------------------------------------------------------------
class VibrationMotor(IVibrationMotor):
    """
    TODO class comment
    """

    # --------------------------------------------------------------------------
    def __init__(self,
                 name,
                 outputPin):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        self.Name = name + " (" + outputPin +")"

    # --------------------------------------------------------------------------
    def __enter__(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Entering vibration motor %s', self.Name)
        return self

    # --------------------------------------------------------------------------
    def __exit__(self, type, value, traceback):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Exiting vibration motor %s', self.Name)
        stop(self)

    # --------------------------------------------------------------------------
    def start(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Started vibration motor \"%s\".', self.Name)

    # --------------------------------------------------------------------------
    def stop(self):
        """TODO strictly one-line summary

        TODO Detailed multi-line description if
        necessary.

        Args:
            arg1 (type1): TODO describe arg, valid values, etc.
            arg2 (type2): TODO describe arg, valid values, etc.
            arg3 (type3): TODO describe arg, valid values, etc.
        Returns:
            TODO describe the return type and details
        Raises:
            TodoError1: if TODO.
            TodoError2: if TODO.

        """
        logger.debug('Stopped vibration motor \"%s\".', self.Name)

# ------------------------------------------------------------------------------
# Module Initialization
# ------------------------------------------------------------------------------
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # TODO - delete
handler = logging.handlers.SysLogHandler(address = '/dev/log')
logger.addHandler(handler)

<|endoftext|>


<commit_before># urllib3/poolmanager.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import logging

from ._collections import RecentlyUsedContainer
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from .connectionpool import connection_from_url, port_by_scheme
from .request import RequestMethods
from .util import parse_url

__all__ = ['PoolManager', 'ProxyManager', 'proxy_from_url']

pool_classes_by_scheme = {
    'http': HTTPConnectionPool,
    'https': HTTPSConnectionPool,
}

log = logging.getLogger(__name__)

class PoolManager(RequestMethods):
    """
    Allows for arbitrary requests while transparently keeping track of
    necessary connection pools for you.

    :param num_pools:
        Number of connection pools to cache before discarding the least recently
        used pool.

    :param \**connection_pool_kw:
        Additional parameters are used to create fresh
        :class:`urllib3.connectionpool.ConnectionPool` instances.

    Example: ::

        >>> manager = PoolManager(num_pools=2)
        >>> r = manager.urlopen("http://google.com/")
        >>> r = manager.urlopen("http://google.com/mail")
        >>> r = manager.urlopen("http://yahoo.com/")
        >>> len(manager.pools)
        2

    """

    def __init__(self, num_pools=10, **connection_pool_kw):
        self.connection_pool_kw = connection_pool_kw
        self.pools = RecentlyUsedContainer(num_pools,
                                           dispose_func=lambda p: p.close())

    def clear(self):
        """
        Empty our store of pools and direct them all to close.

        This will not affect in-flight connections, but they will not be
        re-used after completion.
        """
        self.pools.clear()

    def connection_from_host(self, host, port=None, scheme='http'):
        """
        Get a :class:`ConnectionPool` based on the host, port, and scheme.

        If ``port`` isn't given, it will be derived from the ``scheme`` using
        ``urllib3.connectionpool.port_by_scheme``.
        """
        port = port or port_by_scheme.get(scheme, 80)

        pool_key = (scheme, host, port)

        # If the scheme, host, or port doesn't match existing open connections,
        # open a new ConnectionPool.
        pool = self.pools.get(pool_key)
        if pool:
            return pool

        # Make a fresh ConnectionPool of the desired type
        pool_cls = pool_classes_by_scheme[scheme]
        pool = pool_cls(host, port, **self.connection_pool_kw)

        self.pools[pool_key] = pool

        return pool

    def connection_from_url(self, url):
        """
        Similar to :func:`urllib3.connectionpool.connection_from_url` but
        doesn't pass any additional parameters to the
        :class:`urllib3.connectionpool.ConnectionPool` constructor.

        Additional parameters are taken from the :class:`.PoolManager`
        constructor.
        """
        u = parse_url(url)
        return self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

    def urlopen(self, method, url, redirect=True, **kw):
        """
        Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        """
        u = parse_url(url)
        conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

        kw['assert_same_host'] = False
        kw['redirect'] = False

        response = conn.urlopen(method, u.request_uri, **kw)

        redirect_location = redirect and response.get_redirect_location()
        if not redirect_location:
            return response

        if response.status == 303:
            method = 'GET'

        log.info("Redirecting %s -> %s" % (url, redirect_location))
        kw['retries'] = kw.get('retries', 3) - 1 # Persist retries countdown
        return self.urlopen(method, redirect_location, **kw)

class ProxyManager(RequestMethods):
    """
    Given a ConnectionPool to a proxy, the ProxyManager's ``urlopen`` method
    will make requests to any url through the defined proxy.
    """

    def __init__(self, proxy_pool):
        self.proxy_pool = proxy_pool

    def _set_proxy_headers(self, headers=None):
        headers = headers or {}

        # Same headers are curl passes for --proxy1.0
        headers['Accept'] = '*/*'
        headers['Proxy-Connection'] = 'Keep-Alive'

        return headers

    def urlopen(self, method, url, **kw):
        "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
        kw['assert_same_host'] = False
        kw['headers'] = self._set_proxy_headers(kw.get('headers'))
        return self.proxy_pool.urlopen(method, url, **kw)

def proxy_from_url(url, **pool_kw):
    proxy_pool = connection_from_url(url, **pool_kw)
    return ProxyManager(proxy_pool)
<commit_msg>Added PoolManager._new_pool for customization.<commit_after># urllib3/poolmanager.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import logging

from ._collections import RecentlyUsedContainer
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from .connectionpool import connection_from_url, port_by_scheme
from .request import RequestMethods
from .util import parse_url

__all__ = ['PoolManager', 'ProxyManager', 'proxy_from_url']

pool_classes_by_scheme = {
    'http': HTTPConnectionPool,
    'https': HTTPSConnectionPool,
}

log = logging.getLogger(__name__)

class PoolManager(RequestMethods):
    """
    Allows for arbitrary requests while transparently keeping track of
    necessary connection pools for you.

    :param num_pools:
        Number of connection pools to cache before discarding the least recently
        used pool.

    :param \**connection_pool_kw:
        Additional parameters are used to create fresh
        :class:`urllib3.connectionpool.ConnectionPool` instances.

    Example: ::

        >>> manager = PoolManager(num_pools=2)
        >>> r = manager.urlopen("http://google.com/")
        >>> r = manager.urlopen("http://google.com/mail")
        >>> r = manager.urlopen("http://yahoo.com/")
        >>> len(manager.pools)
        2

    """

    def __init__(self, num_pools=10, **connection_pool_kw):
        self.connection_pool_kw = connection_pool_kw
        self.pools = RecentlyUsedContainer(num_pools,
                                           dispose_func=lambda p: p.close())

    def clear(self):
        """
        Empty our store of pools and direct them all to close.

        This will not affect in-flight connections, but they will not be
        re-used after completion.
        """
        self.pools.clear()

    def connection_from_host(self, host, port=None, scheme='http'):
        """
        Get a :class:`ConnectionPool` based on the host, port, and scheme.

        If ``port`` isn't given, it will be derived from the ``scheme`` using
        ``urllib3.connectionpool.port_by_scheme``.
        """
        port = port or port_by_scheme.get(scheme, 80)

        pool_key = (scheme, host, port)

        # If the scheme, host, or port doesn't match existing open connections,
        # open a new ConnectionPool.
        pool = self.pools.get(pool_key)
        if pool:
            return pool

        # Make a fresh ConnectionPool of the desired type
        pool = self._new_pool(scheme, host, port)
        self.pools[pool_key] = pool
        return pool

    def _new_pool(self, scheme, host, port):
        """
        Create a new :class:`ConnectionPool` based on host, port and scheme.

        This method is used to actually create the connection pools handed out
        by :meth:`connection_from_url` and companion methods. It is intended
        to be overridden for customization.
        """
        pool_cls = pool_classes_by_scheme[scheme]
        return pool_cls(host, port, **self.connection_pool_kw)

    def connection_from_url(self, url):
        """
        Similar to :func:`urllib3.connectionpool.connection_from_url` but
        doesn't pass any additional parameters to the
        :class:`urllib3.connectionpool.ConnectionPool` constructor.

        Additional parameters are taken from the :class:`.PoolManager`
        constructor.
        """
        u = parse_url(url)
        return self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

    def urlopen(self, method, url, redirect=True, **kw):
        """
        Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        """
        u = parse_url(url)
        conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

        kw['assert_same_host'] = False
        kw['redirect'] = False

        response = conn.urlopen(method, u.request_uri, **kw)

        redirect_location = redirect and response.get_redirect_location()
        if not redirect_location:
            return response

        if response.status == 303:
            method = 'GET'

        log.info("Redirecting %s -> %s" % (url, redirect_location))
        kw['retries'] = kw.get('retries', 3) - 1 # Persist retries countdown
        return self.urlopen(method, redirect_location, **kw)

class ProxyManager(RequestMethods):
    """
    Given a ConnectionPool to a proxy, the ProxyManager's ``urlopen`` method
    will make requests to any url through the defined proxy.
    """

    def __init__(self, proxy_pool):
        self.proxy_pool = proxy_pool

    def _set_proxy_headers(self, headers=None):
        headers = headers or {}

        # Same headers are curl passes for --proxy1.0
        headers['Accept'] = '*/*'
        headers['Proxy-Connection'] = 'Keep-Alive'

        return headers

    def urlopen(self, method, url, **kw):
        "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
        kw['assert_same_host'] = False
        kw['headers'] = self._set_proxy_headers(kw.get('headers'))
        return self.proxy_pool.urlopen(method, url, **kw)

def proxy_from_url(url, **pool_kw):
    proxy_pool = connection_from_url(url, **pool_kw)
    return ProxyManager(proxy_pool)
<|endoftext|>


<commit_before>#! /usr/bin/env python
# script.py -- Make typescript of terminal session.
# Usage:
#       -a      Append to typescript.
#       -p      Use Python as shell.
# Author: Steen Lumholt.

import os, time, sys
import pty

def read(fd):
    data = os.read(fd, 1024)
    file.write(data)
    return data

shell = 'sh'
filename = 'typescript'
mode = 'w'
if os.environ.has_key('SHELL'):
    shell = os.environ['SHELL']
if '-a' in sys.argv:
    mode = 'a'
if '-p' in sys.argv:
    shell = 'python'

file = open(filename, mode)

sys.stdout.write('Script started, file is %s\n' % filename)
file.write('Script started on %s\n' % time.ctime(time.time()))
pty.spawn(shell, read)
file.write('Script done on %s\n' % time.ctime(time.time()))
sys.stdout.write('Script done, file is %s\n' % filename)
<commit_msg>Use getopt in script.py demo.<commit_after>#! /usr/bin/env python

# script.py -- Make typescript of terminal session.
# Usage:
#       -a      Append to typescript.
#       -p      Use Python as shell.
# Author: Steen Lumholt.

import os, time, sys, getopt
import pty

def read(fd):
    data = os.read(fd, 1024)
    file.write(data)
    return data

shell = 'sh'
filename = 'typescript'
mode = 'w'
if os.environ.has_key('SHELL'):
    shell = os.environ['SHELL']

try:
    opts, args = getopt.getopt(sys.argv[1:], 'ap')
except getopt.error, msg:
    print '%s: %s' % (sys.argv[0], msg)
    sys.exit(2)

for o, a in opts:
    if o == '-a':
        mode = 'a'
    elif o == '-p':
        shell = 'python'

script = open(filename, mode)

sys.stdout.write('Script started, file is %s\n' % filename)
script.write('Script started on %s\n' % time.ctime(time.time()))
pty.spawn(shell, read)
script.write('Script done on %s\n' % time.ctime(time.time()))
sys.stdout.write('Script done, file is %s\n' % filename)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import os

from six.moves.queue import Queue
from time import sleep, time

from polyaxon_client import settings
from polyaxon_client.logger import logger
from polyaxon_client.workers.base_worker import BaseWorker

class QueueWorker(BaseWorker):
    MIN_TIMEOUT = 0.1
    TIMEOUT_ATTEMPTS = 5
    QUEUE_SIZE = -1  # inf
    END_EVENT = object()
    NAME = 'polyaxon.QueueWorker'

    def __init__(self, timeout=None, queue_size=None):
        super(QueueWorker, self).__init__()
        self._queue = Queue(queue_size or self.QUEUE_SIZE)
        self._timeout = timeout if timeout is not None else settings.TIMEOUT

    def atexit(self):
        with self._lock:
            if not self.is_alive():
                return

            self._queue.put_nowait(self.END_EVENT)

            def timeout_join(timeout, queue):
                end = time() + timeout
                queue.all_tasks_done.acquire()
                try:
                    while queue.unfinished_tasks:
                        current_timeout = end - time()
                        if current_timeout <= 0:
                            # timed out
                            return False

                        queue.all_tasks_done.wait(timeout=current_timeout)

                    return True

                finally:
                    queue.all_tasks_done.release()

            # ensure wait
            timeout = min(self.MIN_TIMEOUT, self._timeout / self.TIMEOUT_ATTEMPTS)
            if timeout_join(timeout=timeout, queue=self._queue):
                timeout = 0
            else:
                # Queue still has message, try another time
                size = self._queue.qsize()

                print('Polyaxon %s is attempting to send %i pending messages' %
                      (self.NAME, size))
                print('Waiting up to {} seconds'.format(self._timeout))
                if not settings.IN_CLUSTER:
                    if os.name == 'nt':
                        print('Press Ctrl-Break to quit')
                    else:
                        print('Press Ctrl-C to quit')

            while timeout > 0 and not timeout_join(timeout=timeout, queue=self._queue):
                timeout = min(timeout + self._timeout / self.TIMEOUT_ATTEMPTS,
                              self._timeout - timeout)

            size = self._queue.qsize()
            if size > 0:
                print('Polyaxon %s timed out and did not manage to send %i messages' %
                      (self.NAME, size))

            self._thread = None

    def stop(self, timeout=None):
        with self._lock:
            if self._thread:
                self._queue.put_nowait(self.END_EVENT)
                self._thread.join(timeout=timeout)
                self._thread = None
                self._thread_for_pid = None

    def queue(self, callback, *args, **kwargs):
        self.is_running()
        self._queue.put_nowait((callback, args, kwargs))

    def _target(self):
        while True:
            record = self._queue.get()
            try:
                if record is self.END_EVENT:
                    break
                callback, args, kwargs = record
                try:
                    callback(*args, **kwargs)
                except Exception:
                    logger.error('Failed processing job', exc_info=True)
            finally:
                self._queue.task_done()

            sleep(0)
<commit_msg>Update min timeout to allow transport to execute tasks in the queue<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import os

from six.moves.queue import Queue
from time import sleep, time

from polyaxon_client import settings
from polyaxon_client.logger import logger
from polyaxon_client.workers.base_worker import BaseWorker

class QueueWorker(BaseWorker):
    MIN_TIMEOUT = 1
    TIMEOUT_ATTEMPTS = 5
    QUEUE_SIZE = -1  # inf
    END_EVENT = object()
    NAME = 'polyaxon.QueueWorker'

    def __init__(self, timeout=None, queue_size=None):
        super(QueueWorker, self).__init__()
        self._queue = Queue(queue_size or self.QUEUE_SIZE)
        self._timeout = timeout if timeout is not None else settings.TIMEOUT

    def atexit(self):
        with self._lock:
            if not self.is_alive():
                return

            self._queue.put_nowait(self.END_EVENT)

            def timeout_join(timeout, queue):
                end = time() + timeout
                queue.all_tasks_done.acquire()
                try:
                    while queue.unfinished_tasks:
                        current_timeout = end - time()
                        if current_timeout <= 0:
                            # timed out
                            return False

                        queue.all_tasks_done.wait(timeout=current_timeout)

                    return True

                finally:
                    queue.all_tasks_done.release()

            # ensure wait
            timeout = min(self.MIN_TIMEOUT, self._timeout / self.TIMEOUT_ATTEMPTS)
            if timeout_join(timeout=timeout, queue=self._queue):
                timeout = 0
            else:
                # Queue still has message, try another time
                size = self._queue.qsize()

                if not settings.IN_CLUSTER:
                    print('Polyaxon %s is attempting to send %i pending messages' %
                          (self.NAME, size))
                    print('Waiting up to {} seconds'.format(self._timeout))
                    if os.name == 'nt':
                        print('Press Ctrl-Break to quit')
                    else:
                        print('Press Ctrl-C to quit')

            while timeout > 0 and not timeout_join(timeout=timeout, queue=self._queue):
                timeout = min(timeout + self._timeout / self.TIMEOUT_ATTEMPTS,
                              self._timeout - timeout)

            size = self._queue.qsize()
            if size > 0:
                print('Polyaxon %s timed out and did not manage to send %i messages' %
                      (self.NAME, size))

            self._thread = None

    def stop(self, timeout=None):
        with self._lock:
            if self._thread:
                self._queue.put_nowait(self.END_EVENT)
                self._thread.join(timeout=timeout)
                self._thread = None
                self._thread_for_pid = None

    def queue(self, callback, *args, **kwargs):
        self.is_running()
        self._queue.put_nowait((callback, args, kwargs))

    def _target(self):
        while True:
            record = self._queue.get()
            try:
                if record is self.END_EVENT:
                    break
                callback, args, kwargs = record
                try:
                    callback(*args, **kwargs)
                except Exception:
                    logger.error('Failed processing job', exc_info=True)
            finally:
                self._queue.task_done()

            sleep(0)
<|endoftext|>


<commit_before>#!/usr/bin/python2.7
#############
# This utility retrieves specific bundle data from a usage meter server using the usage meter api.
# Not all bundle types are returned, The subset of bundles returned were selected to support the analysis of differences between UM versions.
#
# The utility runs from the command line.
# An input range of report months can be specified using a begindate and enddate.
# The resulting report data is written to an output file in tab separated format which can be easily imported into excel.
#
# Supports Usage Meter Versions 3.2, 3.3, 3.4 and 3.5
# Requires: Python 2.7.10
# Author: Bob Webster April 2017
#
# Prior to execution, an API token must be generated using the Usage Meter UI and the value passed with the -t flag.
#
# Test Sample 3.5
# python umCustomerMgmt.py -s 10.134.3.240:8443  -t TOKHYCFE05BIHXAUJQJA1NVNRWKWI4EF5DS  -i ../../test/customers.tsv -d
#
#
# Author: Bob Webster
#
#############

import requests
import sys, getopt
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from httplib import HTTPConnection
import csv
import traceback
import xmltodict
import logging

debug = False

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

# Get all customers and build a dictionary of customer name to id.
# returns a dictionary where key=customername value=id otherwise an empty dictionary
def get_all_customer_ids(UMserver, UMtoken):

    # Using Session, token no longer required, note put/post calls return 403 without Content-Type: application/xml
    headers = {'x-usagemeter-authorization': UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }
    keyDict = {}

    try:
      customerList = requests.get("https://" + UMserver  + "/um/api/customers", headers=headers, verify=False, timeout=10);

      if debug:
          print customerList.status_code

      if customerList.status_code != 200:
        return keyDict

      if debug:
          print customerList.content

      # Parse XML into Dict

      doc = xmltodict.parse(customerList.content)
      if debug:
          print "XML Doc as Dict " + str(doc)

      # Build customer name - id dictionay

      for c in doc['customers']['customer']:
            keyDict[c['name']] = c['id']

      if debug:
          print str(keyDict)

    except requests.exceptions.Timeout:
          print "Unable to access server, timeout"
          sys.exit(4)

    return keyDict

# Return a single customer record from the UM Server
# Returns true on success
def get_customer(UMserver, UMtoken, id):

  # Using Session, token no longer required, note put/post calls return 403 without Content-Type: application/xml
    headers = {'x-usagemeter-authorization': UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    # Get customer

    try:
      customer = requests.get("https://" + UMserver  + "/um/api/customer/" + id, headers=headers, verify=False, timeout=10);

      if debug:
          print "https://" + UMserver  + "/um/api/customer/" + id
          print customer.status_code
          print customer.content

      if customer.status_code != 200:
         print "Error Retrieving customer " + name + " msg= " + customer.content
         return False

      return True

    except requests.exceptions.Timeout:
          print "Unable to access server, timeout"
          sys.exit(4)

# Update an existing customer record in the UM Server
# Returns true on success
def update_customer(UMserver, UMtoken, id, name, country, postalCode):
    global debug

    headers = {'x-usagemeter-authorization': UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    # Update customer

    body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + \
           "<customer xmlns=\"http://www.vmware.com/UM\">\n" + \
           "<name>" + name + "</name>\n" + \
           "<country>" + country + "</country>\n" + \
           "<postalCode>" + postalCode  + "</postalCode>\n" + \
           "</customer>\n"

    try:

      customer = requests.put("https://" + UMserver  + "/um/api/customer/" + id, headers=headers, data=body, verify=False, timeout=10);

      if debug:
          print "https://" + UMserver  + "/um/api/customer/" + id
          print customer.status_code
          print customer.content

      if customer.status_code != 200:
          print "Error Updating customer " + name + " msg= " + customer.content
          return False

      return True

    except requests.exceptions.Timeout:
          print "Unable to access server, timeout"
          sys.exit(4)

# Create an new customer record in the UM Server
# Returns true on success
def create_customer(UMserver, UMtoken, name, country, postalCode):
    global debug

    # note put/post calls return 403 without Content-Type: application/xml
    headers = {'x-usagemeter-authorization': UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    # Create customer

    body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + \
           "<customer xmlns=\"http://www.vmware.com/UM\">\n" + \
           "<name>" + name + "</name>\n" + \
           "<country>" + country + "</country>\n" + \
           "<postalCode>" + postalCode  + "</postalCode>\n" + \
           "</customer>\n"

    try:
      if debug:

         print "https://" + UMserver  + "/um/api/customer/"
         print body

      customer = requests.post("https://" + UMserver  + "/um/api/customer", headers=headers, data=body, verify=False, timeout=10);

      if debug:
          print "https://" + UMserver  + "/um/api/customer/" + id
          print customer.status_code
          print customer.content

      if customer.status_code != 201:
          print "Error Creating customer " + name + " msg= " + customer.content
          return False

      return True

    except requests.exceptions.Timeout:
          print "Unable to access server, timeout"
          sys.exit(4)

# Delete an existing customer record in the UM Server
# Returns true on success
def delete_customer(UMserver, UMtoken, name, id):
    global debug

    # note put/post calls return 403 without Content-Type: application/xml
    headers = {'x-usagemeter-authorization': UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    # Delete customer

    try:
      if debug:

         print "https://" + UMserver  + "/um/api/customer/"
         print body

      customer = requests.delete("https://" + UMserver  + "/um/api/customer/" + id, headers=headers, verify=False, timeout=10);

      if debug:
          print "https://" + UMserver  + "/um/api/customer/" + id
          print customer.status_code
          print customer.content

      if customer.status_code != 204:
          print "Error Deleting customer " + name + " msg= " + customer.content
          return False

      return True

    except requests.exceptions.Timeout:
          print "Unable to access server, timeout"
          sys.exit(4)

def create_session(UMserver, UMtoken):

    global debug

    headers = {'x-usagemeter-authorization': UMtoken, 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    # Supress associated InsecureRequestWarning for UM with Self Signed Cert
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    if debug:

      #logging.basicConfig(level=logging.DEBUG)
      debug_requests_on()

    #
    # Get UM version from banner page title -  <title>VMware vCloud Usage Meter 3.5</title>
    #
    try:
         banner = requests.get("https://" + UMserver  + "/um/", headers=headers, verify=False, timeout=10);
         v = find_between( banner.content, 'VMware vCloud Usage Meter ', '</title>' )
         print "UM Version: " + v
    except requests.exceptions.SSLError as e:

       # For UM 3.2, 3.3 or 3.4 Versions
       # ("bad handshake: Error([('SSL routines', 'tls_process_ske_dhe', 'dh key too small')],)
       # Must adjust ssl Cyphers and try again
       requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
       try:
          requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
       except AttributeError:
          # no pyopenssl support used / needed / available
           pass
       try:
           banner = requests.get("https://" + UMserver  + "/um/", headers=headers, verify=False, timeout=10);
           v = find_between( banner.text, 'VMware vCloud Usage Meter ', '</title>' )
           print "UM Version: " + v
       except requests.exceptions.SSLError as e:
           print "http.requests ssl error: " + str(e)
           sys.exit(2)

    except requests.exceptions.RequestException as e:
       print "http.requests error: " + str(e)
       sys.exit(2)

    #   Use alternative transport adapter to set retry count
    #   So rety if    requests.exceptions.ConnectionError

    session = requests.Session()
    session.mount('https://' + UMserver, HTTPAdapter(max_retries=5))

def process_input_file(UMserver, UMtoken, inputFile, custKeyDict):

    global debug
    updatedCount =0
    createdCount =0
    deletedCount =0
    errorCount =0

    # open input file
    try:
       csv.register_dialect('tab', delimiter='\t')
       f_input_file = open(inputFile, 'r')

    except (Exception) as error:
        print "Error: Unable to open specified input file."
        print(error)
        sys.exit(4)

    print "Successfully opened input file.  \n"

    try:
        reader = csv.reader(f_input_file, dialect='tab')

        # Read input file, ignoring rules section and comments
        for record in reader:
                 if record[0].startswith('# Rules'):
                      break
                 elif record[0].startswith('#'):
                      next(reader)
                 else:
                      # Existing customer?
                      if record[0] in custKeyDict:

                           # delete request? Name and no other columns?
                           if len(record) == 1:
                                if delete_customer(UMserver, UMtoken, record[0], custKeyDict[record[0]]):
                                   deletedCount = deletedCount + 1
                                else:
                                    errorCount = errorCount + 1
                           else:
                                if update_customer(UMserver, UMtoken, custKeyDict[record[0]], record[0], record[1], record[2]):
                                    updatedCount = updatedCount + 1
                                else:
                                    errorCount = errorCount + 1

                      # New Customer
                      else:
                           if len(record) == 1:
                               print "Error: Customer " + record[0] + " does not exist and cannot be deleted."
                               errorCount = errorCount + 1
                           else:
                             #                                  customerName, Country, PostalCode
                             if create_customer(UMserver, UMtoken, record[0], record[1], record[2]):
                                createdCount = createdCount + 1
                             else:
                                errorCount = errorCount + 1

        print "\nProcessing complete."
        print "\tCreated Customers  " + str(createdCount)
        print "\tUpdated Customers: " + str(updatedCount)
        print "\tDeleted Customers  " + str(deletedCount)
        print "\tError Count: " + str(errorCount)

        # get_customer(UMserver, UMtoken, "4")

    except (Exception) as error:
        print "Error: Processing Error "
        print(error)
        print traceback.format_exc()

    finally:
        f_input_file.close()

def debug_requests_on():
    '''Switches on logging of the requests module.'''
    HTTPConnection.debuglevel = 1

    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True

def usage():

      print 'Usage umManageCustomers.py -s 10.32.51.xxx:8443  -t TOK3W1T0BZZGBQ4CUIEFSUF4B3RDX3ETUDT  -i  ./customerFile.tsv -d'
      print ' -s  = Usage Meter Appliance and port'
      print ' -t  = API Token from Usage Meter for API authentication'
      print ' -i  = customer input file containing custmoers in tab separated format'
      print ' -d  = debug'

######################
### Main
######################

def main(argv):

    # Process command line input

    headers = "";
    global debug

    try:
       opts, args = getopt.getopt(argv,"hds:t:i:")
    except getopt.GetoptError:
       usage()
       sys.exit(2)

    for opt, arg in opts:
      if opt == '-h':
         usage()
         sys.exit()
      elif opt in ("-s"):
         UMserver = arg
      elif opt in ("-t"):
         UMtoken = arg
      elif opt in ("-i"):
         inputFile = arg
      elif opt in ("-d"):
         debug = True
    try:
     UMserver
     UMtoken
     inputFile
    except NameError:
     print "Insufficient parameters"
     usage()
     sys.exit(3)

    # Connect to UM Server
    create_session(UMserver, UMtoken)

    # Get all customers and build a name - key dictionary
    custKeyDict = get_all_customer_ids(UMserver, UMtoken)
    if len(custKeyDict) == 0:
      print "Error retrieving customer list from server."
      sys.exit(3)

    # Process updates
    process_input_file(UMserver, UMtoken, inputFile, custKeyDict)

if __name__ == "__main__":
   main(sys.argv[1:])
<commit_msg>changed to Class<commit_after>#!/usr/bin/python2.7
#############
# This utility retrieves specific bundle data from a usage meter server using the usage meter api.
# Not all bundle types are returned, The subset of bundles returned were selected to support the analysis of differences between UM versions.
#
# The utility runs from the command line.
# An input range of report months can be specified using a begindate and enddate.
# The resulting report data is written to an output file in tab separated format which can be easily imported into excel.
#
# Supports Usage Meter Versions 3.2, 3.3, 3.4 and 3.5
# Requires: Python 2.7.10
# Author: Bob Webster April 2017
#
# Prior to execution, an API token must be generated using the Usage Meter UI and the value passed with the -t flag.
#
# Test Sample 3.5
# python umCustomerMgmt.py -s 10.134.3.240:8443  -t TOKHYCFE05BIHXAUJQJA1NVNRWKWI4EF5DS  -i ../../test/customers.tsv -d
#
#
# Author: Bob Webster
#
#############

import requests
import sys, getopt
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from httplib import HTTPConnection
import csv
import traceback
import xmltodict
import logging

class UMCustomerMgmt:

    def __init__(self, debug=False):

        self.debug = debug

    def find_between(self,  s, first, last ):
        try:
            start = s.index( first ) + len( first )
            end = s.index( last, start )
            return s[start:end]
        except ValueError:
            return ""

    # Get all customers and build a dictionary of customer name to id.
    # returns a dictionary where key=customername value=id otherwise an empty dictionary
    def get_all_customer_ids(self):

        # Using Session, token no longer required, note put/post calls return 403 without Content-Type: application/xml
        headers = {'x-usagemeter-authorization': self.UMtoken, 'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }
        keyDict = {}

        try:
          customerList = requests.get("https://" + self.UMserver  + "/um/api/customers", headers=self.headers, verify=False, timeout=10);

          if self.debug:
              print customerList.status_code

          if customerList.status_code != 200:
            return keyDict

          if self.debug:
              print customerList.content

          # Parse XML into Dict

          doc = xmltodict.parse(customerList.content)
          if self.debug:
              print "XML Doc as Dict " + str(doc)

          # Build customer name - id dictionay

          for c in doc['customers']['customer']:
                keyDict[c['name']] = c['id']

          if self.debug:
              print str(keyDict)

        except requests.exceptions.Timeout:
              print "Unable to access server, timeout"
              sys.exit(4)

        return keyDict

    # Return a single customer record from the UM Server
    # Returns true on success
    def get_customer(self, id):

        # Get customer

        try:
          customer = requests.get("https://" + self.UMserver  + "/um/api/customer/" + id, headers=self.headers, verify=False, timeout=10);

          if self.debug:
              print "https://" + self.UMserver  + "/um/api/customer/" + id
              print customer.status_code
              print customer.content

          if customer.status_code != 200:
             print "Error Retrieving customer " + name + " msg= " + customer.content
             return False

          return True

        except requests.exceptions.Timeout:
              print "Unable to access server, timeout"
              sys.exit(4)

    # Update an existing customer record in the UM Server
    # Returns true on success
    def update_customer(self, id, name, country, postalCode):

        # Update customer

        body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + \
               "<customer xmlns=\"http://www.vmware.com/UM\">\n" + \
               "<name>" + name + "</name>\n" + \
               "<country>" + country + "</country>\n" + \
               "<postalCode>" + postalCode  + "</postalCode>\n" + \
               "</customer>\n"

        try:

          customer = requests.put("https://" + self.UMserver  + "/um/api/customer/" + id, headers=self.headers, data=body, verify=False, timeout=10);

          if self.debug:
              print "https://" + self.UMserver  + "/um/api/customer/" + id
              print customer.status_code
              print customer.content

          if customer.status_code != 200:
              print "Error Updating customer " + name + " msg= " + customer.content
              return False

          return True

        except requests.exceptions.Timeout:
              print "Unable to access server, timeout"
              sys.exit(4)

    # Create an new customer record in the UM Server
    # Returns true on success
    def create_customer(self, name, country, postalCode):

        # Create customer

        body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + \
               "<customer xmlns=\"http://www.vmware.com/UM\">\n" + \
               "<name>" + name + "</name>\n" + \
               "<country>" + country + "</country>\n" + \
               "<postalCode>" + postalCode  + "</postalCode>\n" + \
               "</customer>\n"

        try:
          if self.debug:

             print "https://" + self.UMserver  + "/um/api/customer/"
             print body

          customer = requests.post("https://" + self.UMserver  + "/um/api/customer", headers=self.headers, data=body, verify=False, timeout=10);

          if self.debug:
              print "https://" + self.UMserver  + "/um/api/customer/" + id
              print customer.status_code
              print customer.content

          if customer.status_code != 201:
              print "Error Creating customer " + name + " msg= " + customer.content
              return False

          return True

        except requests.exceptions.Timeout:
              print "Unable to access server, timeout"
              sys.exit(4)

    # Delete an existing customer record in the UM Server
    # Returns true on success
    def delete_customer(self, name, id):

        # Delete customer

        try:
          if self.debug:

             print "https://" + self.UMserver  + "/um/api/customer/"
             print body

          customer = requests.delete("https://" + self.UMserver  + "/um/api/customer/" + id, headers=self.headers, verify=False, timeout=10);

          if self.debug:
              print "https://" + self.UMserver  + "/um/api/customer/" + id
              print customer.status_code
              print customer.content

          if customer.status_code != 204:
              print "Error Deleting customer " + name + " msg= " + customer.content
              return False

          return True

        except requests.exceptions.Timeout:
              print "Unable to access server, timeout"
              sys.exit(4)

    # Retrieve UM Banner page to determine UM Version and set required SSL settings.
    # setup SSL for varying versions of Usage Meter and define a retry count for API calls
    def create_session(self, UMserver, UMtoken):

        self.UMserver = UMserver
        self.UMtoken = UMtoken
        self.headers = {'x-usagemeter-authorization': UMtoken,  'Content-Type': 'application/xml', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

        # Supress associated InsecureRequestWarning for UM with Self Signed Cert
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        if self.debug:

          #logging.basicConfig(level=logging.DEBUG)
          self.debug_requests_on()

        #
        # Get UM version from banner page title -  <title>VMware vCloud Usage Meter 3.5</title>
        # Also verifies that SSL handshake works, adjust cyphers for earlier versions of UM
        #
        try:
             banner = requests.get("https://" + self.UMserver  + "/um/", headers=self.headers, verify=False, timeout=10);
             v = self.find_between( banner.content, 'VMware vCloud Usage Meter ', '</title>' )
             print "UM Version: " + v
        except requests.exceptions.SSLError as e:

           # For UM 3.2, 3.3 or 3.4 Versions
           # ("bad handshake: Error([('SSL routines', 'tls_process_ske_dhe', 'dh key too small')],)
           # Must adjust ssl Cyphers and try again
           requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
           try:
              requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
           except AttributeError:
              # no pyopenssl support used / needed / available
               pass
           try:
               banner = requests.get("https://" + self.UMserver  + "/um/", headers=self.headers, verify=False, timeout=10);
               v = self.find_between( banner.text, 'VMware vCloud Usage Meter ', '</title>' )
               print "UM Version: " + v
           except requests.exceptions.SSLError as e:
               print "http.requests ssl error: " + str(e)
               sys.exit(2)

        except requests.exceptions.RequestException as e:
           print "http.requests error: " + str(e)
           sys.exit(2)

        #   Use alternative transport adapter to set retry count
        #   So rety if    requests.exceptions.ConnectionError

        requests.Session().mount('https://' + UMserver, HTTPAdapter(max_retries=5))

    # Returns a dictionary of counters, records updatedCount, createdCount, deletedCount and errorCount
    def process_file(self, UMserver, UMtoken, inputFile):

        updatedCount =0
        createdCount =0
        deletedCount =0
        errorCount =0

        # Connect to server
        self.create_session(UMserver, UMtoken)

        # Get all customers and build a name - key dictionary
        self.custKeyDict = self.get_all_customer_ids()
        if len(self.custKeyDict) == 0:
          print "Error retrieving customer list from server."
          sys.exit(3)

        # open input file
        try:
           csv.register_dialect('tab', delimiter='\t')
           f_input_file = open(inputFile, 'r')

        except (Exception) as error:
            print "Error: Unable to open specified input file."
            print(error)
            sys.exit(4)

        print "Successfully opened input file.  \n"

        try:
            reader = csv.reader(f_input_file, dialect='tab')

            # Read input file, ignoring rules section and comments
            for record in reader:
                     if record[0].startswith('# Rules'):
                          break
                     elif record[0].startswith('#'):
                          next(reader)
                     else:
                          # Existing customer?
                          if record[0] in self.custKeyDict:

                               # delete request? Name and no other columns?
                               if len(record) == 1:
                                    if self.delete_customer(record[0], self.custKeyDict[record[0]]):
                                       deletedCount = deletedCount + 1
                                    else:
                                        errorCount = errorCount + 1
                               else:
                                    if self.update_customer(self.custKeyDict[record[0]], record[0], record[1], record[2]):
                                        updatedCount = updatedCount + 1
                                    else:
                                        errorCount = errorCount + 1

                          # New Customer
                          else:
                               if len(record) == 1:
                                   print "Error: Customer " + record[0] + " does not exist and cannot be deleted."
                                   errorCount = errorCount + 1
                               else:
                                 #                                  customerName, Country, PostalCode
                                 if self.create_customer(record[0], record[1], record[2]):
                                    createdCount = createdCount + 1
                                 else:
                                    errorCount = errorCount + 1

            return {'createdCount': createdCount, 'updatedCount': updatedCount, 'deletedCount': deletedCount, 'errorCount': errorCount}

        except (Exception) as error:
            print "Error: Processing Error "
            print(error)
            print traceback.format_exc()

        finally:
            f_input_file.close()

    def debug_requests_on(self):
        '''Switches on logging of the requests module.'''
        HTTPConnection.debuglevel = 1

        logging.basicConfig()
        logging.getLogger().setLevel(logging.DEBUG)
        requests_log = logging.getLogger("requests.packages.urllib3")
        requests_log.setLevel(logging.DEBUG)
        requests_log.propagate = True

    def usage():

          print 'Usage umManageCustomers.py -s 10.32.51.xxx:8443  -t TOK3W1T0BZZGBQ4CUIEFSUF4B3RDX3ETUDT  -i  ./customerFile.tsv -d'
          print ' -s  = Usage Meter Appliance and port'
          print ' -t  = API Token from Usage Meter for API authentication'
          print ' -i  = customer input file containing custmoers in tab separated format'
          print ' -d  = debug'

######################
### Main
######################

def main(argv):

    # Process command line input

    debug = False

    try:
       opts, args = getopt.getopt(argv,"hds:t:i:")
    except getopt.GetoptError:
       usage()
       sys.exit(2)

    for opt, arg in opts:
      if opt == '-h':
         usage()
         sys.exit()
      elif opt in ("-s"):
         UMserver = arg
      elif opt in ("-t"):
         UMtoken = arg
      elif opt in ("-i"):
         inputFile = arg
      elif opt in ("-d"):
         debug = True
    try:
     UMserver
     UMtoken
     inputFile
    except NameError:
     print "Insufficient parameters"
     usage()
     sys.exit(3)

    mgr = UMCustomerMgmt(debug)
    results = mgr.process_file(UMserver, UMtoken, inputFile)
    print str(results)
    print "\nProcessing complete."
    print "\tCreated Customers  " + str(results['createdCount'])
    print "\tUpdated Customers: " + str(results['updatedCount'])
    print "\tDeleted Customers  " + str(results['deletedCount'])
    print "\tError Count: " + str(results['errorCount'])

if __name__ == "__main__":
   main(sys.argv[1:])
<|endoftext|>


<commit_before>#!/bin/env python
#copyright ReportLab Inc. 2001
#see license.txt for license details
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/docs/userguide/genuserguide.py?cvsroot=reportlab
#$Header: /tmp/reportlab/reportlab/docs/userguide/genuserguide.py,v 1.7 2001/11/29 13:14:21 rgbecker Exp $

__version__=''' $Id: genuserguide.py,v 1.7 2001/11/29 13:14:21 rgbecker Exp $ '''

__doc__ = """
This module contains the script for building the user guide.
"""

from reportlab.tools.docco.rl_doc_utils import *

def run(pagesize, verbose=0):

	# copy to target
	import reportlab
	destfn = os.path.join(os.path.dirname(reportlab.__file__),'docs','userguide.pdf')
	doc = RLDocTemplate(destfn,pagesize = pagesize)

	#this builds the story
	#resetStory()

	import ch1_intro
	import ch2_graphics
	import ch2a_fonts
	import ch3_pdffeatures
	import ch4_platypus_concepts
	import ch5_paragraphs
	import ch6_tables
	import ch7_custom
	import ch9_future

	import app_demos

	story = getStory()
	if verbose: print 'Built story contains %d flowables...' % len(story)
	doc.build(story)
	if verbose: print 'Saved "%s"' % destfn

	# remove *.pyc files
	pat = os.path.join(os.path.dirname(sys.argv[0]), '*.pyc')
	for file in glob.glob(pat):
		os.remove(file)

def makeSuite():
	"standard test harness support - run self as separate process"
	from reportlab.test.utils import ScriptThatMakesFileTest
	return ScriptThatMakesFileTest('../docs/userguide', 'genuserguide.py', 'userguide.pdf')

if __name__=="__main__":
	verbose = '-s' not in sys.argv
	if not verbose: sys.argv.remove('-s')
	timing = '-timing' in sys.argv
	if timing: sys.argv.remove('-timing')
	prof = '-prof' in sys.argv
	if prof: sys.argv.remove('-prof')
	if len(sys.argv) > 1:
		try:
			(w, h) = eval(sys.argv[1])
		except:
			print 'Expected page size in argument 1', sys.argv[1]
			raise
		if verbose:
			print 'set page size to',sys.argv[1]
	else:
		(w, h) = defaultPageSize
	if timing:
		from time import time
		t0 = time()
		run((w, h, verbose))
		if verbose:
			print 'Generation of userguide took %.2f seconds' % (time()-t0)
	elif prof:
		import profile
		profile.run('run((w, h))','genuserguide.stats')
	else:
		run((w, h))
<commit_msg>Added default page size to run<commit_after>#!/bin/env python
#copyright ReportLab Inc. 2001
#see license.txt for license details
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/docs/userguide/genuserguide.py?cvsroot=reportlab
#$Header: /tmp/reportlab/reportlab/docs/userguide/genuserguide.py,v 1.8 2001/12/11 11:17:48 rgbecker Exp $

__version__=''' $Id: genuserguide.py,v 1.8 2001/12/11 11:17:48 rgbecker Exp $ '''

__doc__ = """
This module contains the script for building the user guide.
"""

from reportlab.tools.docco.rl_doc_utils import *

def run(pagesize=defaultpageSize, verbose=0):

	# copy to target
	import reportlab
	destfn = os.path.join(os.path.dirname(reportlab.__file__),'docs','userguide.pdf')
	doc = RLDocTemplate(destfn,pagesize = pagesize)

	#this builds the story
	#resetStory()

	import ch1_intro
	import ch2_graphics
	import ch2a_fonts
	import ch3_pdffeatures
	import ch4_platypus_concepts
	import ch5_paragraphs
	import ch6_tables
	import ch7_custom
	import ch9_future

	import app_demos

	story = getStory()
	if verbose: print 'Built story contains %d flowables...' % len(story)
	doc.build(story)
	if verbose: print 'Saved "%s"' % destfn

	# remove *.pyc files
	pat = os.path.join(os.path.dirname(sys.argv[0]), '*.pyc')
	for file in glob.glob(pat):
		os.remove(file)

def makeSuite():
	"standard test harness support - run self as separate process"
	from reportlab.test.utils import ScriptThatMakesFileTest
	return ScriptThatMakesFileTest('../docs/userguide', 'genuserguide.py', 'userguide.pdf')

if __name__=="__main__":
	verbose = '-s' not in sys.argv
	if not verbose: sys.argv.remove('-s')
	timing = '-timing' in sys.argv
	if timing: sys.argv.remove('-timing')
	prof = '-prof' in sys.argv
	if prof: sys.argv.remove('-prof')
	if len(sys.argv) > 1:
		try:
			(w, h) = eval(sys.argv[1])
		except:
			print 'Expected page size in argument 1', sys.argv[1]
			raise
		if verbose:
			print 'set page size to',sys.argv[1]
	else:
		(w, h) = defaultPageSize
	if timing:
		from time import time
		t0 = time()
		run((w, h, verbose))
		if verbose:
			print 'Generation of userguide took %.2f seconds' % (time()-t0)
	elif prof:
		import profile
		profile.run('run((w, h))','genuserguide.stats')
	else:
		run((w, h))
<|endoftext|>


<commit_before>import os
import random

from chalice import Chalice, CORSConfig, NotFoundError, BadRequestError

from chalicelib import apigateway
from chalicelib import gh_oauth
from chalicelib import s3
from chalicelib import sdb
from chalicelib import settings
from chalicelib import utils

app = Chalice(app_name=settings.APP_NAME)
app.debug = settings.DEBUG

cors_config = CORSConfig(
    allow_origin=os.environ.get('CORS_ALLOW_ORGIN', settings.CORS_ALLOW_ORGIN),
    max_age=86400,
)

@app.route('/')
def index():
    return 'GROT2 server'

@app.route('/gh-oauth', cors=cors_config)
def gh_oauth_endpoint():
    gh_code = app.current_request.query_params.get('code', None)
    if not gh_code:
        raise BadRequestError('no code')

    user_data = gh_oauth.get_user_data(
        gh_code,
        app.current_request.stage_vars.get(
            'GH_OAUTH_CLIENT_ID', os.environ.get('GH_OAUTH_CLIENT_ID')),
        app.current_request.stage_vars.get(
            'GH_OAUTH_CLIENT_SECRET', os.environ.get('GH_OAUTH_CLIENT_SECRET')),
    )
    if not user_data:
        raise BadRequestError('invalid code')

    user_id = user_data['login']
    email = user_data.get('email', '')
    api_key = apigateway.get_api_key(user_id)
    if not api_key:
        api_key = apigateway.new_api_key(user_id, email)
        sdb.new_user(user_id, email, api_key)
    return {'x-api-key': api_key, 'user_id': user_id}

@app.route('/match', methods=['PUT'], api_key_required=True, cors=cors_config)
def match_put():
    api_key = app.current_request.context['identity']['apiKey']
    user_id = sdb.get_user_id(api_key)
    match_id = sdb.new_match(api_key, user_id)
    sdb.increment_total_matches(user_id)
    s3.update_hof(sdb.get_hof_data())
    return {'match_id': match_id}

@app.route('/match/{match_id}', methods=['GET'], api_key_required=True, cors=cors_config)
def match_get(match_id):
    api_key = app.current_request.context['identity']['apiKey']
    match = sdb.get_match(api_key, match_id)
    if not match:
        raise BadRequestError('invalid match_id')

    if app.debug:
        utils.print_match_state(match.get_state())

    return match.get_state()

@app.route('/match/{match_id}', methods=['POST'], api_key_required=True, cors=cors_config)
def match_post(match_id):
    api_key = app.current_request.context['identity']['apiKey']
    match = sdb.get_match(api_key, match_id)
    if not match.is_active():
        return match.get_state()

    request = app.current_request
    data = request.json_body
    try:
        row = int(data['row'])
        col = int(data['col'])
    except (
        KeyError,
        TypeError,
        ValueError,
    ):
        raise BadRequestError('row or col is missing or not int')

    if not 0 <= row < settings.BOARD_SIZE or not 0 <= col < settings.BOARD_SIZE:
        raise BadRequestError('row or col is out of range')

    match.start_move(row, col)
    sdb.update_match(api_key, match_id, match)
    if not match.is_active():
        sdb.increment_total_score(match.user_id, match.score)
        s3.update_hof(sdb.get_hof_data())

    if app.debug:
        print(data)
        utils.print_match_state(match.get_state())

    return match.get_state()

@app.route('/match/{match_id}/results', methods=['GET'], cors=cors_config)
def match_results_get(match_id):
    matches = sdb.get_match_results(match_id)
    matches.sort(key=lambda i: int(i['score']), reverse=True)
    return {
        'players': [
            {'user': match['user_id'], 'score': match['score']}
            for match in matches
        ]
    }<commit_msg>fix gh oauth<commit_after>import os
import random

from chalice import Chalice, CORSConfig, NotFoundError, BadRequestError

from chalicelib import apigateway
from chalicelib import gh_oauth
from chalicelib import s3
from chalicelib import sdb
from chalicelib import settings
from chalicelib import utils

app = Chalice(app_name=settings.APP_NAME)
app.debug = settings.DEBUG

cors_config = CORSConfig(
    allow_origin=os.environ.get('CORS_ALLOW_ORGIN', settings.CORS_ALLOW_ORGIN),
    max_age=86400,
)

@app.route('/')
def index():
    return 'GROT2 server'

@app.route('/gh-oauth', cors=cors_config)
def gh_oauth_endpoint():
    gh_code = app.current_request.query_params.get('code', None)
    if not gh_code:
        raise BadRequestError('no code')

    user_data = gh_oauth.get_user_data(
        gh_code,
        app.current_request.stage_vars.get(
            'GH_OAUTH_CLIENT_ID', os.environ.get('GH_OAUTH_CLIENT_ID')),
        app.current_request.stage_vars.get(
            'GH_OAUTH_CLIENT_SECRET', os.environ.get('GH_OAUTH_CLIENT_SECRET')),
    )
    if not user_data:
        raise BadRequestError('invalid code')

    user_id = user_data['login']
    email = user_data.get('email', '') or ''
    api_key = apigateway.get_api_key(user_id)
    if not api_key:
        api_key = apigateway.new_api_key(user_id, email)
        sdb.new_user(user_id, email, api_key)
    return {'x-api-key': api_key, 'user_id': user_id}

@app.route('/match', methods=['PUT'], api_key_required=True, cors=cors_config)
def match_put():
    api_key = app.current_request.context['identity']['apiKey']
    user_id = sdb.get_user_id(api_key)
    match_id = sdb.new_match(api_key, user_id)
    sdb.increment_total_matches(user_id)
    s3.update_hof(sdb.get_hof_data())
    return {'match_id': match_id}

@app.route('/match/{match_id}', methods=['GET'], api_key_required=True, cors=cors_config)
def match_get(match_id):
    api_key = app.current_request.context['identity']['apiKey']
    match = sdb.get_match(api_key, match_id)
    if not match:
        raise BadRequestError('invalid match_id')

    if app.debug:
        utils.print_match_state(match.get_state())

    return match.get_state()

@app.route('/match/{match_id}', methods=['POST'], api_key_required=True, cors=cors_config)
def match_post(match_id):
    api_key = app.current_request.context['identity']['apiKey']
    match = sdb.get_match(api_key, match_id)
    if not match.is_active():
        return match.get_state()

    request = app.current_request
    data = request.json_body
    try:
        row = int(data['row'])
        col = int(data['col'])
    except (
        KeyError,
        TypeError,
        ValueError,
    ):
        raise BadRequestError('row or col is missing or not int')

    if not 0 <= row < settings.BOARD_SIZE or not 0 <= col < settings.BOARD_SIZE:
        raise BadRequestError('row or col is out of range')

    match.start_move(row, col)
    sdb.update_match(api_key, match_id, match)
    if not match.is_active():
        sdb.increment_total_score(match.user_id, match.score)
        s3.update_hof(sdb.get_hof_data())

    if app.debug:
        print(data)
        utils.print_match_state(match.get_state())

    return match.get_state()

@app.route('/match/{match_id}/results', methods=['GET'], cors=cors_config)
def match_results_get(match_id):
    matches = sdb.get_match_results(match_id)
    matches.sort(key=lambda i: int(i['score']), reverse=True)
    return {
        'players': [
            {'user': match['user_id'], 'score': match['score']}
            for match in matches
        ]
    }<|endoftext|>


<commit_before>from conans import ConanFile
from conans.tools import download, unzip
import os

VERSION = "0.0.6"

class IWYUCTargetCmakeConan(ConanFile):
    name = "iwyu-target-cmake"
    version = os.environ.get("CONAN_VERSION_OVERRIDE", VERSION)
    generators = "cmake"
    requires = ("cmake-include-guard/master@smspillaz/cmake-include-guard",
                "tooling-find-pkg-util/master@smspillaz/tooling-find-pkg-util",
                "tooling-cmake-util/master@smspillaz/tooling-cmake-util")
    url = "http://github.com/polysquare/iwyu-target-cmake"
    license = "MIT"
    options = {
        "dev": [True, False]
    }
    default_options = "dev=False"

    def requirements(self):
        if self.options.dev:
            self.requires("cmake-module-common/master@smspillaz/cmake-module-common")

    def source(self):
        zip_name = "iwyu-target-cmake.zip"
        download("https://github.com/polysquare/"
                 "iwyu-target-cmake/archive/{version}.zip"
                 "".format(version="v" + VERSION),
                 zip_name)
        unzip(zip_name)
        os.unlink(zip_name)

    def package(self):
        self.copy(pattern="Find*.cmake",
                  dst="",
                  src="iwyu-target-cmake-" + VERSION,
                  keep_path=True)
        self.copy(pattern="*.cmake",
                  dst="cmake/iwyu-target-cmake",
                  src="iwyu-target-cmake-" + VERSION,
                  keep_path=True)
<commit_msg>Bump version: 0.0.6 -> 0.0.7<commit_after>from conans import ConanFile
from conans.tools import download, unzip
import os

VERSION = "0.0.7"

class IWYUCTargetCmakeConan(ConanFile):
    name = "iwyu-target-cmake"
    version = os.environ.get("CONAN_VERSION_OVERRIDE", VERSION)
    generators = "cmake"
    requires = ("cmake-include-guard/master@smspillaz/cmake-include-guard",
                "tooling-find-pkg-util/master@smspillaz/tooling-find-pkg-util",
                "tooling-cmake-util/master@smspillaz/tooling-cmake-util")
    url = "http://github.com/polysquare/iwyu-target-cmake"
    license = "MIT"
    options = {
        "dev": [True, False]
    }
    default_options = "dev=False"

    def requirements(self):
        if self.options.dev:
            self.requires("cmake-module-common/master@smspillaz/cmake-module-common")

    def source(self):
        zip_name = "iwyu-target-cmake.zip"
        download("https://github.com/polysquare/"
                 "iwyu-target-cmake/archive/{version}.zip"
                 "".format(version="v" + VERSION),
                 zip_name)
        unzip(zip_name)
        os.unlink(zip_name)

    def package(self):
        self.copy(pattern="Find*.cmake",
                  dst="",
                  src="iwyu-target-cmake-" + VERSION,
                  keep_path=True)
        self.copy(pattern="*.cmake",
                  dst="cmake/iwyu-target-cmake",
                  src="iwyu-target-cmake-" + VERSION,
                  keep_path=True)
<|endoftext|>


<commit_before>#!/usr/bin/env python
""" SAML Pod Check for OpenShift V3 """

# Adding the ignore because it does not like the naming of the script
# to be different than the class name
# pylint: disable=invalid-name

# pylint: disable=wrong-import-position
# pylint: disable=broad-except

import argparse
import time
import urllib2

import logging
logging.basicConfig(
    format='%(asctime)s - %(relativeCreated)6d - %(levelname)-8s - %(message)s',
)
logger = logging.getLogger()
logger.setLevel(logging.WARN)

# Our jenkins server does not include these rpms.
# In the future we might move this to a container where these
# libs might exist
#pylint: disable=import-error

from openshift_tools.monitoring.ocutil import OCUtil
from openshift_tools.monitoring.metric_sender import MetricSender

ocutil = OCUtil()

def runOCcmd(cmd, base_cmd='oc'):
    """ log commands through ocutil """
    logger.info(base_cmd + " " + cmd)
    oc_time = time.time()
    oc_result = ocutil.run_user_cmd(cmd, base_cmd=base_cmd, )
    logger.info("oc command took %s seconds", str(time.time() - oc_time))
    return oc_result

def runOCcmd_yaml(cmd, base_cmd='oc'):
    """ log commands through ocutil """
    logger.info(base_cmd + " " + cmd)
    ocy_time = time.time()
    ocy_result = ocutil.run_user_cmd_yaml(cmd, base_cmd=base_cmd, )
    logger.info("oc command took %s seconds", str(time.time() - ocy_time))
    return ocy_result

def parse_args():
    """ parse the args from the cli """
    parser = argparse.ArgumentParser(description='OpenShift app create end-to-end test')
    parser.add_argument('-v', '--verbose', action='count', default=0,
                        help='verbosity level, specify multiple')
    parser.add_argument('--dc', default="saml-auth", help='deployment config name')
    parser.add_argument('--service', default="saml-auth", help='service name')
    parser.add_argument('--namespace', default="default", help='service namespace')

    args = parser.parse_args()

    if args.verbose > 0:
        logger.setLevel(logging.INFO)
    if args.verbose > 1:
        logger.setLevel(logging.DEBUG)

    return args

def gen_test_url(service):
    """ Generate Test URL """
    return "https://%s:%s/logged_out.html" % (
        service['spec']['clusterIP'],
        443
    )

def curl(url):
    """ Test URL, return http code """
    try:
        return urllib2.urlopen(url, timeout=30).getcode()
    except urllib2.HTTPError as e:
        return e.fp.getcode()
    except Exception:
        logger.exception("unknown error")

    return 0

def test_saml_pod(args=None, ):
    """ Test SAML Pod Status """
    ocutil.namespace = args.namespace
    logger.info('Namespace: %s', args.namespace)

    dc = runOCcmd_yaml("get dc {}".format(args.service))
    logger.info('Deployment Config replicas: %s', dc['status']['replicas'])
    logger.info('Deployment Config availableReplicas: %s', dc['status']['availableReplicas'])
    logger.debug(dc)

    if dc['status']['availableReplicas'] < 1:
        # Pod not running
        return 0

    service = runOCcmd_yaml("get service {}".format(args.service))
    logger.info('Service: %s', args.service)
    logger.debug(service)

    url = gen_test_url(service)
    logger.info('Service IP: %s', service['spec']['clusterIP'])
    logger.info('Service URL: %s', url)

    curl_status = curl(url)
    logger.info("HTTP response (curl): %s", curl_status)

    if curl_status == 200:
        # everything is ok
        return 1
    else:
        # we have the pod, but bad http response
        return 2

def main():
    """ SAML Pod Status """
    args = parse_args()
    logger.debug("args: ")
    logger.debug(args)

    result = test_saml_pod(args=args, )

    #send the value to zabbix
    mts = MetricSender(verbose=args.verbose)
    mts.add_metric({'openshift.master.saml.status': result})
    mts.send_metrics()

if __name__ == "__main__":
    main()
<commit_msg>change the monitor method of saml pod<commit_after>#!/usr/bin/env python
""" SAML Pod Check for OpenShift V3 """

# Adding the ignore because it does not like the naming of the script
# to be different than the class name
# pylint: disable=invalid-name

# pylint: disable=wrong-import-position
# pylint: disable=broad-except
# pylint: disable=line-too-long

import argparse
import time
import urllib2

import logging
logging.basicConfig(
    format='%(asctime)s - %(relativeCreated)6d - %(levelname)-8s - %(message)s',
)
logger = logging.getLogger()
logger.setLevel(logging.WARN)

# Our jenkins server does not include these rpms.
# In the future we might move this to a container where these
# libs might exist
#pylint: disable=import-error

from openshift_tools.monitoring.ocutil import OCUtil
from openshift_tools.monitoring.metric_sender import MetricSender

ocutil = OCUtil()

def runOCcmd(cmd, base_cmd='oc'):
    """ log commands through ocutil """
    logger.info(base_cmd + " " + cmd)
    oc_time = time.time()
    oc_result = ocutil.run_user_cmd(cmd, base_cmd=base_cmd, )
    logger.info("oc command took %s seconds", str(time.time() - oc_time))
    return oc_result

def runOCcmd_yaml(cmd, base_cmd='oc'):
    """ log commands through ocutil """
    logger.info(base_cmd + " " + cmd)
    ocy_time = time.time()
    ocy_result = ocutil.run_user_cmd_yaml(cmd, base_cmd=base_cmd, )
    logger.info("oc command took %s seconds", str(time.time() - ocy_time))
    return ocy_result

def parse_args():
    """ parse the args from the cli """
    parser = argparse.ArgumentParser(description='OpenShift app create end-to-end test')
    parser.add_argument('-v', '--verbose', action='count', default=0,
                        help='verbosity level, specify multiple')
    parser.add_argument('--dc', default="saml-auth", help='deployment config name')
    parser.add_argument('--service', default="saml-auth", help='service name')
    parser.add_argument('--namespace', default="default", help='service namespace')

    args = parser.parse_args()

    if args.verbose > 0:
        logger.setLevel(logging.INFO)
    if args.verbose > 1:
        logger.setLevel(logging.DEBUG)

    return args

def gen_test_url(service):
    """ Generate Test URL """
    return "https://%s:%s/logged_out.html" % (
        service['spec']['clusterIP'],
        443
    )

def curl(url):
    """ Test URL, return http code """
    try:
        return urllib2.urlopen(url, timeout=30).getcode()
    except urllib2.HTTPError as e:
        return e.fp.getcode()
    except Exception:
        logger.exception("unknown error")

    return 0

def test_saml_pod(args=None, ):
    """ Test SAML Pod Status """
    ocutil.namespace = args.namespace
    logger.info('Namespace: %s', args.namespace)

    dc = runOCcmd_yaml("get dc {}".format(args.service))
    logger.info('Deployment Config replicas: %s', dc['status']['replicas'])
    logger.info('Deployment Config availableReplicas: %s', dc['status']['availableReplicas'])
    logger.debug(dc)

    if dc['status']['availableReplicas'] < 1:
        # Pod not running
        return 0
    #get the pod status and see if the saml pod is running
    pods = runOCcmd_yaml("get pod")
    count_running_saml_pod = 0

    for pod in pods['items']:
        if (not pod['metadata']['name'].find(args.dc) == -1) and (pod['status']['phase'] == 'Running') and (pod['status']['containerStatuses'][0]['ready'] is True):
            count_running_saml_pod = count_running_saml_pod + 1
    logger.info('Healthy saml pod count is : %s', count_running_saml_pod)
    if count_running_saml_pod == dc['status']['replicas']:
        #the running pod count  match the dc
        return 1
    else:
        return 2

def main():
    """ SAML Pod Status """
    args = parse_args()
    logger.debug("args: ")
    logger.debug(args)

    result = test_saml_pod(args=args, )

    #send the value to zabbix
    mts = MetricSender(verbose=args.verbose)
    mts.add_metric({'openshift.master.saml.status': result})
    mts.send_metrics()

if __name__ == "__main__":
    main()
<|endoftext|>