<commit_before>#!/usr/bin/env python

# The MIT License (MIT)
#
# Copyright (c) 2016 Richard Hull
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Example usage:
#
#   from oled.device import ssd1306, sh1106
#   from oled.render import canvas
#   from PIL import ImageFont, ImageDraw
#
#   font = ImageFont.load_default()
#   device = ssd1306(port=1, address=0x3C)
#
#   with canvas(device) as draw:
#      draw.rectangle((0, 0, device.width, device.height), outline="white", fill="black")
#      draw.text(30, 40, "Hello World", font=font, fill="white")
#
# As soon as the with-block scope level is complete, the graphics primitives
# will be flushed to the device.
#
# Creating a new canvas is effectively 'carte blanche': If you want to retain
# an existing canvas, then make a reference like:
#
#    c = canvas(device)
#    for X in ...:
#        with c as draw:
#            draw.rectangle(...)
#
# As before, as soon as the with block completes, the canvas buffer is flushed
# to the device

import sys
import atexit
import smbus2
from PIL import Image
import oled.mixin as mixin

class device(object):
    """
    Base class for OLED driver classes
    """

    def __init__(self, bus=None, port=1, address=0x3C, cmd_mode=0x00,
                 data_mode=0x40):
        self.cmd_mode = cmd_mode
        self.data_mode = data_mode
        self.bus = bus or smbus2.SMBus(port)
        self.addr = address

        def cleanup():
            self.clear()
            self.hide()
            # If the bus was not supplied (i.e. we created it in the
            # constructor) then it should be closed.
            if bus is None:
                self.bus.close()

        atexit.register(cleanup)

    def command(self, *cmd):
        """
        Sends a command or sequence of commands through to the
        device - maximum allowed is 32 bytes in one go.
        """
        assert(len(cmd) <= 32)
        self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd))

    def data(self, data):
        """
        Sends a data byte or sequence of data bytes through to the
        device - maximum allowed in one transaction is 32 bytes, so if
        data is larger than this it is sent in chunks.
        """
        i = 0
        n = len(data)
        while i < n:
            self.bus.write_i2c_block_data(self.addr,
                                          self.data_mode,
                                          list(data[i:i + 32]))
            i += 32

    def show(self):
        """
        Sets the display mode ON, waking the device out of a prior
        low-power sleep mode.
        """
        self.command(const.DISPLAYON)

    def hide(self):
        """
        Switches the display mode OFF, putting the device in low-power
        sleep mode.
        """
        self.command(const.DISPLAYOFF)

    def clear(self):
        """
        Initializes the device memory with an empty (blank) image.
        """
        self.display(Image.new(self.mode, (self.width, self.height)))

class sh1106(device, mixin.capabilities):
    """
    A device encapsulates the I2C connection (address/port) to the SH1106
    OLED display hardware. The init method pumps commands to the display
    to properly initialize it. Further control commands can then be
    called to affect the brightness. Direct use of the command() and
    data() methods are discouraged.

    Note: Only one of bus OR port arguments should be supplied; if both
    are, then bus takes precedence.
    """

    def __init__(self, bus=None, port=1, address=0x3C, width=128, height=64):
        try:
            super(sh1106, self).__init__(bus, port, address)
            self.capabilities(width, height)
            self.bounding_box = (0, 0, width - 1, height - 1)
            self.width = width
            self.height = height
            self._pages = self.height // 8

            self.command(
                const.DISPLAYOFF,
                const.MEMORYMODE,
                const.SETHIGHCOLUMN,      0xB0, 0xC8,
                const.SETLOWCOLUMN,       0x10, 0x40,
                const.SETCONTRAST,        0x7F,
                const.SETSEGMENTREMAP,
                const.NORMALDISPLAY,
                const.SETMULTIPLEX,       0x3F,
                const.DISPLAYALLON_RESUME,
                const.SETDISPLAYOFFSET,   0x00,
                const.SETDISPLAYCLOCKDIV, 0xF0,
                const.SETPRECHARGE,       0x22,
                const.SETCOMPINS,         0x12,
                const.SETVCOMDETECT,      0x20,
                const.CHARGEPUMP,         0x14)

            self.clear()
            self.show()

        except IOError as e:
            raise IOError(e.errno,
                "Failed to initialize SH1106 display driver")

    def display(self, image):
        """
        Takes a 1-bit image and dumps it to the SH1106 OLED display.
        """
        assert(image.mode == self.mode)
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        page = 0xB0
        pix = list(image.getdata())
        step = self.width * 8
        for y in range(0, self._pages * step, step):

            # move to given page, then reset the column address
            self.command(page, 0x02, 0x10)
            page += 1

            buf = []
            for x in range(self.width):
                byte = 0
                for n in range(0, step, self.width):
                    byte |= (pix[x + y + n] & 0x01) << 8
                    byte >>= 1

                buf.append(byte)

            self.data(buf)

class ssd1306(device, mixin.capabilities):
    """
    A device encapsulates the I2C connection (address/port) to the SSD1306
    OLED display hardware. The init method pumps commands to the display
    to properly initialize it. Further control commands can then be
    called to affect the brightness. Direct use of the command() and
    data() methods are discouraged.

    Note: Only one of bus OR port arguments should be supplied; if both
    are, then bus takes precedence.
    """
    def __init__(self, bus=None, port=1, address=0x3C, width=128, height=64):
        try:
            super(ssd1306, self).__init__(bus, port, address)
            self.capabilities(width, height)
            self._pages = self.height // 8
            self._buffer = [0] * self.width * self._pages
            self._offsets = [n * self.width for n in range(8)]

            self.command(
                const.DISPLAYOFF,
                const.SETDISPLAYCLOCKDIV, 0x80,
                const.SETMULTIPLEX,       0x3F,
                const.SETDISPLAYOFFSET,   0x00,
                const.SETSTARTLINE,
                const.CHARGEPUMP,         0x14,
                const.MEMORYMODE,         0x00,
                const.SEGREMAP,
                const.COMSCANDEC,
                const.SETCOMPINS,         0x12,
                const.SETCONTRAST,        0xCF,
                const.SETPRECHARGE,       0xF1,
                const.SETVCOMDETECT,      0x40,
                const.DISPLAYALLON_RESUME,
                const.NORMALDISPLAY)

            self.clear()
            self.show()

        except IOError as e:
            raise IOError(e.errno,
                "Failed to initialize SSD1306 display driver")

    def display(self, image):
        """
        Takes a 1-bit image and dumps it to the SSD1306 OLED display.
        """
        assert(image.mode == self.mode)
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        self.command(
            # Column start/end address
            const.COLUMNADDR, 0x00, self.width - 1,
            # Page start/end address
            const.PAGEADDR, 0x00, self._pages - 1)

        pix = list(image.getdata())
        step = self.width * 8
        buf = self._buffer
        offsets = self._offsets
        w = self.width
        j = 0
        for y in range(0, self._pages * step, step):
            i = y + w - 1
            while i >= y:
                buf[j] = (pix[i] & 0x01) | \
                         (pix[i + offsets[1]] & 0x01) << 1 | \
                         (pix[i + offsets[2]] & 0x01) << 2 | \
                         (pix[i + offsets[3]] & 0x01) << 3 | \
                         (pix[i + offsets[4]] & 0x01) << 4 | \
                         (pix[i + offsets[5]] & 0x01) << 5 | \
                         (pix[i + offsets[6]] & 0x01) << 6 | \
                         (pix[i + offsets[7]] & 0x01) << 7

                i -= 1
                j += 1

        self.data(buf)

class capture(device, mixin.noop, mixin.capabilities):
    """
    Pseudo-device that acts like an OLED display, except that it writes
    the image to a numbered PNG file when the :func:`display` method
    is called.

    While the capability of an OLED device is monochrome, there is no
    limitation here, and hence supports 24-bit color depth.
    """
    def __init__(self, width=128, height=64, file_template="oled_{0:06}.png", **kwargs):
        self.capabilities(width, height, mode="RGB")
        self._count = 0
        self._file_template = file_template

    def display(self, image):
        """
        Takes an image and dumps it to a numbered PNG file.
        """
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        im = image.convert("RGB")
        self._count += 1
        filename = self._file_template.format(self._count)
        with open(filename, "wb") as fp:
            print("Writing: {0}".format(filename))
            im.save(fp, "png")

        del im

class pygame(device, mixin.noop, mixin.capabilities):
    """
    Pseudo-device that acts like an OLED display, except that it renders
    to an displayed window. The frame rate is limited to 60FPS (much faster
    than a Raspberry Pi can acheive, but this can be overridden as necessary).

    While the capability of an OLED device is monochrome, there is no
    limitation here, and hence supports 24-bit color depth.

    :mod:`pygame` is used to render the emulated display window, and it's
    event loop is checked to see if the ESC key was pressed or the window
    was dismissed: if so `sys.exit()` is called.
    """
    def __init__(self, width=128, height=64, frame_rate=60, **kwargs):
        self.capabilities(width, height, mode="RGB")

        try:
            import pygame
            pygame.init()
            pygame.font.init()
            self._clock = pygame.time.Clock()
            self._fps = frame_rate
            self._screen = pygame.display.set_mode((width, height))
            self._screen.fill((0, 0, 0))
            self._pygame = pygame
            pygame.display.flip()
        except ImportError:
            raise RuntimeError("Pygame is not an explicit dependency, and must be installed separately")

    def _abort(self):
        keystate = self._pygame.key.get_pressed()
        return keystate[self._pygame.K_ESCAPE] or self._pygame.event.peek(self._pygame.QUIT)

    def display(self, image):
        """
        Takes an image and renders it to a pygame display surface.
        """
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        self._clock.tick(self._fps)
        self._pygame.event.pump()

        if self._abort():
            self._pygame.quit()
            sys.exit()

        im = image.convert("RGB")
        mode = im.mode
        size = im.size
        data = im.tobytes()
        del im

        surface = self._pygame.image.fromstring(data, size, mode)
        self._screen.blit(surface, (0, 0))
        self._pygame.display.flip()

class const:
    CHARGEPUMP = 0x8D
    COLUMNADDR = 0x21
    COMSCANDEC = 0xC8
    COMSCANINC = 0xC0
    DISPLAYALLON = 0xA5
    DISPLAYALLON_RESUME = 0xA4
    DISPLAYOFF = 0xAE
    DISPLAYON = 0xAF
    EXTERNALVCC = 0x1
    INVERTDISPLAY = 0xA7
    MEMORYMODE = 0x20
    NORMALDISPLAY = 0xA6
    PAGEADDR = 0x22
    SEGREMAP = 0xA0
    SETCOMPINS = 0xDA
    SETCONTRAST = 0x81
    SETDISPLAYCLOCKDIV = 0xD5
    SETDISPLAYOFFSET = 0xD3
    SETHIGHCOLUMN = 0x10
    SETLOWCOLUMN = 0x00
    SETMULTIPLEX = 0xA8
    SETPRECHARGE = 0xD9
    SETSEGMENTREMAP = 0xA1
    SETSTARTLINE = 0x40
    SETVCOMDETECT = 0xDB
    SWITCHCAPVCC = 0x2
