<commit_before># -*- coding: utf-8 -*-
__doc__ = """
Server handlers targetting threaded servers rather than event based servers.
"""
import socket
import threading
import traceback
from sys import exc_info
import types

from ws4py.streaming import Stream

__all__ = ['WebSocketHandler', 'EchoWebSocketHandler']

class WebSocketHandler(object):
    def __init__(self, sock, protocols, extensions):
        """
        A handler appropriate for servers. This handler
        runs the connection's read and parsing in a thread,
        meaning that incoming messages will be alerted in a different
        thread from the one that created the handler.

        @param sock: opened connection after the websocket upgrade
        @param protocols: list of protocols from the handshake
        @param extensions: list of extensions from the handshake
        """
        self.stream = Stream()

        self.protocols = protocols
        self.extensions = extensions

        self.sock = sock
        self.sock.settimeout(30.0)

        self.client_terminated = False
        self.server_terminated = False

        self._lock = threading.Lock()
        self._th = threading.Thread(target=self._receive)

    def opened(self):
        """
        Called by the server when the upgrade handshake
        has succeeeded. Starts the internal loop that
        reads bytes from the connection and hands it over
        to the stream for parsing.
        """
        self._th.start()

    def close(self, code=1000, reason=''):
        """
        Call this method to initiate the websocket connection
        closing by sending a close frame to the connected peer.

        Once this method is called, the server_terminated
        attribute is set. Calling this method several times is
        safe as the closing frame will be sent only the first
        time.

        @param code: status code describing why the connection is closed
        @param reason: a human readable message describing why the connection is closed
        """
        if not self.server_terminated:
            self.server_terminated = True
            self.write_to_connection(self.stream.close(code=code, reason=reason))

    def closed(self, code, reason=None):
        """
        Called by the server when the websocket connection
        is finally closed.

        @param code: status code
        @param reason: human readable message of the closing exchange
        """
        pass

    @property
    def terminated(self):
        """
        Returns True if both the client and server have been
        marked as terminated.
        """
        return self.client_terminated is True and self.server_terminated is True

    def write_to_connection(self, bytes):
        """
        Writes the provided bytes to the underlying connection.

        @param bytes: data tio send out
        """
        return self.sock.sendall(bytes)

    def read_from_connection(self, amount):
        """
        Reads bytes from the underlying connection.

        @param amount: quantity to read (if possible)
        """
        return self.sock.recv(amount)

    def close_connection(self):
        """
        Shutdowns then closes the underlying connection.
        """
        try:
            self.sock.shutdown(socket.SHUT_RDWR)
            self.sock.close()
        except:
            pass

    def ponged(self, pong):
        """
        Pong message received on the stream.

        @param pong: messaging.PongControlMessage instance
        """
        pass

    def received_message(self, m):
        """
        Message received on the stream.

        @param pong: messaging.TextMessage or messaging.BinaryMessage instance
        """
        pass

    def send(self, payload, binary=False):
        """
        Sends the given payload out.

        If payload is some bytes or a bytearray,
        then it is sent as a single message not fragmented.

        If payload is a generator, each chunk is sent as part of
        fragmented message.

        @param payload: string, bytes, bytearray or a generator
        @param binary: if set, handles the payload as a binary message
        """
        if isinstance(payload, basestring) or isinstance(payload, bytearray):
            if not binary:
                self.write_to_connection(self.stream.text_message(payload).single())
            else:
                self.write_to_connection(self.stream.binary_message(payload).single())

        elif type(payload) == types.GeneratorType:
            bytes = payload.next()
            first = True
            for chunk in payload:
                if not binary:
                    self.write_to_connection(self.stream.text_message(bytes).fragment(first=first))
                else:
                    self.write_to_connection(self.stream.binary_message(payload).fragment(first=first))
                bytes = chunk
                first = False
            if not binary:
                self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))
            else:
                self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))

    def _receive(self):
        """
        Performs the operation of reading from the underlying
        connection in order to feed the stream of bytes.

        We start with a small size of two bytes to be read
        from the connection so that we can quickly parse an
        incoming frame header. Then the stream indicates
        whatever size must be read from the connection since
        it knows the frame payload length.

        Note that we perform some automatic opererations:

        * On a closing message, we respond with a closing
          message and finally close the connection
        * We respond to pings with pong messages.
        * Whenever an error is raised by the stream parsing,
          we initiate the closing of the connection with the
          appropiate error code.
        """
        next_size = 2
        try:
            self.sock.setblocking(1)
            while not self.terminated:
                bytes = self.read_from_connection(next_size)
                if not bytes and next_size > 0:
                    break

                with self._lock:
                    s = self.stream
                    next_size = s.parser.send(bytes)

                    if s.closing is not None:
                        if not self.server_terminated:
                            next_size = 2
                            self.close(s.closing.code, s.closing.reason)
                        else:
                            self.client_terminated = True
                            break

                    elif s.errors:
                        errors = s.errors[:]
                        for error in s.errors:
                            self.close(error.code, error.reason)
                            s.errors.remove(error)
                        break

                    elif s.has_message:
                        self.received_message(s.message)
                        s.message.data = None
                        s.message = None

                    for ping in s.pings:
                        self.write_to_connection(s.pong(str(ping.data)))
                    s.pings = []

                    for pong in s.pongs:
                        self.ponged(pong)
                    s.pongs = []

        except:
            print "".join(traceback.format_exception(*exc_info()))
        finally:
            self.client_terminated = self.server_terminated = True
            self.close_connection()
        if self.stream.closing:
            self.closed(self.stream.closing.code, self.stream.closing.reason)
        else:
            self.closed(1006)

class EchoWebSocketHandler(WebSocketHandler):
    """
    Simple handler that keeps echoing whatever message
    it receives.
    """
    def received_message(self, m):
        self.send(m.data, m.is_binary)

<commit_msg>removed unused lock<commit_after># -*- coding: utf-8 -*-
__doc__ = """
Server handlers targetting threaded servers rather than event based servers.
"""
import socket
import threading
import traceback
from sys import exc_info
import types

from ws4py.streaming import Stream

__all__ = ['WebSocketHandler', 'EchoWebSocketHandler']

class WebSocketHandler(object):
    def __init__(self, sock, protocols, extensions):
        """
        A handler appropriate for servers. This handler
        runs the connection's read and parsing in a thread,
        meaning that incoming messages will be alerted in a different
        thread from the one that created the handler.

        @param sock: opened connection after the websocket upgrade
        @param protocols: list of protocols from the handshake
        @param extensions: list of extensions from the handshake
        """
        self.stream = Stream()

        self.protocols = protocols
        self.extensions = extensions

        self.sock = sock
        self.sock.settimeout(30.0)

        self.client_terminated = False
        self.server_terminated = False

        self._th = threading.Thread(target=self._receive)

    def opened(self):
        """
        Called by the server when the upgrade handshake
        has succeeeded. Starts the internal loop that
        reads bytes from the connection and hands it over
        to the stream for parsing.
        """
        self._th.start()

    def close(self, code=1000, reason=''):
        """
        Call this method to initiate the websocket connection
        closing by sending a close frame to the connected peer.

        Once this method is called, the server_terminated
        attribute is set. Calling this method several times is
        safe as the closing frame will be sent only the first
        time.

        @param code: status code describing why the connection is closed
        @param reason: a human readable message describing why the connection is closed
        """
        if not self.server_terminated:
            self.server_terminated = True
            self.write_to_connection(self.stream.close(code=code, reason=reason))

    def closed(self, code, reason=None):
        """
        Called by the server when the websocket connection
        is finally closed.

        @param code: status code
        @param reason: human readable message of the closing exchange
        """
        pass

    @property
    def terminated(self):
        """
        Returns True if both the client and server have been
        marked as terminated.
        """
        return self.client_terminated is True and self.server_terminated is True

    def write_to_connection(self, bytes):
        """
        Writes the provided bytes to the underlying connection.

        @param bytes: data tio send out
        """
        return self.sock.sendall(bytes)

    def read_from_connection(self, amount):
        """
        Reads bytes from the underlying connection.

        @param amount: quantity to read (if possible)
        """
        return self.sock.recv(amount)

    def close_connection(self):
        """
        Shutdowns then closes the underlying connection.
        """
        try:
            self.sock.shutdown(socket.SHUT_RDWR)
            self.sock.close()
        except:
            pass

    def ponged(self, pong):
        """
        Pong message received on the stream.

        @param pong: messaging.PongControlMessage instance
        """
        pass

    def received_message(self, m):
        """
        Message received on the stream.

        @param pong: messaging.TextMessage or messaging.BinaryMessage instance
        """
        pass

    def send(self, payload, binary=False):
        """
        Sends the given payload out.

        If payload is some bytes or a bytearray,
        then it is sent as a single message not fragmented.

        If payload is a generator, each chunk is sent as part of
        fragmented message.

        @param payload: string, bytes, bytearray or a generator
        @param binary: if set, handles the payload as a binary message
        """
        if isinstance(payload, basestring) or isinstance(payload, bytearray):
            if not binary:
                self.write_to_connection(self.stream.text_message(payload).single())
            else:
                self.write_to_connection(self.stream.binary_message(payload).single())

        elif type(payload) == types.GeneratorType:
            bytes = payload.next()
            first = True
            for chunk in payload:
                if not binary:
                    self.write_to_connection(self.stream.text_message(bytes).fragment(first=first))
                else:
                    self.write_to_connection(self.stream.binary_message(payload).fragment(first=first))
                bytes = chunk
                first = False
            if not binary:
                self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))
            else:
                self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))

    def _receive(self):
        """
        Performs the operation of reading from the underlying
        connection in order to feed the stream of bytes.

        We start with a small size of two bytes to be read
        from the connection so that we can quickly parse an
        incoming frame header. Then the stream indicates
        whatever size must be read from the connection since
        it knows the frame payload length.

        Note that we perform some automatic opererations:

        * On a closing message, we respond with a closing
          message and finally close the connection
        * We respond to pings with pong messages.
        * Whenever an error is raised by the stream parsing,
          we initiate the closing of the connection with the
          appropiate error code.
        """
        next_size = 2
        try:
            self.sock.setblocking(1)
            while not self.terminated:
                bytes = self.read_from_connection(next_size)
                if not bytes and next_size > 0:
                    break

                s = self.stream
                next_size = s.parser.send(bytes)

                if s.closing is not None:
                    if not self.server_terminated:
                        next_size = 2
                        self.close(s.closing.code, s.closing.reason)
                    else:
                        self.client_terminated = True
                        break

                elif s.errors:
                    errors = s.errors[:]
                    for error in errors:
                        self.close(error.code, error.reason)
                        s.errors.remove(error)
                    break

                elif s.has_message:
                    self.received_message(s.message)
                    s.message.data = None
                    s.message = None

                for ping in s.pings:
                    self.write_to_connection(s.pong(str(ping.data)))
                s.pings = []

                for pong in s.pongs:
                    self.ponged(pong)
                s.pongs = []

        except:
            print "".join(traceback.format_exception(*exc_info()))
        finally:
            self.client_terminated = self.server_terminated = True
            self.close_connection()
        if self.stream.closing:
            self.closed(self.stream.closing.code, self.stream.closing.reason)
        else:
            self.closed(1006)

