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

"""WebSocket HyBi 06 opening handshake processor.
"""

import base64
import logging
import os
import re

from mod_pywebsocket import common
from mod_pywebsocket import stream
from mod_pywebsocket import util
from mod_pywebsocket.handshake._base import HandshakeError
from mod_pywebsocket.handshake._base import check_header_lines
from mod_pywebsocket.handshake._base import get_mandatory_header

_MANDATORY_HEADERS = [
    # key, expected value or None
    [common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE],
    [common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE],
]

_BASE64_REGEX = re.compile('^[+/0-9A-Za-z]*=*$')

def compute_accept(key):
    """Computes value for the Sec-WebSocket-Accept header from value of the
    Sec-WebSocket-Key header.
    """

    accept_binary = util.sha1_hash(
        key + common.WEBSOCKET_ACCEPT_UUID).digest()
    accept = base64.b64encode(accept_binary)

    return (accept, accept_binary)

class Handshaker(object):
    """This class performs WebSocket handshake."""

    def __init__(self, request, dispatcher):
        """Construct an instance.

        Args:
            request: mod_python request.
            dispatcher: Dispatcher (dispatch.Dispatcher).

        Handshaker will add attributes such as ws_resource during handshake.
        """

        self._logger = util.get_class_logger(self)

        self._request = request
        self._dispatcher = dispatcher

    def do_handshake(self):
        check_header_lines(self._request, _MANDATORY_HEADERS)
        self._request.ws_resource = self._request.uri

        unused_host = get_mandatory_header(self._request, common.HOST_HEADER)

        self._get_origin()
        self._check_version()
        self._set_protocol()
        self._set_extensions()

        key = self._get_key()
        (accept, accept_binary) = compute_accept(key)
        self._logger.debug('server accept : %s (%s)' %
                           (accept, util.hexify(accept_binary)))

        self._logger.debug('IETF HyBi 06 protocol')
        self._request.ws_version = common.VERSION_HYBI06
        self._request.ws_stream = stream.Stream(self._request)

        self._request.ws_close_code = None
        self._request.ws_close_reason = None

        self._dispatcher.do_extra_handshake(self._request)

        if self._request.ws_requested_protocols is not None:
            if self._request.ws_protocol is None:
                raise HandshakeError(
                    'do_extra_handshake must choose one subprotocol from '
                    'ws_requested_protocols and set it to ws_protocol')

            # TODO(tyoshino): Validate selected subprotocol value.

            self._logger.debug(
                'protocol accepted  : %r', self._request.ws_protocol)
        else:
            if self._request.ws_protocol is not None:
                raise HandshakeError(
                    'ws_protocol must be None when the client didn\'t request '
                    'any subprotocol')

        self._send_handshake(accept)

    def _get_origin(self):
        origin = self._request.headers_in.get(
            common.SEC_WEBSOCKET_ORIGIN_HEADER)
        self._request.ws_origin = origin

    def _check_version(self):
        unused_value = get_mandatory_header(
            self._request, common.SEC_WEBSOCKET_VERSION_HEADER, '6')

    def _set_protocol(self):
        self._request.ws_protocol = None

        protocol_header = self._request.headers_in.get(
            common.SEC_WEBSOCKET_PROTOCOL_HEADER)

        if not protocol_header:
            self._request.ws_requested_protocols = None
            return

        # TODO(tyoshino): Validate the header value.

        requested_protocols = protocol_header.split(',')
        self._request.ws_requested_protocols = [
            s.strip() for s in requested_protocols]

        self._logger.debug('protocols requested : %r', requested_protocols)

    def _set_extensions(self):
        self._request.ws_deflate = False

        extensions_header = self._request.headers_in.get(
            common.SEC_WEBSOCKET_EXTENSIONS_HEADER)
        if not extensions_header:
            self._request.ws_extensions = None
            return

        self._request.ws_extensions = []

        requested_extensions = extensions_header.split(',')
        # TODO(tyoshino): Follow the ABNF in the spec.
        requested_extensions = [s.strip() for s in requested_extensions]

        for extension in requested_extensions:
            # We now support only deflate-stream extension. Any other
            # extension requests are just ignored for now.
            if extension == 'deflate-stream':
                self._request.ws_extensions.append(extension)
                self._request.ws_deflate = True

        self._logger.debug('extensions requested : %r', requested_extensions)
        self._logger.debug(
            'extensions accepted  : %r', self._request.ws_extensions)

    def _validate_key(self, key):
        # Validate
        key_is_valid = False
        try:
            # Validate key by quick regex match before parsing by base64
            # module. Because base64 module skips invalid characters, we have
            # to do this in advance to make this server strictly reject illegal
            # keys.
            if _BASE64_REGEX.match(key):
                decoded_key = base64.b64decode(key)
                if len(decoded_key) == 16:
                    key_is_valid = True
        except TypeError, e:
            pass

        if not key_is_valid:
            raise HandshakeError(
                'Illegal value for header ' + common.SEC_WEBSOCKET_KEY_HEADER +
                ': ' + key)

        return decoded_key

    def _get_key(self):
        key = get_mandatory_header(
            self._request, common.SEC_WEBSOCKET_KEY_HEADER)

        decoded_key = self._validate_key(key)

        self._logger.debug('client nonce : %s (%s)' %
                           (key, util.hexify(decoded_key)))

        return key

    def _send_handshake(self, accept):
        self._request.connection.write(
            'HTTP/1.1 101 Switching Protocols\r\n')
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE))
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE))
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.SEC_WEBSOCKET_ACCEPT_HEADER, accept))
        # TODO(tyoshino): Encode value of protocol and extensions if any
        # special character that we have to encode by some manner.
        if self._request.ws_protocol is not None:
            self._request.connection.write(
                '%s: %s\r\n' %
                (common.SEC_WEBSOCKET_PROTOCOL_HEADER,
                 self._request.ws_protocol))
        if self._request.ws_extensions is not None:
            self._request.connection.write(
                '%s: %s\r\n' %
                (common.SEC_WEBSOCKET_EXTENSIONS_HEADER,
                 ', '.join(self._request.ws_extensions)))
        self._request.connection.write('\r\n')

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

"""WebSocket HyBi 06 opening handshake processor.
"""

import base64
import logging
import os
import re

from mod_pywebsocket import common
from mod_pywebsocket.stream import Stream
from mod_pywebsocket import util
from mod_pywebsocket.handshake._base import HandshakeError
from mod_pywebsocket.handshake._base import check_header_lines
from mod_pywebsocket.handshake._base import get_mandatory_header

_MANDATORY_HEADERS = [
    # key, expected value or None
    [common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE],
    [common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE],
]

_BASE64_REGEX = re.compile('^[+/0-9A-Za-z]*=*$')

def compute_accept(key):
    """Computes value for the Sec-WebSocket-Accept header from value of the
    Sec-WebSocket-Key header.
    """

    accept_binary = util.sha1_hash(
        key + common.WEBSOCKET_ACCEPT_UUID).digest()
    accept = base64.b64encode(accept_binary)

    return (accept, accept_binary)

class Handshaker(object):
    """This class performs WebSocket handshake."""

    def __init__(self, request, dispatcher):
        """Construct an instance.

        Args:
            request: mod_python request.
            dispatcher: Dispatcher (dispatch.Dispatcher).

        Handshaker will add attributes such as ws_resource during handshake.
        """

        self._logger = util.get_class_logger(self)

        self._request = request
        self._dispatcher = dispatcher

    def do_handshake(self):
        check_header_lines(self._request, _MANDATORY_HEADERS)
        self._request.ws_resource = self._request.uri

        unused_host = get_mandatory_header(self._request, common.HOST_HEADER)

        self._get_origin()
        self._check_version()
        self._set_protocol()
        self._set_extensions()

        key = self._get_key()
        (accept, accept_binary) = compute_accept(key)
        self._logger.debug('server accept : %s (%s)' %
                           (accept, util.hexify(accept_binary)))

        self._logger.debug('IETF HyBi 06 protocol')
        self._request.ws_version = common.VERSION_HYBI06
        self._request.ws_stream = Stream(self._request)

        self._request.ws_close_code = None
        self._request.ws_close_reason = None

        self._dispatcher.do_extra_handshake(self._request)

        if self._request.ws_requested_protocols is not None:
            if self._request.ws_protocol is None:
                raise HandshakeError(
                    'do_extra_handshake must choose one subprotocol from '
                    'ws_requested_protocols and set it to ws_protocol')

            # TODO(tyoshino): Validate selected subprotocol value.

            self._logger.debug(
                'protocol accepted  : %r', self._request.ws_protocol)
        else:
            if self._request.ws_protocol is not None:
                raise HandshakeError(
                    'ws_protocol must be None when the client didn\'t request '
                    'any subprotocol')

        self._send_handshake(accept)

    def _get_origin(self):
        origin = self._request.headers_in.get(
            common.SEC_WEBSOCKET_ORIGIN_HEADER)
        self._request.ws_origin = origin

    def _check_version(self):
        unused_value = get_mandatory_header(
            self._request, common.SEC_WEBSOCKET_VERSION_HEADER, '6')

    def _set_protocol(self):
        self._request.ws_protocol = None

        protocol_header = self._request.headers_in.get(
            common.SEC_WEBSOCKET_PROTOCOL_HEADER)

        if not protocol_header:
            self._request.ws_requested_protocols = None
            return

        # TODO(tyoshino): Validate the header value.

        requested_protocols = protocol_header.split(',')
        self._request.ws_requested_protocols = [
            s.strip() for s in requested_protocols]

        self._logger.debug('protocols requested : %r', requested_protocols)

    def _set_extensions(self):
        self._request.ws_deflate = False

        extensions_header = self._request.headers_in.get(
            common.SEC_WEBSOCKET_EXTENSIONS_HEADER)
        if not extensions_header:
            self._request.ws_extensions = None
            return

        self._request.ws_extensions = []

        requested_extensions = extensions_header.split(',')
        # TODO(tyoshino): Follow the ABNF in the spec.
        requested_extensions = [s.strip() for s in requested_extensions]

        for extension in requested_extensions:
            # We now support only deflate-stream extension. Any other
            # extension requests are just ignored for now.
            if extension == 'deflate-stream':
                self._request.ws_extensions.append(extension)
                self._request.ws_deflate = True

        self._logger.debug('extensions requested : %r', requested_extensions)
        self._logger.debug(
            'extensions accepted  : %r', self._request.ws_extensions)

    def _validate_key(self, key):
        # Validate
        key_is_valid = False
        try:
            # Validate key by quick regex match before parsing by base64
            # module. Because base64 module skips invalid characters, we have
            # to do this in advance to make this server strictly reject illegal
            # keys.
            if _BASE64_REGEX.match(key):
                decoded_key = base64.b64decode(key)
                if len(decoded_key) == 16:
                    key_is_valid = True
        except TypeError, e:
            pass

        if not key_is_valid:
            raise HandshakeError(
                'Illegal value for header ' + common.SEC_WEBSOCKET_KEY_HEADER +
                ': ' + key)

        return decoded_key

    def _get_key(self):
        key = get_mandatory_header(
            self._request, common.SEC_WEBSOCKET_KEY_HEADER)

        decoded_key = self._validate_key(key)

        self._logger.debug('client nonce : %s (%s)' %
                           (key, util.hexify(decoded_key)))

        return key

    def _send_handshake(self, accept):
        self._request.connection.write(
            'HTTP/1.1 101 Switching Protocols\r\n')
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE))
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE))
        self._request.connection.write(
            '%s: %s\r\n' %
            (common.SEC_WEBSOCKET_ACCEPT_HEADER, accept))
        # TODO(tyoshino): Encode value of protocol and extensions if any
        # special character that we have to encode by some manner.
        if self._request.ws_protocol is not None:
            self._request.connection.write(
                '%s: %s\r\n' %
                (common.SEC_WEBSOCKET_PROTOCOL_HEADER,
                 self._request.ws_protocol))
        if self._request.ws_extensions is not None:
            self._request.connection.write(
                '%s: %s\r\n' %
                (common.SEC_WEBSOCKET_EXTENSIONS_HEADER,
                 ', '.join(self._request.ws_extensions)))
        self._request.connection.write('\r\n')