<commit_msg>Fix to override devices data & command methods with noop's<commit_after>#!/usr/bin/env python

# The MIT License (MIT)
#
# Copyright (c) 2016 Richard Hull
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Example usage:
#
#   from oled.device import ssd1306, sh1106
#   from oled.render import canvas
#   from PIL import ImageFont, ImageDraw
#
#   font = ImageFont.load_default()
#   device = ssd1306(port=1, address=0x3C)
#
#   with canvas(device) as draw:
#      draw.rectangle((0, 0, device.width, device.height), outline="white", fill="black")
#      draw.text(30, 40, "Hello World", font=font, fill="white")
#
# As soon as the with-block scope level is complete, the graphics primitives
# will be flushed to the device.
#
# Creating a new canvas is effectively 'carte blanche': If you want to retain
# an existing canvas, then make a reference like:
#
#    c = canvas(device)
#    for X in ...:
#        with c as draw:
#            draw.rectangle(...)
#
# As before, as soon as the with block completes, the canvas buffer is flushed
# to the device

import sys
import atexit
import smbus2
from PIL import Image
import oled.mixin as mixin

class device(object):
    """
    Base class for OLED driver classes
    """

    def __init__(self, bus=None, port=1, address=0x3C, cmd_mode=0x00,
                 data_mode=0x40):
        self.cmd_mode = cmd_mode
        self.data_mode = data_mode
        self.bus = bus or smbus2.SMBus(port)
        self.addr = address

        def cleanup():
            self.clear()
            self.hide()
            # If the bus was not supplied (i.e. we created it in the
            # constructor) then it should be closed.
            if bus is None:
                self.bus.close()

        atexit.register(cleanup)

    def command(self, *cmd):
        """
        Sends a command or sequence of commands through to the
        device - maximum allowed is 32 bytes in one go.
        """
        assert(len(cmd) <= 32)
        self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd))

    def data(self, data):
        """
        Sends a data byte or sequence of data bytes through to the
        device - maximum allowed in one transaction is 32 bytes, so if
        data is larger than this it is sent in chunks.
        """
        i = 0
        n = len(data)
        while i < n:
            self.bus.write_i2c_block_data(self.addr,
                                          self.data_mode,
                                          list(data[i:i + 32]))
            i += 32

    def show(self):
        """
        Sets the display mode ON, waking the device out of a prior
        low-power sleep mode.
        """
        self.command(const.DISPLAYON)

    def hide(self):
        """
        Switches the display mode OFF, putting the device in low-power
        sleep mode.
        """
        self.command(const.DISPLAYOFF)

    def clear(self):
        """
        Initializes the device memory with an empty (blank) image.
        """
        self.display(Image.new(self.mode, (self.width, self.height)))

class sh1106(device, mixin.capabilities):
    """
    A device encapsulates the I2C connection (address/port) to the SH1106
    OLED display hardware. The init method pumps commands to the display
    to properly initialize it. Further control commands can then be
    called to affect the brightness. Direct use of the command() and
    data() methods are discouraged.

    Note: Only one of bus OR port arguments should be supplied; if both
    are, then bus takes precedence.
    """

    def __init__(self, bus=None, port=1, address=0x3C, width=128, height=64):
        try:
            super(sh1106, self).__init__(bus, port, address)
            self.capabilities(width, height)
            self.bounding_box = (0, 0, width - 1, height - 1)
            self.width = width
            self.height = height
            self._pages = self.height // 8

            self.command(
                const.DISPLAYOFF,
                const.MEMORYMODE,
                const.SETHIGHCOLUMN,      0xB0, 0xC8,
                const.SETLOWCOLUMN,       0x10, 0x40,
                const.SETCONTRAST,        0x7F,
                const.SETSEGMENTREMAP,
                const.NORMALDISPLAY,
                const.SETMULTIPLEX,       0x3F,
                const.DISPLAYALLON_RESUME,
                const.SETDISPLAYOFFSET,   0x00,
                const.SETDISPLAYCLOCKDIV, 0xF0,
                const.SETPRECHARGE,       0x22,
                const.SETCOMPINS,         0x12,
                const.SETVCOMDETECT,      0x20,
                const.CHARGEPUMP,         0x14)

            self.clear()
            self.show()

        except IOError as e:
            raise IOError(e.errno,
                "Failed to initialize SH1106 display driver")

    def display(self, image):
        """
        Takes a 1-bit image and dumps it to the SH1106 OLED display.
        """
        assert(image.mode == self.mode)
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        page = 0xB0
        pix = list(image.getdata())
        step = self.width * 8
        for y in range(0, self._pages * step, step):

            # move to given page, then reset the column address
            self.command(page, 0x02, 0x10)
            page += 1

            buf = []
            for x in range(self.width):
                byte = 0
                for n in range(0, step, self.width):
                    byte |= (pix[x + y + n] & 0x01) << 8
                    byte >>= 1

                buf.append(byte)

            self.data(buf)

class ssd1306(device, mixin.capabilities):
    """
    A device encapsulates the I2C connection (address/port) to the SSD1306
    OLED display hardware. The init method pumps commands to the display
    to properly initialize it. Further control commands can then be
    called to affect the brightness. Direct use of the command() and
    data() methods are discouraged.

    Note: Only one of bus OR port arguments should be supplied; if both
    are, then bus takes precedence.
    """
    def __init__(self, bus=None, port=1, address=0x3C, width=128, height=64):
        try:
            super(ssd1306, self).__init__(bus, port, address)
            self.capabilities(width, height)
            self._pages = self.height // 8
            self._buffer = [0] * self.width * self._pages
            self._offsets = [n * self.width for n in range(8)]

            self.command(
                const.DISPLAYOFF,
                const.SETDISPLAYCLOCKDIV, 0x80,
                const.SETMULTIPLEX,       0x3F,
                const.SETDISPLAYOFFSET,   0x00,
                const.SETSTARTLINE,
                const.CHARGEPUMP,         0x14,
                const.MEMORYMODE,         0x00,
                const.SEGREMAP,
                const.COMSCANDEC,
                const.SETCOMPINS,         0x12,
                const.SETCONTRAST,        0xCF,
                const.SETPRECHARGE,       0xF1,
                const.SETVCOMDETECT,      0x40,
                const.DISPLAYALLON_RESUME,
                const.NORMALDISPLAY)

            self.clear()
            self.show()

        except IOError as e:
            raise IOError(e.errno,
                "Failed to initialize SSD1306 display driver")

    def display(self, image):
        """
        Takes a 1-bit image and dumps it to the SSD1306 OLED display.
        """
        assert(image.mode == self.mode)
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        self.command(
            # Column start/end address
            const.COLUMNADDR, 0x00, self.width - 1,
            # Page start/end address
            const.PAGEADDR, 0x00, self._pages - 1)

        pix = list(image.getdata())
        step = self.width * 8
        buf = self._buffer
        offsets = self._offsets
        w = self.width
        j = 0
        for y in range(0, self._pages * step, step):
            i = y + w - 1
            while i >= y:
                buf[j] = (pix[i] & 0x01) | \
                         (pix[i + offsets[1]] & 0x01) << 1 | \
                         (pix[i + offsets[2]] & 0x01) << 2 | \
                         (pix[i + offsets[3]] & 0x01) << 3 | \
                         (pix[i + offsets[4]] & 0x01) << 4 | \
                         (pix[i + offsets[5]] & 0x01) << 5 | \
                         (pix[i + offsets[6]] & 0x01) << 6 | \
                         (pix[i + offsets[7]] & 0x01) << 7

                i -= 1
                j += 1

        self.data(buf)

class capture(mixin.noop, mixin.capabilities, device):
    """
    Pseudo-device that acts like an OLED display, except that it writes
    the image to a numbered PNG file when the :func:`display` method
    is called.

    While the capability of an OLED device is monochrome, there is no
    limitation here, and hence supports 24-bit color depth.
    """
    def __init__(self, width=128, height=64, file_template="oled_{0:06}.png", **kwargs):
        self.capabilities(width, height, mode="RGB")
        self._count = 0
        self._file_template = file_template

    def display(self, image):
        """
        Takes an image and dumps it to a numbered PNG file.
        """
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        im = image.convert("RGB")
        self._count += 1
        filename = self._file_template.format(self._count)
        with open(filename, "wb") as fp:
            print("Writing: {0}".format(filename))
            im.save(fp, "png")

        del im

class pygame(mixin.noop, mixin.capabilities, device):
    """
    Pseudo-device that acts like an OLED display, except that it renders
    to an displayed window. The frame rate is limited to 60FPS (much faster
    than a Raspberry Pi can acheive, but this can be overridden as necessary).

    While the capability of an OLED device is monochrome, there is no
    limitation here, and hence supports 24-bit color depth.

    :mod:`pygame` is used to render the emulated display window, and it's
    event loop is checked to see if the ESC key was pressed or the window
    was dismissed: if so `sys.exit()` is called.
    """
    def __init__(self, width=128, height=64, frame_rate=60, **kwargs):
        self.capabilities(width, height, mode="RGB")

        try:
            import pygame
            pygame.init()
            pygame.font.init()
            self._clock = pygame.time.Clock()
            self._fps = frame_rate
            self._screen = pygame.display.set_mode((width, height))
            self._screen.fill((0, 0, 0))
            self._pygame = pygame
            pygame.display.flip()
        except ImportError:
            raise RuntimeError("Pygame is not an explicit dependency, and must be installed separately")

    def _abort(self):
        keystate = self._pygame.key.get_pressed()
        return keystate[self._pygame.K_ESCAPE] or self._pygame.event.peek(self._pygame.QUIT)

    def display(self, image):
        """
        Takes an image and renders it to a pygame display surface.
        """
        assert(image.size[0] == self.width)
        assert(image.size[1] == self.height)

        self._clock.tick(self._fps)
        self._pygame.event.pump()

        if self._abort():
            self._pygame.quit()
            sys.exit()

        im = image.convert("RGB")
        mode = im.mode
        size = im.size
        data = im.tobytes()
        del im

        surface = self._pygame.image.fromstring(data, size, mode)
        self._screen.blit(surface, (0, 0))
        self._pygame.display.flip()

class const:
    CHARGEPUMP = 0x8D
    COLUMNADDR = 0x21
    COMSCANDEC = 0xC8
    COMSCANINC = 0xC0
    DISPLAYALLON = 0xA5
    DISPLAYALLON_RESUME = 0xA4
    DISPLAYOFF = 0xAE
    DISPLAYON = 0xAF
    EXTERNALVCC = 0x1
    INVERTDISPLAY = 0xA7
    MEMORYMODE = 0x20
    NORMALDISPLAY = 0xA6
    PAGEADDR = 0x22
    SEGREMAP = 0xA0
    SETCOMPINS = 0xDA
    SETCONTRAST = 0x81
    SETDISPLAYCLOCKDIV = 0xD5
    SETDISPLAYOFFSET = 0xD3
    SETHIGHCOLUMN = 0x10
    SETLOWCOLUMN = 0x00
    SETMULTIPLEX = 0xA8
    SETPRECHARGE = 0xD9
    SETSEGMENTREMAP = 0xA1
    SETSTARTLINE = 0x40
    SETVCOMDETECT = 0xDB
    SWITCHCAPVCC = 0x2