class EchoWebSocketHandler(WebSocketHandler):
    """
    Simple handler that keeps echoing whatever message
    it receives.
    """
    def received_message(self, m):
        self.send(m.data, m.is_binary)

<|endoftext|>


<commit_before>from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect

from utils.liu_student import MultipleMatches
from .models import Course, Groupings

def landing(request):
    return render(request, "webgroup/landing.html", {})

def github_stats(request):
    return render(request, "webgroup/github.html", {})

@login_required
def exam_statistics(request):
    course = request.GET.get('course')
    exam = request.GET.get('exam')
    date_range = request.GET.get('daterange')
    try:
        min_participants = int(request.GET.get('min_participants', 0))
    except ValueError:
        min_participants = 0
    try:
        [date_from, date_to] = date_range.split(" - ")
    except AttributeError:
        date_from = None
        date_to = None
    url_filter = {
        "course": course,
        "exam": exam,
        "date_from": date_from,
        "date_to": date_to,
        "min_participants": min_participants
    }
    try:
        if len(course) >= 6:
            try:
                tmp = Course.objects.get(course_code=course.upper())
            except Course.DoesNotExist:
                tmp = Course()
                tmp.course_code = course.upper()
                try:
                    res = tmp.collect_results()
                except MultipleMatches:
                    messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
                    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})
                if res:
                    tmp.name = res[0]["course_name"]
                    tmp.save()
                    tmp.store_results(res)
                else:
                    messages.error(request, "Sökningen gav ingen träff.")
                    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})
            data = tmp.google_chart(exam, date_from, date_to, min_participants)
            return render(request, "webgroup/exam_statistics.html", {"result": tmp, "filter": url_filter, "google_chart": data['json']})
        else:
            messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
    except TypeError:
        pass
    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})

@login_required
def exam_statistics_update(request):
    course = request.GET.get('course')
    if len(course) >= 6:
        try:
            tmp = Course.objects.get(course_code=course.upper())
            tmp.collect_and_store_results()
            return redirect(reverse("webgroup:exam_statistics") + "?course={}".format(course))
        except Course.DoesNotExist:
            messages.error(request, "Kurskoden gav inte någon träff.")
    else:
        messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
    return redirect(reverse("webgroup:exam_statistics")+"?course={}".format(course))

@login_required
def exam_statistics_groups(request):
    group = request.GET.get('group')
    date_range = request.GET.get('daterange')
    try:
        min_participants = int(request.GET.get('min_participants', 0))
    except ValueError:
        min_participants = 0
    try:
        [date_from, date_to] = date_range.split(" - ")
    except AttributeError:
        date_from = None
        date_to = None
    url_filter = {
        "group": group,
        "date_from": date_from,
        "date_to": date_to,
        "min_participants": min_participants
    }
    courses = []
    gk = 0
    gkc = 0
    gu = 0
    g4 = 0
    g4c = 0
    g5 = 0
    g5c = 0
    try:
        g = Groupings.objects.get(pk=group)
        course_objects = g.courses.all()
        for c in course_objects:
            tmp = c.statistics(date_from, date_to, min_participants)
            course_avg = {"U": 0, "sum": 0, "max": 0}
            for k, v in list(tmp.items()):
                if tmp[k]['summed'] == 0:
                    del tmp[k]
                    continue
                try:
                    tmp[k]["kugg"] = round(tmp[k]["U"]*1000/tmp[k]["summed"])/10
                    course_avg["max"] = max(tmp[k]["kugg"], course_avg["max"])
                    course_avg["U"] += tmp[k]["U"]
                    course_avg["sum"] += tmp[k]["summed"]
                except (KeyError, ZeroDivisionError):
                    tmp[k]["kugg"] = 0
                try:
                    tmp[k]["3or"] = round(tmp[k]["3"] * 1000 / tmp[k]["summed"]) / 10
                except (KeyError, ZeroDivisionError):
                    tmp[k]["3or"] = 0
                try:
                    tmp[k]["4or"] = round(tmp[k]["4"] * 1000 / tmp[k]["summed"]) / 10
                    g4 += tmp[k]["4or"]
                    g4c += 1
                except (KeyError, ZeroDivisionError):
                    tmp[k]["4or"] = 0
                try:
                    tmp[k]["5or"] = round(tmp[k]["5"] * 1000 / tmp[k]["summed"]) / 10
                    g5 += tmp[k]["5or"]
                    g5c += 1
                except (KeyError, ZeroDivisionError):
                    tmp[k]["5or"] = 0
                try:
                    tmp[k]["pass"] = round(tmp[k]["G"] * 1000 / tmp[k]["summed"]) / 10
                except (KeyError, ZeroDivisionError):
                    tmp[k]["pass"] = 0

            tmp["course_code"] = c.course_code
            tmp["course_name"] = c.name
            tmp["course_highest_kugg"] = course_avg["max"]
            try:
                tmp["course_pass_avg"] = round(course_avg["U"] * 1000 / course_avg["sum"]) / 10
            except (KeyError, ZeroDivisionError):
                tmp["course_pass_avg"] = 0
            gk += tmp["course_pass_avg"]
            gu += tmp["course_highest_kugg"]
            gkc += 1
            courses.append(tmp)
    except Groupings.DoesNotExist:
        pass
    return render(request, "webgroup/exam_statistics_groups.html", {"result": None, "filter": url_filter, "groups": Groupings.objects.all(), "courses": courses, "ggk": (round(gk * 10 / gkc) / 10), "gu": (round(gu * 10 / gkc) / 10), "g4": (round(g4 * 10 / g4c) / 10), "g5": (round(g5 * 10 / g5c) / 10)})
<commit_msg>bugfix division with zero<commit_after>from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect

from utils.liu_student import MultipleMatches
from .models import Course, Groupings

def landing(request):
    return render(request, "webgroup/landing.html", {})

def github_stats(request):
    return render(request, "webgroup/github.html", {})

