<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2010 Sebastian Wiesner <lunaryorn@googlemail.com>
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. 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.

# 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.

"""
    synaptiks.monitors
    ==================

    Monitor classes for various external event sources.

    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@googlemail.com>
"""

from itertools import ifilter
from collections import namedtuple

import pyudev
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from pyudev.pyqt4 import QUDevMonitorObserver
from PyQt4.QtCore import QObject, pyqtSignal

def _is_mouse(device):
    return (device.sys_name.startswith('mouse') and
            device.get('ID_INPUT_MOUSE') == '1' and
            not device.get('ID_INPUT_TOUCHPAD') == '1')

class MouseDevice(namedtuple('_MouseDevice', ['serial', 'name'])):
    """
    A :class:`~collections.namedtuple` representing a mouse device.

    Currently a mouse device is represented by two attributes (the order
    corresponds to their tuple indexes):

    - ``serial``:  The serial number
    - ``name``:  The product name
    """

    @classmethod
    def from_udev(cls, device):
        """
        Create a :class:`MouseDevice` tuple from a :class:`pyudev.Device`.
        """
        # The name is available from the parent device of the actual event
        # device.  The parent represents the actual physical device.  The name
        # may be decorated with quotation marks, which are removed for the sake
        # of a clean represenation
        name = device.parent['NAME'].strip('"')
        return cls(device['ID_SERIAL'], device.parent['NAME'].strip('"'))

class MouseDevicesMonitor(QObject):
    """
    Watch for plugged or unplugged mouse devices.
    """

    #: emitted, when a mouse is plugged.  The slot gets a single argument of
    #: :class:`MouseDevice`, which represent the plugged mouse device
    mousePlugged = pyqtSignal(MouseDevice)
    #: emitted, when a mouse is unplugged.  The slot gets a single argument of
    #: :class:`MouseDevice`, which represent the unplugged mouse device
    mouseUnplugged = pyqtSignal(MouseDevice)

    def __init__(self, parent=None):
        """
        Create a new monitor.

        ``parent`` is the parent :class:`~PyQt4.QtCore.QObject`.
        """
        QObject.__init__(self, parent)
        self._udev = pyudev.Context()
        self._notifier = QUDevMonitorObserver(
            pyudev.Monitor.from_netlink(self._udev), self)
        self._notifier.deviceEvent.connect(self._handle_udev_event)
        self._notifier.monitor.filter_by('input')
        self._notifier.monitor.start()
        self._event_signal_map = dict(
            add=self.mousePlugged, remove=self.mouseUnplugged)

    @property
    def plugged_devices(self):
        """
        An iterator over all plugged mouse devices as :class:`MouseDevice`
        objects.
        """
        devices = self._udev.list_devices().match_subsystem(
            'input').match_property('ID_INPUT_MOUSE', True)
        for device in ifilter(_is_mouse, devices):
            yield MouseDevice.from_udev(device)

    def _handle_udev_event(self, evt, device):
        signal = self._event_signal_map.get(evt)
        if signal and _is_mouse(device):
            signal.emit(MouseDevice.from_udev(device))

<commit_msg>Removed unused name<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2010 Sebastian Wiesner <lunaryorn@googlemail.com>
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. 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.

# 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.

"""
    synaptiks.monitors
    ==================

    Monitor classes for various external event sources.

    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@googlemail.com>
"""

from itertools import ifilter
from collections import namedtuple

import pyudev
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from pyudev.pyqt4 import QUDevMonitorObserver
from PyQt4.QtCore import QObject, pyqtSignal

def _is_mouse(device):
    return (device.sys_name.startswith('mouse') and
            device.get('ID_INPUT_MOUSE') == '1' and
            not device.get('ID_INPUT_TOUCHPAD') == '1')

class MouseDevice(namedtuple('_MouseDevice', ['serial', 'name'])):
    """
    A :class:`~collections.namedtuple` representing a mouse device.

    Currently a mouse device is represented by two attributes (the order
    corresponds to their tuple indexes):

    - ``serial``:  The serial number
    - ``name``:  The product name
    """

    @classmethod
    def from_udev(cls, device):
        """
        Create a :class:`MouseDevice` tuple from a :class:`pyudev.Device`.
        """
        # The name is available from the parent device of the actual event
        # device.  The parent represents the actual physical device.  The name
        # may be decorated with quotation marks, which are removed for the sake
        # of a clean represenation
        return cls(device['ID_SERIAL'], device.parent['NAME'].strip('"'))

class MouseDevicesMonitor(QObject):
    """
    Watch for plugged or unplugged mouse devices.
    """

    #: emitted, when a mouse is plugged.  The slot gets a single argument of
    #: :class:`MouseDevice`, which represent the plugged mouse device
    mousePlugged = pyqtSignal(MouseDevice)
    #: emitted, when a mouse is unplugged.  The slot gets a single argument of
    #: :class:`MouseDevice`, which represent the unplugged mouse device
    mouseUnplugged = pyqtSignal(MouseDevice)

    def __init__(self, parent=None):
        """
        Create a new monitor.

        ``parent`` is the parent :class:`~PyQt4.QtCore.QObject`.
        """
        QObject.__init__(self, parent)
        self._udev = pyudev.Context()
        self._notifier = QUDevMonitorObserver(
            pyudev.Monitor.from_netlink(self._udev), self)
        self._notifier.deviceEvent.connect(self._handle_udev_event)
        self._notifier.monitor.filter_by('input')
        self._notifier.monitor.start()
        self._event_signal_map = dict(
            add=self.mousePlugged, remove=self.mouseUnplugged)

    @property
    def plugged_devices(self):
        """
        An iterator over all plugged mouse devices as :class:`MouseDevice`
        objects.
        """
        devices = self._udev.list_devices().match_subsystem(
            'input').match_property('ID_INPUT_MOUSE', True)
        for device in ifilter(_is_mouse, devices):
            yield MouseDevice.from_udev(device)

    def _handle_udev_event(self, evt, device):
        signal = self._event_signal_map.get(evt)
        if signal and _is_mouse(device):
            signal.emit(MouseDevice.from_udev(device))

<|endoftext|>


<commit_before>"""
    pyfire.contact
    ~~~~~~~~~~

    Handles Contact ("roster item") interpretation as per RFC-6121

    :copyright: 2011 by the pyfire Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

import xml.etree.ElementTree as ET

class Contact(object):
    """Jabber Contact, aka roster item"""

    __slots__ = ('approved', 'ask', 'jid', 'name', 'subscription', 'group')

    def __init__(self, jid):
        super(Contact, self).__init__()

        # required
        self.jid = jid

        # optional
        self.approved = None
        self.ask = None
        self.name = None
        self.subscription = None
        self.group = []

    def to_element(self):
        """Returns the Contact as etree.ElementTree.Element object"""

        element = ET.Element("item")
        if not self.approved == None:
            element.set("approved", self.approved)
        if not self.ask == None:
            element.set("ask", self.ask)
        element.set("jid", str(self.jid))
        if not self.name == None:
            element.set("name", self.name)
        if not self.subscription == None:
            element.set("subscription", self.subscription)
        for group in self.group:
            group_element = ET.Element("group")
            group_element.text = group
            element.append(group_element)
        return element
<commit_msg>Contacts have no resource<commit_after>"""
    pyfire.contact
    ~~~~~~~~~~

    Handles Contact ("roster item") interpretation as per RFC-6121

    :copyright: 2011 by the pyfire Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

import xml.etree.ElementTree as ET

class Contact(object):
    """Jabber Contact, aka roster item"""

    __slots__ = ('approved', 'ask', 'jid', 'name', 'subscription', 'group')

    def __init__(self, jid):
        super(Contact, self).__init__()

        # required
        self.jid = jid

        # optional
        self.approved = None
        self.ask = None
        self.name = None
        self.subscription = None
        self.group = []

    def to_element(self):
        """Returns the Contact as etree.ElementTree.Element object"""

        element = ET.Element("item")
        if not self.approved == None:
            element.set("approved", self.approved)
        if not self.ask == None:
            element.set("ask", self.ask)
        element.set("jid", str(self.jid.bare))
        if not self.name == None:
            element.set("name", self.name)
        if not self.subscription == None:
            element.set("subscription", self.subscription)
        for group in self.group:
            group_element = ET.Element("group")
            group_element.text = group
            element.append(group_element)
        return element
<|endoftext|>