<|endoftext|>


<commit_before>from collections import OrderedDict
import numpy as np
inf = np.inf

class RegionParameters(object):
    regionName = "NGC6845-26NewDisp"

    blueSpecFile =  'NGC6845_26BNewDisp.fc.fits'             #'NGC6845_26B_SPEC1.wc.fits'
    redSpecFile = 'NGC6845_26RNewDisp.fc.fits'              #'NGC6845_26R_SPEC1.wc.fits'
    blueSpecError = 'NGC6845_26B_ErrorFlux.fc.fits'  #'NGC6845_26B_VAR4.wc.fits'
    redSpecError = 'NGC6845_26R_ErrorFlux.fc.fits'   #'NGC6845_26R_VAR4.wc.fits'
    scaleFlux = 1e14                                # 1

    # SPECTRAL LINE INFO FOR ALL EMISSION LINES
    emProfiles = OrderedDict([
        ('H-Alpha', {'Colour': 'y', 'Order': 21, 'Filter': 'red', 'minI': 1180, 'maxI': 1700, 'restWavelength': 6562.82, 'ampList': [0.98000939, 2.64348986, 1.31011049], 'zone': 'low', 'sigmaT2': 164.96, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': None}),
        ('OIII-5007A', {'Colour': 'c', 'Order': 5, 'Filter': 'red', 'minI': 1500, 'maxI': 2100, 'restWavelength': 5006.84, 'ampList': [0.09838691, 0.29803006, 0.17693078], 'zone': 'high', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': None}),
        ('OIII-4959A', {'Colour': 'g', 'Order': 4, 'Filter': 'red', 'minI': 2200, 'maxI': 2900, 'restWavelength': 4958.91, 'ampList': [0.0327956, 0.0993434, 0.0589769], 'zone': 'high', 'sigmaT2': 10.39, 'compLimits': {'c': False, 's': 0.001, 'a': inf}, 'copyFrom': 'OIII-5007A'}),
        #H-Beta_Blue:
        ('H-Beta', {'Colour': 'b', 'Order': 36, 'Filter': 'blue', 'minI': 1800, 'maxI': 2400, 'restWavelength': 4861.33, 'ampList': [0.2506062, 0.6823344, 0.1824137], 'zone': 'high', 'sigmaT2': 164.96, 'compLimits': {'c': [0.001, 0.001, False], 's': [0.001, 0.001, 0.001], 'a': np.inf}, 'copyFrom': None}),
        # #('H-Beta_Red', {'Colour': 'r', 'Order': 3, 'Filter': 'red', 'minI': 1500, 'maxI': 2040, 'restWavelength': 4861.33, 'ampList': [0.2506062, 0.6823344, 0.1824137], 'zone': 'low', 'sigmaT2': 164.96, 'compLimits': {'c': [0.001, 0.001, False], 's': [0.001, 0.001, 0.001], 'a': np.inf}, 'copyFrom': None}),
        # #('H-Gamma', {'Colour': 'c', 'Order': 28, 'Filter': 'blue', 'minI': 600, 'maxI': 1100, 'restWavelength': 4340.47, 'ampList': [0.0529313, 0.2030545, 0.070263], 'zone': 'low', 'sigmaT2': 164.96, 'compLimits': {'c':False, 's': False, 'a': np.inf}, 'copyFrom': 'H-Beta'}),
        # #('H-Delta', {'Colour': 'c', 'Order': 23, 'Filter': 'blue', 'minI': 1450, 'maxI': 1950, 'restWavelength': 4101.74, 'ampList': [0.0529313, 0.2030545, 0.070263], 'zone': 'low', 'sigmaT2': 164.96, 'compLimits': {'c': False, 's': False, 'a': [inf, inf, 0.001]}, 'copyFrom': 'H-Gamma'}),
        ('NII-6584A', {'Colour': 'violet', 'Order': 21, 'Filter': 'red', 'minI': 1650, 'maxI': 2100, 'restWavelength': 6583.41, 'ampList': [0.194849, 0.5586848, 0.0950584], 'zone': 'low', 'sigmaT2': 11.87, 'compLimits': {'c': 0.001, 's': 0.001, 'a':[inf, inf, 0.001]}, 'copyFrom': 'H-Alpha'}),
        ('NII-6548A', {'Colour': 'violet', 'Order': 21, 'Filter': 'red', 'minI': 900, 'maxI': 1210, 'restWavelength': 6548.03, 'ampList': [0.0599905, 0.2410469, 0.0819148], 'zone': 'low', 'sigmaT2': 11.87, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'NII-6584A'}),
        ('SII-6717A', {'Colour': 'r', 'Order': 22, 'Filter': 'red', 'minI': 1700, 'maxI': 2006, 'restWavelength': 6716.47, 'ampList': [0.0334009, 0.0883627, 0.011889], 'zone': 'low', 'sigmaT2': 5.19, 'compLimits': {'c': 0.1, 's': 0.01, 'a':np.inf}, 'copyFrom': 'H-Alpha'}),
        #('SII-6731A', {'Colour': '#58D68D', 'Order': 22, 'Filter': 'red', 'minI': 2068, 'maxI': 2273, 'restWavelength': 6730.85, 'ampList': [1.3593845, 5.9234259, 7.3026765], 'zone': 'low', 'sigmaT2': 5.19, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'SII-6717A'}),
        #('HeI-5876A', {'Colour': '#641E16', 'Order': 15, 'Filter': 'red', 'minI': 1350, 'maxI': 1650, 'restWavelength': 5875.64, 'ampList': [0.4850817, 0.1915922, 4.6927829], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': None}),
        #('OII-3729A', {'Colour': '#5D6D7E', 'Order': 14, 'Filter': 'blue', 'minI': 2720, 'maxI': 3000, 'restWavelength': 3728.82, 'ampList': [5.0112717, -22679.7862692, -5.7298895], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': None}),
        #('OII-3726A', {'Colour': '#EC7063', 'Order': 14, 'Filter': 'blue', 'minI': 2400, 'maxI': 2720, 'restWavelength': 3726.03, 'ampList': [-77.5325889, 42081.6972815, -36.5274502], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'OII-3729A'}),
        #V('OII-7319A', {'Colour': '#F8C471', 'Order': 26, 'Filter': 'red', 'minI': 2264, 'maxI': 2321, 'restWavelength': 7318.39, 'ampList': [133570.7269853, -58921.5577832, 50738.7676701], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #V('OII-7330A', {'Colour': '#7FB3D5', 'Order': 26, 'Filter': 'red', 'minI': 2444, 'maxI': 2513, 'restWavelength': 7329.66, 'ampList': [-23.2055153, 18.8989013, -7.1261496], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #NO('OII-7331A', {'Colour': '#7FB3D5', 'Order': 26, 'Filter': 'red', 'minI': 2444, 'maxI': 2513, 'restWavelength': 7330.73, 'ampList': [-23.2055153, 18.8989013, -7.1261496], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #('NII-5755A', {'Colour': '#7D6608', 'Order': 14, 'Filter': 'red', 'minI': 933, 'maxI': 1133, 'restWavelength': 5754.64, 'ampList': [0.2383146, -0.0193955, 1.3228768], 'zone': 'low', 'sigmaT2': 11.87, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #('NII-5755A', {'Colour': '#7D6608', 'Order': 13, 'Filter': 'red', 'minI': 3369, 'maxI': 3600, 'restWavelength': 5754.64, 'ampList': [0.2383146, -0.0193955, 1.3228768], 'zone': 'low', 'sigmaT2': 11.87, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #('OI-6300A', {'Colour': '#D35400', 'Order': 19, 'Filter': 'red', 'minI': 960, 'maxI': 1180, 'restWavelength': 6300.3, 'ampList': [0.2383146, -0.0193955, 1.3228768], 'zone': 'low', 'sigmaT2': 10.39, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #('SIII-6312A', {'Colour': '#7D6608', 'Order': 19, 'Filter': 'red', 'minI': 1226, 'maxI': 1425, 'restWavelength': 6310.20, 'ampList': [-287.7876259, 376.2168992, 82.087405], 'zone': 'low', 'sigmaT2': 5.19, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #('SIII-9069A', {'Colour': '#27AE60', 'Order': 35, 'Filter': 'red', 'minI': 1720, 'maxI': 1870, 'restWavelength': 9068.9, 'ampList': [3.8421937, -32687.0427018, 14.1817539], 'zone': 'low', 'sigmaT2': 5.19, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #NO('ArIII-7136A', {'Colour': '#0E6655', 'Order': 25, 'Filter': 'red', 'minI': 1713, 'maxI': 1790, 'restWavelength': 7135.78, 'ampList': [34222774.7678904, 43.1422347, -13.5573009], 'zone': 'low', 'sigmaT2': 4.16, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #NO('HeIH8-3889A', {'Colour': '#5B2C6F', 'Order': 18, 'Filter': 'blue', 'minI': 2250, 'maxI': 2750, 'restWavelength': 3888.65, 'ampList': [-1.3803811, 8.6389436, 5.0423933], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'H-Alpha'}),
        #NO('HeI-4471A', {'Colour': '#78281F', 'Order': 30, 'Filter': 'blue', 'minI': 1750, 'maxI': 1900, 'restWavelength': 4471.48, 'ampList': [14.2789684, 4.0344708, -0.4933927], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'HeI-5876A'}),
        #('HeI-6678A', {'Colour': '#D5D8DC', 'Order': 22, 'Filter': 'red', 'minI': 955, 'maxI': 1200, 'restWavelength': 6678.15, 'ampList': [0.2025295, 0.4079972, 0.8613764], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'HeI-5876A'}),
        #NOsky('HeI-7065A', {'Colour': '#E8DAEF', 'Order': 24, 'Filter': 'red', 'minI': 3150, 'maxI': 3450, 'restWavelength': 7065.19, 'ampList': [0.186136, 1.4569275, 0.1330531], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'HeI-5876A'}),
        #NO('HeI-7281A', {'Colour': '#E8DAEF', 'Order': 26, 'Filter': 'red', 'minI': 1465, 'maxI': 1600, 'restWavelength': 7281.35, 'ampList': [0.1519096, 1.1724037, -0.5370049], 'zone': 'low', 'sigmaT2': 41.54, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'HeI-5876A'}),
        #('NeIII-3868A', {'Colour': 'r', 'Order': 18, 'Filter': 'blue', 'minI': 1250, 'maxI': 1700, 'restWavelength': 3868.75, 'ampList': [3.5213033, 2.0376126, -0.8161938], 'zone': 'high', 'sigmaT2': 8.24, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'OIII-5007A'}),
        ##NO('NeIII-3970A', {'Colour': 'c', 'Order': 20, 'Filter': 'blue', 'minI': 2110, 'maxI': 2290, 'restWavelength': 3970.07, 'ampList': [15340.9535303, -86633.7394279, -71288.2036669], 'zone': 'high', 'sigmaT2': 8.24, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'OIII-5007A'}),
        #('NeIII-3967A', {'Colour': 'm', 'Order': 20, 'Filter': 'blue', 'minI': 2000, 'maxI': 2250, 'restWavelength': 3967.46, 'ampList': [2312.1030516, -33720.7186745, 30544.3475245], 'zone': 'high', 'sigmaT2': 8.24, 'compLimits': {'c': np.inf, 's': np.inf, 'a': np.inf}, 'copyFrom': 'OIII-5007A'}),
    ])

    # Information for the center, sigma and linear for the low (H-alpha) and high (OIII) zones
    centerList = {'low': [6166.52737, 6188.67374, 6193.47753], 'high': [6168.33095, 6190.07761, 6194.95074]}
    sigmaList = {'low': [16.9971820, 50.3988498, 135.296220], 'high': [18.4445474, 49.6392613, 131.164725]}
    linSlope = {'low': -1.1710e-07, 'high': 1.4619e-07}
    linInt = {'low': 0.00245107, 'high': 0.00084421}

    numComps = {'low': 3, 'high': 3}
    componentLabels = ['Narrow 1', 'Narrow 2', 'Broad', 'Label4', 'Label5']
    componentColours = ['b', 'r', 'g', 'c', 'm']
    plottingXRange = [5900, 6400]  # velocities
    sigmaInstrBlue = 5.1
    sigmaInstrRed = 5.7
    distance = 2.68e26  # Distance to region in centimetres (same units as flux)

    emLinesForAvgVelCalc = ['H-Alpha', 'H-Beta', 'OIII-5007A', 'NII-6584A', 'SII-6717A']
    plotResiduals = False  # True

""" NOTES ON HOW TO USE THE ABOVE TABLE
The limits in 'compLimits' can be in the following forms:
    - a list indicating the limits for each component
    - a single number indicating the percentage limits for ALL components
    - a tuple (minValue, maxValue) indicating the min and max not in a percentage
    - inf: indicating that the component cannot vary
    - False: indicating that the value is fixed

ampList
    - if not list it will be take the copyFrom amplitude List and divide by the ampList scalar

numComps:
    - If numComps is not listed in the emProfile dictionary, then the number of components will be taken from the
    numComps variable depending on the zone set in the emProfile dictionary
"""
<commit_msg>Delete profile_info_NGC6845_Region26NewDisp.py<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Load data from CAL-ACCESS' Form 460 into standardized OCD models.
"""
from calaccess_processed import models
from calaccess_processed.management.commands import CalAccessCommand

class Command(CalAccessCommand):
    """
    Load data from CAL-ACCESS' Form 460 into standardized OCD models.
    """
    help = "Load data from CAL-ACCESS' Form 460 into standardized OCD models."

    def add_arguments(self, parser):
        """
        Adds custom arguments specific to this command.
        """
        parser.add_argument(
            "--flush",
            action="store_true",
            dest="flush",
            default=False,
            help="Flush OCD filing models prior to loading"
        )

    def handle(self, *args, **options):
        """
        Make it happen.
        """
        super(Command, self).handle(*args, **options)
        if options['flush']:
            self.flush()
        self.load()

    def flush(self, *args, **kwargs):
        """
        Clear out the OCD models loaded by this command.
        """
        self.header('Flushing OCD data extracted from Form 460 filings')
        model_list = [
            models.OCDTransactionProxy,
            models.OCDFilingActionSummaryAmountProxy,
            models.OCDFilingActionProxy,
            models.OCDFilingProxy,
            models.OCDFilingIdentifierProxy,
            models.OCDCommitteeProxy,
            models.OCDCommitteeIdentifierProxy,
            models.OCDCommitteeNameProxy,
            models.OCDCommitteeTypeProxy,
        ]
        for m in model_list:
            self.log(" Flushing {}".format(m.__name__))
            m.objects.all().delete()

    def load_model(self, model):
        """
        Loads the provided model.
        """
        # Print out what we're doing
        self.log('  Loading {}'.format(model.__name__))

        # Count how many records are in the table now
        before = model.objects.count()

        # Run the update routine
        model.objects.load_form460_data()

        # Count how many records are in the table afterward
        after = model.objects.count()

        # Report the change
        self.log('   {:,} added'.format(after-before))

    def load(self, *args, **kwargs):
        """
        Load data from Form 460 models into OCD models.
        """
        self.header('Loading data extracted from Form 460 filings')

        # Only load committee types if we haven't already
        if not models.OCDCommitteeTypeProxy.objects.exists():
            self.log(' Creating committee types')
            models.OCDCommitteeTypeProxy.objects.seed()

        #
        # Committees
        #

        self.log(' Updating committee roster')
        self.load_model(models.OCDCommitteeProxy)
        self.load_model(models.OCDCommitteeIdentifierProxy)
        self.load_model(models.OCDCommitteeNameProxy)

        #
        # Filings
        #

        self.load_model(models.OCDFilingProxy)
        self.load_model(models.OCDFilingIdentifierProxy)

        #
        # Filing actions
        #

        self.load_model(models.OCDFilingActionProxy)
        self.load_model(models.OCDFilingActionSummaryAmountProxy)

        #
        # Transactions
        #

        self.load_model(models.OCDTransactionProxy)
<commit_msg>Comment out transaction loader until we get the filings and committees ironed out<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Load data from CAL-ACCESS' Form 460 into standardized OCD models.
"""
from calaccess_processed import models
from calaccess_processed.management.commands import CalAccessCommand

class Command(CalAccessCommand):
    """
    Load data from CAL-ACCESS' Form 460 into standardized OCD models.
    """
    help = "Load data from CAL-ACCESS' Form 460 into standardized OCD models."

    def add_arguments(self, parser):
        """
        Adds custom arguments specific to this command.
        """
        parser.add_argument(
            "--flush",
            action="store_true",
            dest="flush",
            default=False,
            help="Flush OCD filing models prior to loading"
        )

    def handle(self, *args, **options):
        """
        Make it happen.
        """
        super(Command, self).handle(*args, **options)
        if options['flush']:
            self.flush()
        self.load()

    def flush(self, *args, **kwargs):
        """
        Clear out the OCD models loaded by this command.
        """
        self.header('Flushing OCD data extracted from Form 460 filings')
        model_list = [
            models.OCDTransactionProxy,
            models.OCDFilingActionSummaryAmountProxy,
            models.OCDFilingActionProxy,
            models.OCDFilingProxy,
            models.OCDFilingIdentifierProxy,
            models.OCDCommitteeProxy,
            models.OCDCommitteeIdentifierProxy,
            models.OCDCommitteeNameProxy,
            models.OCDCommitteeTypeProxy,
        ]
        for m in model_list:
            self.log(" Flushing {}".format(m.__name__))
            m.objects.all().delete()

    def load_model(self, model):
        """
        Loads the provided model.
        """
        # Print out what we're doing
        self.log('  Loading {}'.format(model.__name__))

        # Count how many records are in the table now
        before = model.objects.count()

        # Run the update routine
        model.objects.load_form460_data()

        # Count how many records are in the table afterward
        after = model.objects.count()

        # Report the change
        self.log('   {:,} added'.format(after-before))

    def load(self, *args, **kwargs):
        """
        Load data from Form 460 models into OCD models.
        """
        self.header('Loading data extracted from Form 460 filings')

        # Only load committee types if we haven't already
        if not models.OCDCommitteeTypeProxy.objects.exists():
            self.log(' Creating committee types')
            models.OCDCommitteeTypeProxy.objects.seed()

        #
        # Committees
        #

        self.log(' Updating committee roster')
        self.load_model(models.OCDCommitteeProxy)
        self.load_model(models.OCDCommitteeIdentifierProxy)
        self.load_model(models.OCDCommitteeNameProxy)

        #
        # Filings
        #

        self.load_model(models.OCDFilingProxy)
        self.load_model(models.OCDFilingIdentifierProxy)

        #
        # Filing actions
        #

        self.load_model(models.OCDFilingActionProxy)
        self.load_model(models.OCDFilingActionSummaryAmountProxy)

        #
        # Transactions
        #

        # self.load_model(models.OCDTransactionProxy)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import jsonfield.fields

class Migration(migrations.Migration):

    dependencies = [
        ('structure', '0013_servicesettings_customer'),
    ]

    operations = [
        migrations.AddField(
            model_name='servicesettings',
            name='options',
            field=jsonfield.fields.JSONField(help_text='Extra options', blank=True),
            preserve_default=True,
        ),
    ]
<commit_msg>Fix migration<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import jsonfield.fields

class Migration(migrations.Migration):

    dependencies = [
        ('structure', '0013_servicesettings_customer'),
    ]

    operations = [
        migrations.AddField(
            model_name='servicesettings',
            name='options',
            field=jsonfield.fields.JSONField(help_text='Extra options', blank=True),
            preserve_default=True,
        ),
        migrations.RunSQL(
            "UPDATE structure_servicesettings SET options='\"\"' WHERE options=''"
        ),
    ]
<|endoftext|>


<commit_before>
def process(self):
    # if self.has("LATIN"):
    #     self.scriptTag = scriptPrefixes['latin']
    if self.has("ARMENIAN"):
        # self.scriptTag = scriptPrefixes['armenian']
        self.processAs("Armenian")
    elif self.has("HEBREW"):
        # self.scriptTag = scriptPrefixes['hebrew']
        self.processAs("Hebrew")

    self.edit("LATIN SMALL LIGATURE FFI", "f_f_i")
    self.edit("LATIN SMALL LIGATURE FFL", "f_f_l")
    self.edit("LATIN SMALL LIGATURE FF", "f_f")
    self.edit("LATIN SMALL LIGATURE FI", "fi")
    self.edit("LATIN SMALL LIGATURE FL", "fl")
    self.edit("LATIN SMALL LIGATURE LONG S T", "longs_t")
    self.edit("LATIN SMALL LIGATURE ST", "s_t")

    self.compress()

if __name__ == "__main__":
    from glyphNameFormatter.test import printRange
    printRange("Alphabetic Presentation Forms")
<commit_msg>set script tag to latin for latin alphabetic presentations<commit_after>from glyphNameFormatter.data.scriptPrefixes import scriptPrefixes

def process(self):
    if self.has("LATIN"):
        self.scriptTag = scriptPrefixes['latin']
    if self.has("ARMENIAN"):
        # self.scriptTag = scriptPrefixes['armenian']
        self.processAs("Armenian")
    elif self.has("HEBREW"):
        # self.scriptTag = scriptPrefixes['hebrew']
        self.processAs("Hebrew")

    self.edit("LATIN SMALL LIGATURE FFI", "f_f_i")
    self.edit("LATIN SMALL LIGATURE FFL", "f_f_l")
    self.edit("LATIN SMALL LIGATURE FF", "f_f")
    self.edit("LATIN SMALL LIGATURE FI", "fi")
    self.edit("LATIN SMALL LIGATURE FL", "fl")
    self.edit("LATIN SMALL LIGATURE LONG S T", "longs_t")
    self.edit("LATIN SMALL LIGATURE ST", "s_t")

    self.compress()

if __name__ == "__main__":
    from glyphNameFormatter.test import printRange
    printRange("Alphabetic Presentation Forms")
<|endoftext|>


<commit_before>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from django.core.management import call_command

from uuid import uuid4
from mcfeely.mail import send_mail
from mcfeely.engine import QueueEmailMessage
from mcfeely.engine import QueueEmailMultiAlternatives
from mcfeely.models import Email, Recipient
from mcfeely.models import Queue
from mcfeely.models import Unsubscribe
from django.conf import settings
from django.core import mail

class SimpleTest(TestCase):
    def setUp(self):
        mcfeely_backend = 'mcfeely.backend.DbBackend'
        test_sender = 'django.core.mail.backends.locmem.EmailBackend'

        settings.EMAIL_BACKEND = mcfeely_backend
        settings.MCFEELY_EMAIL_BACKEND = test_sender

        self.q = Queue(queue='Test_Queue', description='Testing')
        self.q.save()

        self.unsub_all = Unsubscribe(address='unsub1@example.com')
        self.unsub_all.save()

        self.unsub_testqueue = Unsubscribe(
            address='unsub2@example.com',
            queue=self.q)
        self.unsub_testqueue.save()

    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)

    def _send_mail_simple(self, subject=uuid4(), mail_to=['tester@example.com'], queue=None):
        send_mail(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            queue=queue,
            fail_silently=False)
        return str(subject)

    def _send_mail_advanced(self, subject=uuid4(), mail_to=['tester@example.com'],
                   mail_cc=None, mail_bcc=None, headers=None, queue=None):
        email = QueueEmailMessage(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            cc=mail_cc,
            bcc=mail_bcc,
            queue=queue,
            headers=headers)

        email.send()
        return str(subject)

    def _send_mail_attachment(self, subject=uuid4(),
                              mail_to=['tester@example.com'], queue=None):
        email = QueueEmailMessage(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            queue=queue)

        email.attach(
            'sample.txt',
            'Sample attachement Text',
            'text/plain')

        email.send()
        return str(subject)

    def _send_mail_alternative(self, subject=uuid4(),
                               mail_to=['tester@example.com'], queue=None):
        email = QueueEmailMultiAlternatives(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            queue=queue)

        email.attach_alternative(
            'Test Alternative',
            'text/html')

        email.send()
        return str(subject)

    def test_simple_mail(self):
        subject = self._send_mail_simple()
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)

    def test_advanced_mail(self):
        subject = self._send_mail_advanced(
            mail_cc = ['cctest@example.com'],
            mail_bcc = ['bcctest@example.com'],
            headers={'Reply-To': 'another@example.com'},
            queue = self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)
        self.assertEqual(len(mail.outbox[0].bcc), 1)
        self.assertEqual(len(mail.outbox[0].cc), 1)
        self.assertEqual(
            mail.outbox[0].extra_headers['Reply-To'],
            'another@example.com')

    def test_queue_mail(self):
        subject = self._send_mail_simple(queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)

    def test_attachment(self):
        subject = self._send_mail_attachment(queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].attachments), 1)

    def test_alternative(self):
        subject = self._send_mail_alternative(queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].alternatives), 1)

    def test_unsubscribe(self):
        subject = self._send_mail_simple(
            mail_to=['unsub1@example.com',
            'tester@example.com'])
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)

    def test_recipient_status(self):
        subject = self._send_mail_simple(queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(
            Email.objects.get(subject=subject, queue=self.q).recipient_set.get().status,
            'sent_success'
        )
<commit_msg>Cleaning up tests<commit_after>"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from django.core.management import call_command
from django.conf import settings
from django.core import mail
from django.core.mail import send_mail as orig_send_mail

from uuid import uuid4

from mcfeely.mail import send_mail
from mcfeely.mail import mail_admins
from mcfeely.mail import mail_managers

from mcfeely.engine import QueueEmailMessage
from mcfeely.engine import QueueEmailMultiAlternatives
from mcfeely.models import Email
from mcfeely.models import Queue
from mcfeely.models import Unsubscribe

default_to = ['tester@example.com']
default_from = 'testemail@example.com'
default_message = "Test E-Mail body"

def django_send_mail(mail_to=default_to):
    subject = str(uuid4())
    results = orig_send_mail(
        subject,
        default_message,
        default_from,
        mail_to,
        fail_silently=False)
    return [subject, results]

def mcfeely_send_mail(mail_to=default_to, queue=None):
    subject = str(uuid4())
    results = send_mail(
        subject,
        default_message,
        default_from,
        mail_to,
        queue=queue,
        fail_silently=False)
    return [subject, results]

def mcfeely_mail_admins(queue=None):
    subject = str(uuid4())
    results = mail_admins(
        subject,
        default_message,
        queue)
    return [subject, results]

def mcfeely_mail_managers(queue=None):
    subject = str(uuid4())
    results = mail_managers(
        subject,
        default_message,
        queue)
    return [subject, results]

def simple_mail(mail_to=default_to, queue=None):
    subject = str(uuid4())
    email = QueueEmailMessage(
        subject,
        default_message,
        default_from,
        mail_to,
        queue=queue)
    return [subject, email]

def simple_alternative_mail(mail_to=default_to, queue=None):
    subject = str(uuid4())
    email = QueueEmailMultiAlternatives(
        subject,
        default_message,
        default_from,
        mail_to,
        queue=queue)
    return [subject, email]

def mail_attachment(mail_to=default_to, queue=None):
    subject, email = simple_mail(mail_to, queue)
    email.attach(
        'sample.txt',
        'Sample attachement Text',
        'text/plain')

    results = email.send()
    return [subject, results]

def mail_alternative(mail_to=default_to, queue=None):
    subject, email = simple_alternative_mail(mail_to, queue)
    email.attach_alternative(
        'Test Alternative',
        'text/html')

    results = email.send()
    return [subject, results]

# Start of Tests
class Simple_Email(TestCase):

    def setUp(self):
        mcfeely_backend = 'mcfeely.backend.DbBackend'
        test_sender = 'django.core.mail.backends.locmem.EmailBackend'

        settings.EMAIL_BACKEND = mcfeely_backend
        settings.MCFEELY_EMAIL_BACKEND = test_sender
        settings.ADMINS = (
            ('Admin One', 'admin_one@example.com'),
            ('Admin Two', 'admin_two@example.com')
        )
        settings.MANAGERS = (
            ('Manager One', 'manager_one@example.com'),
            ('Manager Two', 'manager_two@example.com')
        )

        self.queue = Queue(queue='Test_Queue', description='Testing')
        self.queue.save()

    def test_django_send_mail(self):
        """
            Tests django's send_mail function with the mcfeely backend
        """
        subject, results = django_send_mail()
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(results, 1)

    def test_send_mail(self):
        """
            Tests the mcfeely send_mail() function
        """
        subject, results = mcfeely_send_mail()
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(results, 1)

    def test_queue_mail(self):
        """
            Tests the mcfeely send_mail() function with queue
        """
        subject, results = mcfeely_send_mail(queue=self.queue)
        Email.objects.get(subject=subject, queue=self.queue)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(results, 1)

    def test_mail_admins(self):
        """
            Tests the mcfeely mail_admins() function
        """
        subject, results = mcfeely_mail_admins()
        Email.objects.get(subject="[Django] %s" % subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)

    def test_mail_managers(self):
        """
            Tests the mcfeely mail_admins() function
        """
        subject, results = mcfeely_mail_managers()
        Email.objects.get(subject="[Django] %s" % subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)

    def test_simple_mail(self):
        subject, email = simple_mail()
        results = email.send()
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(results, 1)

class Advanced_Email(TestCase):

    def setUp(self):
        mcfeely_backend = 'mcfeely.backend.DbBackend'
        test_sender = 'django.core.mail.backends.locmem.EmailBackend'

        settings.EMAIL_BACKEND = mcfeely_backend
        settings.MCFEELY_EMAIL_BACKEND = test_sender

        self.queue = Queue(queue='Test_Queue', description='Testing')
        self.queue.save()

    def test_attachment(self):
        subject, results = mail_attachment(queue=self.queue)
        Email.objects.get(subject=subject, queue=self.queue)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].attachments), 1)

    def test_alternative(self):
        subject, results = mail_alternative(queue=self.queue)
        Email.objects.get(subject=subject, queue=self.queue)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].alternatives), 1)