@login_required
def exam_statistics(request):
    course = request.GET.get('course')
    exam = request.GET.get('exam')
    date_range = request.GET.get('daterange')
    try:
        min_participants = int(request.GET.get('min_participants', 0))
    except ValueError:
        min_participants = 0
    try:
        [date_from, date_to] = date_range.split(" - ")
    except AttributeError:
        date_from = None
        date_to = None
    url_filter = {
        "course": course,
        "exam": exam,
        "date_from": date_from,
        "date_to": date_to,
        "min_participants": min_participants
    }
    try:
        if len(course) >= 6:
            try:
                tmp = Course.objects.get(course_code=course.upper())
            except Course.DoesNotExist:
                tmp = Course()
                tmp.course_code = course.upper()
                try:
                    res = tmp.collect_results()
                except MultipleMatches:
                    messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
                    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})
                if res:
                    tmp.name = res[0]["course_name"]
                    tmp.save()
                    tmp.store_results(res)
                else:
                    messages.error(request, "Sökningen gav ingen träff.")
                    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})
            data = tmp.google_chart(exam, date_from, date_to, min_participants)
            return render(request, "webgroup/exam_statistics.html", {"result": tmp, "filter": url_filter, "google_chart": data['json']})
        else:
            messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
    except TypeError:
        pass
    return render(request, "webgroup/exam_statistics.html", {"result": None, "filter": url_filter})

@login_required
def exam_statistics_update(request):
    course = request.GET.get('course')
    if len(course) >= 6:
        try:
            tmp = Course.objects.get(course_code=course.upper())
            tmp.collect_and_store_results()
            return redirect(reverse("webgroup:exam_statistics") + "?course={}".format(course))
        except Course.DoesNotExist:
            messages.error(request, "Kurskoden gav inte någon träff.")
    else:
        messages.error(request, "Kurskoden gav inte en unik träff i sökningen.")
    return redirect(reverse("webgroup:exam_statistics")+"?course={}".format(course))

@login_required
def exam_statistics_groups(request):
    group = request.GET.get('group')
    date_range = request.GET.get('daterange')
    try:
        min_participants = int(request.GET.get('min_participants', 0))
    except ValueError:
        min_participants = 0
    try:
        [date_from, date_to] = date_range.split(" - ")
    except AttributeError:
        date_from = None
        date_to = None
    url_filter = {
        "group": group,
        "date_from": date_from,
        "date_to": date_to,
        "min_participants": min_participants
    }
    courses = []
    gk = 0
    gkc = 0
    gu = 0
    g4 = 0
    g4c = 0
    g5 = 0
    g5c = 0
    try:
        g = Groupings.objects.get(pk=group)
        course_objects = g.courses.all()
        for c in course_objects:
            tmp = c.statistics(date_from, date_to, min_participants)
            course_avg = {"U": 0, "sum": 0, "max": 0}
            for k, v in list(tmp.items()):
                if tmp[k]['summed'] == 0:
                    del tmp[k]
                    continue
                try:
                    tmp[k]["kugg"] = round(tmp[k]["U"]*1000/tmp[k]["summed"])/10
                    course_avg["max"] = max(tmp[k]["kugg"], course_avg["max"])
                    course_avg["U"] += tmp[k]["U"]
                    course_avg["sum"] += tmp[k]["summed"]
                except (KeyError, ZeroDivisionError):
                    tmp[k]["kugg"] = 0
                try:
                    tmp[k]["3or"] = round(tmp[k]["3"] * 1000 / tmp[k]["summed"]) / 10
                except (KeyError, ZeroDivisionError):
                    tmp[k]["3or"] = 0
                try:
                    tmp[k]["4or"] = round(tmp[k]["4"] * 1000 / tmp[k]["summed"]) / 10
                    g4 += tmp[k]["4or"]
                    g4c += 1
                except (KeyError, ZeroDivisionError):
                    tmp[k]["4or"] = 0
                try:
                    tmp[k]["5or"] = round(tmp[k]["5"] * 1000 / tmp[k]["summed"]) / 10
                    g5 += tmp[k]["5or"]
                    g5c += 1
                except (KeyError, ZeroDivisionError):
                    tmp[k]["5or"] = 0
                try:
                    tmp[k]["pass"] = round(tmp[k]["G"] * 1000 / tmp[k]["summed"]) / 10
                except (KeyError, ZeroDivisionError):
                    tmp[k]["pass"] = 0

            tmp["course_code"] = c.course_code
            tmp["course_name"] = c.name
            tmp["course_highest_kugg"] = course_avg["max"]
            try:
                tmp["course_pass_avg"] = round(course_avg["U"] * 1000 / course_avg["sum"]) / 10
            except (KeyError, ZeroDivisionError):
                tmp["course_pass_avg"] = 0
            gk += tmp["course_pass_avg"]
            gu += tmp["course_highest_kugg"]
            gkc += 1
            courses.append(tmp)
    except Groupings.DoesNotExist:
        pass
    try:
        res = {"result": None, "filter": url_filter, "groups": Groupings.objects.all(), "courses": courses,
         "ggk": (round(gk * 10 / gkc) / 10), "gu": (round(gu * 10 / gkc) / 10), "g4": (round(g4 * 10 / g4c) / 10),
         "g5": (round(g5 * 10 / g5c) / 10)}
    except (KeyError, ZeroDivisionError):
        res = {"result": None, "filter": url_filter, "groups": Groupings.objects.all(), "courses": courses}
    return render(request, "webgroup/exam_statistics_groups.html", res)
<|endoftext|>


<commit_before>import winreg
import sys
import exceptions
import array
from types import *
import string

class RegType:
    def __init__( self, msname, friendlyname ):
        self.msname=msname
        self.friendlyname=friendlyname
        self.intval=getattr( winreg, msname )

    def __repr__( self ):
        return "<RegType %d: %s %s>" % \
                        (self.intval, self.msname, self.friendlyname )

_typeConstants={
    winreg.REG_NONE:
        RegType( "REG_NONE", "None" ),
    winreg.REG_SZ:
        RegType( "REG_SZ", "String" ),
    winreg.REG_EXPAND_SZ:
        RegType("REG_EXPAND_SZ", "Expandable Template String" ),
    winreg.REG_BINARY:
        RegType("REG_BINARY", "Binary Data"),
    winreg.REG_DWORD :
        RegType("REG_DWORD", "Integer" ),
#    winreg.REG_DWORD_LITTLE_ENDIAN :
#        RegType("REG_DWORD_LITTLE_ENDIAN", "Integer"),
    winreg.REG_DWORD_BIG_ENDIAN :
        RegType("REG_DWORD_BIG_ENDIAN", "Big Endian Integer"),
    winreg.REG_LINK :
        RegType("REG_LINK", "Link"),
    winreg.REG_MULTI_SZ :
        RegType("REG_MULTI_SZ", "List of Strings"),
    winreg.REG_RESOURCE_LIST :
        RegType("REG_RESOURCE_LIST", "Resource List"),
    winreg.REG_FULL_RESOURCE_DESCRIPTOR :
        RegType( "REG_FULL_RESOURCE_DESCRIPTOR",
                                            "Full Resource Descriptor" ),
    winreg.REG_RESOURCE_REQUIREMENTS_LIST:
        RegType( "REG_RESOURCE_REQUIREMENTS_LIST",
                                            "Resource Requirements List" )
}

regtypes={}
for constant in _typeConstants.values():
    regtypes[constant.msname]=constant

class _DictBase:
    def __init__( self, key ):
        self.key=key

    def clear( self ):
        keys=list( self.keys() )
        map( self.__delitem__, keys )

    def get( self, item, defaultVal=None ):
        try:
            return self.__getitem__( item )
        except (IndexError, EnvironmentError, WindowsError):
            return defaultVal

    def has_key( self, item ):
        try:
            self.__getitem__( item )
            return 1
        except (IndexError, EnvironmentError, WindowsError):
            return 0

    def keys( self ):
        keys=[]
        try:
            for i in xrange( 0, sys.maxint ):
                keyname = self._nameFromNum( i  )
                keys.append( keyname )
        except (IndexError, EnvironmentError, WindowsError):
            pass
        return keys

    def values( self ):
        values=[]  # map() doesn't use the IndexError semantics...
        for i in self:
            values.append( i )
        return values

    def items( self ):
        return map( None, self.keys(), self.values() )

    def __len__( self ):
        return len( self.keys() )

def _getName( item, nameFromNum ):
    if type( item ) == IntType:
        try:
            keyname = nameFromNum( item )
        except (WindowsError, EnvironmentError):
            raise IndexError, item

    elif type( item )==StringType:
        keyname=item
    else:
        raise exceptions.TypeError, \
                "Requires integer index or string key name"
    return keyname

class RegValuesDict( _DictBase ):
    def _nameFromNum( self, i ):
        return self.key._nameFromNum( i )

    def __getitem__( self, item ):
        return self.key.getValueNameDataAndType( item )

    def __setitem__( self, name, val):
        if type( val )==TupleType:
            data, datatype=val
            assert isinstance( datatype, RegType )
            self.key.setValue( name, data, datatype )
        else:
            self.key.setValue( name, val )

    def __delitem__( self, item ):
        valname=_getName( item, self._nameFromNum )
        self.key.deleteValue( valname )