<commit_before>"""
    pygments.style
    ~~~~~~~~~~~~~~

    Basic style object.

    :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from pygments.token import Token, STANDARD_TYPES

# Default mapping of ansixxx to RGB colors.
_ansimap = {
    # dark
    'ansiblack': '000000',
    'ansired': '7f0000',
    'ansigreen': '007f00',
    'ansiyellow': '7f7fe0',
    'ansiblue': '00007f',
    'ansimagenta': '7f007f',
    'ansicyan': '007f7f',
    'ansigray': 'e5e5e5',
    # normal
    'ansibrightblack': '555555',
    'ansibrightred': 'ff0000',
    'ansibrightgreen': '00ff00',
    'ansibrightyellow': 'ffff00',
    'ansibrightblue': '0000ff',
    'ansibrightmagenta': 'ff00ff',
    'ansibrightcyan': '00ffff',
    'ansiwhite': 'ffffff',
}
# mapping of deprecated #ansixxx colors to new color names
_deprecated_ansicolors = {
    # dark
    '#ansiblack': 'ansiblack',
    '#ansidarkred': 'ansired',
    '#ansidarkgreen': 'ansigreen',
    '#ansibrown': 'ansiyellow',
    '#ansidarkblue': 'ansiblue',
    '#ansipurple': 'ansimagenta',
    '#ansiteal': 'ansicyan',
    '#ansilightgray': 'ansigray',
    # normal
    '#ansidarkgray': 'ansibrightblack',
    '#ansired': 'ansibrightred',
    '#ansigreen': 'ansibrightgreen',
    '#ansiyellow': 'ansibrightyellow',
    '#ansiblue': 'ansibrightblue',
    '#ansifuchsia': 'ansibrightmagenta',
    '#ansiturquoise': 'ansibrightcyan',
    '#ansiwhite': 'ansiwhite',
}
ansicolors = set(_ansimap)

class StyleMeta(type):

    def __new__(mcs, name, bases, dct):
        obj = type.__new__(mcs, name, bases, dct)
        for token in STANDARD_TYPES:
            if token not in obj.styles:
                obj.styles[token] = ''

        def colorformat(text):
            if text in ansicolors:
                return text
            if text[0:1] == '#':
                col = text[1:]
                if len(col) == 6:
                    return col
                elif len(col) == 3:
                    return col[0] * 2 + col[1] * 2 + col[2] * 2
            elif text == '':
                return ''
            elif text.startswith('var') or text.startswith('calc'):
                return text
            assert False, "wrong color format %r" % text

        _styles = obj._styles = {}

        for ttype in obj.styles:
            for token in ttype.split():
                if token in _styles:
                    continue
                ndef = _styles.get(token.parent, None)
                styledefs = obj.styles.get(token, '').split()
                if not ndef or token is None:
                    ndef = ['', 0, 0, 0, '', '', 0, 0, 0]
                elif 'noinherit' in styledefs and token is not Token:
                    ndef = _styles[Token][:]
                else:
                    ndef = ndef[:]
                _styles[token] = ndef
                for styledef in obj.styles.get(token, '').split():
                    if styledef == 'noinherit':
                        pass
                    elif styledef == 'bold':
                        ndef[1] = 1
                    elif styledef == 'nobold':
                        ndef[1] = 0
                    elif styledef == 'italic':
                        ndef[2] = 1
                    elif styledef == 'noitalic':
                        ndef[2] = 0
                    elif styledef == 'underline':
                        ndef[3] = 1
                    elif styledef == 'nounderline':
                        ndef[3] = 0
                    elif styledef[:3] == 'bg:':
                        ndef[4] = colorformat(styledef[3:])
                    elif styledef[:7] == 'border:':
                        ndef[5] = colorformat(styledef[7:])
                    elif styledef == 'roman':
                        ndef[6] = 1
                    elif styledef == 'sans':
                        ndef[7] = 1
                    elif styledef == 'mono':
                        ndef[8] = 1
                    else:
                        ndef[0] = colorformat(styledef)

        return obj

    def style_for_token(cls, token):
        t = cls._styles[token]
        ansicolor = bgansicolor = None
        color = t[0]
        if color in _deprecated_ansicolors:
            color = _deprecated_ansicolors[color]
        if color in ansicolors:
            ansicolor = color
            color = _ansimap[color]
        bgcolor = t[4]
        if bgcolor in _deprecated_ansicolors:
            bgcolor = _deprecated_ansicolors[color]
        if bgcolor in ansicolors:
            bgansicolor = bgcolor
            bgcolor = _ansimap[bgcolor]

        return {
            'color':        color or None,
            'bold':         bool(t[1]),
            'italic':       bool(t[2]),
            'underline':    bool(t[3]),
            'bgcolor':      bgcolor or None,
            'border':       t[5] or None,
            'roman':        bool(t[6]) or None,
            'sans':         bool(t[7]) or None,
            'mono':         bool(t[8]) or None,
            'ansicolor':    ansicolor,
            'bgansicolor':  bgansicolor,
        }

    def list_styles(cls):
        return list(cls)

    def styles_token(cls, ttype):
        return ttype in cls._styles

    def __iter__(cls):
        for token in cls._styles:
            yield token, cls.style_for_token(token)

    def __len__(cls):
        return len(cls._styles)

class Style(metaclass=StyleMeta):

    #: overall background color (``None`` means transparent)
    background_color = '#ffffff'

    #: highlight background color
    highlight_color = '#ffffcc'

    #: line number font color
    line_number_color = 'inherit'

    #: line number background color
    line_number_background_color = 'transparent'

    #: special line number font color
    line_number_special_color = '#000000'

    #: special line number background color
    line_number_special_background_color = '#ffffc0'

    #: Style definitions for individual token types.
    styles = {}
<commit_msg>Don't mix foreground for background in _deprecated_ansicolors lookup (#1748)<commit_after>"""
    pygments.style
    ~~~~~~~~~~~~~~

    Basic style object.

    :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from pygments.token import Token, STANDARD_TYPES

# Default mapping of ansixxx to RGB colors.
_ansimap = {
    # dark
    'ansiblack': '000000',
    'ansired': '7f0000',
    'ansigreen': '007f00',
    'ansiyellow': '7f7fe0',
    'ansiblue': '00007f',
    'ansimagenta': '7f007f',
    'ansicyan': '007f7f',
    'ansigray': 'e5e5e5',
    # normal
    'ansibrightblack': '555555',
    'ansibrightred': 'ff0000',
    'ansibrightgreen': '00ff00',
    'ansibrightyellow': 'ffff00',
    'ansibrightblue': '0000ff',
    'ansibrightmagenta': 'ff00ff',
    'ansibrightcyan': '00ffff',
    'ansiwhite': 'ffffff',
}
# mapping of deprecated #ansixxx colors to new color names
_deprecated_ansicolors = {
    # dark
    '#ansiblack': 'ansiblack',
    '#ansidarkred': 'ansired',
    '#ansidarkgreen': 'ansigreen',
    '#ansibrown': 'ansiyellow',
    '#ansidarkblue': 'ansiblue',
    '#ansipurple': 'ansimagenta',
    '#ansiteal': 'ansicyan',
    '#ansilightgray': 'ansigray',
    # normal
    '#ansidarkgray': 'ansibrightblack',
    '#ansired': 'ansibrightred',
    '#ansigreen': 'ansibrightgreen',
    '#ansiyellow': 'ansibrightyellow',
    '#ansiblue': 'ansibrightblue',
    '#ansifuchsia': 'ansibrightmagenta',
    '#ansiturquoise': 'ansibrightcyan',
    '#ansiwhite': 'ansiwhite',
}
ansicolors = set(_ansimap)

class StyleMeta(type):

    def __new__(mcs, name, bases, dct):
        obj = type.__new__(mcs, name, bases, dct)
        for token in STANDARD_TYPES:
            if token not in obj.styles:
                obj.styles[token] = ''

        def colorformat(text):
            if text in ansicolors:
                return text
            if text[0:1] == '#':
                col = text[1:]
                if len(col) == 6:
                    return col
                elif len(col) == 3:
                    return col[0] * 2 + col[1] * 2 + col[2] * 2
            elif text == '':
                return ''
            elif text.startswith('var') or text.startswith('calc'):
                return text
            assert False, "wrong color format %r" % text

        _styles = obj._styles = {}

        for ttype in obj.styles:
            for token in ttype.split():
                if token in _styles:
                    continue
                ndef = _styles.get(token.parent, None)
                styledefs = obj.styles.get(token, '').split()
                if not ndef or token is None:
                    ndef = ['', 0, 0, 0, '', '', 0, 0, 0]
                elif 'noinherit' in styledefs and token is not Token:
                    ndef = _styles[Token][:]
                else:
                    ndef = ndef[:]
                _styles[token] = ndef
                for styledef in obj.styles.get(token, '').split():
                    if styledef == 'noinherit':
                        pass
                    elif styledef == 'bold':
                        ndef[1] = 1
                    elif styledef == 'nobold':
                        ndef[1] = 0
                    elif styledef == 'italic':
                        ndef[2] = 1
                    elif styledef == 'noitalic':
                        ndef[2] = 0
                    elif styledef == 'underline':
                        ndef[3] = 1
                    elif styledef == 'nounderline':
                        ndef[3] = 0
                    elif styledef[:3] == 'bg:':
                        ndef[4] = colorformat(styledef[3:])
                    elif styledef[:7] == 'border:':
                        ndef[5] = colorformat(styledef[7:])
                    elif styledef == 'roman':
                        ndef[6] = 1
                    elif styledef == 'sans':
                        ndef[7] = 1
                    elif styledef == 'mono':
                        ndef[8] = 1
                    else:
                        ndef[0] = colorformat(styledef)

        return obj

    def style_for_token(cls, token):
        t = cls._styles[token]
        ansicolor = bgansicolor = None
        color = t[0]
        if color in _deprecated_ansicolors:
            color = _deprecated_ansicolors[color]
        if color in ansicolors:
            ansicolor = color
            color = _ansimap[color]
        bgcolor = t[4]
        if bgcolor in _deprecated_ansicolors:
            bgcolor = _deprecated_ansicolors[bgcolor]
        if bgcolor in ansicolors:
            bgansicolor = bgcolor
            bgcolor = _ansimap[bgcolor]

        return {
            'color':        color or None,
            'bold':         bool(t[1]),
            'italic':       bool(t[2]),
            'underline':    bool(t[3]),
            'bgcolor':      bgcolor or None,
            'border':       t[5] or None,
            'roman':        bool(t[6]) or None,
            'sans':         bool(t[7]) or None,
            'mono':         bool(t[8]) or None,
            'ansicolor':    ansicolor,
            'bgansicolor':  bgansicolor,
        }

    def list_styles(cls):
        return list(cls)

    def styles_token(cls, ttype):
        return ttype in cls._styles

    def __iter__(cls):
        for token in cls._styles:
            yield token, cls.style_for_token(token)

    def __len__(cls):
        return len(cls._styles)