class Simple(TestCase):

    def setUp(self):
        mcfeely_backend = 'mcfeely.backend.DbBackend'
        test_sender = 'django.core.mail.backends.locmem.EmailBackend'

        settings.EMAIL_BACKEND = mcfeely_backend
        settings.MCFEELY_EMAIL_BACKEND = test_sender

        self.q = Queue(queue='Test_Queue', description='Testing')
        self.q.save()

        self.unsub_all = Unsubscribe(address='unsub1@example.com')
        self.unsub_all.save()

        self.unsub_testqueue = Unsubscribe(
            address='unsub2@example.com',
            queue=self.q)
        self.unsub_testqueue.save()

    def _send_mail_simple(self, subject=uuid4(), mail_to=['tester@example.com'], queue=None):
        send_mail(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            queue=queue,
            fail_silently=False)
        return str(subject)

    def _send_mail_advanced(
        self, subject=uuid4(), mail_to=['tester@example.com'],
        mail_cc=None, mail_bcc=None, headers=None, queue=None):
        email = QueueEmailMessage(
            str(subject),
            'test email',
            'test@example.com',
            mail_to,
            cc=mail_cc,
            bcc=mail_bcc,
            queue=queue,
            headers=headers)

        email.send()
        return str(subject)

    def test_advanced_mail(self):
        subject = self._send_mail_advanced(
            mail_cc=['cctest@example.com'],
            mail_bcc=['bcctest@example.com'],
            headers={'Reply-To': 'another@example.com'},
            queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)
        self.assertEqual(len(mail.outbox[0].bcc), 1)
        self.assertEqual(len(mail.outbox[0].cc), 1)
        self.assertEqual(
            mail.outbox[0].extra_headers['Reply-To'],
            'another@example.com')

    def test_unsubscribe(self):
        subject = self._send_mail_simple(
            mail_to=['unsub1@example.com',
                     'tester@example.com'])
        Email.objects.get(subject=subject)

        call_command('send_queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)

    def test_recipient_status(self):
        subject = self._send_mail_simple(queue=self.q)
        Email.objects.get(subject=subject, queue=self.q)

        call_command('send_queue', 'Test_Queue')
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(
            Email.objects.get(
                subject=subject, queue=self.q).recipient_set.get().status,
            'sent_success'
        )
<|endoftext|>


<commit_before>'''
Created on May 17, 2014

@author: anroco
'''

from flask import Blueprint, abort, g, request, current_app
from flask.ext import restful
from commons import validate_user_auth, decrypt_token
from dynamoDBqueries import User

auth = Blueprint('auth', __name__)
api = restful.Api(auth)
PATHS_EXCEPTION = ['/api/1.0/auth/register/']

@auth.before_app_request
def authentication_user():
    '''
    () -> None

    Se ejecuta cada vez que se realiza una solicitud de recursos de la app.
    Valida si el usuario esta autenticado, verifica si el access_token y
    token_secret generado al autenticarse el usuario con su cuenta de twitter
    es correcto.
    '''

    #verifica si el recurso solicitado necesita que el usuario este autenticado
    if request.method != 'GET' and '/auth/' in request.path:
        user = User()
        user_item = validate_user_auth(request.values['token_user'])
        user_dict = user.user_skills(user_item._data)

        #verificar si se el usuario ya registro el email y sus habilidades
        #validando todos los path a excepcion de los que se encuentran
        #incluidos en la lista PATHS_EXCEPTION
        if not request.path in PATHS_EXCEPTION and \
            (not 'email' in user_dict or user_dict['email'] == '' or \
             not len(user_dict['skills'])):

            abort(428)

        g.user_item = user_item
        g.user_skills = user_dict['skills']
#     else:
#         decrypt_token(request.values['token_user'],
#                       current_app.config['SECRET_KEY_ANONYMOUS'])
<commit_msg>WORKING!!!!! IN AWS!!!!!!!!<commit_after>'''
Created on May 17, 2014

@author: anroco
'''

from flask import Blueprint, abort, g, request, current_app
from flask.ext import restful
from commons import validate_user_auth, decrypt_token
from dynamoDBqueries import User

auth = Blueprint('auth', __name__)
api = restful.Api(auth)
PATHS_EXCEPTION = ['/api/1.0/auth/register/']

@auth.before_app_request
def authentication_user():
    '''
    () -> None

    Se ejecuta cada vez que se realiza una solicitud de recursos de la app.
    Valida si el usuario esta autenticado, verifica si el access_token y
    token_secret generado al autenticarse el usuario con su cuenta de twitter
    es correcto.
    '''

    #verifica si el recurso solicitado necesita que el usuario este autenticado
    if request.method != 'GET' and '/auth/' in request.path:
        user = User()
        user_item = validate_user_auth(request.values['token_user'])
        user_dict = user.user_skills(user_item._data)

        #verificar si se el usuario ya registro el email y sus habilidades
        #validando todos los path a excepcion de los que se encuentran
        #incluidos en la lista PATHS_EXCEPTION
        if not request.path in PATHS_EXCEPTION and \
            (not 'email' in user_dict or user_dict['email'] == '' or \
             not len(user_dict['skills'])):

            abort(428)

        g.user_item = user_item
        g.user_skills = user_dict['skills']
    else:
        decrypt_token(request.values['token_user'],
                      current_app.config['SECRET_KEY_ANONYMOUS'])
<|endoftext|>


<commit_before>from addie.processing.mantid.master_table.tree_definition import LIST_COLUMNS_TO_SEARCH_FOR_FULL_HIGHLIGTHING

class ColumnHighlighting:

    def __init__(self, main_window=None, column=-1):
        self.main_window = main_window
        self.column = column

    def check(self):
        print("checking column {}".format(self.column))

<commit_msg>Prototypes background cells color for items and widgets (just one of them so far)<commit_after>import numpy as np
from qtpy import QtGui
from qtpy.QtWidgets import QTableWidgetItem

from addie.processing.mantid.master_table.tree_definition import (INDEX_OF_COLUMNS_SEARCHABLE,
                                                                  INDEX_OF_COLUMNS_WITH_CHEMICAL_FORMULA,
                                                                  INDEX_OF_COLUMNS_WITH_MASS_DENSITY,
                                                                  INDEX_OF_COLUMNS_WITH_GEOMETRY_INFOS,
                                                                  INDEX_OF_ABS_CORRECTION,
                                                                  INDEX_OF_MULTI_SCATTERING_CORRECTION,
                                                                  INDEX_OF_INELASTIC_CORRECTION,
                                                                  LIST_COLUMNS_TO_SEARCH_FOR_FULL_HIGHLIGTHING)

class ColumnHighlighting:

    def __init__(self, main_window=None, column=-1):
        self.main_window = main_window
        self.column = column

        self.nbr_row = self.get_nbr_row()

    def get_nbr_row(self):
        nbr_row = self.main_window.processing_ui.h3_table.rowCount()
        return nbr_row

    def check(self):
        column = self.column

        for _row in np.arange(self.nbr_row):

            # item
            if column in INDEX_OF_COLUMNS_SEARCHABLE:
                are_all_the_same = self.are_cells_identical()

            elif column in INDEX_OF_COLUMNS_WITH_CHEMICAL_FORMULA:
                are_all_the_same = self.are_chemical_formula_identical()

            elif column in INDEX_OF_COLUMNS_WITH_MASS_DENSITY:
                are_all_the_same = self.are_mass_density_identical()

            elif column in INDEX_OF_COLUMNS_WITH_GEOMETRY_INFOS:
                are_all_the_same = self.are_geometry_identical()

            elif (column in INDEX_OF_ABS_CORRECTION) or \
                    (column in INDEX_OF_MULTI_SCATTERING_CORRECTION):
                are_all_the_same = self.are_abs_or_multi_correction_identical()

            elif column in INDEX_OF_INELASTIC_CORRECTION:
                are_all_the_same = self.are_inelastic_correction_identical()

            elif column in LIST_COLUMNS_TO_SEARCH_FOR_FULL_HIGHLIGTHING[-1]:
                are_all_the_same = self.are_align_and_focus_args_identical()

        self.apply_cell_background(are_all_the_same=are_all_the_same)

    def apply_cell_background(self, are_all_the_same=False):
        if are_all_the_same:
            background_color = QtGui.QColor(255, 255, 0)
            background_color_stylesheet = "rgb(255, 255, 0)"
        else:
            background_color = QtGui.QColor(255, 255, 255)
            background_color_stylesheet = "rgb(255, 255, 255)"

        for _row in np.arange(self.nbr_row):

            if self.column == 5:
                main_widget = self.main_window.processing_ui.h3_table.cellWidget(_row, self.column)
                main_widget.setAutoFillBackground(True)
                main_widget.setStyleSheet("background-color: {};".format(background_color_stylesheet))
            else:
                self.main_window.processing_ui.h3_table.item(_row, self.column).setBackground(background_color)

    def are_cells_identical(self):
        return True

    def are_chemical_formula_identical(self):
        return True

    def are_mass_density_identical(self):
        return False

    def are_geometry_identical(self):
        return False

    def are_abs_or_multi_correction_identical(self):
        return False

    def are_inelastic_correction_identical(self):
        return False

    def are_align_and_focus_args_identical(self):
        return False

<|endoftext|>


<commit_before>from tinydb import TinyDB, Query
from tinydb.operations import increment
import time
from threading import RLock

from settings import DEFAULT_DIR, DATABASE_FN
from utils import log, hash_string

class MessageStore():
    def __init__(self, size_threshold=None):
        self.size_threshold = size_threshold
        self.db = TinyDB(DEFAULT_DIR + DATABASE_FN)
        self.db.purge()
        self.stats = self.db.table('stats')
        self.messages = self.db.table('messages')
        self.lock = RLock()

    def create_new_message(self, message, hash, time):
        self.messages.insert({'idx': hash, 'content': message})
        self.stats.insert({'idx': hash,
                           'first_seen': time,
                           'receive_count': 0,
                           'send_count': 0,
                           'last_received': None,
                           'last_sent': None,
                           'deleted': False})

    def add_message(self, message):
        idx = hash_string(message)
        now = int(time.time())
        with self.lock:
            Stats = Query()
            res = self.stats.search(Stats.idx == idx)
            if len(res) == 0:
                self.create_new_message(message, idx, now)
                log("message inserted: {}".format(message))
            else:
                self.stats.update({'last_received': now}, Stats.idx == idx)
                self.stats.update(increment('receive_count'), Stats.idx == idx)

    def get_messages(self, count=1):
        with self.lock:
            stats = self.stats.all()
            res = sorted(stats, key=lambda x: (x['receive_count'], x['send_count'], x['last_sent']))[:10]
            now = int(time.time())
            messages = []
            for r in res:
                idx = r['idx']
                Messages = Query()
                msg = self.messages.search(Messages.idx == idx)[0]['content']
                messages.append(msg)
                self.stats.update({'last_sent': now}, Messages.idx == idx)
                self.stats.update(increment('send_count'), Messages.idx == idx)
        return messages
<commit_msg>add standalone functionality to send messages<commit_after>from tinydb import TinyDB, Query
from tinydb.operations import increment
import time
from threading import RLock
from argparse import ArgumentParser

from settings import DEFAULT_DIR, DATABASE_FN
from utils import log, hash_string

class MessageStore():
    def __init__(self, size_threshold=None):
        self.size_threshold = size_threshold
        self.db = TinyDB(DEFAULT_DIR + DATABASE_FN)
        self.db.purge()
        self.stats = self.db.table('stats')
        self.messages = self.db.table('messages')
        self.lock = RLock()

    def create_new_message(self, message, hash, time):
        self.messages.insert({'idx': hash, 'content': message})
        self.stats.insert({'idx': hash,
                           'first_seen': time,
                           'receive_count': 0,
                           'send_count': 0,
                           'last_received': None,
                           'last_sent': None,
                           'deleted': False})

    def add_message(self, message):
        idx = hash_string(message)
        now = int(time.time())
        with self.lock:
            Stats = Query()
            res = self.stats.search(Stats.idx == idx)
            if len(res) == 0:
                self.create_new_message(message, idx, now)
                log("message inserted: {}".format(message))
            else:
                self.stats.update({'last_received': now}, Stats.idx == idx)
                self.stats.update(increment('receive_count'), Stats.idx == idx)

    def get_messages(self, count=1):
        with self.lock:
            stats = self.stats.all()
            res = sorted(stats, key=lambda x: (x['receive_count'], x['send_count'], x['last_sent']))[:10]
            now = int(time.time())
            messages = []
            for r in res:
                idx = r['idx']
                Messages = Query()
                msg = self.messages.search(Messages.idx == idx)[0]['content']
                messages.append(msg)
                self.stats.update({'last_sent': now}, Messages.idx == idx)
                self.stats.update(increment('send_count'), Messages.idx == idx)
        return messages

if __name__ == '__main__':
    parser = ArgumentParser(description='Manage aDTN messages')
    parser.add_argument('-c', metavar="message", type=str, dest="message", default=None, help='create a message and add it to the message store for later sending')
    args = parser.parse_args()
    if args.message is not None:
        ms = MessageStore()
        ms.add_message(args.message)<|endoftext|>


<commit_before>from __future__ import print_function
from __future__ import division
import datetime
import socket
import time
import struct
import binascii
import argparse

class Arp_Ping(object):
	"""Run a Arp ping against the target to get there mac the give it to the arp cache poison"""
	def __init__(self, interface):
		global s, sent, rev
		sent = 0
		rev = 0
		super(Arp_Ping, self).__init__()
		s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
		try:
			s.bind((interface, socket.htons(0x0800)))
		except socket.error:
			print("\033[1;31mUnable to bind to interface... unknown type\033[00m")
			exit()

	def craft_packet(self, requested_ip=None, responce_to_ip=None, responce_to_mac=None, send_to_mac=None):
		#thanks to techb for help
		if requested_ip and responce_to_ip and responce_to_mac and send_to_mac != None:
			#target_mac='FF:FF:FF:FF:FF:FF'
			arp_header_code = '\x08\x06'
			target_ip = socket.inet_aton(requested_ip)
			target_mac = binascii.unhexlify(''.join(send_to_mac.split(':')))
			redirect_to_ip = socket.inet_aton(responce_to_ip)
			redirect_to_mac = binascii.unhexlify(''.join(responce_to_mac.split(':')))

			#Ethernet headers
			eth_head =  bytes(target_mac)+bytes(redirect_to_mac)+bytes(arp_header_code.encode('utf-8'))

			#Arp headers
			header_type = '\x00\x01'
			protocol = '\x08\x00'
			mac_size = '\x06'
			ip_size = '\x04'
			option_code = '\x00\x01'

			arp_head = header_type + protocol + mac_size + ip_size + option_code

			#Spoofed Bit
			spoofed_part = redirect_to_mac + redirect_to_ip + target_mac + target_ip
			#Final Packet
			arp_packet = bytes(eth_head) + bytes(arp_head.encode('utf-8')) + bytes(spoofed_part)
			return arp_packet

	def format_mac(self, bin_mac):
		temp = bin_mac.replace(":", "").replace("-", "").replace(".", "")
		return temp[:2] + ":" + ":".join([temp[i] + temp[i+1] for i in range(2,12,2)])

	def await_responce(self, iface):
		global rev
		r = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
		pkt = r.recvfrom(2048)

		eth = pkt[0][0:14]
		eth_d = struct.unpack("!6s6s2s", eth)
		res = binascii.hexlify(eth_d[0])

		dst_mac = self.format_mac(res.decode('utf-8'))

		local_mac = open('/sys/class/net/{}/address'.format(iface)).read().strip('\n')

		if dst_mac == local_mac:
			stop_time = datetime.datetime.now()
			arp_h = pkt[0][14:42]
			arp_d = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_h)
			timee = stop_time - start_time
			rev += 1
			return self.format_mac(binascii.hexlify(arp_d[5]).decode('utf-8')), arp_d[6], timee.total_seconds() * 1000

	def ping(self, addr):
		global start_time, sent
		request_packet = self.craft_packet(requested_ip=addr, responce_to_ip='192.168.1.199', responce_to_mac='5c:e0:c5:a0:d7:91', send_to_mac='FF:FF:FF:FF:FF:FF')
		s.send(request_packet)
		start_time = datetime.datetime.now()
		sent += 1