class RegKeysDict( _DictBase ):
    def _nameFromNum( self, item ):
        return winreg.EnumKey( self.key.handle, item )

    def __getitem__( self, item ):
        keyname=_getName( item, self._nameFromNum )
        return self.key.openSubkey( keyname )

    def __delitem__( self, item ):
        keyname=_getName( item, self._nameFromNum )
        self.key.deleteSubkey( keyname )

def openKey( keyname, samFlags=None ):
    lst=string.split( keyname, "\\", 1 )
    if len( lst )==2:
        hivename,path=lst
        return hives[hivename].openSubkey( path )
    else:
        hivename=lst[0]
        return hives[hivename]

def createKey( keyname ):
    lst=string.split( keyname, "\\", 1 )
    assert len( lst )==2
    hivename,path=lst
    return hives[hivename].createSubkey( path )

def deleteKey( keyname ):
    lst=string.split( keyname, "\\", 1 )
    assert len( lst )==2
    hivename,path=lst
    return hives[hivename].deleteSubkey( path )

class RegKey:
    def _nameFromNum( self, item ):
        (name,data,datatype)=winreg.EnumValue( self.handle, item )
        return name

    def __nonzero__(self):
        if self.handle:
            return 1
        else:
            return 0

    def __cmp__ (self, other ):
        if hasattr( other, "handle" ) and hasattr( other, "name" ):
            return cmp( self.name, other.name )
        else:
            return cmp( self.handle, other )

    def __init__( self, name, handle=None ):
        self.name=name
        self.handle=handle

    def __repr__( self ):
        return "<Windows RegKey: %s>"% self.name

    def close(self ):
        return winreg.CloseKey( self.handle )

    def getSubkeyNames( self ):
        return self.getSubkeys().keys()

    def getValueNames( self ):
        return self.getValues().keys()

    def deleteSubkey( self, subkey ):
        return winreg.DeleteKey( self.handle, subkey )

    def deleteValue( self, valname ):
        return winreg.DeleteValue( self.handle, valname )

    def createSubkey( self, keyname ):
        handle=winreg.CreateKey( self.handle, keyname )
        return RegKey( self.name+"\\"+keyname, handle)

    def openSubkey( self, keyname, samFlags=None ):
        if samFlags:
            handle=winreg.OpenKey( self.handle, keyname, 0, samFlags )
        else:
            handle=winreg.OpenKey( self.handle, keyname, 0 )
        return RegKey( self.name+"\\"+keyname, handle )

    def getSubkeys( self ):
        return RegKeysDict( self )

    def getValues( self ):
        return RegValuesDict( self )

    def getValueNameDataAndType( self, valname ):
        try:
            if type( valname )==IntType:
                (valname,data,datatype)=winreg.EnumValue( self.handle, valname )
            else:
                keyname=_getName( valname, self._nameFromNum )
                (data,datatype)=winreg.QueryValueEx( self.handle, keyname )
        except (WindowsError, EnvironmentError):
            raise IndexError, valname

        if datatype==winreg.REG_BINARY:
            # use arrays for binary data
            data=array.array( 'c', data )

        return (valname, data, _typeConstants[datatype] )

    def getValueData( self, valname ):
        name, data, type=self.getValueNameDataAndType( valname )
        return data

    def setValue( self, valname, data, regtype=None ):
        if regtype:
            typeint=regtype.intval
        else:
            if type( data )==StringType:
                typeint=winreg.REG_SZ
            elif type( data )==IntType:
                typeint=winreg.REG_DWORD
            elif type( data )==array.ArrayType:
                typeint=winreg.REG_BINARY
                data=data.tostring()
        winreg.SetValueEx( self.handle, valname, 0, typeint, data )

    def flush(self ):
        winreg.FlushKey( self.keyobbj )

    def save( self, filename ):
        winreg.SaveKey( self.keyobj, filename )

    def load( self, subkey, filename ):
        return winreg.RegLoadKey( self.handle, subkey, filename )

class RemoteKey( RegKey ):
    def __init__( self, machine, topLevelKey ):
        assert topLevelKey in _hivenames
        self.handle =  winreg.ConnectRegistry( machine, parentKey )
        self.name=r"\\%s\%s" % (machine, topLevelKey )

_hivenames = ["HKEY_CLASSES_ROOT","HKEY_CURRENT_USER","HKEY_LOCAL_MACHINE",
                "HKEY_USERS","HKEY_CURRENT_CONFIG","HKEY_DYN_DATA",
                "HKEY_PERFORMANCE_DATA"]
hives={}
for name in _hivenames:
    hives[name]=RegKey( name, getattr( winreg, name ) )
hives["HKLM"]=hives["HKEY_LOCAL_MACHINE"]
hives["HKCR"]=hives["HKEY_CLASSES_ROOT"]
hives["HKCU"]=hives["HKEY_CURRENT_USER"]

_flagnames = ["KEY_ALL_ACCESS","KEY_CREATE_LINK", "KEY_CREATE_SUB_KEY",
             "KEY_ENUMERATE_SUB_KEYS", "KEY_EXECUTE", "KEY_NOTIFY",
             "KEY_QUERY_VALUE", "KEY_READ", "KEY_SET_VALUE"]
flags={}
for name in _flagnames:
    flags[name]=getattr( winreg, name )

_RegNotifyChangeKeyValueOptions=[ "REG_NOTIFY_CHANGE_ATTRIBUTES",
            "REG_NOTIFY_CHANGE_SECURITY", "REG_NOTIFY_CHANGE_LAST_SET",
            "REG_NOTIFY_CHANGE_NAME", "REG_LEGAL_CHANGE_FILTER" ]

_RegRestoreKeyOptions=["REG_WHOLE_HIVE_VOLATILE",
    "REG_NO_LAZY_FLUSH",
    "REG_OPTION_VOLATILE",
    "REG_REFRESH_HIVE",
    "REG_OPTION_NON_VOLATILE",
    "REG_OPTION_BACKUP_RESTORE" ]

_RegCreateKeyExOptions=[
    "REG_LEGAL_OPTION",
    "REG_OPTION_RESERVED",
    "REG_OPTION_VOLATILE",
    "REG_OPTION_NON_VOLATILE",
    "REG_OPTION_BACKUP_RESTORE",
    "REG_CREATED_NEW_KEY",
    "REG_OPENED_EXISTING_KEY",
    "REG_OPTION_CREATE_LINK"]

def test():
    import testreg

#unusednames=_RegNotifyChangeKeyValueOptions+_RegRestoreKeyOptions+_RegCreateKeyExOptions

#typeConstantNames=map( lambda x: x.msname, typeConstants.values() )

#allnames=_hivenames+_flagnames+typeConstantNames+unusednames
#winregnames=winreg.__dict__.keys()
#for name in winregnames:
#    if name not in allnames:
#        print name

<commit_msg>Ooops!  I didn't finish all the renaming needed here, so this was attempting a recursive import and causing a fatal error.<commit_after>import _winreg
import sys
import exceptions
import array
from types import *
import string

class RegType:
    def __init__( self, msname, friendlyname ):
        self.msname=msname
        self.friendlyname=friendlyname
        self.intval=getattr( _winreg, msname )

    def __repr__( self ):
        return "<RegType %d: %s %s>" % \
                        (self.intval, self.msname, self.friendlyname )

_typeConstants={
    _winreg.REG_NONE:
        RegType( "REG_NONE", "None" ),
    _winreg.REG_SZ:
        RegType( "REG_SZ", "String" ),
    _winreg.REG_EXPAND_SZ:
        RegType("REG_EXPAND_SZ", "Expandable Template String" ),
    _winreg.REG_BINARY:
        RegType("REG_BINARY", "Binary Data"),
    _winreg.REG_DWORD :
        RegType("REG_DWORD", "Integer" ),
#    _winreg.REG_DWORD_LITTLE_ENDIAN :
#        RegType("REG_DWORD_LITTLE_ENDIAN", "Integer"),
    _winreg.REG_DWORD_BIG_ENDIAN :
        RegType("REG_DWORD_BIG_ENDIAN", "Big Endian Integer"),
    _winreg.REG_LINK :
        RegType("REG_LINK", "Link"),
    _winreg.REG_MULTI_SZ :
        RegType("REG_MULTI_SZ", "List of Strings"),
    _winreg.REG_RESOURCE_LIST :
        RegType("REG_RESOURCE_LIST", "Resource List"),
    _winreg.REG_FULL_RESOURCE_DESCRIPTOR :
        RegType( "REG_FULL_RESOURCE_DESCRIPTOR",
                                            "Full Resource Descriptor" ),
    _winreg.REG_RESOURCE_REQUIREMENTS_LIST:
        RegType( "REG_RESOURCE_REQUIREMENTS_LIST",
                                            "Resource Requirements List" )
}

regtypes={}
for constant in _typeConstants.values():
    regtypes[constant.msname]=constant