class Style(metaclass=StyleMeta):

    #: overall background color (``None`` means transparent)
    background_color = '#ffffff'

    #: highlight background color
    highlight_color = '#ffffcc'

    #: line number font color
    line_number_color = 'inherit'

    #: line number background color
    line_number_background_color = 'transparent'

    #: special line number font color
    line_number_special_color = '#000000'

    #: special line number background color
    line_number_special_background_color = '#ffffc0'

    #: Style definitions for individual token types.
    styles = {}
<|endoftext|>


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

import re, time
import errors

# arch: pacman -S python-pyserial
# debian: apt-get install pyserial
import serial

class GsmModem(object):

    # override these after init, and
    # before boot. they're not sanity
    # checked, so go crazy.
    baud             = 9600
    cmd_delay        = 0.1
    device_timeout   = 10
    reset_on_failure = True

    def __init__(self, port_or_device):

        # accept an existing serial port object,
        # for modems which require non-default
        # parameters or init strings (rather than
        # wrapping all those calls opaquely here)
        if isinstance(port_or_device, serial.Serial):
            self.device = port_or_device

        # otherwise, assume that the single argument
        # is a string containing the serial port that
        # we should connect to
        else:
            self.port = port

    def boot(self):
        """Initializes the modem. Must be called after init, but
           before doing anything that expects the modem to be ready."""

        # create the conection to the modem,
        # if it hasn't already been done
        if not self.device:
            self.device = serial.Serial(self.port)

        # set some sensible defaults, to make
        # the various modems more consistant
        self.command("AT+CFUN=1") # reset the modem
        self.command("ATE0")      # echo off
        self.command("AT+CMEE=1") # useful error messages
        self.command("AT+WIND=0") # disable notifications
        self.command("AT+CMGF=1") # switch to TEXT mode

    def _write(self, str):
        """Write a string to the modem."""
        print "WRITE: %r" % str
        self.device.write(str)

    def _read(self, read_term=None, read_timeout=None):
        """Read from the modem (blocking) until _terminator_ is hit,
           (defaults to \r\n, which reads a single "line"), and return."""
        buffer = []

        # if a different timeout was requested just
        # for _this_ read, store and override the
        # current device setting (not thread safe!)
        if read_timeout is not None:
            old_timeout = self.device.timeout
            self.device.timeout = read_timeout

        def __reset_timeout():
            """restore the device's previous timeout
               setting, if we overrode it earlier."""
            if read_timeout is not None:
                self.device.timeout =\
                    old_timeout

        # the default terminator reads
        # until a newline is hit
        if not read_term:
            read_term = "\r\n"

        while(True):
            buf = self.device.read()
            print "READ: %r" % buf
            buffer.append(buf)

            # if a timeout was hit, raise an exception including the raw data that
            # we've already read (in case the calling func was _expecting_ a timeout
            # (wouldn't it be nice if serial.Serial.read returned None for this?)
            if buf == "":
                __reset_timeout()
                print "_Read Timeout: %r" % (buffer)
                raise(errors.GsmReadTimeoutError(buffer))

            # if last n characters of the buffer match the read
            # terminator, return what we've received so far
            if buffer[-len(read_term)::] == list(read_term):
                buf_str = "".join(buffer).strip()
                print "_Read: %r" % buf_str

                __reset_timeout()
                return buf_str

    def wait(self, read_term=None, read_timeout=None):
        print "Waiting for response"
        buffer = []

        while(True):
            buf = self._read(
                read_term=read_term,
                read_timeout=read_timeout)

            buffer.append(buf)

            # if a timeout was hit during
            # _read, just return what we have
            if buf is None:
                return buffer

            if(buf=="OK"):
                return buffer

            # some errors contain useful error codes, so raise a
            # proper error with a description from pygsm/errors.py
            m = re.match(r"^\+(CM[ES]) ERROR: (\d+)$", buf)
            if m is not None:
                type, code = m.groups()
                raise(errors.GsmModemError(type, int(code)))

            # ...some errors are not so useful
            # (at+cmee=1 should enable error codes)
            if buf=="ERROR":
                raise(errors.GsmModemError)

    def command(self, cmd, read_term=None, read_timeout=None, write_term="\r"):
        print "Command: %r" % cmd

        # TODO: lock the modem
        self._write(cmd + write_term)
        lines = self.wait(
            read_term=read_term,
            read_timeout=read_timeout)

        # remove all blank lines and unsolicited
        # status messages. i can't seem to figure
        # out how to reliably disable them, and
        # AT+WIND=0 doesn't work on this modem
        lines = [
            line
            for line in lines
            if line      != "" or\
               line[0:6] == "+WIND:" or\
               line[0:6] == "+CREG:" or\
               line[0:7] == "+CGRED:"]

        # rest up for a bit (modems are
        # slow, and get confused easily)
        time.sleep(self.cmd_delay)

        print "Output: %s" % lines
        return lines

    def query(self, cmd):
        print "Query: %r" % cmd
        out = self.command(cmd)

        # the only valid response to a "query" is a
        # single line followed by "OK". if all looks
        # well, return just the single line
        if(len(out) == 2) and (out[-1] == "OK"):
            return out[0]

        # something went wrong, so return the very
        # ambiguous None. it's better than blowing up
        return None

    def receive(self, callback):
        pass

    def hardware(self):
        """Returns a dict of containing information about the physical
           modem. The contents of each value are entirely manufacturer
           dependant, and vary wildly between devices."""

        return {
            "manufacturer": self.query("AT+CGMI"),
            "model":        self.query("AT+CGMM"),
            "revision":     self.query("AT+CGMR"),
            "serial":       self.query("AT+CGSN") }

    def signal_strength(self):
        """Returns an integer between 1 and 99, representing the current
           signal strength of the GSM network, False if we don't know, or
           None if the modem can't report it."""

        data = self.query("AT+CSQ")
        md = re.match(r"^\+CSQ: (\d+),", data)

        # 99 represents "not known or not detectable". we'll
        # return False for that (so we can test it for boolean
        # equality), or an integer of the signal strength.
        if md is not None:
            csq = int(md.group(1))
            return csq if csq < 99 else False

        # the response from AT+CSQ couldn't be parsed. return
        # None, so we can test it in the same way as False, but
        # check the type without raising an exception
        return None

    def wait_for_network(self):
        """Blocks until the signal strength indicates that the
           device is active on the GSM network. It's a good idea
           to call this before trying to send or receive anything."""

        while True:
            csq = self.signal_strength()
            if csq: return csq
            time.sleep(1)
<commit_msg>added GsmModem.send_sms<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import re, time
import errors

# arch: pacman -S python-pyserial
# debian: apt-get install pyserial
import serial