if __name__ == '__main__':
	programme_start = datetime.datetime.now()
	print("\033[1;3mArping v1\033[00m")

	ap = argparse.ArgumentParser(description="Theseus")
	ap.add_argument("-t", help="This is the targets ip address", required=True)
	ap.add_argument("-i", help="This is the network cards current interface", required=True)
	args = ap.parse_args()

	p = Arp_Ping(args.i)

	times = []

	o = -1
	while True:
		try:
			o += 1
			p.ping(args.t)
			mac, ip, timee = p.await_responce(args.i)
			try:
				ip = socket.inet_ntop(socket.AF_INET, ip)
				pkt_from = socket.gethostbyaddr(ip)[0]
			except socket.error as e:
				print(type(e))

			print(pkt_from+': ('+ip, 'is at', mac+')', 'index='+str(o),'time='+str(timee)+'ms')
			times.append(timee)
			time.sleep(1)
		except KeyboardInterrupt:
			programme_stop = datetime.datetime.now()
			overall_time = programme_stop - programme_start
			avg = sum(times) / float(len(times))
			print("\n--- {} arping statistics ---".format(args.t))
			print("{} packets transmitted, {} packets received, time {}ms".format(sent, rev, overall_time.total_seconds() * 1000))
			print("min/avg/max = {}/{}/{}".format(min(times), avg, max(times)))
			exit()<commit_msg>arp ping mac and ip from cfg file<commit_after>from __future__ import print_function