class _DictBase:
    def __init__( self, key ):
        self.key=key

    def clear( self ):
        keys=list( self.keys() )
        map( self.__delitem__, keys )

    def get( self, item, defaultVal=None ):
        try:
            return self.__getitem__( item )
        except (IndexError, EnvironmentError, WindowsError):
            return defaultVal

    def has_key( self, item ):
        try:
            self.__getitem__( item )
            return 1
        except (IndexError, EnvironmentError, WindowsError):
            return 0

    def keys( self ):
        keys=[]
        try:
            for i in xrange( 0, sys.maxint ):
                keyname = self._nameFromNum( i  )
                keys.append( keyname )
        except (IndexError, EnvironmentError, WindowsError):
            pass
        return keys

    def values( self ):
        values=[]  # map() doesn't use the IndexError semantics...
        for i in self:
            values.append( i )
        return values

    def items( self ):
        return map( None, self.keys(), self.values() )

    def __len__( self ):
        return len( self.keys() )

def _getName( item, nameFromNum ):
    if type( item ) == IntType:
        try:
            keyname = nameFromNum( item )
        except (WindowsError, EnvironmentError):
            raise IndexError, item

    elif type( item )==StringType:
        keyname=item
    else:
        raise exceptions.TypeError, \
                "Requires integer index or string key name"
    return keyname

class RegValuesDict( _DictBase ):
    def _nameFromNum( self, i ):
        return self.key._nameFromNum( i )

    def __getitem__( self, item ):
        return self.key.getValueNameDataAndType( item )

    def __setitem__( self, name, val):
        if type( val )==TupleType:
            data, datatype=val
            assert isinstance( datatype, RegType )
            self.key.setValue( name, data, datatype )
        else:
            self.key.setValue( name, val )

    def __delitem__( self, item ):
        valname=_getName( item, self._nameFromNum )
        self.key.deleteValue( valname )

class RegKeysDict( _DictBase ):
    def _nameFromNum( self, item ):
        return _winreg.EnumKey( self.key.handle, item )

    def __getitem__( self, item ):
        keyname=_getName( item, self._nameFromNum )
        return self.key.openSubkey( keyname )

    def __delitem__( self, item ):
        keyname=_getName( item, self._nameFromNum )
        self.key.deleteSubkey( keyname )

def openKey( keyname, samFlags=None ):
    lst=string.split( keyname, "\\", 1 )
    if len( lst )==2:
        hivename,path=lst
        return hives[hivename].openSubkey( path )
    else:
        hivename=lst[0]
        return hives[hivename]

def createKey( keyname ):
    lst=string.split( keyname, "\\", 1 )
    assert len( lst )==2
    hivename,path=lst
    return hives[hivename].createSubkey( path )

def deleteKey( keyname ):
    lst=string.split( keyname, "\\", 1 )
    assert len( lst )==2
    hivename,path=lst
    return hives[hivename].deleteSubkey( path )

class RegKey:
    def _nameFromNum( self, item ):
        (name,data,datatype)=_winreg.EnumValue( self.handle, item )
        return name

    def __nonzero__(self):
        if self.handle:
            return 1
        else:
            return 0

    def __cmp__ (self, other ):
        if hasattr( other, "handle" ) and hasattr( other, "name" ):
            return cmp( self.name, other.name )
        else:
            return cmp( self.handle, other )

    def __init__( self, name, handle=None ):
        self.name=name
        self.handle=handle

    def __repr__( self ):
        return "<Windows RegKey: %s>"% self.name

    def close(self ):
        return _winreg.CloseKey( self.handle )

    def getSubkeyNames( self ):
        return self.getSubkeys().keys()

    def getValueNames( self ):
        return self.getValues().keys()

    def deleteSubkey( self, subkey ):
        return _winreg.DeleteKey( self.handle, subkey )

    def deleteValue( self, valname ):
        return _winreg.DeleteValue( self.handle, valname )

    def createSubkey( self, keyname ):
        handle=_winreg.CreateKey( self.handle, keyname )
        return RegKey( self.name+"\\"+keyname, handle)

    def openSubkey( self, keyname, samFlags=None ):
        if samFlags:
            handle=_winreg.OpenKey( self.handle, keyname, 0, samFlags )
        else:
            handle=_winreg.OpenKey( self.handle, keyname, 0 )
        return RegKey( self.name+"\\"+keyname, handle )

    def getSubkeys( self ):
        return RegKeysDict( self )

    def getValues( self ):
        return RegValuesDict( self )

    def getValueNameDataAndType( self, valname ):
        try:
            if type( valname )==IntType:
                (valname,data,datatype)=_winreg.EnumValue( self.handle, valname )
            else:
                keyname=_getName( valname, self._nameFromNum )
                (data,datatype)=_winreg.QueryValueEx( self.handle, keyname )
        except (WindowsError, EnvironmentError):
            raise IndexError, valname

        if datatype==_winreg.REG_BINARY:
            # use arrays for binary data
            data=array.array( 'c', data )

        return (valname, data, _typeConstants[datatype] )

    def getValueData( self, valname ):
        name, data, type=self.getValueNameDataAndType( valname )
        return data

    def setValue( self, valname, data, regtype=None ):
        if regtype:
            typeint=regtype.intval
        else:
            if type( data )==StringType:
                typeint=_winreg.REG_SZ
            elif type( data )==IntType:
                typeint=_winreg.REG_DWORD
            elif type( data )==array.ArrayType:
                typeint=_winreg.REG_BINARY
                data=data.tostring()
        _winreg.SetValueEx( self.handle, valname, 0, typeint, data )

    def flush(self ):
        _winreg.FlushKey( self.keyobbj )

    def save( self, filename ):
        _winreg.SaveKey( self.keyobj, filename )

    def load( self, subkey, filename ):
        return _winreg.RegLoadKey( self.handle, subkey, filename )

class RemoteKey( RegKey ):
    def __init__( self, machine, topLevelKey ):
        assert topLevelKey in _hivenames
        self.handle =  _winreg.ConnectRegistry( machine, parentKey )
        self.name=r"\\%s\%s" % (machine, topLevelKey )

_hivenames = ["HKEY_CLASSES_ROOT","HKEY_CURRENT_USER","HKEY_LOCAL_MACHINE",
                "HKEY_USERS","HKEY_CURRENT_CONFIG","HKEY_DYN_DATA",
                "HKEY_PERFORMANCE_DATA"]
hives={}
for name in _hivenames:
    hives[name]=RegKey( name, getattr( _winreg, name ) )
hives["HKLM"]=hives["HKEY_LOCAL_MACHINE"]
hives["HKCR"]=hives["HKEY_CLASSES_ROOT"]
hives["HKCU"]=hives["HKEY_CURRENT_USER"]

_flagnames = ["KEY_ALL_ACCESS","KEY_CREATE_LINK", "KEY_CREATE_SUB_KEY",
             "KEY_ENUMERATE_SUB_KEYS", "KEY_EXECUTE", "KEY_NOTIFY",
             "KEY_QUERY_VALUE", "KEY_READ", "KEY_SET_VALUE"]
flags={}
for name in _flagnames:
    flags[name]=getattr( _winreg, name )

_RegNotifyChangeKeyValueOptions=[ "REG_NOTIFY_CHANGE_ATTRIBUTES",
            "REG_NOTIFY_CHANGE_SECURITY", "REG_NOTIFY_CHANGE_LAST_SET",
            "REG_NOTIFY_CHANGE_NAME", "REG_LEGAL_CHANGE_FILTER" ]

_RegRestoreKeyOptions=["REG_WHOLE_HIVE_VOLATILE",
    "REG_NO_LAZY_FLUSH",
    "REG_OPTION_VOLATILE",
    "REG_REFRESH_HIVE",
    "REG_OPTION_NON_VOLATILE",
    "REG_OPTION_BACKUP_RESTORE" ]

_RegCreateKeyExOptions=[
    "REG_LEGAL_OPTION",
    "REG_OPTION_RESERVED",
    "REG_OPTION_VOLATILE",
    "REG_OPTION_NON_VOLATILE",
    "REG_OPTION_BACKUP_RESTORE",
    "REG_CREATED_NEW_KEY",
    "REG_OPENED_EXISTING_KEY",
    "REG_OPTION_CREATE_LINK"]

def test():
    import testreg

#unusednames=_RegNotifyChangeKeyValueOptions+_RegRestoreKeyOptions+_RegCreateKeyExOptions

#typeConstantNames=map( lambda x: x.msname, typeConstants.values() )

#allnames=_hivenames+_flagnames+typeConstantNames+unusednames
#winregnames=_winreg.__dict__.keys()
#for name in winregnames:
#    if name not in allnames:
#        print name

<|endoftext|>