# vi:sts=4 sw=4 et
<|endoftext|>


<commit_before>"""
    pygments.lexers.postgres
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Lexers for PostgreSQL-specific SQL and psql interactive session.

    :copyright: Copyright 2011 by Daniele Varrazzo.
    :license: BSD, see LICENSE for details.
"""

import re
import urllib2

from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
     this, do_insertions
from pygments.token import Error, Punctuation, Literal, Token, \
     Text, Comment, Operator, Keyword, Name, String, Number, Generic

from pygments.lexers._postgres_builtins import KEYWORDS, DATATYPES

__all__ = [ 'PostgresLexer', 'PostgresConsoleLexer' ]

line_re  = re.compile('.*?\n')

class PostgresLexer(RegexLexer):
    """
    Lexer for the PostgreSQL dialect of SQL.
    """

    name = 'PostgreSQL SQL dialect'
    aliases = ['postgresql', 'postgres']
    mimetypes = ['text/x-postgresql']

    flags = re.IGNORECASE
    tokens = {
        'root': [
            (r'\s+', Text),
            (r'--.*?\n', Comment.Single),
            (r'/\*', Comment.Multiline, 'multiline-comments'),
            (r'(' + '|'.join([s.replace(" ", "\s+") for s in DATATYPES])
                  + r')\b', Name.Builtin),
            (r'(' + '|'.join(KEYWORDS) + r')\b', Keyword),
            (r'[+*/<>=~!@#%^&|`?^-]', Operator),
            (r'::', Operator),  # cast
            (r'([0-9]*\.[0-9]*|[0-9]+)(e[+-]?[0-9]+)?', Number.Float),
            (r'[0-9]+', Number.Integer),
            # TODO: Backslash escapes?
            (r"'(''|[^'])*'", String.Single),
            (r'"(""|[^"])*"', String.Name), # quoted identifier
            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),

            # psql backslash command and variable
            # These actually belongs to the console lexer,
            # but putting it here makes things easier.
            (r'\\.*?\n', Name),             # TODO: what is a good token?
            (r':[a-z][a-z0-9_]*\b', Name.Variable),

            (r'[;:()\[\],\.]', Punctuation),
        ],
        'multiline-comments': [
            (r'/\*', Comment.Multiline, 'multiline-comments'),
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[^/\*]+', Comment.Multiline),
            (r'[/*]', Comment.Multiline)
        ]
    }

re_prompt = re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]+)?[=\-\(]#')
re_psql_command = re.compile(r'(\s*)(\\.+?)(\s+)$')
re_error = re.compile(r'ERROR:')
re_message = re.compile(r'(DEBUG|INFO|WARNING|ERROR|HINT|LINE [0-9]+:?)(.*?\n)')
re_charhint = re.compile(r'\s*\^\s*\n')

class PostgresConsoleLexer(Lexer):
    """
    Lexer for psql sessions.

    TODO: multiline comments are broken.
    """

    name = 'PostgreSQL console (psql)'
    aliases = ['psql', 'postgresql-console', 'postgres-console']
    mimetypes = ['text/x-postgresql-psql']

    def get_tokens_unprocessed(self, data):
        sql = PostgresLexer(**self.options)

        curcode = ''
        insertions = []
        out_token = Generic.Output
        for match in line_re.finditer(data):
            line = match.group()
            mprompt = re_prompt.match(line)
            if mprompt is not None:
                out_token = Generic.Output
                insertions.append((len(curcode),
                                   [(0, Generic.Prompt, mprompt.group())]))
                curcode += line[len(mprompt.group()):]
            else:
                if curcode:
                    for item in do_insertions(insertions,
                                              sql.get_tokens_unprocessed(curcode)):
                        yield item
                    curcode = ''
                    insertions = []
                mmsg = re_message.match(line)
                if mmsg is not None:
                    if mmsg.group(1).startswith("ERROR"):
                        out_token = Generic.Error
                    yield (mmsg.start(1), Generic.Strong, mmsg.group(1))
                    yield (mmsg.start(2), out_token, mmsg.group(2))
                elif re_charhint.match(line):
                    yield (match.start(), out_token, line)
                else:
                    yield (match.start(), Generic.Output, line)

        if curcode:
            for item in do_insertions(insertions,
                                      sql.get_tokens_unprocessed(curcode)):
                yield item

<commit_msg>Added prompt style => in psql lexer<commit_after>"""
    pygments.lexers.postgres
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Lexers for PostgreSQL-specific SQL and psql interactive session.

    :copyright: Copyright 2011 by Daniele Varrazzo.
    :license: BSD, see LICENSE for details.
"""

import re
import urllib2

from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
     this, do_insertions
from pygments.token import Error, Punctuation, Literal, Token, \
     Text, Comment, Operator, Keyword, Name, String, Number, Generic

from pygments.lexers._postgres_builtins import KEYWORDS, DATATYPES

__all__ = [ 'PostgresLexer', 'PostgresConsoleLexer' ]

line_re  = re.compile('.*?\n')

class PostgresLexer(RegexLexer):
    """
    Lexer for the PostgreSQL dialect of SQL.
    """

    name = 'PostgreSQL SQL dialect'
    aliases = ['postgresql', 'postgres']
    mimetypes = ['text/x-postgresql']

    flags = re.IGNORECASE
    tokens = {
        'root': [
            (r'\s+', Text),
            (r'--.*?\n', Comment.Single),
            (r'/\*', Comment.Multiline, 'multiline-comments'),
            (r'(' + '|'.join([s.replace(" ", "\s+") for s in DATATYPES])
                  + r')\b', Name.Builtin),
            (r'(' + '|'.join(KEYWORDS) + r')\b', Keyword),
            (r'[+*/<>=~!@#%^&|`?^-]', Operator),
            (r'::', Operator),  # cast
            (r'([0-9]*\.[0-9]*|[0-9]+)(e[+-]?[0-9]+)?', Number.Float),
            (r'[0-9]+', Number.Integer),
            # TODO: Backslash escapes?
            (r"'(''|[^'])*'", String.Single),
            (r'"(""|[^"])*"', String.Name), # quoted identifier
            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),

            # psql backslash command and variable
            # These actually belongs to the console lexer,
            # but putting it here makes things easier.
            (r'\\.*?\n', Name),             # TODO: what is a good token?
            (r':[a-z][a-z0-9_]*\b', Name.Variable),

            (r'[;:()\[\],\.]', Punctuation),
        ],
        'multiline-comments': [
            (r'/\*', Comment.Multiline, 'multiline-comments'),
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[^/\*]+', Comment.Multiline),
            (r'[/*]', Comment.Multiline)
        ]
    }