class GsmModem(object):

    # override these after init, and
    # before boot. they're not sanity
    # checked, so go crazy.
    baud             = 9600
    cmd_delay        = 0.1
    device_timeout   = 10
    reset_on_failure = True

    def __init__(self, port_or_device):

        # accept an existing serial port object,
        # for modems which require non-default
        # parameters or init strings (rather than
        # wrapping all those calls opaquely here)
        if isinstance(port_or_device, serial.Serial):
            self.device = port_or_device

        # otherwise, assume that the single argument
        # is a string containing the serial port that
        # we should connect to
        else:
            self.port = port

    def boot(self):
        """Initializes the modem. Must be called after init, but
           before doing anything that expects the modem to be ready."""

        # create the conection to the modem,
        # if it hasn't already been done
        if not self.device:
            self.device = serial.Serial(self.port)

        # set some sensible defaults, to make
        # the various modems more consistant
        self.command("AT+CFUN=1") # reset the modem
        self.command("ATE0")      # echo off
        self.command("AT+CMEE=1") # useful error messages
        self.command("AT+WIND=0") # disable notifications
        self.command("AT+CMGF=1") # switch to TEXT mode

    def _write(self, str):
        """Write a string to the modem."""
        print "WRITE: %r" % str
        self.device.write(str)

    def _read(self, read_term=None, read_timeout=None):
        """Read from the modem (blocking) until _terminator_ is hit,
           (defaults to \r\n, which reads a single "line"), and return."""
        buffer = []

        # if a different timeout was requested just
        # for _this_ read, store and override the
        # current device setting (not thread safe!)
        if read_timeout is not None:
            old_timeout = self.device.timeout
            self.device.timeout = read_timeout

        def __reset_timeout():
            """restore the device's previous timeout
               setting, if we overrode it earlier."""
            if read_timeout is not None:
                self.device.timeout =\
                    old_timeout

        # the default terminator reads
        # until a newline is hit
        if not read_term:
            read_term = "\r\n"

        while(True):
            buf = self.device.read()
            print "READ: %r" % buf
            buffer.append(buf)

            # if a timeout was hit, raise an exception including the raw data that
            # we've already read (in case the calling func was _expecting_ a timeout
            # (wouldn't it be nice if serial.Serial.read returned None for this?)
            if buf == "":
                __reset_timeout()
                print "_Read Timeout: %r" % (buffer)
                raise(errors.GsmReadTimeoutError(buffer))

            # if last n characters of the buffer match the read
            # terminator, return what we've received so far
            if buffer[-len(read_term)::] == list(read_term):
                buf_str = "".join(buffer).strip()
                print "_Read: %r" % buf_str

                __reset_timeout()
                return buf_str

    def wait(self, read_term=None, read_timeout=None):
        print "Waiting for response"
        buffer = []

        while(True):
            buf = self._read(
                read_term=read_term,
                read_timeout=read_timeout)

            buffer.append(buf)

            # if a timeout was hit during
            # _read, just return what we have
            if buf is None:
                return buffer

            if(buf=="OK"):
                return buffer

            # some errors contain useful error codes, so raise a
            # proper error with a description from pygsm/errors.py
            m = re.match(r"^\+(CM[ES]) ERROR: (\d+)$", buf)
            if m is not None:
                type, code = m.groups()
                raise(errors.GsmModemError(type, int(code)))

            # ...some errors are not so useful
            # (at+cmee=1 should enable error codes)
            if buf=="ERROR":
                raise(errors.GsmModemError)

    def command(self, cmd, read_term=None, read_timeout=None, write_term="\r"):
        print "Command: %r" % cmd

        # TODO: lock the modem
        self._write(cmd + write_term)
        lines = self.wait(
            read_term=read_term,
            read_timeout=read_timeout)

        # remove all blank lines and unsolicited
        # status messages. i can't seem to figure
        # out how to reliably disable them, and
        # AT+WIND=0 doesn't work on this modem
        lines = [
            line
            for line in lines
            if line      != "" or\
               line[0:6] == "+WIND:" or\
               line[0:6] == "+CREG:" or\
               line[0:7] == "+CGRED:"]

        # rest up for a bit (modems are
        # slow, and get confused easily)
        time.sleep(self.cmd_delay)

        print "Output: %s" % lines
        return lines

    def query(self, cmd):
        print "Query: %r" % cmd
        out = self.command(cmd)

        # the only valid response to a "query" is a
        # single line followed by "OK". if all looks
        # well, return just the single line
        if(len(out) == 2) and (out[-1] == "OK"):
            return out[0]

        # something went wrong, so return the very
        # ambiguous None. it's better than blowing up
        return None

    def send_sms(self, recipient, text):
        print "MSG to %r: %r" % (recipient, text)

        # TODO: lock the modem

        try:
            try:

                # initiate the sms, and give the device a second
                # to raise an error. unfortunately, we can't just
                # wait for the "> " prompt, because some modems
                # will echo it FOLLOWED BY a CMS error
                self.command(
                    "AT+CMGS=\"%s\"\r" % (recipient),
                    read_timeout=1)

            # if no error is raised within the timeout period,
            # and the text-mode prompt WAS received, send the
            # sms text, wait until it is accepted or rejected
            # (text-mode messages are terminated with ascii char 26
            # "SUBSTITUTE" (ctrl+z)), and return True (message sent)
            except errors.GsmReadTimeoutError, err:
                if err.pending_data[0] == ">":
                    self.command(text, write_term=chr(26))
                    return True

                # a timeout was raised, but no prompt nor
                # error was received. i have no idea what
                # is going on, so allow the error to propagate
                else: raise

        # for all other errors...
        # (likely CMS or CME from device)
        except:

            # whatever went wrong, break out of the
            # message prompt. if this is missed, all
            # subsequent writes will go into the message!
            self._write(chr(27))

            # rule of thumb: pygsm is meant to be embedded,
            # so DO NOT EVER allow exceptions to propagate
            # (obviously, this sucks. there should be an
            # option, at least, but i'm being cautious)
            return None

    def receive(self, callback):
        pass

    def hardware(self):
        """Returns a dict of containing information about the physical
           modem. The contents of each value are entirely manufacturer
           dependant, and vary wildly between devices."""

        return {
            "manufacturer": self.query("AT+CGMI"),
            "model":        self.query("AT+CGMM"),
            "revision":     self.query("AT+CGMR"),
            "serial":       self.query("AT+CGSN") }

    def signal_strength(self):
        """Returns an integer between 1 and 99, representing the current
           signal strength of the GSM network, False if we don't know, or
           None if the modem can't report it."""

        data = self.query("AT+CSQ")
        md = re.match(r"^\+CSQ: (\d+),", data)

        # 99 represents "not known or not detectable". we'll
        # return False for that (so we can test it for boolean
        # equality), or an integer of the signal strength.
        if md is not None:
            csq = int(md.group(1))
            return csq if csq < 99 else False

        # the response from AT+CSQ couldn't be parsed. return
        # None, so we can test it in the same way as False, but
        # check the type without raising an exception
        return None

    def wait_for_network(self):
        """Blocks until the signal strength indicates that the
           device is active on the GSM network. It's a good idea
           to call this before trying to send or receive anything."""

        while True:
            csq = self.signal_strength()
            if csq: return csq
            time.sleep(1)
<|endoftext|>


<commit_before>import sys
import urllib2
import base64
import json

VERSION = '1'
API_URL = "http://kbase.us/services/communities/"+VERSION
ID_URL  = 'http://www.kbase.us/services/idserver'
OAUTH_URL = 'https://nexus.api.globusonline.org/goauth/token?grant_type=client_credentials'
AUTH_LIST = "Jared Bischof, Travis Harrison, Folker Meyer, Tobias Paczian, Andreas Wilke"

def obj_from_url(url, auth=None, debug=False):
    header = {'Accept': 'application/json'}
    if auth:
        header['Auth'] = auth
    if debug:
        print json.dumps(header)
        print url
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned\n" %url)
        sys.exit(1)
    obj = json.loads(res.read())
    if obj is None:
        sys.stderr.write("ERROR (%s): return structure not valid json format\n" %url)
        sys.exit(1)
    if len(obj.keys()) == 0:
        sys.stderr.write("ERROR (%s): no data available\n" %url)
        sys.exit(1)
    if 'ERROR' in obj:
        sys.stderr.write("ERROR (%s): %s\n" %(url, obj['ERROR']))
        sys.exit(1)
    return obj

def stout_from_url(url, auth=None, debug=False):
    header = {'Accept': 'text/plain'}
    if auth:
        header['Auth'] = auth
    if debug:
        print json.dumps(header)
        print url
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned\n"%url)
        sys.exit(1)
    while True:
        chunk = res.read(8192)
        if not chunk:
            break
        sys.stderr.write(chunk)

def sparse_to_dense(sMatrix, rmax, cmax):
    dMatrix = [[0 for i in range(cmax)] for j in range(rmax)]
    for sd in sMatrix:
        r, c, v = sd
        dMatrix[r][c] = v
    return dMatrix

def biom_to_tab(biom, hdl):
    matrix = sparse_to_dense(biom['data'], biom['shape'][0], biom['shape'][1])
    hdl.write( "\t%s\n" %"\t".join([c['id'] for c in biom['columns']]) )
    for i, row in enumerate(matrix):
        hdl.write( "%s\t%s\n" %(biom['rows'][i], "\t".join([str(r) for r in row])) )

def kbid_to_mgid(kbid):
    id_map = kbid_lookup([kbid])
    if kbid not in id_map:
        sys.stderr.write("ERROR: (%s) not a valid KBase ID\n" %kbid)
        sys.exit(1)
    return id_map[kbid]