<commit_before>"""
=============================================
Integration and ODEs (:mod:`scipy.integrate`)
=============================================

.. currentmodule:: scipy.integrate

Integrating functions, given function object
============================================

.. autosummary::
   :toctree: generated/

   quad          -- General purpose integration
   dblquad       -- General purpose double integration
   tplquad       -- General purpose triple integration
   fixed_quad    -- Integrate func(x) using Gaussian quadrature of order n
   quadrature    -- Integrate with given tolerance using Gaussian quadrature
   romberg       -- Integrate func using Romberg integration

Integrating functions, given fixed samples
==========================================

.. autosummary::
   :toctree: generated/

   cumtrapz      -- Use trapezoidal rule to cumulatively compute integral.
   simps         -- Use Simpson's rule to compute integral from samples.
   romb          -- Use Romberg Integration to compute integral from
                 -- (2**k + 1) evenly-spaced samples.

.. seealso::

   :mod:`scipy.special` for orthogonal polynomials (special) for Gaussian
   quadrature roots and weights for other weighting factors and regions.

Integrators of ODE systems
==========================

.. autosummary::
   :toctree: generated/

   odeint        -- General integration of ordinary differential equations.
   ode           -- Integrate ODE using VODE and ZVODE routines.
   complex_ode   -- Convert a complex-valued ODE to real-valued and integrate.

"""
from __future__ import division, print_function, absolute_import

from .quadrature import *
from .odepack import *
from .quadpack import *
from ._ode import *

__all__ = [s for s in dir() if not s.startswith('_')]
from numpy.testing import Tester
test = Tester().test
<commit_msg>DOC: include integrate.nquad in reference guide.<commit_after>"""
=============================================
Integration and ODEs (:mod:`scipy.integrate`)
=============================================

.. currentmodule:: scipy.integrate

Integrating functions, given function object
============================================

.. autosummary::
   :toctree: generated/

   quad          -- General purpose integration
   dblquad       -- General purpose double integration
   tplquad       -- General purpose triple integration
   nquad         -- General purpose n-dimensional integration
   fixed_quad    -- Integrate func(x) using Gaussian quadrature of order n
   quadrature    -- Integrate with given tolerance using Gaussian quadrature
   romberg       -- Integrate func using Romberg integration

Integrating functions, given fixed samples
==========================================

.. autosummary::
   :toctree: generated/

   cumtrapz      -- Use trapezoidal rule to cumulatively compute integral.
   simps         -- Use Simpson's rule to compute integral from samples.
   romb          -- Use Romberg Integration to compute integral from
                 -- (2**k + 1) evenly-spaced samples.

.. seealso::

   :mod:`scipy.special` for orthogonal polynomials (special) for Gaussian
   quadrature roots and weights for other weighting factors and regions.

Integrators of ODE systems
==========================

.. autosummary::
   :toctree: generated/

   odeint        -- General integration of ordinary differential equations.
   ode           -- Integrate ODE using VODE and ZVODE routines.
   complex_ode   -- Convert a complex-valued ODE to real-valued and integrate.

"""
from __future__ import division, print_function, absolute_import

from .quadrature import *
from .odepack import *
from .quadpack import *
from ._ode import *

__all__ = [s for s in dir() if not s.startswith('_')]
from numpy.testing import Tester
test = Tester().test
<|endoftext|>


<commit_before>from unittest import TestCase

from mock import Mock, patch

from wtfmongoengine.forms import DocumentFormMetaClassBase

class DocumentFormMetaClassBaseTestCase(TestCase):
    """
    Tests :py:class:`.DocumentFormMetaClassBase`.
    """
    def setUp(self):
        self.fields = {
            'title': Mock(return_value='title-value'),
            'body': Mock(return_value='body-value'),
            'author': Mock(return_value='author-value'),
            'timestamp': Mock(return_value='timestamp-value'),
        }

        self.document = Mock()
        self.document._fields = self.fields

    @patch('wtfmongoengine.forms.DocumentFormMetaClassBase.model_fields')
    def test___new__(self, model_fields):
        """
        Test that ``__new__`` is creating the new class properly.
        """
        model_fields.return_value = {
            'field_a': 'a-value',
            'field_b': 'b-value'
        }

        class TestClass(object):
            __metaclass__ = DocumentFormMetaClassBase

            class Meta:
                fields = ('title', 'body',)
                exclude = ('author', 'timestamp',)
                document = 'a-document'

        model_fields.assert_called_once_with(
            'a-document',
            ('title', 'body',),
            ('author', 'timestamp',),
        )
        self.assertEqual('a-value', TestClass.field_a)
        self.assertEqual('b-value', TestClass.field_b)

    @patch('wtfmongoengine.forms.DocumentFieldConverter')
    def test_model_fields(self, DocumentFieldConverter):
        """
        Test that ``model_fields`` extracts the fields.

        Test :py:meth:`.DocumentFormMetaClassBase.model_fields`.

        """
        converter = Mock()
        converter.convert.side_effect = lambda x: x()
        DocumentFieldConverter.return_value = converter

        result = DocumentFormMetaClassBase.model_fields(self.document)
        self.assertEqual({
            'title': 'title-value',
            'body': 'body-value',
            'author': 'author-value',
            'timestamp': 'timestamp-value',
        }, result)
<commit_msg>Tests added for fields and exclude arguments.<commit_after>from unittest import TestCase

from mock import Mock, patch

from wtfmongoengine.forms import DocumentFormMetaClassBase

class DocumentFormMetaClassBaseTestCase(TestCase):
    """
    Test :py:class:`.DocumentFormMetaClassBase`.
    """
    def setUp(self):
        self.fields = {
            'title': Mock(return_value='title-value'),
            'body': Mock(return_value='body-value'),
            'author': Mock(return_value='author-value'),
            'timestamp': Mock(return_value='timestamp-value'),
        }

        self.document = Mock()
        self.document._fields = self.fields

        self.converter = Mock()
        self.converter.convert.side_effect = lambda x: x()

    @patch('wtfmongoengine.forms.DocumentFormMetaClassBase.model_fields')
    def test___new__(self, model_fields):
        """
        Test that ``__new__`` is creating the new class properly.
        """
        model_fields.return_value = {
            'field_a': 'a-value',
            'field_b': 'b-value'
        }

        class TestClass(object):
            __metaclass__ = DocumentFormMetaClassBase

            class Meta:
                fields = ('title', 'body',)
                exclude = ('author', 'timestamp',)
                document = 'a-document'

        model_fields.assert_called_once_with(
            'a-document',
            ('title', 'body',),
            ('author', 'timestamp',),
        )
        self.assertEqual('a-value', TestClass.field_a)
        self.assertEqual('b-value', TestClass.field_b)

    @patch('wtfmongoengine.forms.DocumentFieldConverter')
    def test_model_fields(self, DocumentFieldConverter):
        """
        Test that ``model_fields`` extracts the fields.

        Tests :py:meth:`.DocumentFormMetaClassBase.model_fields`.
        """
        DocumentFieldConverter.return_value = self.converter

        result = DocumentFormMetaClassBase.model_fields(self.document)
        self.assertEqual({
            'title': 'title-value',
            'body': 'body-value',
            'author': 'author-value',
            'timestamp': 'timestamp-value',
        }, result)

    @patch('wtfmongoengine.forms.DocumentFieldConverter')
    def test_model_fields_fields(self, DocumentFieldConverter):
        """
        Test that ``model_fields`` uses only fields in ``fields`` argument.

        Tests :py:meth:`.DocumentFormMetaClassBase.model_fields`.
        """
        DocumentFieldConverter.return_value = self.converter

        result = DocumentFormMetaClassBase.model_fields(
            self.document, fields=('title', 'author',))

        self.assertEqual({
            'title': 'title-value',
            'author': 'author-value',
        }, result)

    @patch('wtfmongoengine.forms.DocumentFieldConverter')
    def test_model_fields_exclude(self, DocumentFieldConverter):
        """
        Test that ``model_fields`` excludes fields in ``exclude`` argument.

        Tests :py:meth:`.DocumentFormMetaClassBase.model_fields`.
        """
        DocumentFieldConverter.return_value = self.converter

        result = DocumentFormMetaClassBase.model_fields(
            self.document, exclude=('title', 'author',))

        self.assertEqual({
            'body': 'body-value',
            'timestamp': 'timestamp-value',
        }, result)
<|endoftext|>


<commit_before>"""A dumb and slow but simple dbm clone.

For database spam, spam.dir contains the index (a text file),
spam.bak *may* contain a backup of the index (also a text file),
while spam.dat contains the data (a binary file).

XXX TO DO:

- seems to contain a bug when updating...

- reclaim free space (currently, space once occupied by deleted or expanded
items is never reused)

- support concurrent access (currently, if two processes take turns making
updates, they can mess up the index)

- support efficient access to large databases (currently, the whole index
is read when the database is opened, and some updates rewrite the whole index)

- support opening for read-only (flag = 'm')

"""

_os = __import__('os')
import __builtin__

_open = __builtin__.open