re_prompt = re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]+)?[=\-\(][#>]')
re_psql_command = re.compile(r'(\s*)(\\.+?)(\s+)$')
re_error = re.compile(r'ERROR:')
re_message = re.compile(r'(DEBUG|INFO|WARNING|ERROR|HINT|LINE [0-9]+:?)(.*?\n)')
re_charhint = re.compile(r'\s*\^\s*\n')

class PostgresConsoleLexer(Lexer):
    """
    Lexer for psql sessions.

    TODO: multiline comments are broken.
    """

    name = 'PostgreSQL console (psql)'
    aliases = ['psql', 'postgresql-console', 'postgres-console']
    mimetypes = ['text/x-postgresql-psql']

    def get_tokens_unprocessed(self, data):
        sql = PostgresLexer(**self.options)

        curcode = ''
        insertions = []
        out_token = Generic.Output
        for match in line_re.finditer(data):
            line = match.group()
            mprompt = re_prompt.match(line)
            if mprompt is not None:
                out_token = Generic.Output
                insertions.append((len(curcode),
                                   [(0, Generic.Prompt, mprompt.group())]))
                curcode += line[len(mprompt.group()):]
            else:
                if curcode:
                    for item in do_insertions(insertions,
                                              sql.get_tokens_unprocessed(curcode)):
                        yield item
                    curcode = ''
                    insertions = []
                mmsg = re_message.match(line)
                if mmsg is not None:
                    if mmsg.group(1).startswith("ERROR"):
                        out_token = Generic.Error
                    yield (mmsg.start(1), Generic.Strong, mmsg.group(1))
                    yield (mmsg.start(2), out_token, mmsg.group(2))
                elif re_charhint.match(line):
                    yield (match.start(), out_token, line)
                else:
                    yield (match.start(), Generic.Output, line)

        if curcode:
            for item in do_insertions(insertions,
                                      sql.get_tokens_unprocessed(curcode)):
                yield item

<|endoftext|>


<commit_before>import datetime
import hashlib
import textwrap
from collections import defaultdict

class LogCluster(object):
    def __init__(self, support, rsupport, log_file, outlier_file='', output_file=''):
        self.support = support
        self.rsupport = rsupport
        self.log_file = log_file
        self.outlier_file = outlier_file
        self.output_file = output_file
        self.wsize = 0
        self.csize = 0
        self.wsketch = {}
        self.wfilter = 0
        self.frequent_words = defaultdict(lambda: 0)
        self.candidates = {}
        self.clusters = {}
        self.outliers = []

    # This function logs the message given with parameter2,++,parameterN to
    # syslog, using the level parameter1+ The message is also written to stderr+
    def log_msg(*parameters):
        level = parameters[0]
        msg = ' '.join(parameters[1:])
        now = datetime.datetime.now()

        print now, level, msg

    # This function hashes the string given with parameter1 to an integer
    # in the range (0+++wsize-1) and returns the integer+ The wsize integer
    # can be set with the --wsize command line option+
    # L is unsigned 32 bit integer
    def hash_string(self, parameter):
        wraps = textwrap.wrap(parameter, self.wsize)
        hashes = []
        for wrap in wraps:
            hash_value = hashlib.md5(wrap).hexdigest()
            hashes.append(int(hash_value, 32))

        return hashes

    # This function hashes the candidate ID given with parameter1 to an integer
    # in the range (0+++csize-1) and returns the integer+ The csize integer
    # can be set with the --csize command line option+
    def hash_candidate(self, parameter):
        wraps = textwrap.wrap(parameter, self.csize)
        hashes = []
        for wrap in wraps:
            hash_value = hashlib.md5(wrap).hexdigest()
            hashes.append(int(hash_value, 32))

        return hashes

    # This function matches the line given with parameter1 with a regular
    # expression lineregexp (the expression can be set with the --lfilter
    # command line option)+ If the template string is defined (can be set
    # with the --template command line option), the line is converted
    # according to template (match variables in template are substituted
    # with values from regular expression match, and the resulting string
    # replaces the line)+ If the regular expression lineregexp does not match
    # the line, 0 is returned, otherwise the line (or converted line, if
    # --template option has been given) is returned+
    # If the --lfilter option has not been given but --lcfunc option is
    # present, the Perl function given with --lcfunc is used for matching
    # and converting the line+ If the function returns 'undef', line is
    # regarded non-matching, otherwise the value returned by the function
    # replaces the original line+
    # If neither --lfilter nor --lcfunc option has been given, the line
    # is returned without a trailing newline+
    # This function is OPTIONAL
    @staticmethod
    def process_line(line):
        return line

    # This function makes a pass over the data set and builds the sketch
    # @wsketch which is used for finding frequent words+ The sketch contains
    # wsize counters (wsize can be set with --wsize command line option)+
    # This function is OPTIONAL
    def build_word_sketch(self):
        words_dict = {}
        # for index in range(self.wsize):
        #     self.wsketch[index] = 0

        i = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                processed_line = self.process_line(line)
                i += 1
                words_list = processed_line.split()
                for word in words_list:
                    words_dict[word] = 1
                    index = self.hash_string(word)
                    self.wsketch[index] += 1

                    if self.wfilter:
                        # replace string
                        index = self.hash_string(word)
                        self.wsketch[index] += 1

    # This function makes a pass over the data set, finds frequent words and
    # stores them to %fwords hash table.
    # This function is MANDATORY
    def find_frequent_words(self):
        i = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue
                i += 1

                words_split = line.split()
                for word in words_split:
                    if word.strip() not in self.frequent_words.keys():
                        self.frequent_words[word] = 1
                    else:
                        self.frequent_words[word] += 1

        if not self.support:
            self.support = int(self.rsupport * i / 100)

        for word, frequency in self.frequent_words.iteritems():
            if frequency < self.support:
                self.frequent_words[word] = 0

        frequent_words_nonzero = {}
        for word, frequency in self.frequent_words.iteritems():
            if frequency != 0:
                frequent_words_nonzero[word] = frequency

        self.frequent_words = frequent_words_nonzero

    # This function makes a pass over the data set and builds the sketch
    # @csketch which is used for finding frequent candidates. The sketch contains
    # $csize counters ($csize can be set with --csize command line option).
    # This function is OPTIONAL
    def build_candidate_sketch(self):
        pass

    # This function logs the description for candidate parameter1.
    def print_candidate(self):
        for candidate in self.candidates:
            msg = ''
            for index in range(self.candidates[candidate]['WordCount']):
                if self.candidates[candidate]['Vars'][index][1] > 0:
                    msg += '*{' + str(self.candidates[candidate]['Vars'][index][0]) + ',' + \
                           str(self.candidates[candidate]['Vars'][index][1]) + '} '
                msg += self.candidates[candidate]['Words'][index] + ' '

            print msg
            print 'Support:', len(self.candidates[candidate]['Members'])

    # This function makes a pass over the data set, identifies cluster candidates
    # and stores them to %candidates hash table. If the --wweight command line
    # option has been provided, dependencies between frequent words are also
    # identified during the data pass and stored to %fword_deps hash table.
    def find_candidates(self):
        line_index = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                words = line.split()
                candidate = []
                varx = []
                varnum = 0

                for word in words:
                    if word in self.frequent_words:
                        candidate.append(word)
                        varx.append(varnum)
                        varnum = 0
                    else:
                        varnum += 1
                varx.append(varnum)

                # if the given candidate already exists, increase its support and
                # adjust its wildcard information, otherwise create a new candidate
                if candidate:
                    candidate_join = ' '.join(candidate)
                    if candidate_join not in self.candidates:
                        self.candidates[candidate_join] = {}
                        self.candidates[candidate_join]['Words'] = candidate
                        self.candidates[candidate_join]['WordCount'] = len(candidate)
                        self.candidates[candidate_join]['Vars'] = []
                        for varnum in varx:
                            self.candidates[candidate_join]['Vars'].append([varnum, varnum])
                        self.candidates[candidate_join]['Count'] = 1
                        self.candidates[candidate_join]['Members'] = []

                    else:
                        total = len(varx)
                        for index in range(total):
                            if self.candidates[candidate_join]['Vars'][index][0] > varx[index]:
                                self.candidates[candidate_join]['Vars'][index][0] = varx[index]
                            elif self.candidates[candidate_join]['Vars'][index][1] < varx[index]:
                                self.candidates[candidate_join]['Vars'][index][1] = varx[index]
                        self.candidates[candidate_join]['Count'] += 1
                    self.candidates[candidate_join]['Members'].append(line_index)
                line_index += 1

    # This function finds frequent candidates by removing candidates with
    # insufficient support from the %candidates hash table.
    def find_frequent_candidates(self):
        for candidate in self.candidates.keys():
            if self.candidates[candidate]['Count'] < self.support:
                self.candidates[candidate]['Count'] = 0

        candidates_nonzero = {}
        for key, val in self.candidates.iteritems():
            if self.candidates[key]['Count'] != 0:
                candidates_nonzero[key] = val

        self.candidates = candidates_nonzero

    # This function inserts a candidate into the prefix tree
    # This function is OPTIONAL
    def insert_into_prefix_tree(self):
        pass

    # This function arranges all candidates into the prefix tree data structure,
    # in order to facilitate fast matching between candidates
    # This function is OPTIONAL
    def build_prefix_tree(self):
        pass

    # This function finds more specific candidates for the given candidate with
    # the help of the prefix tree, and records more specific candidates into
    # the SubClusters hash table of the given candidate
    # This function is OPTIONAL
    def find_more_specific(self):
        pass

    # This function scans all cluster candidates (stored in %candidates hash
    # table), and for each candidate X it finds all candidates Y1,..,Yk which
    # represent more specific line patterns. After finding such clusters Yi
    # for each X, the supports of Yi are added to the support of each X.
    # For speeding up the process, previously created prefix tree is used.
    # In order to facilitate the detection of outliers, for each X with sufficient
    # support the clusters Yi are stored to %outlierpat hash table (this allows
    # for fast detection of non-outliers which match X).
    # This function is OPTIONAL
    def aggregate_supports(self):
        pass

    # This function makes a pass over the data set, find outliers and stores them
    # to file $outlierfile (can be set with the --outliers command line option).
    def find_outliers(self):
        line_index = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                words = line.split()
                candidate = []

                for word in words:
                    if word in self.frequent_words:
                        candidate.append(word)

                if candidate:
                    candidate_join = ' '.join(candidate)
                    if candidate_join in self.candidates:
                        continue

                # set outlier cluster
                self.outliers.append(line_index)
                line_index += 1

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=1.
    # This function is OPTIONAL
    def find_weights(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=2.
    # This function is OPTIONAL
    def find_weights2(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=3.
    # This function is OPTIONAL
    def find_weights3(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=4.
    # This function is OPTIONAL
    def find_weights4(self):
        pass

    # This function prints word weights for cluster candidate parameter1.
    # This function is OPTIONAL
    def print_weights(self):
        pass

    # This function joins the cluster candidate parameter1 to a suitable cluster
    # by words with insufficient weights. If there is no suitable cluster,
    # a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidate(self):
        pass

    # This function joins the cluster candidate parameter1 to a suitable cluster
    # by words with insufficient weights. If there is no suitable cluster,
    # a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidate2(self):
        pass

    # This function joins frequent cluster candidates into final clusters
    # by words with insufficient weights. For each candidate, word weights
    # are first calculated and the candidate is then compared to already
    # existing clusters, in order to find a suitable cluster for joining.
    # If no such cluster exists, a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidates(self):
        pass

    # This function finds frequent words in detected clusters
    def cluster_freq_words(self):
        pass

    # This function prints the cluster parameter1 to standard output.
    def print_cluster(self):
        pass

    # This function prints all clusters to standard output.
    def print_clusters(self):
        pass

    def get_clusters(self):
        # make a pass over the data set and find frequent words (words which appear
        # in $support or more lines), and store them to %fwords hash table
        self.find_frequent_words()

        # make a pass over the data set and find cluster candidates;
        # if --wweight option has been given, dependencies between frequent
        # words are also identified during the data pass
        self.find_candidates()

        # find frequent candidates
        self.find_frequent_candidates()

        # get cluster dictionary
        cluster_id = 0
        for candidate, values in self.candidates.iteritems():
            self.clusters[cluster_id] = values['Members']
            cluster_id += 1

        # add outlier cluster to the whole cluster
        self.clusters[cluster_id] = self.outliers

        # report clusters
        self.print_candidate()

filename = '/home/hudan/Git/datasets/SecRepo/perday/dec-2.log'
lc = LogCluster(None, 0.1, filename)
lc.get_clusters()
<commit_msg>LogCluster returns clusters<commit_after>import datetime
import hashlib
import textwrap
from collections import defaultdict

class LogCluster(object):
    def __init__(self, support, rsupport, log_file, outlier_file='', output_file=''):
        self.support = support
        self.rsupport = rsupport
        self.log_file = log_file
        self.outlier_file = outlier_file
        self.output_file = output_file
        self.wsize = 0
        self.csize = 0
        self.wsketch = {}
        self.wfilter = 0
        self.frequent_words = defaultdict(lambda: 0)
        self.candidates = {}
        self.clusters = {}
        self.outliers = []

    # This function logs the message given with parameter2,++,parameterN to
    # syslog, using the level parameter1+ The message is also written to stderr+
    def log_msg(*parameters):
        level = parameters[0]
        msg = ' '.join(parameters[1:])
        now = datetime.datetime.now()

        print now, level, msg

    # This function hashes the string given with parameter1 to an integer
    # in the range (0+++wsize-1) and returns the integer+ The wsize integer
    # can be set with the --wsize command line option+
    # L is unsigned 32 bit integer
    def hash_string(self, parameter):
        wraps = textwrap.wrap(parameter, self.wsize)
        hashes = []
        for wrap in wraps:
            hash_value = hashlib.md5(wrap).hexdigest()
            hashes.append(int(hash_value, 32))

        return hashes

    # This function hashes the candidate ID given with parameter1 to an integer
    # in the range (0+++csize-1) and returns the integer+ The csize integer
    # can be set with the --csize command line option+
    def hash_candidate(self, parameter):
        wraps = textwrap.wrap(parameter, self.csize)
        hashes = []
        for wrap in wraps:
            hash_value = hashlib.md5(wrap).hexdigest()
            hashes.append(int(hash_value, 32))

        return hashes

    # This function matches the line given with parameter1 with a regular
    # expression lineregexp (the expression can be set with the --lfilter
    # command line option)+ If the template string is defined (can be set
    # with the --template command line option), the line is converted
    # according to template (match variables in template are substituted
    # with values from regular expression match, and the resulting string
    # replaces the line)+ If the regular expression lineregexp does not match
    # the line, 0 is returned, otherwise the line (or converted line, if
    # --template option has been given) is returned+
    # If the --lfilter option has not been given but --lcfunc option is
    # present, the Perl function given with --lcfunc is used for matching
    # and converting the line+ If the function returns 'undef', line is
    # regarded non-matching, otherwise the value returned by the function
    # replaces the original line+
    # If neither --lfilter nor --lcfunc option has been given, the line
    # is returned without a trailing newline+
    # This function is OPTIONAL
    @staticmethod
    def process_line(line):
        return line

    # This function makes a pass over the data set and builds the sketch
    # @wsketch which is used for finding frequent words+ The sketch contains
    # wsize counters (wsize can be set with --wsize command line option)+
    # This function is OPTIONAL
    def build_word_sketch(self):
        words_dict = {}
        # for index in range(self.wsize):
        #     self.wsketch[index] = 0

        i = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                processed_line = self.process_line(line)
                i += 1
                words_list = processed_line.split()
                for word in words_list:
                    words_dict[word] = 1
                    index = self.hash_string(word)
                    self.wsketch[index] += 1

                    if self.wfilter:
                        # replace string
                        index = self.hash_string(word)
                        self.wsketch[index] += 1

    # This function makes a pass over the data set, finds frequent words and
    # stores them to %fwords hash table.
    # This function is MANDATORY
    def find_frequent_words(self):
        i = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue
                i += 1

                words_split = line.split()
                for word in words_split:
                    if word.strip() not in self.frequent_words.keys():
                        self.frequent_words[word] = 1
                    else:
                        self.frequent_words[word] += 1

        if not self.support:
            self.support = int(self.rsupport * i / 100)

        for word, frequency in self.frequent_words.iteritems():
            if frequency < self.support:
                self.frequent_words[word] = 0

        frequent_words_nonzero = {}
        for word, frequency in self.frequent_words.iteritems():
            if frequency != 0:
                frequent_words_nonzero[word] = frequency

        self.frequent_words = frequent_words_nonzero

    # This function makes a pass over the data set and builds the sketch
    # @csketch which is used for finding frequent candidates. The sketch contains
    # $csize counters ($csize can be set with --csize command line option).
    # This function is OPTIONAL
    def build_candidate_sketch(self):
        pass

    # This function logs the description for candidate parameter1.
    def print_candidate(self):
        for candidate in self.candidates:
            msg = ''
            for index in range(self.candidates[candidate]['WordCount']):
                if self.candidates[candidate]['Vars'][index][1] > 0:
                    msg += '*{' + str(self.candidates[candidate]['Vars'][index][0]) + ',' + \
                           str(self.candidates[candidate]['Vars'][index][1]) + '} '
                msg += self.candidates[candidate]['Words'][index] + ' '

            print msg
            print 'Support:', len(self.candidates[candidate]['Members'])

    # This function makes a pass over the data set, identifies cluster candidates
    # and stores them to %candidates hash table. If the --wweight command line
    # option has been provided, dependencies between frequent words are also
    # identified during the data pass and stored to %fword_deps hash table.
    def find_candidates(self):
        line_index = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                words = line.split()
                candidate = []
                varx = []
                varnum = 0

                for word in words:
                    if word in self.frequent_words:
                        candidate.append(word)
                        varx.append(varnum)
                        varnum = 0
                    else:
                        varnum += 1
                varx.append(varnum)

                # if the given candidate already exists, increase its support and
                # adjust its wildcard information, otherwise create a new candidate
                if candidate:
                    candidate_join = ' '.join(candidate)
                    if candidate_join not in self.candidates:
                        self.candidates[candidate_join] = {}
                        self.candidates[candidate_join]['Words'] = candidate
                        self.candidates[candidate_join]['WordCount'] = len(candidate)
                        self.candidates[candidate_join]['Vars'] = []
                        for varnum in varx:
                            self.candidates[candidate_join]['Vars'].append([varnum, varnum])
                        self.candidates[candidate_join]['Count'] = 1
                        self.candidates[candidate_join]['Members'] = []

                    else:
                        total = len(varx)
                        for index in range(total):
                            if self.candidates[candidate_join]['Vars'][index][0] > varx[index]:
                                self.candidates[candidate_join]['Vars'][index][0] = varx[index]
                            elif self.candidates[candidate_join]['Vars'][index][1] < varx[index]:
                                self.candidates[candidate_join]['Vars'][index][1] = varx[index]
                        self.candidates[candidate_join]['Count'] += 1
                    self.candidates[candidate_join]['Members'].append(line_index)
                line_index += 1

    # This function finds frequent candidates by removing candidates with
    # insufficient support from the %candidates hash table.
    def find_frequent_candidates(self):
        for candidate in self.candidates.keys():
            if self.candidates[candidate]['Count'] < self.support:
                self.candidates[candidate]['Count'] = 0

        candidates_nonzero = {}
        for key, val in self.candidates.iteritems():
            if self.candidates[key]['Count'] != 0:
                candidates_nonzero[key] = val

        self.candidates = candidates_nonzero

    # This function inserts a candidate into the prefix tree
    # This function is OPTIONAL
    def insert_into_prefix_tree(self):
        pass

    # This function arranges all candidates into the prefix tree data structure,
    # in order to facilitate fast matching between candidates
    # This function is OPTIONAL
    def build_prefix_tree(self):
        pass

    # This function finds more specific candidates for the given candidate with
    # the help of the prefix tree, and records more specific candidates into
    # the SubClusters hash table of the given candidate
    # This function is OPTIONAL
    def find_more_specific(self):
        pass

    # This function scans all cluster candidates (stored in %candidates hash
    # table), and for each candidate X it finds all candidates Y1,..,Yk which
    # represent more specific line patterns. After finding such clusters Yi
    # for each X, the supports of Yi are added to the support of each X.
    # For speeding up the process, previously created prefix tree is used.
    # In order to facilitate the detection of outliers, for each X with sufficient
    # support the clusters Yi are stored to %outlierpat hash table (this allows
    # for fast detection of non-outliers which match X).
    # This function is OPTIONAL
    def aggregate_supports(self):
        pass

    # This function makes a pass over the data set, find outliers and stores them
    # to file $outlierfile (can be set with the --outliers command line option).
    def find_outliers(self):
        line_index = 0
        with open(self.log_file, 'r') as f:
            for line in f:
                if not line:
                    continue

                words = line.split()
                candidate = []

                for word in words:
                    if word in self.frequent_words:
                        candidate.append(word)

                if candidate:
                    candidate_join = ' '.join(candidate)
                    if candidate_join in self.candidates:
                        continue

                # set outlier cluster
                self.outliers.append(line_index)
                line_index += 1

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=1.
    # This function is OPTIONAL
    def find_weights(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=2.
    # This function is OPTIONAL
    def find_weights2(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=3.
    # This function is OPTIONAL
    def find_weights3(self):
        pass

    # This function inspects the cluster candidate parameter1 and finds the weight
    # of each word in the candidate description. The weights are calculated from
    # word dependency information according to --weightf=4.
    # This function is OPTIONAL
    def find_weights4(self):
        pass

    # This function prints word weights for cluster candidate parameter1.
    # This function is OPTIONAL
    def print_weights(self):
        pass

    # This function joins the cluster candidate parameter1 to a suitable cluster
    # by words with insufficient weights. If there is no suitable cluster,
    # a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidate(self):
        pass

    # This function joins the cluster candidate parameter1 to a suitable cluster
    # by words with insufficient weights. If there is no suitable cluster,
    # a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidate2(self):
        pass

    # This function joins frequent cluster candidates into final clusters
    # by words with insufficient weights. For each candidate, word weights
    # are first calculated and the candidate is then compared to already
    # existing clusters, in order to find a suitable cluster for joining.
    # If no such cluster exists, a new cluster is created from the candidate.
    # This function is OPTIONAL
    def join_candidates(self):
        pass

    # This function finds frequent words in detected clusters
    def cluster_freq_words(self):
        pass

    # This function prints the cluster parameter1 to standard output.
    def print_cluster(self):
        pass

    # This function prints all clusters to standard output.
    def print_clusters(self):
        pass

    def get_clusters(self):
        # make a pass over the data set and find frequent words (words which appear
        # in $support or more lines), and store them to %fwords hash table
        self.find_frequent_words()

        # make a pass over the data set and find cluster candidates;
        # if --wweight option has been given, dependencies between frequent
        # words are also identified during the data pass
        self.find_candidates()

        # find frequent candidates
        self.find_frequent_candidates()

        # get cluster dictionary
        cluster_id = 0
        for candidate, values in self.candidates.iteritems():
            self.clusters[cluster_id] = values['Members']
            cluster_id += 1

        # add outlier cluster to the whole cluster
        self.clusters[cluster_id] = self.outliers

        return self.clusters

        # report clusters
        # self.print_candidate()

# filename = '/home/hudan/Git/datasets/SecRepo/perday/dec-2.log'
# lc = LogCluster(None, 0.1, filename)
# lc.get_clusters()
<|endoftext|>


<commit_before>from unittest import TestCase
from pyiterable.iterable import Iterable

class TestIterableTestClazz:
        def __init__(self, stub):
            self.__stub = stub

class TestIterable(TestCase):

    def setUp(self):
        self.__bool_list = [True, False, False, True, False, False]
        self.__int_list = [1, 2, 2, 5, 0, -8]
        self.__float_list = [1.3, 74.5, 9837.293, -283.4, 74.5]
        self.__string_list = ["akd", "fadskl", "dfa", "akd"]
        self.__clazz_list = [TestIterableTestClazz(i) for i in range(7)]
        self.__clazz_list.append(self.__clazz_list[0])  # Creates duplicates in list

        self.__test_lists = [
            self.__bool_list,
            self.__int_list,
            self.__float_list,
            self.__string_list,
            self.__clazz_list
        ]

        self.__test_sets = [set(l) for l in self.__test_lists]

        self.__test_input = self.__test_lists + self.__test_sets

    def test_to_list(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                actual = Iterable(test_input).to_list()

                self.assertCountEqual(
                    list(test_input),
                    actual
                )

                self.assertIs(
                    type([]),
                    type(actual)
                )

    def test_to_set(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                actual = Iterable(test_input).to_set()

                self.assertCountEqual(
                    set(test_input),
                    actual
                )

                self.assertIs(
                    type(set()),
                    type(actual)
                )

    def test_all_hasFalse_returnsFalse(self):
        test_list = [True, False, True, True]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    all(test_input),
                    Iterable(test_input).all()
                )

    def test_all_onlyHasTrue_returnsTrue(self):
        test_list = [True for i in range(7)]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    all(test_input),
                    Iterable(test_input).all()
                )

    def test_any_hasTrue_returnsTrue(self):
        test_list = [True, False, True, True]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    any(test_input),
                    Iterable(test_input).any()
                )

    def test_any_onlyHasFalse_returnsFalse(self):
        test_list = [False for i in range(7)]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    any(test_input),
                    Iterable(test_input).any()
                )

    def test_enumerate_defaultStart(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                self.assertCountEqual(
                    list(enumerate(test_input)),
                    list(Iterable(test_input).enumerate())
                )

    def test_enumerate_customStart(self):
        start = 3
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                self.assertCountEqual(
                    list(enumerate(test_input, start)),
                    list(Iterable(test_input).enumerate(start))
                )

    def test_filter(self):
        self.fail()

    def test_len(self):
        self.fail()

    def test_map(self):
        self.fail()

    def test_max(self):
        self.fail()

    def test_min(self):
        self.fail()

    def test_reversed(self):
        self.fail()

    def test_sorted(self):
        self.fail()

    def test_sum(self):
        self.fail()

    def test_zip(self):
        self.fail()

    def test_reduce(self):
        self.fail()<commit_msg>Added unit tests for 'filter'.<commit_after>from unittest import TestCase
from pyiterable.iterable import Iterable

class TestIterableTestClazz:
        def __init__(self, stub):
            self.__stub = stub

class TestIterable(TestCase):

    def setUp(self):
        self.__bool_list = [True, False, False, True, False, False]
        self.__int_list = [1, 2, 2, 5, 0, -8]
        self.__float_list = [1.3, 74.5, 9837.293, -283.4, 74.5]
        self.__string_list = ["akd", "fadskl", "dfa", "akd"]
        self.__clazz_list = [TestIterableTestClazz(i) for i in range(7)]
        self.__clazz_list.append(self.__clazz_list[0])  # Creates duplicates in list

        self.__test_lists = [
            self.__bool_list,
            self.__int_list,
            self.__float_list,
            self.__string_list,
            self.__clazz_list
        ]

        self.__test_sets = [set(l) for l in self.__test_lists]

        self.__test_input = self.__test_lists + self.__test_sets

    def test_to_list(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                actual = Iterable(test_input).to_list()

                self.assertCountEqual(
                    list(test_input),
                    actual
                )

                self.assertIs(
                    type([]),
                    type(actual)
                )

    def test_to_set(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                actual = Iterable(test_input).to_set()

                self.assertCountEqual(
                    set(test_input),
                    actual
                )

                self.assertIs(
                    type(set()),
                    type(actual)
                )

    def test_all_hasFalse_returnsFalse(self):
        test_list = [True, False, True, True]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    all(test_input),
                    Iterable(test_input).all()
                )

    def test_all_onlyHasTrue_returnsTrue(self):
        test_list = [True for i in range(7)]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    all(test_input),
                    Iterable(test_input).all()
                )

    def test_any_hasTrue_returnsTrue(self):
        test_list = [True, False, True, True]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    any(test_input),
                    Iterable(test_input).any()
                )

    def test_any_onlyHasFalse_returnsFalse(self):
        test_list = [False for i in range(7)]

        test_input = [
            test_list,
            set(test_list)
        ]

        for test_input in test_input:
            with self.subTest(test_input=test_input):
                self.assertEqual(
                    any(test_input),
                    Iterable(test_input).any()
                )

    def test_enumerate_defaultStart(self):
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                self.assertCountEqual(
                    list(enumerate(test_input)),
                    list(Iterable(test_input).enumerate())
                )

    def test_enumerate_customStart(self):
        start = 3
        for test_input in self.__test_input:
            with self.subTest(test_input=test_input):
                self.assertCountEqual(
                    list(enumerate(test_input, start)),
                    list(Iterable(test_input).enumerate(start))
                )

    def test_filter(self):
        for test_input in self.__test_input:
            # Extract a random item to filter on
            filtered_item = set(test_input).pop()
            func = lambda x: x is filtered_item

            with self.subTest(test_input=test_input):
                self.assertCountEqual(
                    filter(func, test_input),
                    Iterable(test_input).filter(func)
                )

    def test_len(self):
        self.fail()

    def test_map(self):
        self.fail()

    def test_max(self):
        self.fail()

    def test_min(self):
        self.fail()

    def test_reversed(self):
        self.fail()

    def test_sorted(self):
        self.fail()

    def test_sum(self):
        self.fail()

    def test_zip(self):
        self.fail()

    def test_reduce(self):
        self.fail()<|endoftext|>


<commit_before>import os.path

from pykeg.core import features

from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib.auth.views import password_reset
from django.contrib.auth.views import password_reset_done
from django.contrib.auth.views import password_reset_confirm
from django.contrib.auth.views import password_reset_complete
from django.contrib import admin
admin.autodiscover()

try:
  from registration.views import register
  from pykeg.web.kegweb.forms import KegbotRegistrationForm
  USE_DJANGO_REGISTRATION = True
except ImportError:
  USE_DJANGO_REGISTRATION = False

def basedir():
  """ Get the pwd of this module, eg for use setting absolute paths """
  return os.path.abspath(os.path.dirname(__file__))

urlpatterns = patterns('',
    ### django admin site
    (r'^admin/', include(admin.site.urls)),

    ### static media
    url(r'^site_media/(.*)$',
      'django.views.static.serve',
      {'document_root': os.path.join(basedir(), 'media')},
      name='site-media'),

    url(r'^media/(.*)$',
     'django.views.static.serve',
     {'document_root': settings.MEDIA_ROOT, 'show_indexes': True},
     name='media'),

    (r'^favicon.ico$', 'django.views.generic.simple.redirect_to',
      {'url': '/site_media/images/favicon.ico'}),

    ### RESTful api
    (r'^api/', include('pykeg.web.api.urls')),

    ### account
    (r'^account/', include('pykeg.web.account.urls')),
    (r'^accounts/password/reset/$', password_reset, {'template_name':
     'registration/password_reset.html'}),
    (r'^accounts/password/reset/done/$', password_reset_done, {'template_name':
     'registration/password_reset_done.html'}),
    (r'^accounts/password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', password_reset_confirm, {'template_name':
     'registration/password_reset_confirm.html'}),
    (r'^accounts/password/reset/complete/$', password_reset_complete, {'template_name':
     'registration/password_reset_complete.html'}),

    ### socialregistration
    (r'^sr/', include('socialregistration.urls')),

    ### charts
    (r'^(?P<kbsite_name>)charts/', include('pykeg.web.charts.urls')),

    ### kegadmin
    (r'^(?P<kbsite_name>)kegadmin/', include('pykeg.web.kegadmin.urls')),

    ### main kegweb urls
    (r'(?P<kbsite_name>)', include('pykeg.web.kegweb.urls')),
)

if features.use_facebook():
  urlpatterns += patterns('',
      ### facebook kegweb stuff
      (r'^fb/', include('pykeg.web.contrib.facebook.urls')),
  )

if features.use_twitter():
  urlpatterns += patterns('',
      ### twitter kegweb stuff
      (r'^twitter/', include('pykeg.web.contrib.twitter.urls')),
  )

### accounts and registration
# uses the stock django-registration views, except we need to override the
# registration class for acocunt/register
if USE_DJANGO_REGISTRATION:
  from django.contrib.auth import views as auth_views
  urlpatterns += patterns('',
    url(r'^accounts/register/$', register,
      {'form_class':KegbotRegistrationForm},
      name='registration_register',
    ),
   (r'^accounts/', include('registration.urls')),
  )

<commit_msg>point site_media to static<commit_after>import os.path

from pykeg.core import features

from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib.auth.views import password_reset
from django.contrib.auth.views import password_reset_done
from django.contrib.auth.views import password_reset_confirm
from django.contrib.auth.views import password_reset_complete
from django.contrib import admin
admin.autodiscover()

try:
  from registration.views import register
  from pykeg.web.kegweb.forms import KegbotRegistrationForm
  USE_DJANGO_REGISTRATION = True
except ImportError:
  USE_DJANGO_REGISTRATION = False

def basedir():
  """ Get the pwd of this module, eg for use setting absolute paths """
  return os.path.abspath(os.path.dirname(__file__))

urlpatterns = patterns('',
    ### django admin site
    (r'^admin/', include(admin.site.urls)),

    ### static media
    url(r'^site_media/(.*)$',
      'django.views.static.serve',
      {'document_root': os.path.join(basedir(), 'static')},
      name='site-media'),

    url(r'^media/(.*)$',
     'django.views.static.serve',
     {'document_root': settings.MEDIA_ROOT, 'show_indexes': True},
     name='media'),

    (r'^favicon.ico$', 'django.views.generic.simple.redirect_to',
      {'url': '/site_media/images/favicon.ico'}),

    ### RESTful api
    (r'^api/', include('pykeg.web.api.urls')),

    ### account
    (r'^account/', include('pykeg.web.account.urls')),
    (r'^accounts/password/reset/$', password_reset, {'template_name':
     'registration/password_reset.html'}),
    (r'^accounts/password/reset/done/$', password_reset_done, {'template_name':
     'registration/password_reset_done.html'}),
    (r'^accounts/password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', password_reset_confirm, {'template_name':
     'registration/password_reset_confirm.html'}),
    (r'^accounts/password/reset/complete/$', password_reset_complete, {'template_name':
     'registration/password_reset_complete.html'}),

    ### socialregistration
    (r'^sr/', include('socialregistration.urls')),

    ### charts
    (r'^(?P<kbsite_name>)charts/', include('pykeg.web.charts.urls')),

    ### kegadmin
    (r'^(?P<kbsite_name>)kegadmin/', include('pykeg.web.kegadmin.urls')),

    ### main kegweb urls
    (r'(?P<kbsite_name>)', include('pykeg.web.kegweb.urls')),
)

if features.use_facebook():
  urlpatterns += patterns('',
      ### facebook kegweb stuff
      (r'^fb/', include('pykeg.web.contrib.facebook.urls')),
  )

if features.use_twitter():
  urlpatterns += patterns('',
      ### twitter kegweb stuff
      (r'^twitter/', include('pykeg.web.contrib.twitter.urls')),
  )

### accounts and registration
# uses the stock django-registration views, except we need to override the
# registration class for acocunt/register
if USE_DJANGO_REGISTRATION:
  from django.contrib.auth import views as auth_views
  urlpatterns += patterns('',
    url(r'^accounts/register/$', register,
      {'form_class':KegbotRegistrationForm},
      name='registration_register',
    ),
   (r'^accounts/', include('registration.urls')),
  )

<|endoftext|>


<commit_before>import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
import logging, uuid, os, subprocess
from datetime import datetime
from tornado.escape import json_encode, json_decode
from mopidy import config, ext
from mopidy.core import CoreListener

logger = logging.getLogger(__name__)
connections = {}

# send a message to all connections
# @param event = string (event name, ie connection_opened)
# @param data = array (any data required to include in our message)
def send_message( event, data ):
    for connection in connections.itervalues():
        message = '{"type": "'+event+'", "data": '+ json_encode( data ) +'}'
        connection['connection'].write_message( message )

# digest a protocol header into it's id/name parts
def digest_protocol( protocol ):

    # facilitate wrapping list objects, silly various browser formats and also Jupiter's orbit...
    protocol = ''.join(protocol)
    protocol = protocol.replace(',','').replace('[','').replace(']','')
    elements = protocol.split('_')

    # if we've been given a valid array
    try:
      username = elements[0]
      connectionid = elements[1]

    # invalid, so just create a default connection, and auto-generate an ID
    except:
      username = str(uuid.uuid4().hex)
      connectionid = username

    # construct our protocol object, and return
    return {"protocol": protocol, "username": username, "connectionid": connectionid}

## PUSHER WEBSOCKET SERVER
class PusherHandler(tornado.websocket.WebSocketHandler, CoreListener):

  def check_origin(self, origin):
    return True

  def initialize(self, version):
    self.version = version

  # when a new connection is opened
  def open(self):

    # decode our connection protocol value (which is a payload of id/name from javascript)
    protocolElements = digest_protocol(self.request.headers.get('Sec-Websocket-Protocol', '[]'))
    connectionid = protocolElements['connectionid']
    self.connectionid = connectionid
    username = protocolElements['username']
    created = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')

    # construct our client object, and add to our list of connections
    client = { "connectionid": connectionid, "username": username, "ip": self.request.remote_ip, "created": created, "version": self.version }
    connections[connectionid] = { 'client': client, 'connection': self }

    logger.debug( 'New Spotmop Pusher connection: '+ connectionid +'  '+ username )

    # notify all other clients that a new user has connected
    send_message( 'client_connected', client )

  def select_subprotocol(self, subprotocols):
    # return the id. This lets the client know we've accepted this connection
    return digest_protocol( subprotocols )['protocol']

  # server received a message
  def on_message(self, message):
    messageJson = json_decode(message)

    if messageJson['type'] == 'client_updated':
        if messageJson['origin']['connectionid'] in connections:
            connections[messageJson['origin']['connectionid']]['client']['username'] = messageJson['data']['newVal']
            logger.debug( 'Spotmop Pusher connection '+ self.connectionid +' updated' )

    # recipients array has items, so only send to specific clients
    if messageJson['recipients']:
      for connectionid in messageJson['recipients']:
        connectionid = connectionid.encode("utf-8")
        connections[connectionid]['connection'].write_message(message)

    # empty, so send to all clients
    else:
      for connection in connections.itervalues():
        if connection['client']['connectionid'] != self.connectionid:
          connection['connection'].write_message(message)

    logger.debug( 'Spotmop Pusher message received from '+ self.connectionid )

  # connection closed
  def on_close(self):
    if self.connectionid in connections:

        clientRemoved = connections[self.connectionid]['client']
        logger.debug( 'Spotmop Pusher connection to '+ self.connectionid +' closed' )

        # now actually remove it
        del connections[self.connectionid]

        send_message( 'client_disconnected', clientRemoved )

  def ThreadWorker(self, stopThreadEvent):
    logger.info('Starting')
    wsURL = 'ws://music.james:6680/mopidy/ws'
    try:
      ws = websocket.create_connection(wsURL)
      while not stopThreadEvent.isSet():
        response = ws.recv()
        logger.info(response)
    except:
      logger.info('Error')

## PUSHER HTTP ENDPOINT
class PusherRequestHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Methods", "GET,POST")
        self.set_header("Access-Control-Allow-Headers", "X-Requested-With")
        self.set_header("Content-Type", "application/json")

    def initialize(self, core, config):
		self.core = core
		self.config = config

    # get method
    def get(self, action):

        if action == 'connections':
            connectionsDetailsList = []
            for connection in connections.itervalues():
                connectionsDetailsList.append(connection['client'])
            self.write(json_encode(connectionsDetailsList))

def spotmop_pusher_factory(config, core):
    return [
        ('/', PusherRequestHandler, {
                'core': core,
                'config': config
            })
    ]

  <commit_msg>Send events, even to self (for callbacks)<commit_after>import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
import logging, uuid, os, subprocess
from datetime import datetime
from tornado.escape import json_encode, json_decode
from mopidy import config, ext
from mopidy.core import CoreListener

logger = logging.getLogger(__name__)
connections = {}

# send a message to all connections
# @param event = string (event name, ie connection_opened)
# @param data = array (any data required to include in our message)
def send_message( event, data ):
    for connection in connections.itervalues():
        message = '{"type": "'+event+'", "data": '+ json_encode( data ) +'}'
        connection['connection'].write_message( message )

# digest a protocol header into it's id/name parts
def digest_protocol( protocol ):

    # facilitate wrapping list objects, silly various browser formats and also Jupiter's orbit...
    protocol = ''.join(protocol)
    protocol = protocol.replace(',','').replace('[','').replace(']','')
    elements = protocol.split('_')

    # if we've been given a valid array
    try:
      username = elements[0]
      connectionid = elements[1]

    # invalid, so just create a default connection, and auto-generate an ID
    except:
      username = str(uuid.uuid4().hex)
      connectionid = username

    # construct our protocol object, and return
    return {"protocol": protocol, "username": username, "connectionid": connectionid}

## PUSHER WEBSOCKET SERVER
class PusherHandler(tornado.websocket.WebSocketHandler, CoreListener):

  def check_origin(self, origin):
    return True

  def initialize(self, version):
    self.version = version

  # when a new connection is opened
  def open(self):

    # decode our connection protocol value (which is a payload of id/name from javascript)
    protocolElements = digest_protocol(self.request.headers.get('Sec-Websocket-Protocol', '[]'))
    connectionid = protocolElements['connectionid']
    self.connectionid = connectionid
    username = protocolElements['username']
    created = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')

    # construct our client object, and add to our list of connections
    client = { "connectionid": connectionid, "username": username, "ip": self.request.remote_ip, "created": created, "version": self.version }
    connections[connectionid] = { 'client': client, 'connection': self }

    logger.debug( 'New Spotmop Pusher connection: '+ connectionid +'  '+ username )

    # notify all other clients that a new user has connected
    send_message( 'client_connected', client )

  def select_subprotocol(self, subprotocols):
    # return the id. This lets the client know we've accepted this connection
    return digest_protocol( subprotocols )['protocol']

  # server received a message
  def on_message(self, message):
    messageJson = json_decode(message)

    if messageJson['type'] == 'client_updated':
        if messageJson['origin']['connectionid'] in connections:
            connections[messageJson['origin']['connectionid']]['client']['username'] = messageJson['data']['newVal']
            logger.debug( 'Spotmop Pusher connection '+ self.connectionid +' updated' )

    # recipients array has items, so only send to specific clients
    if messageJson['recipients']:
      for connectionid in messageJson['recipients']:
        connectionid = connectionid.encode("utf-8")
        connections[connectionid]['connection'].write_message(message)

    # empty, so send to all clients
    else:
      for connection in connections.itervalues():
        connection['connection'].write_message(message)

    logger.debug( 'Spotmop Pusher message received from '+ self.connectionid )

  # connection closed
  def on_close(self):
    if self.connectionid in connections:

        clientRemoved = connections[self.connectionid]['client']
        logger.debug( 'Spotmop Pusher connection to '+ self.connectionid +' closed' )

        # now actually remove it
        del connections[self.connectionid]

        send_message( 'client_disconnected', clientRemoved )

  def ThreadWorker(self, stopThreadEvent):
    logger.info('Starting')
    wsURL = 'ws://music.james:6680/mopidy/ws'
    try:
      ws = websocket.create_connection(wsURL)
      while not stopThreadEvent.isSet():
        response = ws.recv()
        logger.info(response)
    except:
      logger.info('Error')

## PUSHER HTTP ENDPOINT
class PusherRequestHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Methods", "GET,POST")
        self.set_header("Access-Control-Allow-Headers", "X-Requested-With")
        self.set_header("Content-Type", "application/json")

    def initialize(self, core, config):
		self.core = core
		self.config = config

    # get method
    def get(self, action):

        if action == 'connections':
            connectionsDetailsList = []
            for connection in connections.itervalues():
                connectionsDetailsList.append(connection['client'])
            self.write(json_encode(connectionsDetailsList))

def spotmop_pusher_factory(config, core):
    return [
        ('/', PusherRequestHandler, {
                'core': core,
                'config': config
            })
    ]

  <|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from openerp import http
from openerp.http import request
from openerp import SUPERUSER_ID

class MyLearning(http.Controller):
    @http.route('/mylearning/mylearning/', type='http', auth='public', website=True)
    def index(self, **kw):
        # Partners = http.request.env['res.partner']
        partner = request.env['res.users'].browse(request.uid).partner_id
        if partner:
            ResPartner = request.env['res.partner'].browse(partner.id)
            currentex = ResPartner.next_exercise_id
            return request.render('mqo_exercises.index', {
                'exercises': currentex
            })
        else:
            return request.render('mqo_exercises.index', {'exercises': []})

class ExerciseResponse(http.Controller):

    def _check_bad_cases(self, cr, uid, request, assignment, assignment_obj, token, context=None):
        # In case of bad survey, redirect to surveys list
        if assignment_obj.exists(cr, SUPERUSER_ID, assignment.id, context=context) == []:
            print('assignment not found')
            return request.website.render("website.403")

        # redirect if token is invalid.
        if assignment.response_token != token:
            print('response token was invalid')
            return request.website.render("website.403")

        # In case of auth required, block public user
        if uid == request.website.user_id.id:
            print('authorisation required')
            return request.website.render("survey.auth_required", {'survey': assignment.response_survey})

        # no surveys assigned
        if not assignment.response_survey:
            print('assignment has no response surveys')
            return request.website.render("website.403")

        # Everything seems to be ok
        return None

# Survey wrapper for starting a response
    @http.route(['/exercise/response/<model("mqo.assignment"):assignment>/<string:token>'],
                type='http', auth='public', website=True)
    def survey_wrapper(self, assignment, token, prev=None, **post):
        '''wraps a response'''
        cr, uid, context = request.cr, request.uid, request.context
        assignment_obj = request.registry['mqo.assignment']
        survey_obj = request.registry['survey.survey']
        user_input_obj = request.registry['survey.user_input']

        # Controls if the survey can be displayed
        errpage = self._check_bad_cases(cr, uid, request, assignment, assignment_obj, token, context=context)
        if errpage:
            return errpage

        # see if response for this assignment already exists, and create one for this assignment if needed.
        try:
            user_input_id = user_input_obj.search(cr, SUPERUSER_ID, [('token', '=', assignment.response_token)], context=context)[0]
        except IndexError:  # No response currently exists for this assignment
            vals = {'survey_id': assignment.response_survey, 'partner_id': assignment.partner_id, 'token': assignment.response_token, 'state': 'Not started yet'}
            user_input_id = user_input_obj.create(cr, uid, vals, context=context)
            user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
        else:
            user_input = user_input_obj.browse(cr, SUPERUSER_ID, [user_input_id], context=context)[0]

        return request.redirect('/survey/fill/%s/%s' % (user_input.survey_id, user_input.token))
<commit_msg>Bugfixing.<commit_after># -*- coding: utf-8 -*-
from openerp import http
from openerp.http import request
from openerp import SUPERUSER_ID

class MyLearning(http.Controller):
    @http.route('/mylearning/mylearning/', type='http', auth='public', website=True)
    def index(self, **kw):
        # Partners = http.request.env['res.partner']
        partner = request.env['res.users'].browse(request.uid).partner_id
        if partner:
            ResPartner = request.env['res.partner'].browse(partner.id)
            currentex = ResPartner.next_exercise_id
            return request.render('mqo_exercises.index', {
                'exercises': currentex
            })
        else:
            return request.render('mqo_exercises.index', {'exercises': []})

class ExerciseResponse(http.Controller):

    def _check_bad_cases(self, cr, uid, request, assignment, assignment_obj, token, context=None):
        # In case of bad survey, redirect to surveys list
        if assignment_obj.exists(cr, SUPERUSER_ID, assignment.id, context=context) == []:
            print('assignment not found')
            return request.website.render("website.403")

        # redirect if token is invalid.
        if assignment.response_token != token:
            print('response token was invalid')
            return request.website.render("website.403")

        # In case of auth required, block public user
        if uid == request.website.user_id.id:
            print('authorisation required')
            return request.website.render("survey.auth_required", {'survey': assignment.response_survey})

        # no surveys assigned
        if not assignment.response_survey:
            print('assignment has no response surveys')
            return request.website.render("website.403")

        # Everything seems to be ok
        return None

# Survey wrapper for starting a response
    @http.route(['/exercise/response/<model("mqo.assignment"):assignment>/<string:token>'],
                type='http', auth='public', website=True)
    def survey_wrapper(self, assignment, token, prev=None, **post):
        '''wraps a response'''
        cr, uid, context = request.cr, request.uid, request.context
        assignment_obj = request.registry['mqo.assignment']
        survey_obj = request.registry['survey.survey']
        user_input_obj = request.registry['survey.user_input']

        # Controls if the survey can be displayed
        errpage = self._check_bad_cases(cr, uid, request, assignment, assignment_obj, token, context=context)
        if errpage:
            return errpage

        # see if response for this assignment already exists, and create one for this assignment if needed.
        try:
            user_input_id = user_input_obj.search(cr, SUPERUSER_ID, [('token', '=', assignment.response_token)], context=context)[0]
        except IndexError:  # No response currently exists for this assignment
            vals = {'survey_id': assignment.response_survey, 'partner_id': assignment.partner_id, 'token': assignment.response_token, 'state': 'skip'}
            user_input_id = user_input_obj.create(cr, uid, vals, context=context)
            user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
        else:
            user_input = user_input_obj.browse(cr, SUPERUSER_ID, [user_input_id], context=context)[0]

        return request.redirect('/survey/fill/%s/%s' % (user_input.survey_id, user_input.token))
<|endoftext|>


<commit_before>#!/usr/bin/env python
import ftplib
import pifacedigitalio
import time
from time import sleep

DELAY = 1.0 # seconds
count = 0
pump_start_time = 0
pump_stop_time = 0
pump_interval = 0
vacuum_status = 'NA'
pfd = pifacedigitalio.PiFaceDigital()
pifacedigitalio.init()
def tank_full(event):
	if pfd.leds[0].value == 0:
		global pump_start_time
		if pump_start_time == 0:
			pump_start_time = time.time()
		if pfd.input_pins[1].value == 1:
			pfd.leds[0].turn_on() #Turn on Releaser pump
			f = open('/home/pi/releaser/pump_times.csv',"a")
			f.write('Auto pump start,'+time.strftime("%Y-%m-%d,%X,")+',,,,\n')
			f.close()
		else:
			f = open('/home/pi/releaser/error.log',"a")
			f.write(time.strftime("%y-%m-%d,%X,")+'Auto start when tank was not empty \n')
			f.close()
def manual_pump(event):
	global pump_start_time
	global vacuum_status
	vacuum_status = 'ON'
	pfd.leds[1].turn_on()  #Turn on Vacuum pump
	if pump_start_time == 0:
		pump_start_time = time.time()
	if pfd.input_pins[1].value == 1:
		pfd.leds[0].turn_on() #Turn on Releaser Pump
		f = open('/home/pi/releaser/pump_times.csv',"a")
		f.write('Manual pump start,'+time.strftime("%Y-%m-%d,%X,")+',,,,\n')
		f.close()
	else:
		f = open('/home/pi/releaser/error.log',"a")
		f.write(time.strftime("%Y-%m-%d,%X,")+'Manual start when tank was not empty \n')
		f.close
def tank_empty(event):
	if pfd.leds[0].value == 1:
		pfd.leds[0].turn_off()
		global pump_start_time
		global pump_stop_time
		global pump_interval
		global vacuum_status
		pump_run_time = 0
		pump_interval = time.time() - pump_stop_time
		fill_time = pump_start_time - pump_stop_time
		flow_rate = (12.18/fill_time)*3600
		pump_stop_time = time.time()
		if pump_interval > 1800:
			pfd.leds[1].turn_off() # Turn off Vacuum pump if pump interval is greater than 30 min
			vacuum_status = 'Off'
		if pump_start_time != 0:
			pump_run_time = pump_stop_time - pump_start_time
		else:
			pump_run_time = 0
		f = open('/home/pi/releaser/pump_times.csv',"a")
		f.write('pump stop,'+time.strftime("%Y-%m-%d,%X,"+str(round(pump_run_time,2))+',pump_interval,'+str(round(pump_interval/60,2))+',gallons per hour,'+str(round(flow_rate,2))+'\n'))
		f.close()
		pump_start_time = 0
		try:
			session = ftplib.FTP('mattsrepair.com','sapreleaser','BMWr1100rs!')
			f = open('/home/pi/releaser/index.html',"w")
			f.write('<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> \n  \n\n <TITLE> \n Sap Releaser \n </TITLE> \n  \n \n
Pumped for '+str(pump_run_time)+' Sec At '+time.strftime("%Y-%m-%d, %X,")+' Time between pumps: '+str(pump_interval/60)+' Vacuum pump is '+vacuum_status+'
 \n  \n ')
			f.close()
			file = open('/home/pi/releaser/index.html','rb')                  # file to send
			session.storbinary('STOR index.html', file)     # send the file
			file.close()                                    # close file
			file = open('/home/pi/releaser/pump_times.csv','rb')
			session.storbinary('STOR pump_times.csv', file)
			file.close()
			session.quit()
		except:
			print "failed to upload"
			f = open('/home/pi/releaser/error.log',"a")
			f.write('failed to upload \n')
			f.close()

if __name__ == "__main__":
	pfd.leds[2].turn_on()	#Turn on LED to show program is running
	listener = pifacedigitalio.InputEventListener(chip=pfd)
	listener.register(0, pifacedigitalio.IODIR_ON, tank_full)
	listener.register(1, pifacedigitalio.IODIR_OFF, tank_empty)
	listener.register(2, pifacedigitalio.IODIR_ON, manual_pump)
	listener.activate()

<commit_msg>Updated indentation formating and made program read from a password file so that information won't be public on github<commit_after>#!/usr/bin/env python
import ftplib
import pifacedigitalio
import time
from time import sleep

DELAY = 1.0 # seconds
count = 0
pump_start_time = 0
pump_stop_time = 0
pump_interval = 0
vacuum_status = 'NA'
pfd = pifacedigitalio.PiFaceDigital()
pifacedigitalio.init()
def tank_full(event):
    if pfd.leds[0].value == 0:
        global pump_start_time
        if pump_start_time == 0:
            pump_start_time = time.time()
        if pfd.input_pins[1].value == 1:
            pfd.leds[0].turn_on() #Turn on Releaser pump
            f = open('/home/pi/releaser/pump_times.csv',"a")
            f.write('Auto pump start,'+time.strftime("%Y-%m-%d,%X,")+',,,,\n')
            f.close()
        else:
            f = open('/home/pi/releaser/error.log',"a")
            f.write(time.strftime("%y-%m-%d,%X,")+'Auto start when tank was not empty \n')
            f.close()
def manual_pump(event):
    global pump_start_time
    global vacuum_status
    vacuum_status = 'ON'
    pfd.leds[1].turn_on()  #Turn on Vacuum pump
    if pump_start_time == 0:
        pump_start_time = time.time()
    if pfd.input_pins[1].value == 1:
        pfd.leds[0].turn_on() #Turn on Releaser Pump
        f = open('/home/pi/releaser/pump_times.csv',"a")
        f.write('Manual pump start,'+time.strftime("%Y-%m-%d,%X,")+',,,,\n')
        f.close()
    else:
        f = open('/home/pi/releaser/error.log',"a")
        f.write(time.strftime("%Y-%m-%d,%X,")+'Manual start when tank was not empty \n')
        f.close
def tank_empty(event):
    if pfd.leds[0].value == 1:
        pfd.leds[0].turn_off()
        global pump_start_time
        global pump_stop_time
        global pump_interval
        global vacuum_status
        pump_run_time = 0
        pump_interval = time.time() - pump_stop_time
        fill_time = pump_start_time - pump_stop_time
        flow_rate = (12.18/fill_time)*3600
        pump_stop_time = time.time()
        if pump_interval > 1800:
            pfd.leds[1].turn_off() # Turn off Vacuum pump if pump interval is greater than 30 min
            vacuum_status = 'Off'
        if pump_start_time != 0:
            pump_run_time = pump_stop_time - pump_start_time
        else:
            pump_run_time = 0
        f = open('/home/pi/releaser/pump_times.csv',"a")
        f.write('pump stop,'+time.strftime("%Y-%m-%d,%X,"+str(round(pump_run_time,2))+',pump_interval,'+str(round(pump_interval/60,2))+',gallons per hour,'+str(round(flow_rate,2))+'\n'))
        f.close()
        pump_start_time = 0
        try:
            with open('login') as up:
                login = up.readlines()
            up.close()
            session = ftplib.FTP('mattsrepair.com',login[0],login[1])
            f = open('/home/pi/releaser/index.html',"w")
            f.write('<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> \n  \n\n <TITLE> \n Sap Releaser \n </TITLE> \n  \n \n
Pumped for '+str(pump_run_time)+' Sec At '+time.strftime("%Y-%m-%d, %X,")+' Time between pumps: '+str(pump_interval/60)+' Vacuum pump is '+vacuum_status+'
 \n  \n ')
            f.close()
            file = open('/home/pi/releaser/index.html','rb')                  # file to send
            session.storbinary('STOR index.html', file)     # send the file
            file.close()                                    # close file
            file = open('/home/pi/releaser/pump_times.csv','rb')
            session.storbinary('STOR pump_times.csv', file)
            file.close()
            session.quit()
        except:
            print ("failed to upload")
            f = open('/home/pi/releaser/error.log',"a")
            f.write('failed to upload \n')
            f.close()

if __name__ == "__main__":
    pfd.leds[2].turn_on()   #Turn on LED to show program is running
    listener = pifacedigitalio.InputEventListener(chip=pfd)
    listener.register(0, pifacedigitalio.IODIR_ON, tank_full)
    listener.register(1, pifacedigitalio.IODIR_OFF, tank_empty)
    listener.register(2, pifacedigitalio.IODIR_ON, manual_pump)
    listener.activate()

<|endoftext|>