from __future__ import division
import datetime
import socket
import time
import struct
import binascii
import argparse

class Arp_Ping(object):
	"""Run a Arp ping against the target to get there mac the give it to the arp cache poison"""
	def __init__(self, interface):
		global s, sent, rev
		sent = 0
		rev = 0
		super(Arp_Ping, self).__init__()
		s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
		try:
			s.bind((interface, socket.htons(0x0800)))
		except socket.error:
			print("\033[1;31mUnable to bind to interface... unknown type\033[00m")
			exit()

	def craft_packet(self, requested_ip=None, responce_to_ip=None, responce_to_mac=None, send_to_mac=None):
		#thanks to techb for help
		if requested_ip and responce_to_ip and responce_to_mac and send_to_mac != None:
			#target_mac='FF:FF:FF:FF:FF:FF'
			arp_header_code = '\x08\x06'
			target_ip = socket.inet_aton(requested_ip)
			target_mac = binascii.unhexlify(''.join(send_to_mac.split(':')))
			redirect_to_ip = socket.inet_aton(responce_to_ip)
			redirect_to_mac = binascii.unhexlify(''.join(responce_to_mac.split(':')))

			#Ethernet headers
			eth_head =  bytes(target_mac)+bytes(redirect_to_mac)+bytes(arp_header_code.encode('utf-8'))

			#Arp headers
			header_type = '\x00\x01'
			protocol = '\x08\x00'
			mac_size = '\x06'
			ip_size = '\x04'
			option_code = '\x00\x01'

			arp_head = header_type + protocol + mac_size + ip_size + option_code

			#Spoofed Bit
			spoofed_part = redirect_to_mac + redirect_to_ip + target_mac + target_ip
			#Final Packet
			arp_packet = bytes(eth_head) + bytes(arp_head.encode('utf-8')) + bytes(spoofed_part)
			return arp_packet

	def format_mac(self, bin_mac):
		temp = bin_mac.replace(":", "").replace("-", "").replace(".", "")
		return temp[:2] + ":" + ":".join([temp[i] + temp[i+1] for i in range(2,12,2)])

	def await_responce(self, iface):
		global rev
		r = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
		pkt = r.recvfrom(2048)

		eth = pkt[0][0:14]
		eth_d = struct.unpack("!6s6s2s", eth)
		res = binascii.hexlify(eth_d[0])

		dst_mac = self.format_mac(res.decode('utf-8'))

		local_mac = open('/sys/class/net/{}/address'.format(iface)).read().strip('\n')

		if dst_mac == local_mac:
			stop_time = datetime.datetime.now()
			arp_h = pkt[0][14:42]
			arp_d = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_h)
			timee = stop_time - start_time
			rev += 1
			return self.format_mac(binascii.hexlify(arp_d[5]).decode('utf-8')), arp_d[6], timee.total_seconds() * 1000

	def ping(self, addr, local, iface):
		global start_time, sent
		local_mac = open('/sys/class/net/{}/address'.format(iface)).read().strip('\n')
		request_packet = self.craft_packet(requested_ip=addr, responce_to_ip=local, responce_to_mac=local_mac, send_to_mac='FF:FF:FF:FF:FF:FF')
		s.send(request_packet)
		start_time = datetime.datetime.now()
		sent += 1