_BLOCKSIZE = 512

error = IOError             # For anydbm

class _Database(object):

    def __init__(self, file):
        self._dirfile = file + '.dir'
        self._datfile = file + '.dat'
        self._bakfile = file + '.bak'
        # Mod by Jack: create data file if needed
        try:
            f = _open(self._datfile, 'r')
        except IOError:
            f = _open(self._datfile, 'w')
        f.close()
        self._update()

    def _update(self):
        import string
        self._index = {}
        try:
            f = _open(self._dirfile)
        except IOError:
            pass
        else:
            while 1:
                line = string.rstrip(f.readline())
                if not line: break
                key, (pos, siz) = eval(line)
                self._index[key] = (pos, siz)
            f.close()

    def _commit(self):
        try: _os.unlink(self._bakfile)
        except _os.error: pass
        try: _os.rename(self._dirfile, self._bakfile)
        except _os.error: pass
        f = _open(self._dirfile, 'w')
        for key, (pos, siz) in self._index.items():
            f.write("%s, (%s, %s)\n" % (`key`, `pos`, `siz`))
        f.close()

    def __getitem__(self, key):
        pos, siz = self._index[key] # may raise KeyError
        f = _open(self._datfile, 'rb')
        f.seek(pos)
        dat = f.read(siz)
        f.close()
        return dat

    def _addval(self, val):
        f = _open(self._datfile, 'rb+')
        f.seek(0, 2)
        pos = f.tell()
## Does not work under MW compiler
##      pos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE
##      f.seek(pos)
        npos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE
        f.write('\0'*(npos-pos))
        pos = npos

        f.write(val)
        f.close()
        return (pos, len(val))

    def _setval(self, pos, val):
        f = _open(self._datfile, 'rb+')
        f.seek(pos)
        f.write(val)
        f.close()
        return (pos, len(val))

    def _addkey(self, key, (pos, siz)):
        self._index[key] = (pos, siz)
        f = _open(self._dirfile, 'a')
        f.write("%s, (%s, %s)\n" % (`key`, `pos`, `siz`))
        f.close()

    def __setitem__(self, key, val):
        if not isinstance(key, str) or not isinstance(val, str):
            raise TypeError, "keys and values must be strings"
        if not self._index.has_key(key):
            (pos, siz) = self._addval(val)
            self._addkey(key, (pos, siz))
        else:
            pos, siz = self._index[key]
            oldblocks = (siz + _BLOCKSIZE - 1) / _BLOCKSIZE
            newblocks = (len(val) + _BLOCKSIZE - 1) / _BLOCKSIZE
            if newblocks <= oldblocks:
                pos, siz = self._setval(pos, val)
                self._index[key] = pos, siz
            else:
                pos, siz = self._addval(val)
                self._index[key] = pos, siz
            self._addkey(key, (pos, siz))

    def __delitem__(self, key):
        del self._index[key]
        self._commit()

    def keys(self):
        return self._index.keys()

    def has_key(self, key):
        return self._index.has_key(key)

    def __len__(self):
        return len(self._index)

    def close(self):
        self._index = None
        self._datfile = self._dirfile = self._bakfile = None

def open(file, flag = None, mode = None):
    # flag, mode arguments are currently ignored
    return _Database(file)
<commit_msg>Fixed #855: add __contains__ to dumbdbm, don't rely on default implementation<commit_after>"""A dumb and slow but simple dbm clone.

For database spam, spam.dir contains the index (a text file),
spam.bak *may* contain a backup of the index (also a text file),
while spam.dat contains the data (a binary file).

XXX TO DO:

- seems to contain a bug when updating...

- reclaim free space (currently, space once occupied by deleted or expanded
items is never reused)

- support concurrent access (currently, if two processes take turns making
updates, they can mess up the index)

- support efficient access to large databases (currently, the whole index
is read when the database is opened, and some updates rewrite the whole index)

- support opening for read-only (flag = 'm')