def kbid_lookup(kbids):
    params = {'method': 'IDServerAPI.kbase_ids_to_external_ids', 'params': [kbids], 'version': '1.1'}
    data = json.dumps(params)
    try:
        req = urllib2.Request(ID_URL, data)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(ID_URL, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned for ids (%s)\n" %(ID_URL, ','.join(kbids)))
        sys.exit(1)
    obj = json.loads(res.read())
    if obj is None:
        sys.stderr.write("ERROR (%s): return structure not valid json format\n" %ID_URL)
        sys.exit(1)
    if 'error' in obj:
        sys.stderr.write("ERROR: %s\n" %obj['error']['message'])
        sys.exit(1)
    if ('result' not in obj) or (len(obj['result'][0].keys()) == 0):
        sys.stderr.write("ERROR (%s): no results returned for ids (%s)\n" %(ID_URL, ','.join(kbids)))
        sys.exit(1)
    return dict([(k, obj['result'][0][k][1]) for k in obj['result'][0].keys()])

def get_auth_token(opts):
    if opts.token:
        return opts.token
    elif opts.user or opts.passwd:
        if opts.user and opts.passwd:
            return token_from_login(opts.user, opts.passwd)
        else:
            sys.stderr.write("ERROR: both username and password are required\n")
            sys.exit(1)
    else:
        return None

def token_from_login(user, passwd, url=OAUTH_URL):
    base64string = base64.b64encode('%s:%s' %(user, passwd)).replace('\n', '')
    header = {'Authorization': 'Basic %s'%base64string}
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR: could not reach auth server\n")
        sys.exit(1)
    obj = json.loads(res.read())
    if (obj is None) or (len(obj.keys()) == 0) or ('access_token' not in obj):
        sys.stderr.write("ERROR: authentication failed\n")
        sys.exit(1)
    return obj['access_token']

<commit_msg>updated id lookup, fixed some formatting<commit_after>import sys
import urllib2
import base64
import json

VERSION = '1'
API_URL = "http://kbase.us/services/communities/"+VERSION
ID_URL  = 'http://www.kbase.us/services/idserver'
OAUTH_URL = 'https://nexus.api.globusonline.org/goauth/token?grant_type=client_credentials'
AUTH_LIST = "Jared Bischof, Travis Harrison, Folker Meyer, Tobias Paczian, Andreas Wilke"

# return python struct from JSON output of MG-RAST API
def obj_from_url(url, auth=None, debug=False):
    header = {'Accept': 'application/json'}
    if auth:
        header['Auth'] = auth
    if debug:
        print json.dumps(header)
        print url
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned\n" %url)
        sys.exit(1)
    obj = json.loads(res.read())
    if obj is None:
        sys.stderr.write("ERROR (%s): return structure not valid json format\n" %url)
        sys.exit(1)
    if len(obj.keys()) == 0:
        sys.stderr.write("ERROR (%s): no data available\n" %url)
        sys.exit(1)
    if 'ERROR' in obj:
        sys.stderr.write("ERROR (%s): %s\n" %(url, obj['ERROR']))
        sys.exit(1)
    return obj

# print to stdout results of MG-RAST API
def stout_from_url(url, auth=None, debug=False):
    header = {'Accept': 'text/plain'}
    if auth:
        header['Auth'] = auth
    if debug:
        print json.dumps(header)
        print url
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned\n"%url)
        sys.exit(1)
    while True:
        chunk = res.read(8192)
        if not chunk:
            break
        sys.stderr.write(chunk)

# transform sparse matrix to dense matrix (2D array)
def sparse_to_dense(sMatrix, rmax, cmax):
    dMatrix = [[0 for i in range(cmax)] for j in range(rmax)]
    for sd in sMatrix:
        r, c, v = sd
        dMatrix[r][c] = v
    return dMatrix

# transform BIIOM format to tabbed table
def biom_to_tab(biom, hdl):
    matrix = sparse_to_dense(biom['data'], biom['shape'][0], biom['shape'][1])
    hdl.write( "\t%s\n" %"\t".join([c['id'] for c in biom['columns']]) )
    for i, row in enumerate(matrix):
        hdl.write( "%s\t%s\n" %(biom['rows'][i]['id'], "\t".join([str(r) for r in row])) )

# return MG-RAST id for given KBase id
def kbid_to_mgid(kbid):
    id_map = kbid_lookup([kbid])
    if kbid not in id_map:
        sys.stderr.write("ERROR: (%s) not a valid KBase ID\n" %kbid)
        sys.exit(1)
    return id_map[kbid]

# return list of MG-RAST ids for given list of KBase ids
#  handels mixed ids / all mgrast ids
def kbids_to_mgids(kbids):
    id_map = kbid_lookup(kbids)
    mgids = []
    for i in kbids:
        if i in id_map:
            mgids.append(id_map[i])
        else:
            mgids.append(i)
    return mgids

# return map (KBase id -> MG-RAST id) for given list of KBase ids
def kbid_lookup(kbids):
    params = {'method': 'IDServerAPI.kbase_ids_to_external_ids', 'params': [kbids], 'version': '1.1'}
    data = json.dumps(params)
    try:
        req = urllib2.Request(ID_URL, data)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(ID_URL, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR (%s): no results returned for ids (%s)\n" %(ID_URL, ','.join(kbids)))
        sys.exit(1)
    obj = json.loads(res.read())
    if obj is None:
        sys.stderr.write("ERROR (%s): return structure not valid json format\n" %ID_URL)
        sys.exit(1)
    if 'error' in obj:
        sys.stderr.write("ERROR: %s\n" %obj['error']['message'])
        sys.exit(1)
    if len(obj['result'][0].keys()) == 0:
        return {}
    else:
        return dict([(k, obj['result'][0][k][1]) for k in obj['result'][0].keys()])

def get_auth_token(opts):
    if opts.token:
        return opts.token
    elif opts.user or opts.passwd:
        if opts.user and opts.passwd:
            return token_from_login(opts.user, opts.passwd)
        else:
            sys.stderr.write("ERROR: both username and password are required\n")
            sys.exit(1)
    else:
        return None

def token_from_login(user, passwd, url=OAUTH_URL):
    base64string = base64.b64encode('%s:%s' %(user, passwd)).replace('\n', '')
    header = {'Authorization': 'Basic %s'%base64string}
    try:
        req = urllib2.Request(url, headers=header)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError, error:
        sys.stderr.write("ERROR (%s):%s, %s\n" %(url, error.code, error.read()))
        sys.exit(1)
    if not res:
        sys.stderr.write("ERROR: could not reach auth server\n")
        sys.exit(1)
    obj = json.loads(res.read())
    if (obj is None) or (len(obj.keys()) == 0) or ('access_token' not in obj):
        sys.stderr.write("ERROR: authentication failed\n")
        sys.exit(1)
    return obj['access_token']

<|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

import uuid
import datetime
import hashlib
import random

class Config(models.Model):
    """Represent a website configuration"""

    name = models.CharField(max_length=255)

    active = models.BooleanField(default=True)
    admin_enable = models.BooleanField(default=True)

    test_mode = models.BooleanField(default=True)

    url_ipn = models.URLField()
    url_back_ok = models.URLField()
    url_back_err = models.URLField()

    key_request = models.CharField(max_length=255)
    key_ipn = models.CharField(max_length=255)
    key_api = models.CharField(max_length=255)

    allowed_users = models.ManyToManyField(User, blank=True)

    group = models.CharField(max_length=255, help_text=_('To group configs in summary'), blank=True, null=True)

    def __unicode__(self):
        bonus = u''

        if self.test_mode:
            bonus += u''

        if not self.active or not self.admin_enable:
            bonus += u''

        if bonus:
            bonus = u' (' + bonus + u')'

        return escape(self.name) + bonus

    def build_user_list(self):
        """Return a list of user in text format"""
        return u','.join([user.username for user in self.allowed_users.order_by('username')])

    def generate_diff(self, other):
        """Generate diff from this objet an another one (for logs)"""
        retour = u'\n\n'

        for (prop, prop_name) in (('name', 'Name'), ('active', 'Active'), ('admin_enable', 'Admin enable'), ('test_mode', 'Test mode'), ('url_ipn', 'URL Ipn'), ('url_back_ok', 'Return URL for success'), ('url_back_err', 'Return URL for error'), ('group', 'Group')):
            if not other.pk or getattr(self, prop) != getattr(other, prop):
                retour += unicode(prop_name) + u'=' + unicode(getattr(self, prop))

                if other.pk:
                    retour += _(u' (was ') + unicode(getattr(other, prop)) + u')'

                retour += u'\n'

        retour += _(u'User list: ') + self.build_user_list()
        return retour

    def is_user_allowed(self, user):
        """Return true is a user is allowed to display / edit the config"""

        if user.is_superuser:
            return True
        elif not self.pk:
            return user.is_staff  # Only staff can create configs
        else:
            return user in self.allowed_users

    def gen_key(self):
        """Return a random key suitable for keys of the model"""

        h = hashlib.sha512()

        for i in range(2):
            h.update(str(random.SystemRandom().random()))
            h.update(str(uuid.uuid4()))
            h.update(str(datetime.datetime.now()))
            h.update(str(self.pk))
            h.update(self.name)

        return h.hexdigest()

    def gen_key_api(self):
        """Generate a new key for api"""
        self.key_api = self.gen_key()

    def gen_key_ipn(self):
        """Generate a new key for ipn"""
        self.key_ipn = self.gen_key()

    def gen_key_request(self):
        """Generate a new key for requests"""
        self.key_request = self.gen_key()

    def save(self, *args, **kwargs):
        """Overide the save request to check if all keys have been generated"""

        if not self.key_api:
            self.gen_key_api()

        if not self.key_ipn:
            self.gen_key_ipn()

        if not self.key_request:
            self.gen_key_request()

        super(Config, self).save(*args, **kwargs)

class ConfigLogs(models.Model):

    config = models.ForeignKey(Config)
    user = models.ForeignKey(User)
    when = models.DateTimeField(auto_now_add=True)
    text = models.TextField()
<commit_msg>Wrong user test<commit_after>from django.db import models
from django.contrib.auth.models import User
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _

import uuid
import datetime
import hashlib
import random

class Config(models.Model):
    """Represent a website configuration"""

    name = models.CharField(max_length=255)

    active = models.BooleanField(default=True)
    admin_enable = models.BooleanField(default=True)

    test_mode = models.BooleanField(default=True)

    url_ipn = models.URLField()
    url_back_ok = models.URLField()
    url_back_err = models.URLField()

    key_request = models.CharField(max_length=255)
    key_ipn = models.CharField(max_length=255)
    key_api = models.CharField(max_length=255)

    allowed_users = models.ManyToManyField(User, blank=True)

    group = models.CharField(max_length=255, help_text=_('To group configs in summary'), blank=True, null=True)

    def __unicode__(self):
        bonus = u''

        if self.test_mode:
            bonus += u''

        if not self.active or not self.admin_enable:
            bonus += u''

        if bonus:
            bonus = u' (' + bonus + u')'

        return escape(self.name) + bonus

    def build_user_list(self):
        """Return a list of user in text format"""
        return u','.join([user.username for user in self.allowed_users.order_by('username')])

    def generate_diff(self, other):
        """Generate diff from this objet an another one (for logs)"""
        retour = u'\n\n'

        for (prop, prop_name) in (('name', 'Name'), ('active', 'Active'), ('admin_enable', 'Admin enable'), ('test_mode', 'Test mode'), ('url_ipn', 'URL Ipn'), ('url_back_ok', 'Return URL for success'), ('url_back_err', 'Return URL for error'), ('group', 'Group')):
            if not other.pk or getattr(self, prop) != getattr(other, prop):
                retour += unicode(prop_name) + u'=' + unicode(getattr(self, prop))

                if other.pk:
                    retour += _(u' (was ') + unicode(getattr(other, prop)) + u')'

                retour += u'\n'

        retour += _(u'User list: ') + self.build_user_list()
        return retour

    def is_user_allowed(self, user):
        """Return true is a user is allowed to display / edit the config"""

        if user.is_superuser:
            return True
        elif not self.pk:
            return user.is_staff  # Only staff can create configs
        else:
            return user in list(self.allowed_users.all())

    def gen_key(self):
        """Return a random key suitable for keys of the model"""

        h = hashlib.sha512()

        for i in range(2):
            h.update(str(random.SystemRandom().random()))
            h.update(str(uuid.uuid4()))
            h.update(str(datetime.datetime.now()))
            h.update(str(self.pk))
            h.update(self.name)

        return h.hexdigest()

    def gen_key_api(self):
        """Generate a new key for api"""
        self.key_api = self.gen_key()

    def gen_key_ipn(self):
        """Generate a new key for ipn"""
        self.key_ipn = self.gen_key()

    def gen_key_request(self):
        """Generate a new key for requests"""
        self.key_request = self.gen_key()

    def save(self, *args, **kwargs):
        """Overide the save request to check if all keys have been generated"""

        if not self.key_api:
            self.gen_key_api()

        if not self.key_ipn:
            self.gen_key_ipn()

        if not self.key_request:
            self.gen_key_request()

        super(Config, self).save(*args, **kwargs)

class ConfigLogs(models.Model):

    config = models.ForeignKey(Config)
    user = models.ForeignKey(User)
    when = models.DateTimeField(auto_now_add=True)
    text = models.TextField()
<|endoftext|>


<commit_before>from pyhdl.wire import *
from pyhdl.primitives import *
from pyhdl.declarative import Module

__all__ = [
    'Wire',
    'SubWire',
    'Module',
    'NandGate',
    'AndGate',
    'NorGate',
    'OrGate',
    'XorGate',
    'NotGate',
    'DFF'
]<commit_msg>Updated __init__.py<commit_after>from pyhdl.wire import *
from pyhdl.primitives import *
from pyhdl.declarative import Module

__all__ = [
    'Wire',
    'SubWire',
    'ConstantWire',
    'Module',
    'NandGate',
    'AndGate',
    'NorGate',
    'OrGate',
    'XorGate',
    'NotGate',
    'Multiplexer',
    'Demultiplexer',
    'HalfAdder',
    'FullAdder',
    'Adder',
    'DFF'
]<|endoftext|>


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

"""Version Handling"""

from __future__ import with_statement

import os
import sys

__VERSION__ = "0.6.0"

def current_git_hash():
    """Return the current git hash"""
    git_file = ".git/refs/heads/master"
    git_path = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
            os.pardir, os.pardir, git_file))

    if not os.path.exists(git_path):
        git_path = os.getcwd() + "/" + git_file
        if not os.path.exists(git_path):
            git_path = os.getcwd() + "/../" + git_file
            if not os.path.exists(git_path):
                return None

    with open(git_path, "r") as git:
        git_hash = git.read()

    return git_hash[0:5]