if __name__ == '__main__':
	programme_start = datetime.datetime.now()
	print("\033[1;3mArping v1\033[00m")

	ap = argparse.ArgumentParser(description="Theseus")
	ap.add_argument("-t", help="This is the targets ip address", required=True)
	ap.add_argument("-i", help="This is the network cards current interface", required=True)
	args = ap.parse_args()

	p = Arp_Ping(args.i)

	times = []

	o = -1
	while True:
		try:
			o += 1
			p.ping(args.t)
			mac, ip, timee = p.await_responce(args.i)
			try:
				ip = socket.inet_ntop(socket.AF_INET, ip)
				pkt_from = socket.gethostbyaddr(ip)[0]
			except socket.error as e:
				print(type(e))

			print(pkt_from+': ('+ip, 'is at', mac+')', 'index='+str(o),'time='+str(timee)+'ms')
			times.append(timee)
			time.sleep(1)
		except KeyboardInterrupt:
			programme_stop = datetime.datetime.now()
			overall_time = programme_stop - programme_start
			avg = sum(times) / float(len(times))
			print("\n--- {} arping statistics ---".format(args.t))
			print("{} packets transmitted, {} packets received, time {}ms".format(sent, rev, overall_time.total_seconds() * 1000))
			print("min/avg/max = {}/{}/{}".format(min(times), avg, max(times)))
			exit()<|endoftext|>


<commit_before>import marisa_trie
from pluginDBAPI import DBAPI, VerificationStatus
from plugin.common import PluginType, PluginTypeError
import logging

class Core:

    def __init__(self, plugins):
        self.plugins = plugins
        self.log = logging.getLogger("crawlcheck")

    def initialize(self, uriAcceptor, typeAcceptor, db, entryPoints, maxDepth, agent, uriMap):
        self.db = DBAPI(db)
        self.uriAcceptor = uriAcceptor
        self.typeAcceptor = typeAcceptor
        self.agent = agent
        self.maxDepth = maxDepth
        self.uriMap = uriMap

        self.queue = Queue(self.db)
	self.queue.load()

        self.journal = Journal(self.db)
        self.journal.load()

        self.rack = Rack(self.plugins, uriAcceptor, typeAcceptor)

        for entryPoint in entryPoints:
            self.queue.push(Transaction(entryPoint, 0))

        for plugin in self.plugins:
            self.__initializePlugin(plugin, typeAcceptor, uriAcceptor)

    def run(self):
        while not self.queue.isEmpty():
            transaction = self.queue.pop()

            if transaction.depth > self.maxDepth:
                continue #skip

            self.log.info("Processing "+transaction.uri)
            try:
                transaction.loadResponse(self.uriAcceptor, self.uriMap, self.journal, self.agent)
                self.rack.run(transaction)
            except TouchException, NetworkError:
                self.journal.stopChecking(transaction, VerificationStatus.done_ko)
                raise
            self.journal.stopChecking(transaction, VerificationStatus.done_ok)

    def finalize(self):
        self.queue.store()
        self.journal.store()

    def __initializePlugin(self, plugin, typeAcceptor, uriAcceptor):
        plugin.setJournal(self.journal)

        # TODO: refactor to be more Pythonic
        if plugin.type == PluginType.CRAWLER:
            plugin.setTypes(typeAcceptor.getValues())
            plugin.setUris(uriAcceptor.getValues())
            plugin.setQueue(self.queue)

        elif plugin.type == PluginType.CHECKER:
            pass

        else:
            #FATAL ERROR: unknown plugin type
            raise PluginTypeError