"""

_os = __import__('os')
import __builtin__

_open = __builtin__.open

_BLOCKSIZE = 512

error = IOError             # For anydbm

class _Database(object):

    def __init__(self, file):
        self._dirfile = file + '.dir'
        self._datfile = file + '.dat'
        self._bakfile = file + '.bak'
        # Mod by Jack: create data file if needed
        try:
            f = _open(self._datfile, 'r')
        except IOError:
            f = _open(self._datfile, 'w')
        f.close()
        self._update()

    def _update(self):
        import string
        self._index = {}
        try:
            f = _open(self._dirfile)
        except IOError:
            pass
        else:
            while 1:
                line = string.rstrip(f.readline())
                if not line: break
                key, (pos, siz) = eval(line)
                self._index[key] = (pos, siz)
            f.close()

    def _commit(self):
        try: _os.unlink(self._bakfile)
        except _os.error: pass
        try: _os.rename(self._dirfile, self._bakfile)
        except _os.error: pass
        f = _open(self._dirfile, 'w')
        for key, (pos, siz) in self._index.items():
            f.write("%s, (%s, %s)\n" % (`key`, `pos`, `siz`))
        f.close()

    def __getitem__(self, key):
        pos, siz = self._index[key] # may raise KeyError
        f = _open(self._datfile, 'rb')
        f.seek(pos)
        dat = f.read(siz)
        f.close()
        return dat

    def __contains__(self, key):
        return key in self._index

    def _addval(self, val):
        f = _open(self._datfile, 'rb+')
        f.seek(0, 2)
        pos = f.tell()
## Does not work under MW compiler
##      pos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE
##      f.seek(pos)
        npos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE
        f.write('\0'*(npos-pos))
        pos = npos

        f.write(val)
        f.close()
        return (pos, len(val))

    def _setval(self, pos, val):
        f = _open(self._datfile, 'rb+')
        f.seek(pos)
        f.write(val)
        f.close()
        return (pos, len(val))

    def _addkey(self, key, (pos, siz)):
        self._index[key] = (pos, siz)
        f = _open(self._dirfile, 'a')
        f.write("%s, (%s, %s)\n" % (`key`, `pos`, `siz`))
        f.close()

    def __setitem__(self, key, val):
        if not isinstance(key, str) or not isinstance(val, str):
            raise TypeError, "keys and values must be strings"
        if not self._index.has_key(key):
            (pos, siz) = self._addval(val)
            self._addkey(key, (pos, siz))
        else:
            pos, siz = self._index[key]
            oldblocks = (siz + _BLOCKSIZE - 1) / _BLOCKSIZE
            newblocks = (len(val) + _BLOCKSIZE - 1) / _BLOCKSIZE
            if newblocks <= oldblocks:
                pos, siz = self._setval(pos, val)
                self._index[key] = pos, siz
            else:
                pos, siz = self._addval(val)
                self._index[key] = pos, siz
            self._addkey(key, (pos, siz))

    def __delitem__(self, key):
        del self._index[key]
        self._commit()

    def keys(self):
        return self._index.keys()

    def has_key(self, key):
        return self._index.has_key(key)

    def __len__(self):
        return len(self._index)

    def close(self):
        self._index = None
        self._datfile = self._dirfile = self._bakfile = None

def open(file, flag = None, mode = None):
    # flag, mode arguments are currently ignored
    return _Database(file)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from pokemongo_bot.event_manager import EventHandler
from pokemongo_bot.base_dir import _base_dir
import json
import os
import time
import telegram
import thread
import re

DEBUG_ON = False

class FileIOException(Exception):
    pass

class TelegramClass:

    update_id = None

    def __init__(self, bot, master, pokemons):
        self.bot = bot
        self.master = master
        self.pokemons = pokemons
        self._tbot = None

    def sendMessage(self, chat_id=None, parse_mode='Markdown', text=None):
        self._tbot.sendMessage(chat_id=chat_id, parse_mode=parse_mode, text=text)

    def connect(self):
        self._tbot = telegram.Bot(self.bot.config.telegram_token)
        try:
            self.update_id = self._tbot.getUpdates()[0].update_id
        except IndexError:
            self.update_id = None

    def _get_player_stats(self):
        web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
        try:
            with open(web_inventory, "r") as infile:
                json_inventory = json.load(infile)
        except ValueError as exception:
            self.bot.logger.info('[x] Error while opening inventory file for read: %s' % exception)
            json_inventory = []
        except:
            raise FileIOException("Unexpected error reading from {}".format(web_inventory))
        return next((x["inventory_item_data"]["player_stats"]
                for x in json_inventory
                if x.get("inventory_item_data", {}).get("player_stats", {})),
            None)

    def run(self):
        time.sleep(1)
        while True:
            try:
                for update in self._tbot.getUpdates(offset=self.update_id, timeout=10):
                    self.update_id = update.update_id+1
                    if update.message:
                        self.bot.logger.info("message from {} ({}): {}".format(update.message.from_user.username, update.message.from_user.id, update.message.text))
                        if self.master and self.master not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]:
                            continue
                        if update.message.text == "/info":
                            stats = self._get_player_stats()
                            if stats:
                                with self.bot.database as conn:
                                    cur = conn.cursor()
                                    cur.execute("SELECT DISTINCT COUNT(encounter_id) FROM catch_log WHERE dated >= datetime('now','-1 day')")
                                    catch_day = cur.fetchone()[0]
                                    cur.execute("SELECT DISTINCT COUNT(pokestop) FROM pokestop_log WHERE dated >= datetime('now','-1 day')")
                                    ps_day = cur.fetchone()[0]
                                    res = (
                                        "*"+self.bot.config.username+"*",
                                        "_Level:_ "+str(stats["level"]),
                                        "_XP:_ "+str(stats["experience"])+"/"+str(stats["next_level_xp"]),
                                        "_Pokemons Captured:_ "+str(stats["pokemons_captured"])+" ("+str(catch_day)+" _last 24h_)",
                                        "_Poke Stop Visits:_ "+str(stats["poke_stop_visits"])+" ("+str(ps_day)+" _last 24h_)",
                                        "_KM Walked:_ "+str("%.2f" % stats["km_walked"])
                                    )
                                self._tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
                                self._tbot.send_location(chat_id=update.message.chat_id, latitude=self.bot.api._position_lat, longitude=self.bot.api._position_lng)
                            else:
                                self._tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="Stats not loaded yet\n")
                        elif update.message.text == "/start" or update.message.text == "/help":
                            res = (
                                "Commands: ",
                                "/info - info about bot"
                            )
                            self._tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
            except telegram.error.NetworkError:
                time.sleep(1)
            except telegram.error.Unauthorized:
                self.update_id += 1

class TelegramHandler(EventHandler):
    def __init__(self, bot, config):
        self.bot = bot
        self.tbot = None
        self.master = config.get('master', None)
        self.pokemons = config.get('alert_catch', {})
        self.whoami = "TelegramHandler"

    def handle_event(self, event, sender, level, formatted_msg, data):
        if self.tbot is None:
            try:
                self.tbot = TelegramClass(self.bot, self.master, self.pokemons)
                self.tbot.connect()
                thread.start_new_thread(self.tbot.run)
            except Exception as inst:
                self.tbot = None
        if self.master:
            if not re.match(r'^[0-9]+$', str(self.master)):
                return
            master = self.master

            if event == 'level_up':
                msg = "level up ({})".format(data["current_level"])
            elif event == 'pokemon_caught':
                if isinstance(self.pokemons, list):
                    if data["pokemon"] in self.pokemons or "all" in self.pokemons:
                        msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"], data["cp"], data["iv"])
                    else:
                        return
                else:
                    if data["pokemon"] in self.pokemons:
                        trigger = self.pokemons[data["pokemon"]]
                    elif "all" in self.pokemons:
                        trigger = self.pokemons["all"]
                    else:
                        return
                    if (not "operator" in trigger or trigger["operator"] == "and") and data["cp"] >= trigger["cp"] and data["iv"] >= trigger["iv"] or ("operator" in trigger and trigger["operator"] == "or" and (data["cp"] >= trigger["cp"] or data["iv"] >= trigger["iv"])):
                        msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"], data["cp"], data["iv"])
                    else:
                        return
            else:
                return
            self.tbot.sendMessage(chat_id=master, parse_mode='Markdown', text=msg)

<commit_msg>Added quitting notify to telegram.<commit_after># -*- coding: utf-8 -*-
from pokemongo_bot.event_manager import EventHandler
from pokemongo_bot.base_dir import _base_dir
import json
import os
import time
import telegram
import thread
import re

DEBUG_ON = False

class FileIOException(Exception):
    pass

class TelegramClass:

    update_id = None

    def __init__(self, bot, master, pokemons):
        self.bot = bot
        self.master = master
        self.pokemons = pokemons
        self._tbot = None

    def sendMessage(self, chat_id=None, parse_mode='Markdown', text=None):
        self._tbot.sendMessage(chat_id=chat_id, parse_mode=parse_mode, text=text)

    def connect(self):
        self._tbot = telegram.Bot(self.bot.config.telegram_token)
        try:
            self.update_id = self._tbot.getUpdates()[0].update_id
        except IndexError:
            self.update_id = None

    def _get_player_stats(self):
        web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
        try:
            with open(web_inventory, "r") as infile:
                json_inventory = json.load(infile)
        except ValueError as exception:
            self.bot.logger.info('[x] Error while opening inventory file for read: %s' % exception)
            json_inventory = []
        except:
            raise FileIOException("Unexpected error reading from {}".format(web_inventory))
        return next((x["inventory_item_data"]["player_stats"]
                     for x in json_inventory
                     if x.get("inventory_item_data", {}).get("player_stats", {})),
                    None)
    def send_player_stats_to_chat(self, chat_id):
        stats = self._get_player_stats()
        if stats:
            with self.bot.database as conn:
                cur = conn.cursor()
                cur.execute("SELECT DISTINCT COUNT(encounter_id) FROM catch_log WHERE dated >= datetime('now','-1 day')")
                catch_day = cur.fetchone()[0]
                cur.execute("SELECT DISTINCT COUNT(pokestop) FROM pokestop_log WHERE dated >= datetime('now','-1 day')")
                ps_day = cur.fetchone()[0]
                res = (
                    "*"+self.bot.config.username+"*",
                    "_Level:_ "+str(stats["level"]),
                    "_XP:_ "+str(stats["experience"])+"/"+str(stats["next_level_xp"]),
                    "_Pokemons Captured:_ "+str(stats["pokemons_captured"])+" ("+str(catch_day)+" _last 24h_)",
                    "_Poke Stop Visits:_ "+str(stats["poke_stop_visits"])+" ("+str(ps_day)+" _last 24h_)",
                    "_KM Walked:_ "+str("%.2f" % stats["km_walked"])
                )
            self._tbot.sendMessage(chat_id=chat_id, parse_mode='Markdown', text="\n".join(res))
            self._tbot.send_location(chat_id=chat_id, latitude=self.bot.api._position_lat, longitude=self.bot.api._position_lng)
        else:
            self._tbot.sendMessage(chat_id=chat_id, parse_mode='Markdown', text="Stats not loaded yet\n")
    def run(self):
        time.sleep(1)
        while True:
            try:
                for update in self._tbot.getUpdates(offset=self.update_id, timeout=10):
                    self.update_id = update.update_id+1
                    if update.message:
                        self.bot.logger.info("message from {} ({}): {}".format(update.message.from_user.username, update.message.from_user.id, update.message.text))
                        if self.master and self.master not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]:
                            continue
                        if update.message.text == "/info":
                            self.send_player_stats_to_chat(update.message.chat_id)
                        elif update.message.text == "/start" or update.message.text == "/help":
                            res = (
                                "Commands: ",
                                "/info - info about bot"
                            )
                            self._tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))
            except telegram.error.NetworkError:
                time.sleep(1)
            except telegram.error.Unauthorized:
                self.update_id += 1

class TelegramHandler(EventHandler):
    def __init__(self, bot, config):
        self.bot = bot
        self.tbot = None
        self.master = config.get('master', None)
        self.pokemons = config.get('alert_catch', {})
        self.whoami = "TelegramHandler"

    def handle_event(self, event, sender, level, formatted_msg, data):
        if self.tbot is None:
            try:
                self.tbot = TelegramClass(self.bot, self.master, self.pokemons)
                self.tbot.connect()
                thread.start_new_thread(self.tbot.run)
            except Exception as inst:
                self.tbot = None
        if self.master:
            if not re.match(r'^[0-9]+$', str(self.master)):
                return
            master = self.master

            if event == 'level_up':
                msg = "level up ({})".format(data["current_level"])
            elif event == 'pokemon_caught':
                if isinstance(self.pokemons, list):
                    if data["pokemon"] in self.pokemons or "all" in self.pokemons:
                        msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"], data["cp"], data["iv"])
                    else:
                        return
                else:
                    if data["pokemon"] in self.pokemons:
                        trigger = self.pokemons[data["pokemon"]]
                    elif "all" in self.pokemons:
                        trigger = self.pokemons["all"]
                    else:
                        return
                    if (not "operator" in trigger or trigger["operator"] == "and") and data["cp"] >= trigger["cp"] and data["iv"] >= trigger["iv"] or ("operator" in trigger and trigger["operator"] == "or" and (data["cp"] >= trigger["cp"] or data["iv"] >= trigger["iv"])):
                        msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"], data["cp"], data["iv"])
                    else:
                        return
            elif event == 'catch_limit':
                self.tbot.send_player_stats_to_chat(master)
                msg = "*You have reached your daily catch limit, quitting.*"
            else:
                return
            self.tbot.sendMessage(chat_id=master, parse_mode='Markdown', text=msg)
<|endoftext|>