def version_string():
    """Return the full version"""
    git_hash = current_git_hash()
    if git_hash:
        return "pyhole v%s (%s) - http://pyhole.org" % (__VERSION__, git_hash)

    return "pyhole v%s - http://pyhole.org" % __VERSION__

def version_hash():
    """Return the current version with git hash"""
    git_hash = current_git_hash()
    return "%s-%s" % (__VERSION__, git_hash)

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

"""Version Handling"""

from __future__ import with_statement

import os
import sys

__VERSION__ = "0.6.1"

def current_git_hash():
    """Return the current git hash"""
    git_file = ".git/refs/heads/master"
    git_path = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
            os.pardir, os.pardir, git_file))

    if not os.path.exists(git_path):
        git_path = os.getcwd() + "/" + git_file
        if not os.path.exists(git_path):
            git_path = os.getcwd() + "/../" + git_file
            if not os.path.exists(git_path):
                return None

    with open(git_path, "r") as git:
        git_hash = git.read()

    return git_hash[0:5]

def version_string():
    """Return the full version"""
    git_hash = current_git_hash()
    if git_hash:
        return "pyhole v%s (%s) - http://pyhole.org" % (__VERSION__, git_hash)

    return "pyhole v%s - http://pyhole.org" % __VERSION__

def version_hash():
    """Return the current version with git hash"""
    git_hash = current_git_hash()
    return "%s-%s" % (__VERSION__, git_hash)

def version():
    """Return the current version"""
    return __VERSION__
<|endoftext|>


<commit_before>"""
Module containing the Train class and support functionality.
"""
__authors__ = "Ian Goodfellow"
__copyright__ = "Copyright 2010-2012, Universite de Montreal"
__credits__ = ["Ian Goodfellow"]
__license__ = "3-clause BSD"
__maintainer__ = "Ian Goodfellow"
__email__ = "goodfeli@iro"
from datetime import datetime
import os
import sys
from pylearn2.utils import serial
import logging
import warnings
from pylearn2.monitor import Monitor
from pylearn2.space import NullSpace
from pylearn2.utils.timing import log_timing, total_seconds
from pylearn2.utils import sharedX

log = logging.getLogger(__name__)

class Train(object):
    """
    A class representing the main loop of the training script.  Trains the
    specified model using the specified algorithm on the specified dataset.
    After each call to the training algorithm, the model is saved to save_path.
    May be enhanced with TrainExtension plugins.
    """
    def __init__(self, dataset, model, algorithm=None, save_path=None,
                 save_freq=0, extensions=None, allow_overwrite=True):
        """
        Construct a Train instance.

        Parameters
        ----------
        dataset : pylearn2.datasets.dataset.Dataset
        model : pylearn2.models.model.Model
        algorithm : <Optional>
        pylearn2.training_algorithms.training_algorithm.TrainingAlgorithm
        save_path : <Optional> str
            Path to save (with pickle / joblib) the model.
        save_freq : <Optional> int
            Frequency of saves, in epochs. A frequency of zero disables
            automatic saving altogether. A frequency of 1 saves every
            epoch. A frequency of 2 saves every other epoch, etc.
            (default=0, i.e. never save). Note: when automatic saving is
            enabled (eg save_freq > 0), the model is always saved after
            learning, even when the final epoch is not a multiple of save_freq.
        extensions : <Optional> iterable
            A collection of TrainExtension objects whose callbacks are
            triggered at various points in learning.
        allow_overwrite : <Optional> bool
            If true, will save the model to save_path even if there is already
            something there. Otherwise, will raise an error if the save_path
            is already occupied.
        """
        self.allow_overwrite = allow_overwrite
        self.first_save = True
        self.dataset = dataset
        self.model = model
        self.algorithm = algorithm
        if save_path is not None:
            if save_freq == 0:
                warnings.warn('save_path specified but save_freq is 0 '
                              '(never save). Is this intentional?')
            self.save_path = save_path
        else:
            if save_freq > 0:
                phase_variable = 'PYLEARN2_TRAIN_PHASE'
                if phase_variable in os.environ:
                    phase = 'phase%d' % os.environ[phase_variable]
                    tokens = [os.environ['PYLEARN2_TRAIN_FILE_FULL_STEM'],
                              phase, 'pkl']
                else:
                    tokens = os.environ['PYLEARN2_TRAIN_FILE_FULL_STEM'], 'pkl'
                self.save_path = '.'.join(tokens)
        self.save_freq = save_freq

        if hasattr(self.dataset, 'yaml_src'):
            self.model.dataset_yaml_src = self.dataset.yaml_src
        else:
            warnings.warn("dataset has no yaml src, model won't know what " +
                          "data it was trained on")

        self.extensions = extensions if extensions is not None else []
        self.monitor_time = sharedX(value=0, name='seconds_per_epoch')

    def setup_extensions(self):
        """
        Calls setup on all extensions.
        """
        for ext in self.extensions:
            ext.setup(self.model, self.dataset, self.algorithm)

    def exceeded_time_budget(self, t0, time_budget):
        """
        .. todo::

            WRITEME
        """
        dt = total_seconds(datetime.now() - t0)
        if time_budget is not None and dt >= time_budget:
            log.warning("Time budget exceeded (%.3f/%d seconds).",
                        dt, time_budget)
            self.model.monitor.time_budget_exceeded = True
            return True
        else:
            return False

    def main_loop(self, time_budget=None):
        """
        Repeatedly runs an epoch of the training algorithm, runs any
        epoch-level callbacks, and saves the model.

        Parameters
        ----------
        time_budget : int, optional
            The maximum number of seconds before interrupting
            training. Default is `None`, no time limit.
        """
        t0 = datetime.now()
        if self.algorithm is None:
            self.model.monitor = Monitor.get_monitor(self.model)
            self.model.monitor.time_budget_exceeded = False
            self.setup_extensions()
            self.run_callbacks_and_monitoring()
            while True:
                if self.exceeded_time_budget(t0, time_budget):
                    break

                rval = self.model.train_all(dataset=self.dataset)
                if rval is not None:
                    raise ValueError("Model.train_all should not return " +
                                     "anything. Use Model.continue_learning " +
                                     "to control whether learning continues.")
                self.model.monitor.report_epoch()
                extension_continue = self.run_callbacks_and_monitoring()
                freq = self.save_freq
                if freq > 0 and self.model.monitor.epochs_seen % freq == 0:
                    self.save()
                continue_learning = (self.model.continue_learning() and
                                     extension_continue)
                assert continue_learning in [True, False, 0, 1]
                if not continue_learning:
                    break
        else:
            self.algorithm.setup(model=self.model, dataset=self.dataset)
            self.setup_extensions()
            if not hasattr(self.model, 'monitor'):
                # TODO: is this really necessary? I just put this error here
                # to prevent an AttributeError later, but I think we could
                # rewrite to avoid the AttributeError
                raise RuntimeError("The algorithm is responsible for setting"
                                   " up the Monitor, but failed to.")
            if len(self.model.monitor._datasets)>0:
                # This monitoring channel keeps track of a shared variable,
                # which does not need inputs nor data.
                self.model.monitor.add_channel(name="monitor_seconds_per_epoch",
                                               ipt=None,
                                               val=self.monitor_time,
                                               data_specs=(NullSpace(), ''),
                                               dataset=self.model.monitor._datasets[0])
            self.run_callbacks_and_monitoring()
            while True:
                if self.exceeded_time_budget(t0, time_budget):
                    break
                with log_timing(log, None, final_msg='Time this epoch:',
                                callbacks=[self.monitor_time.set_value]):
                    rval = self.algorithm.train(dataset=self.dataset)
                if rval is not None:
                    raise ValueError("TrainingAlgorithm.train should not " +
                                     "return anything. Use " +
                                     "TrainingAlgorithm.continue_learning " +
                                     "to control whether learning continues.")
                self.model.monitor.report_epoch()
                extension_continue = self.run_callbacks_and_monitoring()
                if self.save_freq > 0 and self.model.monitor._epochs_seen % self.save_freq == 0:
                    self.save()
                continue_learning = (
                    self.algorithm.continue_learning(self.model) and
                    extension_continue
                )
                assert continue_learning in [True, False, 0, 1]
                if not continue_learning:
                    break

        self.model.monitor.training_succeeded = True

        if self.save_freq > 0:
            self.save()

    def run_callbacks_and_monitoring(self):
        """
        Runs the monitor, then calls Extension.on_monitor for all extensions.

        Returns
        -------
        continue_learning: bool
            If `False`, signals that at least one train
            extension wants to stop learning.
        """
        self.model.monitor()
        continue_learning = True
        for extension in self.extensions:
            try:
                extension.on_monitor(self.model, self.dataset, self.algorithm)
            except TypeError:
                logging.warning('Failure during callback ' + str(extension))
                raise
            # We catch an exception here instead of relying on return
            # values for backward compatibility. Lots of extensions
            # exist that don't return anything, currently.
            except StopIteration:
                log.info("Extension requested training halt.")
                continue_learning = False
        return continue_learning

    def save(self):
        """Saves the model."""
        #TODO-- save state of training algorithm so training can be
        # resumed after a crash
        for extension in self.extensions:
            extension.on_save(self.model, self.dataset, self.algorithm)
        if self.save_path is not None:
            with log_timing(log, 'Saving to ' + self.save_path):
                if self.first_save and (not self.allow_overwrite) \
                    and os.path.exists(self.save_path):
                    # Every job overwrites its own output on the second save
                    # and every save thereafter. The "allow_overwrite" flag
                    # only pertains to overwriting the output of previous jobs.
                    raise IOError("Trying to overwrite file when not allowed.")
                try:
                    # Make sure that saving does not serialize the dataset
                    self.dataset._serialization_guard = SerializationGuard()
                    serial.save(self.save_path, self.model,
                                on_overwrite='backup')
                finally:
                    self.dataset._serialization_guard = None
            self.first_save = False