class TouchException(Exception):

    pass

class Transaction:

   def __init__(self, uri, depth, srcId = -1):
       self.uri = uri
       self.depth = depth
       self.type = None
       self.file = None

   def loadResponse(self, uriAcceptor, uriMap, journal, agent):
       if self.isTouchable(uriAcceptor):
           try:
               acceptedTypes = self.__get_accepted_types(uriMap)
               self.type, self.file = Network.getLink(self.uri, self, journal, agent, acceptedTypes)
           except NetworkError:
               raise
       else:
           raise TouchException()

   def getContent(self):
        return "" #info.getContent().encode('utf-8')

   def isTouchable(self, uriAcceptor):
       return uriAcceptor.defaultAcceptValue(self.uri) != Resolution.no

   def getMaxPrefix(self, uriAcceptor):

       prefixes = uriAcceptor.getValues()

       # seznam prefixu, pro nas uri chceme nejdelsi prefix
       trie = marisa_trie.Trie(prefixes)
       prefList = trie.prefixes(unicode(str(self.uri), encoding="utf-8"))

       if len(prefList) > 0:
           return prefList[-1]

       else:
           return uri

   def __get_accepted_types(self, uriMap):
        if uriMap[self.uri]:
            return uriMap[self.uri]
        else:
            return []

class Rack:

    def __init__(self, plugins = [], uriAcceptor, typeAcceptor):
        self.plugins = plugins
        self.uriAcceptor = uriAcceptor
        self.typeAcceptor = typeAcceptor

    def run(self, transaction):
        log = logging.getLogger("crawlcheck")
        for plugin in self.plugins:
            fakeTransaction = transaction
            fakeTransaction.uri = transaction.getMaxPrefix(uriAcceptor)
            if self.__accept(fakeTransaction, plugin):
                log.info(plugin.id + " started checking " + transaction.uri)
                plugin.check(transaction)
                log.info(plugin.id + " stopped checking " + transaction.uri)

    def insert(self, plugin):
        self.plugins.append(plugin)

    def __accept(fakeTransaction, plugin):
        return self.typeAcceptor.accept(fakeTransaction, plugin.id) and self.uriAcceptor.accept(fakeTransaction, plugin.id)

class Queue:

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

    def isEmpty(self):
        pass

    def pop(self):
        pass

    def push(self, transaction, parent=None):
        pass

    def load(self):
        pass

    def store(self):
        pass

class Journal:

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

    def load(self):
        pass

    def store(self):
        pass

    def startChecking(self, transaction):
        pass

    def stopChecking(self, transaction, status):
        pass

    def foundDefect(self, transaction, defect):
        pass

class Defect:

    def __init__(self, name, additional):
        self.name = name
        self.additional = additional

<commit_msg>Accept header<commit_after>import marisa_trie
from pluginDBAPI import DBAPI, VerificationStatus
from plugin.common import PluginType, PluginTypeError
import logging

class Core:

    def __init__(self, plugins):
        self.plugins = plugins
        self.log = logging.getLogger("crawlcheck")

    def initialize(self, uriAcceptor, typeAcceptor, db, entryPoints, maxDepth, agent, uriMap):
        self.db = DBAPI(db)
        self.uriAcceptor = uriAcceptor
        self.typeAcceptor = typeAcceptor
        self.agent = agent
        self.maxDepth = maxDepth
        self.uriMap = uriMap

        self.queue = Queue(self.db)
	self.queue.load()

        self.journal = Journal(self.db)
        self.journal.load()

        self.rack = Rack(self.plugins, uriAcceptor, typeAcceptor)

        for entryPoint in entryPoints:
            self.queue.push(Transaction(entryPoint, 0))

        for plugin in self.plugins:
            self.__initializePlugin(plugin, typeAcceptor, uriAcceptor)

    def run(self):
        while not self.queue.isEmpty():
            transaction = self.queue.pop()

            if transaction.depth > self.maxDepth:
                continue #skip

            self.log.info("Processing "+transaction.uri)
            try:
                transaction.loadResponse(self.uriAcceptor, self.uriMap, self.journal, self.agent)
                self.rack.run(transaction)
            except TouchException, NetworkError:
                self.journal.stopChecking(transaction, VerificationStatus.done_ko)
                raise
            self.journal.stopChecking(transaction, VerificationStatus.done_ok)

    def finalize(self):
        self.queue.store()
        self.journal.store()

    def __initializePlugin(self, plugin, typeAcceptor, uriAcceptor):
        plugin.setJournal(self.journal)

        # TODO: refactor to be more Pythonic
        if plugin.type == PluginType.CRAWLER:
            plugin.setTypes(typeAcceptor.getValues())
            plugin.setUris(uriAcceptor.getValues())
            plugin.setQueue(self.queue)

        elif plugin.type == PluginType.CHECKER:
            pass

        else:
            #FATAL ERROR: unknown plugin type
            raise PluginTypeError

class TouchException(Exception):

    pass

class Transaction:

   def __init__(self, uri, depth, srcId = -1):
       self.uri = uri
       self.depth = depth
       self.type = None
       self.file = None

   def loadResponse(self, uriAcceptor, uriMap, journal, agent):
       if self.isTouchable(uriAcceptor):
           try:
               acceptedTypes = self.__get_accepted_types(uriMap, uriAcceptor)
               self.type, self.file = Network.getLink(self.uri, self, journal, agent, acceptedTypes)
           except NetworkError:
               raise
       else:
           raise TouchException()

   def getContent(self):
        return "" #info.getContent().encode('utf-8')

   def isTouchable(self, uriAcceptor):
       return uriAcceptor.defaultAcceptValue(self.uri) != Resolution.no

   def getMaxPrefix(self, uriAcceptor):

       prefixes = uriAcceptor.getValues()

       # seznam prefixu, pro nas uri chceme nejdelsi prefix
       trie = marisa_trie.Trie(prefixes)
       prefList = trie.prefixes(unicode(str(self.uri), encoding="utf-8"))

       if len(prefList) > 0:
           return prefList[-1]

       else:
           return uri

   def __get_accepted_types(self, uriMap, uriAcceptor):
        if uriMap[self.uri]:
            return uriMap[self.getMaxPrefix(uriAcceptor)]
        else:
            return []

class Rack:

    def __init__(self, plugins = [], uriAcceptor, typeAcceptor):
        self.plugins = plugins
        self.uriAcceptor = uriAcceptor
        self.typeAcceptor = typeAcceptor

    def run(self, transaction):
        log = logging.getLogger("crawlcheck")
        for plugin in self.plugins:
            fakeTransaction = transaction
            fakeTransaction.uri = transaction.getMaxPrefix(uriAcceptor)
            if self.__accept(fakeTransaction, plugin):
                log.info(plugin.id + " started checking " + transaction.uri)
                plugin.check(transaction)
                log.info(plugin.id + " stopped checking " + transaction.uri)

    def insert(self, plugin):
        self.plugins.append(plugin)

    def __accept(fakeTransaction, plugin):
        return self.typeAcceptor.accept(fakeTransaction, plugin.id) and self.uriAcceptor.accept(fakeTransaction, plugin.id)

class Queue:

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

    def isEmpty(self):
        pass

    def pop(self):
        pass

    def push(self, transaction, parent=None):
        pass

    def load(self):
        pass

    def store(self):
        pass

class Journal:

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

    def load(self):
        pass

    def store(self):
        pass

    def startChecking(self, transaction):
        pass

    def stopChecking(self, transaction, status):
        pass

    def foundDefect(self, transaction, defect):
        pass

class Defect:

    def __init__(self, name, additional):
        self.name = name
        self.additional = additional

<|endoftext|>


<commit_before># encoding: cinje

: from . import table_args
: from .helpers.funcs import format_decimal

: def li_active resource, link

    : yield

: end

: def mainnavbar ctx
    : try
        : resource = ctx.resource.__resource__
    : except AttributeError
        : resource = None
    : end

         ${ctx.djname} ${format_decimal(ctx.dbstats.total_songs)}

Last Played

         : for d in ctx.alldjs
            : if d.dj != ctx.djname
                : if ctx.djprefix != ''
                    : dj = '-'.join((ctx.djprefix, d.dj))
                : else
                    : dj = d.dj
                : end

${d.dj}

            : end
         : end

By Artist

By Album

        : using li_active resource, 'requests'
         Requests ${format_decimal(ctx.requests_info.request_count)}
        : end
        : if ctx.new_counts.new_count > 0

          New ${format_decimal(ctx.new_counts.new_count)}

        : end

Stats

Suggestion

       <form class='navbar-form navbar-left hidden-xs hidden-sm' action='/search' method='post' role='search'>

          <input type='hidden' class='form-comtrol' name='navbarsearch' placeholder='Search'>
          <input type='text' class='form-comtrol' data-target="#searchModal" name='stext' id='stext' placeholder='Search'>

       </form>

Admin

Site Options

        : if ctx.listeners is not None

                            ${ctx.listeners.current}/${ctx.listeners.max}

        : end

: end
<commit_msg>Added more responsive actions to the main menubar.<commit_after># encoding: cinje

: from . import table_args
: from .helpers.funcs import format_decimal

: def li_active resource, link

    : yield

: end

: def mainnavbar ctx
    : try
        : resource = ctx.resource.__resource__
    : except AttributeError
        : resource = None
    : end

         ${ctx.djname} ${format_decimal(ctx.dbstats.total_songs)}

Last Played

         : for d in ctx.alldjs
            : if d.dj != ctx.djname
                : if ctx.djprefix != ''
                    : dj = '-'.join((ctx.djprefix, d.dj))
                : else
                    : dj = d.dj
                : end

${d.dj}

            : end
         : end

By Artist

By Album

        : using li_active resource, 'requests'
         Requests ${format_decimal(ctx.requests_info.request_count)}
        : end
        : if ctx.new_counts.new_count > 0

          New ${format_decimal(ctx.new_counts.new_count)}

        : end

Stats

Suggestion

       <form class='navbar-form navbar-left hidden-xs hidden-sm' action='/search' method='post' role='search'>

          <input type='hidden' class='form-comtrol' name='navbarsearch' placeholder='Search'>
          <input type='text' class='form-comtrol' data-target="#searchModal" name='stext' id='stext' placeholder='Search'>

       </form>

Admin

Site Options

        : if ctx.listeners is not None

                            ${ctx.listeners.current}/${ctx.listeners.max}

        : end

Admin

Site Options

: end
<|endoftext|>