class SerializationGuard(object):
    """
    This class exists to make objects that cannot be serialized. It is used to
    make sure you don't accidentally put pointers to objects that should not
    be serialized, such as the dataset, into objects that Train automatically
    serializes, such as the Model.
    """

    def __getstate__(self):
        """
        This method is called when someone attempts to serialize the object.
        This method raises an exception to prevent the serialization from
        occurring.
        """
        raise IOError("You tried to serialize something that should not"
                      " be serialized.")

if __name__ == "__main__":
    print >>sys.stderr, "ERROR: You probably meant to run scripts/train.py"
    sys.exit(1)
<commit_msg>Backticks in pylearn2/train.py<commit_after>"""
Module containing the Train class and support functionality.
"""
__authors__ = "Ian Goodfellow"
__copyright__ = "Copyright 2010-2012, Universite de Montreal"
__credits__ = ["Ian Goodfellow"]
__license__ = "3-clause BSD"
__maintainer__ = "Ian Goodfellow"
__email__ = "goodfeli@iro"
from datetime import datetime
import os
import sys
from pylearn2.utils import serial
import logging
import warnings
from pylearn2.monitor import Monitor
from pylearn2.space import NullSpace
from pylearn2.utils.timing import log_timing, total_seconds
from pylearn2.utils import sharedX

log = logging.getLogger(__name__)

class Train(object):
    """
    A class representing the main loop of the training script.  Trains the
    specified model using the specified algorithm on the specified dataset.
    After each call to the training algorithm, the model is saved to
    `save_path`. May be enhanced with `TrainExtension` plugins.
    """
    def __init__(self, dataset, model, algorithm=None, save_path=None,
                 save_freq=0, extensions=None, allow_overwrite=True):
        """
        Construct a Train instance.

        Parameters
        ----------
        dataset : `pylearn2.datasets.dataset.Dataset`
        model : `pylearn2.models.model.Model`
        algorithm : <Optional>
        `pylearn2.training_algorithms.training_algorithm.TrainingAlgorithm`
        save_path : <Optional> str
            Path to save (with pickle / joblib) the model.
        save_freq : <Optional> int
            Frequency of saves, in epochs. A frequency of zero disables
            automatic saving altogether. A frequency of 1 saves every
            epoch. A frequency of 2 saves every other epoch, etc.
            (default=0, i.e. never save). Note: when automatic saving is
            enabled (eg save_freq > 0), the model is always saved after
            learning, even when the final epoch is not a multiple of
            `save_freq`.
        extensions : <Optional> iterable
            A collection of `TrainExtension` objects whose callbacks are
            triggered at various points in learning.
        allow_overwrite : <Optional> bool
            If `True`, will save the model to save_path even if there is already
            something there. Otherwise, will raise an error if the `save_path`
            is already occupied.
        """
        self.allow_overwrite = allow_overwrite
        self.first_save = True
        self.dataset = dataset
        self.model = model
        self.algorithm = algorithm
        if save_path is not None:
            if save_freq == 0:
                warnings.warn('save_path specified but save_freq is 0 '
                              '(never save). Is this intentional?')
            self.save_path = save_path
        else:
            if save_freq > 0:
                phase_variable = 'PYLEARN2_TRAIN_PHASE'
                if phase_variable in os.environ:
                    phase = 'phase%d' % os.environ[phase_variable]
                    tokens = [os.environ['PYLEARN2_TRAIN_FILE_FULL_STEM'],
                              phase, 'pkl']
                else:
                    tokens = os.environ['PYLEARN2_TRAIN_FILE_FULL_STEM'], 'pkl'
                self.save_path = '.'.join(tokens)
        self.save_freq = save_freq

        if hasattr(self.dataset, 'yaml_src'):
            self.model.dataset_yaml_src = self.dataset.yaml_src
        else:
            warnings.warn("dataset has no yaml src, model won't know what " +
                          "data it was trained on")

        self.extensions = extensions if extensions is not None else []
        self.monitor_time = sharedX(value=0, name='seconds_per_epoch')

    def setup_extensions(self):
        """
        Calls setup on all extensions.
        """
        for ext in self.extensions:
            ext.setup(self.model, self.dataset, self.algorithm)

    def exceeded_time_budget(self, t0, time_budget):
        """
        .. todo::

            WRITEME
        """
        dt = total_seconds(datetime.now() - t0)
        if time_budget is not None and dt >= time_budget:
            log.warning("Time budget exceeded (%.3f/%d seconds).",
                        dt, time_budget)
            self.model.monitor.time_budget_exceeded = True
            return True
        else:
            return False

    def main_loop(self, time_budget=None):
        """
        Repeatedly runs an epoch of the training algorithm, runs any
        epoch-level callbacks, and saves the model.

        Parameters
        ----------
        time_budget : int, optional
            The maximum number of seconds before interrupting
            training. Default is `None`, no time limit.
        """
        t0 = datetime.now()
        if self.algorithm is None:
            self.model.monitor = Monitor.get_monitor(self.model)
            self.model.monitor.time_budget_exceeded = False
            self.setup_extensions()
            self.run_callbacks_and_monitoring()
            while True:
                if self.exceeded_time_budget(t0, time_budget):
                    break

                rval = self.model.train_all(dataset=self.dataset)
                if rval is not None:
                    raise ValueError("Model.train_all should not return " +
                                     "anything. Use Model.continue_learning " +
                                     "to control whether learning continues.")
                self.model.monitor.report_epoch()
                extension_continue = self.run_callbacks_and_monitoring()
                freq = self.save_freq
                if freq > 0 and self.model.monitor.epochs_seen % freq == 0:
                    self.save()
                continue_learning = (self.model.continue_learning() and
                                     extension_continue)
                assert continue_learning in [True, False, 0, 1]
                if not continue_learning:
                    break
        else:
            self.algorithm.setup(model=self.model, dataset=self.dataset)
            self.setup_extensions()
            if not hasattr(self.model, 'monitor'):
                # TODO: is this really necessary? I just put this error here
                # to prevent an AttributeError later, but I think we could
                # rewrite to avoid the AttributeError
                raise RuntimeError("The algorithm is responsible for setting"
                                   " up the Monitor, but failed to.")
            if len(self.model.monitor._datasets)>0:
                # This monitoring channel keeps track of a shared variable,
                # which does not need inputs nor data.
                self.model.monitor.add_channel(name="monitor_seconds_per_epoch",
                                               ipt=None,
                                               val=self.monitor_time,
                                               data_specs=(NullSpace(), ''),
                                               dataset=self.model.monitor._datasets[0])
            self.run_callbacks_and_monitoring()
            while True:
                if self.exceeded_time_budget(t0, time_budget):
                    break
                with log_timing(log, None, final_msg='Time this epoch:',
                                callbacks=[self.monitor_time.set_value]):
                    rval = self.algorithm.train(dataset=self.dataset)
                if rval is not None:
                    raise ValueError("TrainingAlgorithm.train should not " +
                                     "return anything. Use " +
                                     "TrainingAlgorithm.continue_learning " +
                                     "to control whether learning continues.")
                self.model.monitor.report_epoch()
                extension_continue = self.run_callbacks_and_monitoring()
                if self.save_freq > 0 and self.model.monitor._epochs_seen % self.save_freq == 0:
                    self.save()
                continue_learning = (
                    self.algorithm.continue_learning(self.model) and
                    extension_continue
                )
                assert continue_learning in [True, False, 0, 1]
                if not continue_learning:
                    break

        self.model.monitor.training_succeeded = True

        if self.save_freq > 0:
            self.save()

    def run_callbacks_and_monitoring(self):
        """
        Runs the monitor, then calls Extension.on_monitor for all extensions.

        Returns
        -------
        continue_learning: bool
            If `False`, signals that at least one train
            extension wants to stop learning.
        """
        self.model.monitor()
        continue_learning = True
        for extension in self.extensions:
            try:
                extension.on_monitor(self.model, self.dataset, self.algorithm)
            except TypeError:
                logging.warning('Failure during callback ' + str(extension))
                raise
            # We catch an exception here instead of relying on return
            # values for backward compatibility. Lots of extensions
            # exist that don't return anything, currently.
            except StopIteration:
                log.info("Extension requested training halt.")
                continue_learning = False
        return continue_learning

    def save(self):
        """Saves the model."""
        #TODO-- save state of training algorithm so training can be
        # resumed after a crash
        for extension in self.extensions:
            extension.on_save(self.model, self.dataset, self.algorithm)
        if self.save_path is not None:
            with log_timing(log, 'Saving to ' + self.save_path):
                if self.first_save and (not self.allow_overwrite) \
                    and os.path.exists(self.save_path):
                    # Every job overwrites its own output on the second save
                    # and every save thereafter. The "allow_overwrite" flag
                    # only pertains to overwriting the output of previous jobs.
                    raise IOError("Trying to overwrite file when not allowed.")
                try:
                    # Make sure that saving does not serialize the dataset
                    self.dataset._serialization_guard = SerializationGuard()
                    serial.save(self.save_path, self.model,
                                on_overwrite='backup')
                finally:
                    self.dataset._serialization_guard = None
            self.first_save = False

class SerializationGuard(object):
    """
    This class exists to make objects that cannot be serialized. It is used to
    make sure you don't accidentally put pointers to objects that should not
    be serialized, such as the dataset, into objects that Train automatically
    serializes, such as the Model.
    """

    def __getstate__(self):
        """
        This method is called when someone attempts to serialize the object.
        This method raises an exception to prevent the serialization from
        occurring.
        """
        raise IOError("You tried to serialize something that should not"
                      " be serialized.")

if __name__ == "__main__":
    print >>sys.stderr, "ERROR: You probably meant to run scripts/train.py"
    sys.exit(1)
<|endoftext|>


<commit_before>import math
import bread
from . import bread_spec as spec
from .song import Song
from . import filepack
from . import blockutils
from .blockutils import BlockReader, BlockWriter, BlockFactory

def load_lsdsng(filename):
    """Load a Project from a ``.lsdsng`` file.

    :param filename: the name of the file from which to load
    :rtype: :py:class:`pylsdj.Project`
    """

    # Load preamble data so that we know the name and version of the song
    with open(filename, 'rb') as fp:
        preamble_data = bread.parse(fp, spec.lsdsng_preamble)

    with open(filename, 'rb') as fp:
        # Skip the preamble this time around
        fp.seek(int(len(preamble_data) / 8))

        # Load compressed data into a block map and use BlockReader to
        # decompress it
        factory = BlockFactory()

        while True:
            block_data = bytearray(fp.read(blockutils.BLOCK_SIZE))

            if len(block_data) == 0:
                break

            block = factory.new_block()
            block.data = block_data

        remapped_blocks = filepack.renumber_block_keys(factory.blocks)

        reader = BlockReader()
        compressed_data = reader.read(remapped_blocks)

        # Now, decompress the raw data and use it and the preamble to construct
        # a Project
        raw_data = filepack.decompress(compressed_data)

        name = preamble_data.name
        version = preamble_data.version
        size_blks = int(math.ceil(
            float(len(compressed_data)) / blockutils.BLOCK_SIZE))

        return Project(name, version, size_blks, raw_data)

def load_srm(filename):
    """Load a Project from an ``.srm`` file.

    :param filename: the name of the file from which to load
    :rtype: :py:class:`pylsdj.Project`
    """

    # .srm files are just decompressed projects without headers

    # In order to determine the file's size in compressed blocks, we have to
    # compress it first
    with open(filename, 'rb') as fp:
        raw_data = fp.read()

    compressed_data = filepack.compress(raw_data)

    factory = BlockFactory()
    writer = BlockWriter()
    writer.write(compressed_data, factory)

    size_in_blocks = len(factory.blocks)

    # We'll give the file a dummy name ("SRMLOAD") and version, since we know
    # neither
    name = "SRMLOAD"
    version = 0

    return Project(name, version, size_in_blocks, raw_data)

class Project(object):

    def __init__(self, name, version, size_blks, data):
        self.name = name
        """the project's name"""

        self.version = version
        """the project's version (incremented on every save in LSDJ)"""

        self._song_data = bread.parse(data, spec.song)

        self.song = Song(self._song_data)
        """the song associated with the project"""

        self.size_blks = size_blks
        """the size of the song in filesystem blocks"""

        # Useful for applications tracking whether a project was modified since
        # it was loaded.
        self.modified = False

    def get_raw_data(self):
        return bread.write(self._song_data, spec.song)

    def save(self, filename):
        """Save a project in .lsdsng format to the target file.

        :param filename: the name of the file to which to save

        :deprecated: use ``save_lsdsng(filename)`` instead
        """
        with open(filename, 'wb') as fp:
            writer = BlockWriter()
            factory = BlockFactory()

            preamble_data = bread.write(self, spec.lsdsng_preamble)
            raw_data = self.get_raw_data()
            compressed_data = filepack.compress(raw_data)

            writer.write(compressed_data, factory)

            fp.write(preamble_data)

            for key in sorted(factory.blocks.keys()):
                fp.write(bytearray(factory.blocks[key].data))

    def save_lsdsng(self, filename):
        """Save a project in .lsdsng format to the target file.

        :param filename: the name of the file to which to save
        """
        return self.save(filename)

    def save_srm(self, filename):
        """Save a project in .srm format to the target file.

        :param filename: the name of the file to which to save
        """
        with open(filename, 'wb') as fp:
            raw_data = bread.write(self._song_data, spec.song)
            fp.write(raw_data)

    def __eq__(self, other):
        return self._song_data == other._song_data

    def __str__(self):
        return "<%s, %d>\n" % (self.name, self.version)
<commit_msg>Write song data, not the song itself.<commit_after>import math
import bread
from . import bread_spec as spec
from .song import Song
from . import filepack
from . import blockutils
from .blockutils import BlockReader, BlockWriter, BlockFactory

def load_lsdsng(filename):
    """Load a Project from a ``.lsdsng`` file.

    :param filename: the name of the file from which to load
    :rtype: :py:class:`pylsdj.Project`
    """

    # Load preamble data so that we know the name and version of the song
    with open(filename, 'rb') as fp:
        preamble_data = bread.parse(fp, spec.lsdsng_preamble)

    with open(filename, 'rb') as fp:
        # Skip the preamble this time around
        fp.seek(int(len(preamble_data) / 8))

        # Load compressed data into a block map and use BlockReader to
        # decompress it
        factory = BlockFactory()

        while True:
            block_data = bytearray(fp.read(blockutils.BLOCK_SIZE))

            if len(block_data) == 0:
                break

            block = factory.new_block()
            block.data = block_data

        remapped_blocks = filepack.renumber_block_keys(factory.blocks)

        reader = BlockReader()
        compressed_data = reader.read(remapped_blocks)

        # Now, decompress the raw data and use it and the preamble to construct
        # a Project
        raw_data = filepack.decompress(compressed_data)

        name = preamble_data.name
        version = preamble_data.version
        size_blks = int(math.ceil(
            float(len(compressed_data)) / blockutils.BLOCK_SIZE))

        return Project(name, version, size_blks, raw_data)

def load_srm(filename):
    """Load a Project from an ``.srm`` file.

    :param filename: the name of the file from which to load
    :rtype: :py:class:`pylsdj.Project`
    """

    # .srm files are just decompressed projects without headers

    # In order to determine the file's size in compressed blocks, we have to
    # compress it first
    with open(filename, 'rb') as fp:
        raw_data = fp.read()

    compressed_data = filepack.compress(raw_data)

    factory = BlockFactory()
    writer = BlockWriter()
    writer.write(compressed_data, factory)

    size_in_blocks = len(factory.blocks)

    # We'll give the file a dummy name ("SRMLOAD") and version, since we know
    # neither
    name = "SRMLOAD"
    version = 0

    return Project(name, version, size_in_blocks, raw_data)

class Project(object):

    def __init__(self, name, version, size_blks, data):
        self.name = name
        """the project's name"""

        self.version = version
        """the project's version (incremented on every save in LSDJ)"""

        self._song_data = bread.parse(data, spec.song)

        self.song = Song(self._song_data)
        """the song associated with the project"""

        self.size_blks = size_blks
        """the size of the song in filesystem blocks"""

        # Useful for applications tracking whether a project was modified since
        # it was loaded.
        self.modified = False

    def get_raw_data(self):
        return bread.write(self._song_data, spec.song)

    def save(self, filename):
        """Save a project in .lsdsng format to the target file.

        :param filename: the name of the file to which to save

        :deprecated: use ``save_lsdsng(filename)`` instead
        """
        with open(filename, 'wb') as fp:
            writer = BlockWriter()
            factory = BlockFactory()

            preamble_data = bread.write(self._song_data)
            raw_data = self.get_raw_data()
            compressed_data = filepack.compress(raw_data)

            writer.write(compressed_data, factory)

            fp.write(preamble_data)

            for key in sorted(factory.blocks.keys()):
                fp.write(bytearray(factory.blocks[key].data))

    def save_lsdsng(self, filename):
        """Save a project in .lsdsng format to the target file.

        :param filename: the name of the file to which to save
        """
        return self.save(filename)

    def save_srm(self, filename):
        """Save a project in .srm format to the target file.

        :param filename: the name of the file to which to save
        """
        with open(filename, 'wb') as fp:
            raw_data = bread.write(self._song_data, spec.song)
            fp.write(raw_data)

    def __eq__(self, other):
        return self._song_data == other._song_data

    def __str__(self):
        return "<%s, %d>\n" % (self.name, self.version)
<|endoftext|>


<commit_before>version = '1.1.0'
<commit_msg>switch to revision 1.2.0<commit_after>version = '1.2.0'
<|endoftext|>