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

import numpy as np
import cv2
from math import sqrt, pow, acos
import json

sample = 5

def rotate(imgl, key1, key2, matches):
    x1, y1 = key1[matches[1].queryIdx].pt
    x2, y2 = key1[matches[2].queryIdx].pt
    x3, y3 = key2[matches[1].trainIdx].pt
    x4, y4 = key2[matches[2].trainIdx].pt

    dotproduct = ((x2-x1)*(x4-x3)) + ((y2-y1)*(y4-y3))
    dist_l = sqrt(pow((x2-x1),2) + pow((y2-y1),2))
    dist_r = sqrt(pow((x4-x3),2) + pow((y4-y3),2))
    cos_theta = dotproduct/(dist_l*dist_r)
    theta = acos(cos_theta)
    theta = (theta/3.14159265359)*180

    rowsl,colsl = imgl.shape
    M = cv2.getRotationMatrix2D((colsl/2, rowsl/2), theta, 1)
    dst = cv2.warpAffine(imgl, M, (colsl,rowsl))
    return dst, theta

def analyze(key1, key2, matches, scale):
    totalx, totaly = 0, 0
    for i in range(sample):
        x1, y1 = key1[matches[i].queryIdx].pt
        x2, y2 = key2[matches[i].trainIdx].pt
        totalx += x2-x1*scale[0]
        totaly += y2-y1*scale[1]
    return totalx/sample, totaly/sample

def scale(key1, key2, matches, amount):
    totalx = 0
    totaly = 0
    for i in range(sample):
        x1, y1 = key1[matches[i].queryIdx].pt
        x2, y2 = key1[matches[i+1].queryIdx].pt
        x3, y3 = key2[matches[i].trainIdx].pt
        x4, y4 = key2[matches[i+1].trainIdx].pt
        if abs(x4-x3)>1:
            totalx += abs(x2-x1)/abs(x4-x3)
        else:
            totalx += abs(x2-x1)
        if abs(y4-y3)>1:
            totaly += abs(y2-y1)/abs(y4-y3)
        else:
            totaly += abs(y2-y1)
    return totalx/sample, totaly/sample

def score(first, second):
    alpha = cv2.imread(first, 0)
    beta = cv2.imread(second, 0)

    # Initiate the SIFT detector
    orb= cv2.ORB()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = orb.detectAndCompute(alpha, None)
    kp2, des2 = orb.detectAndCompute(beta, None)

    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Match Descriptors
    matches = bf.match(des1, des2)

    #Sort them in order of distance
    matches = sorted(matches, key = lambda x:x.distance)

    alpha, angle = rotate(alpha, kp1, kp2, matches)

    # Initiate the SIFT detector
    orb= cv2.ORB()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = orb.detectAndCompute(alpha, None)
    kp2, des2 = orb.detectAndCompute(beta, None)

    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Match Descriptors
    matches = bf.match(des1, des2)

    #Sort them in order of distance
    matches = sorted(matches, key = lambda x:x.distance)

    imgscale = scale(kp1, kp2, matches, 1)
    return analyze(kp1, kp2, matches, imgscale)

if __name__ == "__main__":
    myfile="manifest.json"
    data = open(myfile,'r')
    f=json.load(data)

    #head wrapper
    t=0
    fatdump={}
    scrs={}
    for i in f["nodes"]:
        ips=i['ip']
        fatdump[t]=i['ip']
        #score wrapper
        h=0
        for j in f["nodes"]:
            scrs[t,h]=score(f["nodes"][t]["image"],f["nodes"][h]["image"])
            fatdump["scr"]=scrs
            h+=1

        t+=1

    print(fatdump)
<commit_msg>Mixed up my sheBANG for a BANGshe<commit_after>#!/usr/bin/env pyhton

import numpy as np
import cv2
from math import sqrt, pow, acos
import json

sample = 5

def rotate(imgl, key1, key2, matches):
    x1, y1 = key1[matches[1].queryIdx].pt
    x2, y2 = key1[matches[2].queryIdx].pt
    x3, y3 = key2[matches[1].trainIdx].pt
    x4, y4 = key2[matches[2].trainIdx].pt

    dotproduct = ((x2-x1)*(x4-x3)) + ((y2-y1)*(y4-y3))
    dist_l = sqrt(pow((x2-x1),2) + pow((y2-y1),2))
    dist_r = sqrt(pow((x4-x3),2) + pow((y4-y3),2))
    cos_theta = dotproduct/(dist_l*dist_r)
    theta = acos(cos_theta)
    theta = (theta/3.14159265359)*180

    rowsl,colsl = imgl.shape
    M = cv2.getRotationMatrix2D((colsl/2, rowsl/2), theta, 1)
    dst = cv2.warpAffine(imgl, M, (colsl,rowsl))
    return dst, theta

def analyze(key1, key2, matches, scale):
    totalx, totaly = 0, 0
    for i in range(sample):
        x1, y1 = key1[matches[i].queryIdx].pt
        x2, y2 = key2[matches[i].trainIdx].pt
        totalx += x2-x1*scale[0]
        totaly += y2-y1*scale[1]
    return totalx/sample, totaly/sample

def scale(key1, key2, matches, amount):
    totalx = 0
    totaly = 0
    for i in range(sample):
        x1, y1 = key1[matches[i].queryIdx].pt
        x2, y2 = key1[matches[i+1].queryIdx].pt
        x3, y3 = key2[matches[i].trainIdx].pt
        x4, y4 = key2[matches[i+1].trainIdx].pt
        if abs(x4-x3)>1:
            totalx += abs(x2-x1)/abs(x4-x3)
        else:
            totalx += abs(x2-x1)
        if abs(y4-y3)>1:
            totaly += abs(y2-y1)/abs(y4-y3)
        else:
            totaly += abs(y2-y1)
    return totalx/sample, totaly/sample

def score(first, second):
    alpha = cv2.imread(first, 0)
    beta = cv2.imread(second, 0)

    # Initiate the SIFT detector
    orb= cv2.ORB()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = orb.detectAndCompute(alpha, None)
    kp2, des2 = orb.detectAndCompute(beta, None)

    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Match Descriptors
    matches = bf.match(des1, des2)

    #Sort them in order of distance
    matches = sorted(matches, key = lambda x:x.distance)

    alpha, angle = rotate(alpha, kp1, kp2, matches)

    # Initiate the SIFT detector
    orb= cv2.ORB()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = orb.detectAndCompute(alpha, None)
    kp2, des2 = orb.detectAndCompute(beta, None)

    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    # Match Descriptors
    matches = bf.match(des1, des2)

    #Sort them in order of distance
    matches = sorted(matches, key = lambda x:x.distance)

    imgscale = scale(kp1, kp2, matches, 1)
    return analyze(kp1, kp2, matches, imgscale)

if __name__ == "__main__":
    myfile="manifest.json"
    data = open(myfile,'r')
    f=json.load(data)

    #head wrapper
    t=0
    fatdump={}
    scrs={}
    for i in f["nodes"]:
        ips=i['ip']
        fatdump[t]=i['ip']
        #score wrapper
        h=0
        for j in f["nodes"]:
            scrs[t,h]=score(f["nodes"][t]["image"],f["nodes"][h]["image"])
            fatdump["scr"]=scrs
            h+=1

        t+=1

    print(fatdump)
<|endoftext|>


<commit_before>from ctypes import *

# The definition of PyCArgObject (that is the type of object that
# byref returns):
class PyCArgObject(Structure):
    class value(Union):
        _fields_ = [("c", c_char),
                    ("h", c_short),
                    ("i", c_int),
                    ("l", c_long),
                    ("q", c_longlong),
                    ("d", c_double),
                    ("f", c_float),
                    ("p", c_void_p)]
    # Thanks to Lenard Lindstrom for this tip: The sizeof(PyObject_HEAD)
    # is the same as object.__basicsize__.
    _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                ("pffi_type", c_void_p),
                ("tag", c_char),
                ("value", value),
                ("obj", c_void_p),
                ("size", c_int)]

    _anonymous_ = ["value"]

def _validate():
    # additional checks to make sure that everything works as expected

    if sizeof(PyCArgObject) != type(byref(c_int())).__basicsize__:
        raise RuntimeError("sizeof(PyCArgObject) invalid")

    obj = c_int()
    ref = byref(obj)

    argobj = PyCArgObject.from_address(id(ref))

    if argobj.obj != id(obj) or \
       argobj.p != addressof(obj) or \
       argobj.tag != 'P':
        raise RuntimeError("PyCArgObject field definitions incorrect")

    return PyCArgObject.p.offset # offset of the pointer field

_byref_pointer_offset = _validate()

def byref_offset(obj, offset):
    """byref_offset(cobj, offset) behaves similar this C code:

        (((char *)&obj) + offset)

    In other words, the returned 'reference' points to the address
    of 'cobj' + 'offset'.  'offset' is in units of bytes.
    """
    ref = byref(obj)
    # correct pointer field in the created byref object by 'offset'
    c_void_p.from_address(id(ref) + 16).value += offset
    return ref

__all__ = ["byref_offset"]
<commit_msg>Repair and cleanup byref_offset.<commit_after>from ctypes import *

def _calc_offset():
    # The definition of PyCArgObject (that is the type of object that
    # byref returns):
    class PyCArgObject(Structure):
        class value(Union):
            _fields_ = [("c", c_char),
                        ("h", c_short),
                        ("i", c_int),
                        ("l", c_long),
                        ("q", c_longlong),
                        ("d", c_double),
                        ("f", c_float),
                        ("p", c_void_p)]
        # Thanks to Lenard Lindstrom for this tip: The sizeof(PyObject_HEAD)
        # is the same as object.__basicsize__.
        _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                    ("pffi_type", c_void_p),
                    ("tag", c_char),
                    ("value", value),
                    ("obj", c_void_p),
                    ("size", c_int)]

        _anonymous_ = ["value"]

    # additional checks to make sure that everything works as expected

    if sizeof(PyCArgObject) != type(byref(c_int())).__basicsize__:
        raise RuntimeError("sizeof(PyCArgObject) invalid")

    obj = c_int()
    ref = byref(obj)

    argobj = PyCArgObject.from_address(id(ref))

    if argobj.obj != id(obj) or \
       argobj.p != addressof(obj) or \
       argobj.tag != 'P':
        raise RuntimeError("PyCArgObject field definitions incorrect")

    return PyCArgObject.p.offset # offset of the pointer field

_byref_pointer_offset = _calc_offset()

def byref_offset(obj, offset):
    """byref_offset(cobj, offset) behaves similar this C code:

        (((char *)&obj) + offset)

    In other words, the returned 'reference' points to the address
    of 'cobj' + 'offset'.  'offset' is in units of bytes.
    """
    ref = byref(obj)
    # correct pointer field in the created byref object by 'offset'
    c_void_p.from_address(id(ref) + _byref_pointer_offset).value += offset
    return ref

__all__ = ["byref_offset"]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
==============================================================================
Base class for structure data atoms (:mod:`sknano.core.atoms._atoms`)
==============================================================================

.. currentmodule:: sknano.core.atoms._atoms

"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
__docformat__ = 'restructuredtext en'

from operator import attrgetter

import numpy as np

from sknano.core import UserList
from sknano.core.math import convert_condition_str
from ._atom import Atom

__all__ = ['Atoms']

class Atoms(UserList):
    """Base class for collection of `Atom` objects.

    Parameters
    ----------
    atoms : {None, sequence, `Atoms`}, optional
        if not `None`, then a list of `Atom` instance objects or an
        existing `Atoms` instance object.

    """
    def __init__(self, atoms=None, **kwargs):
        if atoms is not None and (isinstance(atoms, str) or
                                  isinstance(atoms, Atom)):
            atoms = [atoms]
        if not isinstance(atoms, type(self)) and isinstance(atoms, list):
            atoms = atoms[:]
            for i, atom in enumerate(atoms):
                try:
                    # atoms[i] = self.__atom_class__(**atom.todict())

                    atomdict = atom.todict()
                    # filtered_atomdict = \
                    #     {k: atomdict[k] for k in set(dir(atom)) &
                    #      set(dir(self.__atom_class__()))}
                    # print('filtered_atomdict: {}'.format(filtered_atomdict))

                    atoms[i] = self.__atom_class__(
                        **{k: atomdict[k] for k in set(dir(atom)) &
                           set(dir(self.__atom_class__()))})
                except AttributeError:
                    atoms[i] = self.__atom_class__(atom)
        super().__init__(initlist=atoms, **kwargs)
        self.fmtstr = "{atoms!r}"

    @property
    def __atom_class__(self):
        return Atom

    def __repr__(self):
        """Return canonical string representation of `Atoms`."""
        return "{}({})".format(self.__class__.__name__,
                               self.fmtstr.format(**self.todict()))

    def sort(self, key=attrgetter('element', 'Z', 'mass'), reverse=False):
        super().sort(key=key, reverse=reverse)

    def __getitem__(self, index):
        data = super().__getitem__(index)
        if isinstance(data, list):
            return self.__class__(data, **self.kwargs)
        return data

    def __setitem__(self, index, value):
        if not isinstance(value, (self.__class__, self.__atom_class__)):
            if isinstance(index, slice):
                value = self.__class__(value)
            else:
                value = self.__atom_class__(value)
        super().__setitem__(index, value)

    def __atoms_not_in_self(self, other):
        return [atom for atom in other if atom not in self]

    def __add__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(self.data + addlist, **self.kwargs)

    def __radd__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(addlist + self.data, **self.kwargs)

    def __iadd__(self, other):
        try:
            self.data += self.__atoms_not_in_self(other)
        except TypeError:
            self.data += self.__atoms_not_in_self(self.__class__(other))
        return self

    # def __atoms_not_in_other(self, other):
    #     return [atom for atom in self if atom not in other]

    # def __sub__(self, other):
    #     if isinstance(other, self.__class__):
    #         return self.__class__(self.__atoms_not_in_other(other),
    #                               **self.kwargs)
    #     return NotImplemented

    # def __rsub__(self, other):
    #     return NotImplemented

    # def __isub__(self, other):
    #     if isinstance(other, self.__class__):
    #         self.data -= self.__atoms_not_in_other(other)
    #     return self

    @property
    def fmtstr(self):
        return self._fmtstr

    @fmtstr.setter
    def fmtstr(self, value):
        self._fmtstr = value

    @property
    def Natoms(self):
        """Number of atoms in `Atoms`."""
        return len(self)

    @property
    def M(self):
        """Total mass of `Atoms`."""
        # return math.fsum(self.masses)
        return self.masses.sum()

    @property
    def elements(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.element`\ s \
            in `Atoms`."""
        return np.asarray([atom.element for atom in self])

    @property
    def masses(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.mass`\ s \
            in `Atoms`."""
        return np.asarray([atom.mass for atom in self])

    @property
    def symbols(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.symbol`\ s \
            in `Atoms`."""
        return np.asarray([atom.symbol for atom in self])

    def filter(self, condition, invert=False):
        """Filter `Atoms` by `condition`.

        .. versionchanged:: 0.3.11

           Filters the list of `Atoms` **in-place**. Use
           :meth:`~Atoms.filtered` to generate a new filtered list
           of `Atoms`.

        Parameters
        ----------
        condition : :class:`~python:str` or boolean array
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            self.data = np.asarray(self)[condition].tolist()
        except AttributeError:
            self.data = np.asarray(self)[condition]

    def filtered(self, condition, invert=False):
        """Return new list of `Atoms` filtered by `condition`.

        .. versionadded:: 0.3.11

        Parameters
        ----------
        condition : array_like, bool
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        Returns
        -------
        filtered_atoms : `Atoms`
            If `invert` is `False`, return the elements where `condition`
            is `True`.

            If `invert` is `True`, return the elements where `~condition`
            (i.e., numpy.invert(condition)) is `True`.

        Examples
        --------
        An example using the structure data of a 10 nm `(10, 0)`
        `SWCNT`:

        >>> from sknano.generators import SWNTGenerator
        >>> swnt = SWNTGenerator(10, 0, Lz=10, fix_Lz=True).atoms
        >>> # select 'left', 'middle', 'right' atoms
        >>> latoms = swnt.filtered(swnt.z <= 25)
        >>> matoms = swnt.filtered((swnt.z < 75) & (swnt.z > 25))
        >>> ratoms = swnt.filtered(swnt.z >= 75)
        >>> from pprint import pprint
        >>> pprint([getattr(atoms, 'bounds') for atoms in
        ...         (latoms, matoms, ratoms)])
        [Cuboid(pmin=Point([-3.914435, -3.914435, 0.0]), \
                pmax=Point([3.914435, 3.914435, 24.85])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 25.56]), \
                pmax=Point([3.914435, 3.914435, 74.55])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 75.97]), \
                pmax=Point([3.914435, 3.914435, 100.11]))]
        >>> latoms.Natoms + matoms.Natoms + ratoms.Natoms == swnt.Natoms
        True

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            return self.__class__(atoms=np.asarray(self)[condition].tolist(),
                                  **self.kwargs)
        except AttributeError:
            return self.__class__(atoms=np.asarray(self)[condition],
                                  **self.kwargs)

    def get_atoms(self, asarray=False):
        """Return list of `Atoms`.

        Parameters
        ----------
        asarray : bool, optional

        Returns
        -------
        sequence or ndarray

        """
        if asarray:
            return np.asarray(self.data)
        else:
            return self.data

    def getatomattr(self, attr):
        """Get :class:`~numpy:numpy.ndarray` of atom attributes `attr`.

        Parameters
        ----------
        attr : str
            Name of attribute to pass to `getattr` from each `XAtom` in
            `XAtoms`.

        Returns
        -------
        :class:`~numpy:numpy.ndarray`

        """
        try:
            return np.asarray([getattr(atom, attr) for atom in self])
        except AttributeError:
            return None

    def mapatomattr(self, from_attr=None, to_attr=None, attrmap=None):
        """Set/update atom attribute from another atom attribute with dict.

        .. versionchanged:: 0.3.11

           Made all arguments required keyword arguments and reversed the
           order of the former positional arguments `from_attr` and
           `to_attr` to be more natural and consistent with the key, value
           pairs in the `attrmap` dictionary.

        Parameters
        ----------
        from_attr, to_attr : :class:`python:str`
        attrmap : :class:`python:dict`

        Examples
        --------
        Suppose you have an `XAtoms` instance named ``atoms`` that has
        `XAtom` instances of two atom types `1` and `2` and we want to set
        all `XAtom`\ s with `type=1` to Nitrogen and all `XAtom`\ s with
        `type=2` to Argon. In other words, we want to map the
        `XAtom.type` attribute to the `XAtom.element` attribute.

        We'd call this method like so::

        >>> atoms.mapatomattr('type', 'element', {1: 'N', 2: 'Ar'})

        """
        try:
            [setattr(atom, to_attr, attrmap[getattr(atom, from_attr)])
             for atom in self if getattr(atom, from_attr) is not None]
        except (KeyError, TypeError) as e:
            print(e)

    def rotate(self, **kwargs):
        assert not hasattr(super(), 'rotate')

    def translate(self, *args, **kwargs):
        assert not hasattr(super(), 'translate')

    def todict(self):
        return dict(atoms=self.data)
<commit_msg>Hacking the `Atoms.__setitem__` method to...<commit_after># -*- coding: utf-8 -*-
"""
==============================================================================
Base class for structure data atoms (:mod:`sknano.core.atoms._atoms`)
==============================================================================

.. currentmodule:: sknano.core.atoms._atoms

"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
__docformat__ = 'restructuredtext en'

from operator import attrgetter

import numpy as np

from sknano.core import UserList
from sknano.core.math import convert_condition_str
from ._atom import Atom

__all__ = ['Atoms']

class Atoms(UserList):
    """Base class for collection of `Atom` objects.

    Parameters
    ----------
    atoms : {None, sequence, `Atoms`}, optional
        if not `None`, then a list of `Atom` instance objects or an
        existing `Atoms` instance object.

    """
    def __init__(self, atoms=None, **kwargs):
        if atoms is not None and (isinstance(atoms, str) or
                                  isinstance(atoms, Atom)):
            atoms = [atoms]
        if not isinstance(atoms, type(self)) and isinstance(atoms, list):
            atoms = atoms[:]
            for i, atom in enumerate(atoms):
                try:
                    # atoms[i] = self.__atom_class__(**atom.todict())

                    atomdict = atom.todict()
                    # filtered_atomdict = \
                    #     {k: atomdict[k] for k in set(dir(atom)) &
                    #      set(dir(self.__atom_class__()))}
                    # print('filtered_atomdict: {}'.format(filtered_atomdict))

                    atoms[i] = self.__atom_class__(
                        **{k: atomdict[k] for k in set(dir(atom)) &
                           set(dir(self.__atom_class__()))})
                except AttributeError:
                    atoms[i] = self.__atom_class__(atom)
        super().__init__(initlist=atoms, **kwargs)
        self.fmtstr = "{atoms!r}"

    @property
    def __atom_class__(self):
        return Atom

    def __repr__(self):
        """Return canonical string representation of `Atoms`."""
        return "{}({})".format(self.__class__.__name__,
                               self.fmtstr.format(**self.todict()))

    def sort(self, key=attrgetter('element', 'Z', 'mass'), reverse=False):
        super().sort(key=key, reverse=reverse)

    def __getitem__(self, index):
        data = super().__getitem__(index)
        if isinstance(data, list):
            return self.__class__(data, **self.kwargs)
        return data

    def __setitem__(self, index, value):
        if not isinstance(value, (self.__class__, self.__atom_class__)):
            if isinstance(index, slice):
                value = self.__class__(value)
            else:
                try:
                    atomdict = super().__getitem__(index).todict()
                    if isinstance(value, str):
                        atomdict['element'] = value
                    elif isinstance(value, int):
                        atomdict['Z'] = value
                    elif isinstance(value, float):
                        atomdict['mass'] = value
                    else:
                        raise ValueError
                    value = self.__atom_class__(**atomdict)
                except (IndexError, AttributeError, ValueError):
                    value = self.__atom_class__(value)
        super().__setitem__(index, value)

    def __atoms_not_in_self(self, other):
        return [atom for atom in other if atom not in self]

    def __add__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(self.data + addlist, **self.kwargs)

    def __radd__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(addlist + self.data, **self.kwargs)

    def __iadd__(self, other):
        try:
            self.data += self.__atoms_not_in_self(other)
        except TypeError:
            self.data += self.__atoms_not_in_self(self.__class__(other))
        return self

    # def __atoms_not_in_other(self, other):
    #     return [atom for atom in self if atom not in other]

    # def __sub__(self, other):
    #     if isinstance(other, self.__class__):
    #         return self.__class__(self.__atoms_not_in_other(other),
    #                               **self.kwargs)
    #     return NotImplemented

    # def __rsub__(self, other):
    #     return NotImplemented

    # def __isub__(self, other):
    #     if isinstance(other, self.__class__):
    #         self.data -= self.__atoms_not_in_other(other)
    #     return self

    @property
    def fmtstr(self):
        return self._fmtstr

    @fmtstr.setter
    def fmtstr(self, value):
        self._fmtstr = value

    @property
    def Natoms(self):
        """Number of atoms in `Atoms`."""
        return len(self)

    @property
    def M(self):
        """Total mass of `Atoms`."""
        # return math.fsum(self.masses)
        return self.masses.sum()

    @property
    def elements(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.element`\ s \
            in `Atoms`."""
        return np.asarray([atom.element for atom in self])

    @property
    def masses(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.mass`\ s \
            in `Atoms`."""
        return np.asarray([atom.mass for atom in self])

    @property
    def symbols(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.symbol`\ s \
            in `Atoms`."""
        return np.asarray([atom.symbol for atom in self])

    def filter(self, condition, invert=False):
        """Filter `Atoms` by `condition`.

        .. versionchanged:: 0.3.11

           Filters the list of `Atoms` **in-place**. Use
           :meth:`~Atoms.filtered` to generate a new filtered list
           of `Atoms`.

        Parameters
        ----------
        condition : :class:`~python:str` or boolean array
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            self.data = np.asarray(self)[condition].tolist()
        except AttributeError:
            self.data = np.asarray(self)[condition]

    def filtered(self, condition, invert=False):
        """Return new list of `Atoms` filtered by `condition`.

        .. versionadded:: 0.3.11

        Parameters
        ----------
        condition : array_like, bool
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        Returns
        -------
        filtered_atoms : `Atoms`
            If `invert` is `False`, return the elements where `condition`
            is `True`.

            If `invert` is `True`, return the elements where `~condition`
            (i.e., numpy.invert(condition)) is `True`.

        Examples
        --------
        An example using the structure data of a 10 nm `(10, 0)`
        `SWCNT`:

        >>> from sknano.generators import SWNTGenerator
        >>> swnt = SWNTGenerator(10, 0, Lz=10, fix_Lz=True).atoms
        >>> # select 'left', 'middle', 'right' atoms
        >>> latoms = swnt.filtered(swnt.z <= 25)
        >>> matoms = swnt.filtered((swnt.z < 75) & (swnt.z > 25))
        >>> ratoms = swnt.filtered(swnt.z >= 75)
        >>> from pprint import pprint
        >>> pprint([getattr(atoms, 'bounds') for atoms in
        ...         (latoms, matoms, ratoms)])
        [Cuboid(pmin=Point([-3.914435, -3.914435, 0.0]), \
                pmax=Point([3.914435, 3.914435, 24.85])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 25.56]), \
                pmax=Point([3.914435, 3.914435, 74.55])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 75.97]), \
                pmax=Point([3.914435, 3.914435, 100.11]))]
        >>> latoms.Natoms + matoms.Natoms + ratoms.Natoms == swnt.Natoms
        True

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            return self.__class__(atoms=np.asarray(self)[condition].tolist(),
                                  **self.kwargs)
        except AttributeError:
            return self.__class__(atoms=np.asarray(self)[condition],
                                  **self.kwargs)

    def get_atoms(self, asarray=False):
        """Return list of `Atoms`.

        Parameters
        ----------
        asarray : bool, optional

        Returns
        -------
        sequence or ndarray

        """
        if asarray:
            return np.asarray(self.data)
        else:
            return self.data

    def getatomattr(self, attr):
        """Get :class:`~numpy:numpy.ndarray` of atom attributes `attr`.

        Parameters
        ----------
        attr : str
            Name of attribute to pass to `getattr` from each `XAtom` in
            `XAtoms`.

        Returns
        -------
        :class:`~numpy:numpy.ndarray`

        """
        try:
            return np.asarray([getattr(atom, attr) for atom in self])
        except AttributeError:
            return None

    def mapatomattr(self, from_attr=None, to_attr=None, attrmap=None):
        """Set/update atom attribute from another atom attribute with dict.

        .. versionchanged:: 0.3.11

           Made all arguments required keyword arguments and reversed the
           order of the former positional arguments `from_attr` and
           `to_attr` to be more natural and consistent with the key, value
           pairs in the `attrmap` dictionary.

        Parameters
        ----------
        from_attr, to_attr : :class:`python:str`
        attrmap : :class:`python:dict`

        Examples
        --------
        Suppose you have an `XAtoms` instance named ``atoms`` that has
        `XAtom` instances of two atom types `1` and `2` and we want to set
        all `XAtom`\ s with `type=1` to Nitrogen and all `XAtom`\ s with
        `type=2` to Argon. In other words, we want to map the
        `XAtom.type` attribute to the `XAtom.element` attribute.

        We'd call this method like so::

        >>> atoms.mapatomattr('type', 'element', {1: 'N', 2: 'Ar'})

        """
        try:
            [setattr(atom, to_attr, attrmap[getattr(atom, from_attr)])
             for atom in self if getattr(atom, from_attr) is not None]
        except (KeyError, TypeError) as e:
            print(e)

    def rotate(self, **kwargs):
        assert not hasattr(super(), 'rotate')

    def translate(self, *args, **kwargs):
        assert not hasattr(super(), 'translate')

    def todict(self):
        return dict(atoms=self.data)
<|endoftext|>


<commit_before>import os
import numpy as np
from tests.data import voyager_h5, voyager_fil
import blimpy as bl
from blimpy.waterfall import cmd_tool

import pytest

OUTDIR = os.path.dirname(voyager_h5) + "/"

def test_info():
    a = bl.Waterfall(voyager_h5)
    print(a)
    a.info()
    a.blank_dc(n_coarse_chan=1)
    a.calibrate_band_pass_N1()
    # Below: orphaned functions (not used anywhere else).
    # That is a little strange...
    a.grab_data()
    a.read_data()
    # It is VERY strange for INTERNAL functions to be orphaned!
    a._get_chunk_dimensions()
    # plenty of missed if suites
    a._get_blob_dimensions((300, 300, 300, 300))
    a._update_header()
    del a

def test_get_freqs():
    wf = bl.Waterfall(voyager_h5)
    freqs = wf.container.populate_freqs()
    sum1 = np.sum(freqs)
    freqs = wf.get_freqs()
    sum2 = np.sum(freqs)
    assert sum1 == sum2
    wf = bl.Waterfall(voyager_fil)
    freqs = wf.container.populate_freqs()
    sum1 = np.sum(freqs)
    freqs = wf.get_freqs()
    sum2 = np.sum(freqs)
    assert sum1 == sum2

def test_cmdline():
    # Plots

    args = [voyager_h5, '-S', '-p', 'w', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 's', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'mm', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'k', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 't', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'a', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'ank', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'ank', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    # Blank DC to .h5

    args = [voyager_h5, '-D', '-H', '-o', OUTDIR + 'test.h5']
    cmd_tool(args)

    # Blank DC to .fil

    args = [voyager_h5, '-D', '-F', '-o', OUTDIR + 'test.fil']
    cmd_tool(args)

    # info with foff negative

    args = [voyager_h5, '-i']
    cmd_tool(args)

    if os.path.exists(OUTDIR + 'test.h5'):
        os.remove(OUTDIR + 'test.h5')
        args = [voyager_h5, '-H', '-o', OUTDIR + 'test.h5']
        cmd_tool(args)
        assert os.path.exists(OUTDIR + 'test.h5')
        os.remove(OUTDIR + 'test.h5')

    if os.path.exists(OUTDIR + 'test.fil'):
        os.remove(OUTDIR + 'test.fil')
        args = [voyager_h5, '-F', '-o', OUTDIR + 'test.fil']
        cmd_tool(args)
        assert os.path.exists(OUTDIR + 'test.fil')
        os.remove(OUTDIR + 'test.fil')

def test_cmd_arguments():
    args = [voyager_h5, '-H', '-F', '-o', OUTDIR + 'test.fil']
    with pytest.raises(ValueError):
        cmd_tool(args)

def test_neg_blank_dc():
    wf = bl.Waterfall(voyager_h5)
    wf.blank_dc(0)
    wf.blank_dc(1.1)
    del wf

def test_get_chunk_dimensions():
    wf = bl.Waterfall(voyager_h5)

    wf.header['foff'] = 0.99e-5
    assert wf._get_chunk_dimensions() == (1, 1, 1048576)
    wf.header['foff'] = 1.1e-5

    wf.header['tsamp'] = 0.99e-3
    assert wf._get_chunk_dimensions() == (2048, 1, 512)
    wf.header['tsamp'] = 1.1e-3

    wf.header['foff'] = 0.99e-2
    assert wf._get_chunk_dimensions() == (10, 1, 65536)

    wf.header['foff'] = 1e-1
    assert wf._get_chunk_dimensions() == (1, 1, 512)

    del wf

def test_neg_info_foff():
    wf = bl.Waterfall(voyager_h5)
    wf.header['foff'] = -1
    wf.info()
    del wf

def test_no_filename():
    header = { "banana": "is a fruit" }
    data = np.arange(1, 11)
    class Puddle(bl.Waterfall):
        def _setup_freqs(self):
            print("Hello from function _setup_freqs")
    puddle = Puddle(header_dict=header, data_array=data)
    puddle = Puddle(header_dict=header)
    del puddle

test_cmdline()

<commit_msg>Make sure that issue #118 never hapens again<commit_after>import os
import numpy as np
from tests.data import voyager_h5, voyager_fil
import blimpy as bl
from blimpy.waterfall import cmd_tool

import pytest

OUTDIR = os.path.dirname(voyager_h5) + "/"

def test_info():
    a = bl.Waterfall(voyager_h5)
    print(a)
    a.info()
    a.blank_dc(n_coarse_chan=1)
    a.calibrate_band_pass_N1()
    # Below: orphaned functions (not used anywhere else).
    # That is a little strange...
    a.grab_data()
    a.read_data()
    # It is VERY strange for INTERNAL functions to be orphaned!
    a._get_chunk_dimensions()
    # plenty of missed if suites
    a._get_blob_dimensions((300, 300, 300, 300))
    a._update_header()
    del a

def test_get_freqs():
    wf = bl.Waterfall(voyager_h5)
    freqs = wf.container.populate_freqs()
    sum1 = np.sum(freqs)
    freqs = wf.get_freqs()
    sum2 = np.sum(freqs)
    assert sum1 == sum2
    wf = bl.Waterfall(voyager_fil)
    freqs = wf.container.populate_freqs()
    sum1 = np.sum(freqs)
    freqs = wf.get_freqs()
    sum2 = np.sum(freqs)
    assert sum1 == sum2
    len_f = len(freqs)
    first = wf.header["fch1"]
    last_1 = freqs[-1]
    last_2 = first + (len_f - 1 ) *  wf.header["foff"]
    assert np.isclose(last_1, last_2, rtol=0.0001)

def test_cmdline():
    # Plots

    args = [voyager_h5, '-S', '-p', 'w', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 's', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'mm', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'k', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 't', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'a', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'ank', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    args = [voyager_h5, '-S', '-p', 'ank', '-s', OUTDIR + 'test.png']
    cmd_tool(args)

    # Blank DC to .h5

    args = [voyager_h5, '-D', '-H', '-o', OUTDIR + 'test.h5']
    cmd_tool(args)

    # Blank DC to .fil

    args = [voyager_h5, '-D', '-F', '-o', OUTDIR + 'test.fil']
    cmd_tool(args)

    # info with foff negative

    args = [voyager_h5, '-i']
    cmd_tool(args)

    if os.path.exists(OUTDIR + 'test.h5'):
        os.remove(OUTDIR + 'test.h5')
        args = [voyager_h5, '-H', '-o', OUTDIR + 'test.h5']
        cmd_tool(args)
        assert os.path.exists(OUTDIR + 'test.h5')
        os.remove(OUTDIR + 'test.h5')

    if os.path.exists(OUTDIR + 'test.fil'):
        os.remove(OUTDIR + 'test.fil')
        args = [voyager_h5, '-F', '-o', OUTDIR + 'test.fil']
        cmd_tool(args)
        assert os.path.exists(OUTDIR + 'test.fil')
        os.remove(OUTDIR + 'test.fil')

def test_cmd_arguments():
    args = [voyager_h5, '-H', '-F', '-o', OUTDIR + 'test.fil']
    with pytest.raises(ValueError):
        cmd_tool(args)

def test_neg_blank_dc():
    wf = bl.Waterfall(voyager_h5)
    wf.blank_dc(0)
    wf.blank_dc(1.1)
    del wf

def test_get_chunk_dimensions():
    wf = bl.Waterfall(voyager_h5)

    wf.header['foff'] = 0.99e-5
    assert wf._get_chunk_dimensions() == (1, 1, 1048576)
    wf.header['foff'] = 1.1e-5

    wf.header['tsamp'] = 0.99e-3
    assert wf._get_chunk_dimensions() == (2048, 1, 512)
    wf.header['tsamp'] = 1.1e-3

    wf.header['foff'] = 0.99e-2
    assert wf._get_chunk_dimensions() == (10, 1, 65536)

    wf.header['foff'] = 1e-1
    assert wf._get_chunk_dimensions() == (1, 1, 512)

    del wf

def test_neg_info_foff():
    wf = bl.Waterfall(voyager_h5)
    wf.header['foff'] = -1
    wf.info()
    del wf

def test_no_filename():
    header = { "banana": "is a fruit" }
    data = np.arange(1, 11)
    class Puddle(bl.Waterfall):
        def _setup_freqs(self):
            print("Hello from function _setup_freqs")
    puddle = Puddle(header_dict=header, data_array=data)
    puddle = Puddle(header_dict=header)
    del puddle

test_cmdline()

<|endoftext|>


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

INCLUDES = """
#include <openssl/ssl.h>
"""

TYPES = """
/* Internally invented symbol to tell us if SSLv2 is supported */
static const int Cryptography_HAS_SSL2;

/* Internally invented symbol to tell us if SNI is supported */
static const int Cryptography_HAS_TLSEXT_HOSTNAME;

/* Internally invented symbol to tell us if SSL_MODE_RELEASE_BUFFERS is
 * supported
 */
static const int Cryptography_HAS_RELEASE_BUFFERS;

/* Internally invented symbol to tell us if SSL_OP_NO_COMPRESSION is
 * supported
 */
static const int Cryptography_HAS_OP_NO_COMPRESSION;

static const int SSL_FILETYPE_PEM;
static const int SSL_FILETYPE_ASN1;
static const int SSL_ERROR_NONE;
static const int SSL_ERROR_ZERO_RETURN;
static const int SSL_ERROR_WANT_READ;
static const int SSL_ERROR_WANT_WRITE;
static const int SSL_ERROR_WANT_X509_LOOKUP;
static const int SSL_ERROR_SYSCALL;
static const int SSL_ERROR_SSL;
static const int SSL_SENT_SHUTDOWN;
static const int SSL_RECEIVED_SHUTDOWN;
static const int SSL_OP_NO_SSLv2;
static const int SSL_OP_NO_SSLv3;
static const int SSL_OP_NO_TLSv1;
static const int SSL_OP_NO_COMPRESSION;
static const int SSL_OP_SINGLE_DH_USE;
static const int SSL_OP_EPHEMERAL_RSA;
static const int SSL_OP_MICROSOFT_SESS_ID_BUG;
static const int SSL_OP_NETSCAPE_CHALLENGE_BUG;
static const int SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
static const int SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG;
static const int SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER;
static const int SSL_OP_MSIE_SSLV2_RSA_PADDING;
static const int SSL_OP_SSLEAY_080_CLIENT_DH_BUG;
static const int SSL_OP_TLS_D5_BUG;
static const int SSL_OP_TLS_BLOCK_PADDING_BUG;
static const int SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
static const int SSL_OP_CIPHER_SERVER_PREFERENCE;
static const int SSL_OP_TLS_ROLLBACK_BUG;
static const int SSL_OP_PKCS1_CHECK_1;
static const int SSL_OP_PKCS1_CHECK_2;
static const int SSL_OP_NETSCAPE_CA_DN_BUG;
static const int SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG;
static const int SSL_OP_NO_QUERY_MTU;
static const int SSL_OP_COOKIE_EXCHANGE;
static const int SSL_OP_NO_TICKET;
static const int SSL_OP_ALL;
static const int SSL_VERIFY_PEER;
static const int SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
static const int SSL_VERIFY_CLIENT_ONCE;
static const int SSL_VERIFY_NONE;
static const int SSL_SESS_CACHE_OFF;
static const int SSL_SESS_CACHE_CLIENT;
static const int SSL_SESS_CACHE_SERVER;
static const int SSL_SESS_CACHE_BOTH;
static const int SSL_SESS_CACHE_NO_AUTO_CLEAR;
static const int SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
static const int SSL_SESS_CACHE_NO_INTERNAL_STORE;
static const int SSL_SESS_CACHE_NO_INTERNAL;
static const int SSL_ST_CONNECT;
static const int SSL_ST_ACCEPT;
static const int SSL_ST_MASK;
static const int SSL_ST_INIT;
static const int SSL_ST_BEFORE;
static const int SSL_ST_OK;
static const int SSL_ST_RENEGOTIATE;
static const int SSL_CB_LOOP;
static const int SSL_CB_EXIT;
static const int SSL_CB_READ;
static const int SSL_CB_WRITE;
static const int SSL_CB_ALERT;
static const int SSL_CB_READ_ALERT;
static const int SSL_CB_WRITE_ALERT;
static const int SSL_CB_ACCEPT_LOOP;
static const int SSL_CB_ACCEPT_EXIT;
static const int SSL_CB_CONNECT_LOOP;
static const int SSL_CB_CONNECT_EXIT;
static const int SSL_CB_HANDSHAKE_START;
static const int SSL_CB_HANDSHAKE_DONE;
static const int SSL_MODE_RELEASE_BUFFERS;
static const int SSL_MODE_ENABLE_PARTIAL_WRITE;
static const int SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
static const int SSL_MODE_AUTO_RETRY;
static const int SSL3_RANDOM_SIZE;
typedef ... X509_STORE_CTX;
static const int X509_V_OK;
typedef ... SSL_METHOD;
typedef ... SSL_CTX;

typedef struct {
    int master_key_length;
    unsigned char master_key[...];
    ...;
} SSL_SESSION;

typedef struct {
    unsigned char server_random[...];
    unsigned char client_random[...];
    ...;
} SSL3_STATE;

typedef struct {
    SSL3_STATE *s3;
    SSL_SESSION *session;
    ...;
} SSL;

static const int TLSEXT_NAMETYPE_host_name;
"""

FUNCTIONS = """
void SSL_load_error_strings();
int SSL_library_init();

/*  SSL */
SSL_CTX *SSL_set_SSL_CTX(SSL *, SSL_CTX *);
SSL_SESSION *SSL_get1_session(SSL *);
int SSL_set_session(SSL *, SSL_SESSION *);
int SSL_get_verify_mode(const SSL *);
void SSL_set_verify_depth(SSL *, int);
int SSL_get_verify_depth(const SSL *);
int (*SSL_get_verify_callback(const SSL *))(int, X509_STORE_CTX *);
void SSL_set_info_callback(SSL *, void (*)());
void (*SSL_get_info_callback(const SSL *))();
SSL *SSL_new(SSL_CTX *);
void SSL_free(SSL *);
int SSL_set_fd(SSL *, int);
void SSL_set_bio(SSL *, BIO *, BIO *);
void SSL_set_connect_state(SSL *);
void SSL_set_accept_state(SSL *);
void SSL_set_shutdown(SSL *, int);
int SSL_get_shutdown(const SSL *);
int SSL_pending(const SSL *);
int SSL_write(SSL *, const void *, int);
int SSL_read(SSL *, void *, int);
X509 *SSL_get_peer_certificate(const SSL *);
int SSL_get_error(const SSL *, int);
int SSL_do_handshake(SSL *);
int SSL_shutdown(SSL *);
const char *SSL_get_cipher_list(const SSL *, int);

/*  context */
void SSL_CTX_free(SSL_CTX *);
long SSL_CTX_set_timeout(SSL_CTX *, long);
int SSL_CTX_set_default_verify_paths(SSL_CTX *);
void SSL_CTX_set_verify(SSL_CTX *, int, int (*)(int, X509_STORE_CTX *));
void SSL_CTX_set_verify_depth(SSL_CTX *, int);
int (*SSL_CTX_get_verify_callback(const SSL_CTX *))(int, X509_STORE_CTX *);
void SSL_CTX_set_info_callback(SSL_CTX *, void (*)(const SSL *, int, int));
void (*SSL_CTX_get_info_callback(SSL_CTX *))(const SSL *, int, int);
int SSL_CTX_get_verify_mode(const SSL_CTX *);
int SSL_CTX_get_verify_depth(const SSL_CTX *);
int SSL_CTX_set_cipher_list(SSL_CTX *, const char *);
int SSL_CTX_load_verify_locations(SSL_CTX *, const char *, const char *);
void SSL_CTX_set_default_passwd_cb(SSL_CTX *, pem_password_cb *);
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void *);
int SSL_CTX_use_certificate(SSL_CTX *, X509 *);
int SSL_CTX_use_certificate_file(SSL_CTX *, const char *, int);
int SSL_CTX_use_certificate_chain_file(SSL_CTX *, const char *);
int SSL_CTX_use_PrivateKey(SSL_CTX *, EVP_PKEY *);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *, const char *, int);
void SSL_CTX_set_cert_store(SSL_CTX *, X509_STORE *);
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *);
int SSL_CTX_add_client_CA(SSL_CTX *, X509 *);

/*  X509_STORE_CTX */
int X509_STORE_CTX_get_error(X509_STORE_CTX *);
void X509_STORE_CTX_set_error(X509_STORE_CTX *, int);
int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *);
X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *);

/*  SSL_SESSION */
void SSL_SESSION_free(SSL_SESSION *);
"""

MACROS = """
long SSL_set_mode(SSL *, long);
long SSL_get_mode(SSL *);

long SSL_set_options(SSL *, long);
long SSL_get_options(SSL *);

int SSL_want_read(const SSL *);
int SSL_want_write(const SSL *);

int SSL_total_renegotiations(const SSL *);

long SSL_CTX_set_options(SSL_CTX *, long);
long SSL_CTX_get_options(SSL_CTX *);
long SSL_CTX_set_mode(SSL_CTX *, long);
long SSL_CTX_get_mode(SSL_CTX *);
long SSL_CTX_set_session_cache_mode(SSL_CTX *, long);
long SSL_CTX_get_session_cache_mode(SSL_CTX *);
long SSL_CTX_set_tmp_dh(SSL_CTX *, DH *);
long SSL_CTX_add_extra_chain_cert(SSL_CTX *, X509 *);

/*- These aren't macros these functions are all const X on openssl > 1.0.x -*/

/* SSLv2 support is compiled out of some versions of OpenSSL.  These will
 * get special support when we generate the bindings so that if they are
 * available they will be wrapped, but if they are not they won't cause
 * problems (like link errors).
 */
const SSL_METHOD *SSLv2_method();
const SSL_METHOD *SSLv2_server_method();
const SSL_METHOD *SSLv2_client_method();

/*  methods */
const SSL_METHOD *SSLv3_method();
const SSL_METHOD *SSLv3_server_method();
const SSL_METHOD *SSLv3_client_method();
const SSL_METHOD *TLSv1_method();
const SSL_METHOD *TLSv1_server_method();
const SSL_METHOD *TLSv1_client_method();
const SSL_METHOD *SSLv23_method();
const SSL_METHOD *SSLv23_server_method();
const SSL_METHOD *SSLv23_client_method();

/*- These aren't macros these arguments are all const X on openssl > 1.0.x -*/
SSL_CTX *SSL_CTX_new(const SSL_METHOD *);
long SSL_CTX_get_timeout(const SSL_CTX *);

/* SNI APIs were introduced in OpenSSL 1.0.0.  To continue to support
 * earlier versions some special handling of these is necessary.
 */
void SSL_set_tlsext_host_name(SSL *, char *);
void SSL_CTX_set_tlsext_servername_callback(
    SSL_CTX *,
    int (*)(const SSL *, int *, void *));
"""

CUSTOMIZATIONS = """
#ifdef OPENSSL_NO_SSL2
static const int Cryptography_HAS_SSL2 = 0;
SSL_METHOD* (*SSLv2_method)() = NULL;
SSL_METHOD* (*SSLv2_client_method)() = NULL;
SSL_METHOD* (*SSLv2_server_method)() = NULL;
#else
static const int Cryptography_HAS_SSL2 = 1;
#endif

#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
static const int Cryptography_HAS_TLSEXT_HOSTNAME = 1;
#else
static const int Cryptography_HAS_TLSEXT_HOSTNAME = 0;
void (*SSL_set_tlsext_host_name)(SSL *, char *) = NULL;
const char* (*SSL_get_servername)(const SSL *, const int) = NULL;
void (*SSL_CTX_set_tlsext_servername_callback)(
    SSL_CTX *,
    int (*)(const SSL *, int *, void *)) = NULL;
#endif

#ifdef SSL_MODE_RELEASE_BUFFERS
static const int Cryptography_HAS_RELEASE_BUFFERS = 1;
#else
static const int Cryptography_HAS_RELEASE_BUFFERS = 0;
const int SSL_MODE_RELEASE_BUFFERS = 0;
#endif

#ifdef SSL_OP_NO_COMPRESSION
static const int Cryptography_HAS_OP_NO_COMPRESSION = 1;
#else
static const int Cryptography_HAS_OP_NO_COMPRESSION = 0;
const int SSL_OP_NO_COMPRESSION = 0;
#endif
"""

CONDITIONAL_NAMES = {
    "Cryptography_HAS_SSL2": [
        "SSLv2_method",
        "SSLv2_client_method",
        "SSLv2_server_method",
    ],

    "Cryptography_HAS_TLSEXT_HOSTNAME": [
        "SSL_set_tlsext_host_name",
        "SSL_get_servername",
        "SSL_CTX_set_tlsext_servername_callback",
    ],

    "Cryptography_HAS_RELEASE_BUFFERS": [
        "SSL_MODE_RELEASE_BUFFERS",
    ],

    "Cryptography_HAS_OP_NO_COMPRESSION": [
        "SSL_OP_NO_COMPRESSION",
    ],

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

INCLUDES = """
#include <openssl/ssl.h>
"""

TYPES = """
/* Internally invented symbol to tell us if SSLv2 is supported */
static const int Cryptography_HAS_SSL2;

/* Internally invented symbol to tell us if SNI is supported */
static const int Cryptography_HAS_TLSEXT_HOSTNAME;

/* Internally invented symbol to tell us if SSL_MODE_RELEASE_BUFFERS is
 * supported
 */
static const int Cryptography_HAS_RELEASE_BUFFERS;

/* Internally invented symbol to tell us if SSL_OP_NO_COMPRESSION is
 * supported
 */
static const int Cryptography_HAS_OP_NO_COMPRESSION;

static const int SSL_FILETYPE_PEM;
static const int SSL_FILETYPE_ASN1;
static const int SSL_ERROR_NONE;
static const int SSL_ERROR_ZERO_RETURN;
static const int SSL_ERROR_WANT_READ;
static const int SSL_ERROR_WANT_WRITE;
static const int SSL_ERROR_WANT_X509_LOOKUP;
static const int SSL_ERROR_SYSCALL;
static const int SSL_ERROR_SSL;
static const int SSL_SENT_SHUTDOWN;
static const int SSL_RECEIVED_SHUTDOWN;
static const int SSL_OP_NO_SSLv2;
static const int SSL_OP_NO_SSLv3;
static const int SSL_OP_NO_TLSv1;
static const int SSL_OP_NO_COMPRESSION;
static const int SSL_OP_SINGLE_DH_USE;
static const int SSL_OP_EPHEMERAL_RSA;
static const int SSL_OP_MICROSOFT_SESS_ID_BUG;
static const int SSL_OP_NETSCAPE_CHALLENGE_BUG;
static const int SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
static const int SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG;
static const int SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER;
static const int SSL_OP_SSLEAY_080_CLIENT_DH_BUG;
static const int SSL_OP_TLS_D5_BUG;
static const int SSL_OP_TLS_BLOCK_PADDING_BUG;
static const int SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
static const int SSL_OP_CIPHER_SERVER_PREFERENCE;
static const int SSL_OP_TLS_ROLLBACK_BUG;
static const int SSL_OP_PKCS1_CHECK_1;
static const int SSL_OP_PKCS1_CHECK_2;
static const int SSL_OP_NETSCAPE_CA_DN_BUG;
static const int SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG;
static const int SSL_OP_NO_QUERY_MTU;
static const int SSL_OP_COOKIE_EXCHANGE;
static const int SSL_OP_NO_TICKET;
static const int SSL_OP_ALL;
static const int SSL_VERIFY_PEER;
static const int SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
static const int SSL_VERIFY_CLIENT_ONCE;
static const int SSL_VERIFY_NONE;
static const int SSL_SESS_CACHE_OFF;
static const int SSL_SESS_CACHE_CLIENT;
static const int SSL_SESS_CACHE_SERVER;
static const int SSL_SESS_CACHE_BOTH;
static const int SSL_SESS_CACHE_NO_AUTO_CLEAR;
static const int SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
static const int SSL_SESS_CACHE_NO_INTERNAL_STORE;
static const int SSL_SESS_CACHE_NO_INTERNAL;
static const int SSL_ST_CONNECT;
static const int SSL_ST_ACCEPT;
static const int SSL_ST_MASK;
static const int SSL_ST_INIT;
static const int SSL_ST_BEFORE;
static const int SSL_ST_OK;
static const int SSL_ST_RENEGOTIATE;
static const int SSL_CB_LOOP;
static const int SSL_CB_EXIT;
static const int SSL_CB_READ;
static const int SSL_CB_WRITE;
static const int SSL_CB_ALERT;
static const int SSL_CB_READ_ALERT;
static const int SSL_CB_WRITE_ALERT;
static const int SSL_CB_ACCEPT_LOOP;
static const int SSL_CB_ACCEPT_EXIT;
static const int SSL_CB_CONNECT_LOOP;
static const int SSL_CB_CONNECT_EXIT;
static const int SSL_CB_HANDSHAKE_START;
static const int SSL_CB_HANDSHAKE_DONE;
static const int SSL_MODE_RELEASE_BUFFERS;
static const int SSL_MODE_ENABLE_PARTIAL_WRITE;
static const int SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
static const int SSL_MODE_AUTO_RETRY;
static const int SSL3_RANDOM_SIZE;
typedef ... X509_STORE_CTX;
static const int X509_V_OK;
typedef ... SSL_METHOD;
typedef ... SSL_CTX;

typedef struct {
    int master_key_length;
    unsigned char master_key[...];
    ...;
} SSL_SESSION;

typedef struct {
    unsigned char server_random[...];
    unsigned char client_random[...];
    ...;
} SSL3_STATE;

typedef struct {
    SSL3_STATE *s3;
    SSL_SESSION *session;
    ...;
} SSL;

static const int TLSEXT_NAMETYPE_host_name;
"""

FUNCTIONS = """
void SSL_load_error_strings();
int SSL_library_init();

/*  SSL */
SSL_CTX *SSL_set_SSL_CTX(SSL *, SSL_CTX *);
SSL_SESSION *SSL_get1_session(SSL *);
int SSL_set_session(SSL *, SSL_SESSION *);
int SSL_get_verify_mode(const SSL *);
void SSL_set_verify_depth(SSL *, int);
int SSL_get_verify_depth(const SSL *);
int (*SSL_get_verify_callback(const SSL *))(int, X509_STORE_CTX *);
void SSL_set_info_callback(SSL *, void (*)());
void (*SSL_get_info_callback(const SSL *))();
SSL *SSL_new(SSL_CTX *);
void SSL_free(SSL *);
int SSL_set_fd(SSL *, int);
void SSL_set_bio(SSL *, BIO *, BIO *);
void SSL_set_connect_state(SSL *);
void SSL_set_accept_state(SSL *);
void SSL_set_shutdown(SSL *, int);
int SSL_get_shutdown(const SSL *);
int SSL_pending(const SSL *);
int SSL_write(SSL *, const void *, int);
int SSL_read(SSL *, void *, int);
X509 *SSL_get_peer_certificate(const SSL *);
int SSL_get_error(const SSL *, int);
int SSL_do_handshake(SSL *);
int SSL_shutdown(SSL *);
const char *SSL_get_cipher_list(const SSL *, int);

/*  context */
void SSL_CTX_free(SSL_CTX *);
long SSL_CTX_set_timeout(SSL_CTX *, long);
int SSL_CTX_set_default_verify_paths(SSL_CTX *);
void SSL_CTX_set_verify(SSL_CTX *, int, int (*)(int, X509_STORE_CTX *));
void SSL_CTX_set_verify_depth(SSL_CTX *, int);
int (*SSL_CTX_get_verify_callback(const SSL_CTX *))(int, X509_STORE_CTX *);
void SSL_CTX_set_info_callback(SSL_CTX *, void (*)(const SSL *, int, int));
void (*SSL_CTX_get_info_callback(SSL_CTX *))(const SSL *, int, int);
int SSL_CTX_get_verify_mode(const SSL_CTX *);
int SSL_CTX_get_verify_depth(const SSL_CTX *);
int SSL_CTX_set_cipher_list(SSL_CTX *, const char *);
int SSL_CTX_load_verify_locations(SSL_CTX *, const char *, const char *);
void SSL_CTX_set_default_passwd_cb(SSL_CTX *, pem_password_cb *);
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void *);
int SSL_CTX_use_certificate(SSL_CTX *, X509 *);
int SSL_CTX_use_certificate_file(SSL_CTX *, const char *, int);
int SSL_CTX_use_certificate_chain_file(SSL_CTX *, const char *);
int SSL_CTX_use_PrivateKey(SSL_CTX *, EVP_PKEY *);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *, const char *, int);
void SSL_CTX_set_cert_store(SSL_CTX *, X509_STORE *);
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *);
int SSL_CTX_add_client_CA(SSL_CTX *, X509 *);

/*  X509_STORE_CTX */
int X509_STORE_CTX_get_error(X509_STORE_CTX *);
void X509_STORE_CTX_set_error(X509_STORE_CTX *, int);
int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *);
X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *);

/*  SSL_SESSION */
void SSL_SESSION_free(SSL_SESSION *);
"""

MACROS = """
long SSL_set_mode(SSL *, long);
long SSL_get_mode(SSL *);

long SSL_set_options(SSL *, long);
long SSL_get_options(SSL *);

int SSL_want_read(const SSL *);
int SSL_want_write(const SSL *);

int SSL_total_renegotiations(const SSL *);

long SSL_CTX_set_options(SSL_CTX *, long);
long SSL_CTX_get_options(SSL_CTX *);
long SSL_CTX_set_mode(SSL_CTX *, long);
long SSL_CTX_get_mode(SSL_CTX *);
long SSL_CTX_set_session_cache_mode(SSL_CTX *, long);
long SSL_CTX_get_session_cache_mode(SSL_CTX *);
long SSL_CTX_set_tmp_dh(SSL_CTX *, DH *);
long SSL_CTX_add_extra_chain_cert(SSL_CTX *, X509 *);

/*- These aren't macros these functions are all const X on openssl > 1.0.x -*/

/* SSLv2 support is compiled out of some versions of OpenSSL.  These will
 * get special support when we generate the bindings so that if they are
 * available they will be wrapped, but if they are not they won't cause
 * problems (like link errors).
 */
const SSL_METHOD *SSLv2_method();
const SSL_METHOD *SSLv2_server_method();
const SSL_METHOD *SSLv2_client_method();

/*  methods */
const SSL_METHOD *SSLv3_method();
const SSL_METHOD *SSLv3_server_method();
const SSL_METHOD *SSLv3_client_method();
const SSL_METHOD *TLSv1_method();
const SSL_METHOD *TLSv1_server_method();
const SSL_METHOD *TLSv1_client_method();
const SSL_METHOD *SSLv23_method();
const SSL_METHOD *SSLv23_server_method();
const SSL_METHOD *SSLv23_client_method();

/*- These aren't macros these arguments are all const X on openssl > 1.0.x -*/
SSL_CTX *SSL_CTX_new(const SSL_METHOD *);
long SSL_CTX_get_timeout(const SSL_CTX *);

/* SNI APIs were introduced in OpenSSL 1.0.0.  To continue to support
 * earlier versions some special handling of these is necessary.
 */
void SSL_set_tlsext_host_name(SSL *, char *);
void SSL_CTX_set_tlsext_servername_callback(
    SSL_CTX *,
    int (*)(const SSL *, int *, void *));
"""

CUSTOMIZATIONS = """
#ifdef OPENSSL_NO_SSL2
static const int Cryptography_HAS_SSL2 = 0;
SSL_METHOD* (*SSLv2_method)() = NULL;
SSL_METHOD* (*SSLv2_client_method)() = NULL;
SSL_METHOD* (*SSLv2_server_method)() = NULL;
#else
static const int Cryptography_HAS_SSL2 = 1;
#endif

#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
static const int Cryptography_HAS_TLSEXT_HOSTNAME = 1;
#else
static const int Cryptography_HAS_TLSEXT_HOSTNAME = 0;
void (*SSL_set_tlsext_host_name)(SSL *, char *) = NULL;
const char* (*SSL_get_servername)(const SSL *, const int) = NULL;
void (*SSL_CTX_set_tlsext_servername_callback)(
    SSL_CTX *,
    int (*)(const SSL *, int *, void *)) = NULL;
#endif

#ifdef SSL_MODE_RELEASE_BUFFERS
static const int Cryptography_HAS_RELEASE_BUFFERS = 1;
#else
static const int Cryptography_HAS_RELEASE_BUFFERS = 0;
const int SSL_MODE_RELEASE_BUFFERS = 0;
#endif

#ifdef SSL_OP_NO_COMPRESSION
static const int Cryptography_HAS_OP_NO_COMPRESSION = 1;
#else
static const int Cryptography_HAS_OP_NO_COMPRESSION = 0;
const int SSL_OP_NO_COMPRESSION = 0;
#endif
"""

CONDITIONAL_NAMES = {
    "Cryptography_HAS_SSL2": [
        "SSLv2_method",
        "SSLv2_client_method",
        "SSLv2_server_method",
    ],

    "Cryptography_HAS_TLSEXT_HOSTNAME": [
        "SSL_set_tlsext_host_name",
        "SSL_get_servername",
        "SSL_CTX_set_tlsext_servername_callback",
    ],

    "Cryptography_HAS_RELEASE_BUFFERS": [
        "SSL_MODE_RELEASE_BUFFERS",
    ],

    "Cryptography_HAS_OP_NO_COMPRESSION": [
        "SSL_OP_NO_COMPRESSION",
    ],

}
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""Setup specs for packaging, distributing, and installing Pipeline lib."""

import setuptools

# To debug, set DISTUTILS_DEBUG env var to anything.
setuptools.setup(
    name="pipeline",
    version="1.2.0",
    name="GoogleAppEnginePipeline",
    packages=setuptools.find_packages(),
    author="Kevin Sookocheff",
    author_email="ksookocheff@vendasta.com",
    keywords="google app engine pipeline data processing",
    url="https://github.com/vendasta/appengine-mapreduce.git",
    license="Apache License 2.0",
    description=("Enable asynchronous pipeline style data processing on "
                 "App Engine"),
    zip_safe=True,
    include_package_data=True,
    # Exclude these files from installation.
    exclude_package_data={"": ["README"]},
    install_requires=[
      "GoogleAppEngineCloudStorageClient >= 1.9.21",
      ],
    extras_require={'python2.5': ["simplejson >= 3.6.5"]}
)
<commit_msg>Update setup.py file<commit_after>#!/usr/bin/env python
"""Setup specs for packaging, distributing, and installing Pipeline lib."""

import setuptools

setuptools.setup(
    name="pipeline",
    version="1.2.0",
    packages=setuptools.find_packages(),
    author="Kevin Sookocheff",
    author_email="ksookocheff@vendasta.com",
    keywords="google app engine pipeline data processing",
    url="https://github.com/vendasta/appengine-mapreduce.git",
    license="Apache License 2.0",
    description=("Enable asynchronous pipeline style data processing on "
                 "App Engine"),
    zip_safe=True,
    include_package_data=True,
    # Exclude these files from installation.
    exclude_package_data={"": ["README"]},
    install_requires=[
        "GoogleAppEngineCloudStorageClient >= 1.9.21",
    ]
)
<|endoftext|>


<commit_before>from flask import Flask, render_template, make_response, redirect
from flask.ext.restful import Api, Resource, reqparse, abort

import json
import string
import random
from datetime import datetime

# Define our priority levels.
# These are the values that the "priority" property can take on a help request.
PRIORITIES = ('closed', 'low', 'normal', 'high')

# Load data from disk.
# This simply loads the data from our "database," which is just a JSON file.
with open('data.jsonld') as data:
    data = json.load(data)

# Generate a unique ID for a new help request.
# By default this will consist of six lowercase numbers and letters.
def generate_id(size=6, chars=string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

# Respond with 404 Not Found if no help request with the specified ID exists.
def error_if_helprequest_not_found(helprequest_id):
    if helprequest_id not in data['helprequests']:
        message = "No help request with ID: {}".format(helprequest_id)
        abort(404, message=message)

# Filter and sort a list of helprequests.
def filter_and_sort_helprequests(query='', sort_by='time'):

    # Returns True if the query string appears in the help request's
    # title or description.
    def matches_query(item):
        (helprequest_id, helprequest) = item
        text = helprequest['title'] + helprequest['description']
        return query.lower() in text

    # Returns the help request's value for the sort property (which by
    # default is the "time" property).
    def get_sort_value(item):
        (helprequest_id, helprequest) = item
        return helprequest[sort_by]

    filtered_helprequests = filter(matches_query, data['helprequests'].items())

    return sorted(filtered_helprequests, key=get_sort_value, reverse=True)

# Given the data for a help request, generate an HTML representation
# of that help request.
def render_helprequest_as_html(helprequest):
    return render_template(
        'helprequest+microdata+rdfa.html',
        helprequest=helprequest,
        priorities=reversed(list(enumerate(PRIORITIES))))

# Given the data for a list of help requests, generate an HTML representation
# of that list.
def render_helprequest_list_as_html(helprequests):
    return render_template(
        'helprequests+microdata+rdfa.html',
        helprequests=helprequests,
        priorities=PRIORITIES)

# Raises an error if the string x is empty (has zero length).
def nonempty_string(x):
    s = str(x)
    if len(x) == 0:
        raise ValueError('string is empty')
    return s

# Specify the data necessary to create a new help request.
# "from", "title", and "description" are all required values.
new_helprequest_parser = reqparse.RequestParser()
for arg in ['from', 'title', 'description']:
    new_helprequest_parser.add_argument(
        arg, type=nonempty_string, required=True,
        help="'{}' is a required value".format(arg))

# Specify the data necessary to update an existing help request.
# Only the priority and comments can be updated.
update_helprequest_parser = reqparse.RequestParser()
update_helprequest_parser.add_argument(
    'priority', type=int, default=PRIORITIES.index('normal'))
update_helprequest_parser.add_argument(
    'comment', type=str, default='')

# Specify the parameters for filtering and sorting help requests.
# See `filter_and_sort_helprequests` above.
query_parser = reqparse.RequestParser()
query_parser.add_argument(
    'query', type=str, default='')
query_parser.add_argument(
    'sort_by', type=str, choices=('priority', 'time'), default='time')

# Define our help request resource.
class HelpRequest(Resource):

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise respond with an HTML representation.
    def get(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        return make_response(
            render_helprequest_as_html(
                data['helprequests'][helprequest_id]), 200)

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise update the help request and respond
    # with the updated HTML representation.
    def patch(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        helprequest = data['helprequests'][helprequest_id]
        update = update_helprequest_parser.parse_args()
        helprequest['priority'] = update['priority']
        if len(update['comment'].strip()) > 0:
            helprequest.setdefault('comments', []).append(update['comment'])
        return make_response(
            render_helprequest_as_html(helprequest), 200)

# Define a resource for getting a JSON representation of a help request.
class HelpRequestAsJSON(Resource):

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise respond with a JSON representation.
    def get(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        helprequest = data['helprequests'][helprequest_id]
        helprequest['@context'] = data['@context']
        return helprequest

# Define our help request list resource.
class HelpRequestList(Resource):

    # Respond with an HTML representation of the help request list, after
    # applying any filtering and sorting parameters.
    def get(self):
        query = query_parser.parse_args()
        return make_response(
            render_helprequest_list_as_html(
                filter_and_sort_helprequests(**query)), 200)

    # Add a new help request to the list, and respond with an HTML
    # representation of the updated list.
    def post(self):
        helprequest = new_helprequest_parser.parse_args()
        helprequest_id = generate_id()
        helprequest['@id'] = 'request/' + helprequest_id
        helprequest['@type'] = 'helpdesk:HelpRequest'
        helprequest['time'] = datetime.isoformat(datetime.now())
        helprequest['priority'] = PRIORITIES.index('normal')
        data['helprequests'][helprequest_id] = helprequest
        return make_response(
            render_helprequest_list_as_html(
                filter_and_sort_helprequests()), 201)

# Define a resource for getting a JSON representation of the help request list.
class HelpRequestListAsJSON(Resource):
    def get(self):
        return data

# Assign URL paths to our resources.
app = Flask(__name__)
api = Api(app)
api.add_resource(HelpRequestList, '/requests')
api.add_resource(HelpRequestListAsJSON, '/requests.json')
api.add_resource(HelpRequest, '/request/<string:helprequest_id>')
api.add_resource(HelpRequestAsJSON, '/request/<string:helprequest_id>.json')

# Redirect from the index to the list of help requests.
@app.route('/')
def index():
    return redirect(api.url_for(HelpRequestList), code=303)

# This is needed to load JSON from Javascript running in the browser.
@app.after_request
def after_request(response):
  response.headers.add('Access-Control-Allow-Origin', '*')
  response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
  response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
  return response

# Start the server.
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5555, debug=True)
<commit_msg>Add example of HTTP Basic authentication.<commit_after>from flask import (Flask, Response, request, render_template, make_response,
                   redirect)
from flask.ext.restful import Api, Resource, reqparse, abort

import json
import string
import random
from functools import wraps
from datetime import datetime

# Define our priority levels.
# These are the values that the "priority" property can take on a help request.
PRIORITIES = ('closed', 'low', 'normal', 'high')

# Load data from disk.
# This simply loads the data from our "database," which is just a JSON file.
with open('data.jsonld') as data:
    data = json.load(data)

# Check that username and password are OK; DON'T DO THIS FOR REAL
def check_auth(username, password):
    return username == 'admin' and password == 'secret'

# Issue an authentication challenge
def authenticate():
    return Response(
        'Please authenticate yourself', 401,
        {'WWW-Authenticate': 'Basic realm="helpdesk"'})

# Decorator for methods that require authentication
def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated

# Generate a unique ID for a new help request.
# By default this will consist of six lowercase numbers and letters.
def generate_id(size=6, chars=string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

# Respond with 404 Not Found if no help request with the specified ID exists.
def error_if_helprequest_not_found(helprequest_id):
    if helprequest_id not in data['helprequests']:
        message = "No help request with ID: {}".format(helprequest_id)
        abort(404, message=message)

# Filter and sort a list of helprequests.
def filter_and_sort_helprequests(query='', sort_by='time'):

    # Returns True if the query string appears in the help request's
    # title or description.
    def matches_query(item):
        (helprequest_id, helprequest) = item
        text = helprequest['title'] + helprequest['description']
        return query.lower() in text

    # Returns the help request's value for the sort property (which by
    # default is the "time" property).
    def get_sort_value(item):
        (helprequest_id, helprequest) = item
        return helprequest[sort_by]

    filtered_helprequests = filter(matches_query, data['helprequests'].items())

    return sorted(filtered_helprequests, key=get_sort_value, reverse=True)

# Given the data for a help request, generate an HTML representation
# of that help request.
def render_helprequest_as_html(helprequest):
    return render_template(
        'helprequest+microdata+rdfa.html',
        helprequest=helprequest,
        priorities=reversed(list(enumerate(PRIORITIES))))

# Given the data for a list of help requests, generate an HTML representation
# of that list.
def render_helprequest_list_as_html(helprequests):
    return render_template(
        'helprequests+microdata+rdfa.html',
        helprequests=helprequests,
        priorities=PRIORITIES)

# Raises an error if the string x is empty (has zero length).
def nonempty_string(x):
    s = str(x)
    if len(x) == 0:
        raise ValueError('string is empty')
    return s

# Specify the data necessary to create a new help request.
# "from", "title", and "description" are all required values.
new_helprequest_parser = reqparse.RequestParser()
for arg in ['from', 'title', 'description']:
    new_helprequest_parser.add_argument(
        arg, type=nonempty_string, required=True,
        help="'{}' is a required value".format(arg))

# Specify the data necessary to update an existing help request.
# Only the priority and comments can be updated.
update_helprequest_parser = reqparse.RequestParser()
update_helprequest_parser.add_argument(
    'priority', type=int, default=PRIORITIES.index('normal'))
update_helprequest_parser.add_argument(
    'comment', type=str, default='')

# Specify the parameters for filtering and sorting help requests.
# See `filter_and_sort_helprequests` above.
query_parser = reqparse.RequestParser()
query_parser.add_argument(
    'query', type=str, default='')
query_parser.add_argument(
    'sort_by', type=str, choices=('priority', 'time'), default='time')

# Define our help request resource.
class HelpRequest(Resource):

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise respond with an HTML representation.
    def get(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        return make_response(
            render_helprequest_as_html(
                data['helprequests'][helprequest_id]), 200)

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise update the help request and respond
    # with the updated HTML representation.
    def patch(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        helprequest = data['helprequests'][helprequest_id]
        update = update_helprequest_parser.parse_args()
        helprequest['priority'] = update['priority']
        if len(update['comment'].strip()) > 0:
            helprequest.setdefault('comments', []).append(update['comment'])
        return make_response(
            render_helprequest_as_html(helprequest), 200)

# Define a resource for getting a JSON representation of a help request.
class HelpRequestAsJSON(Resource):

    # If a help request with the specified ID does not exist,
    # respond with a 404, otherwise respond with a JSON representation.
    def get(self, helprequest_id):
        error_if_helprequest_not_found(helprequest_id)
        helprequest = data['helprequests'][helprequest_id]
        helprequest['@context'] = data['@context']
        return helprequest

# Define our help request list resource.
class HelpRequestList(Resource):

    # Respond with an HTML representation of the help request list, after
    # applying any filtering and sorting parameters.
    @requires_auth
    def get(self):
        query = query_parser.parse_args()
        return make_response(
            render_helprequest_list_as_html(
                filter_and_sort_helprequests(**query)), 200)

    # Add a new help request to the list, and respond with an HTML
    # representation of the updated list.
    def post(self):
        helprequest = new_helprequest_parser.parse_args()
        helprequest_id = generate_id()
        helprequest['@id'] = 'request/' + helprequest_id
        helprequest['@type'] = 'helpdesk:HelpRequest'
        helprequest['time'] = datetime.isoformat(datetime.now())
        helprequest['priority'] = PRIORITIES.index('normal')
        data['helprequests'][helprequest_id] = helprequest
        return make_response(
            render_helprequest_list_as_html(
                filter_and_sort_helprequests()), 201)

# Define a resource for getting a JSON representation of the help request list.
class HelpRequestListAsJSON(Resource):
    def get(self):
        return data

# Assign URL paths to our resources.
app = Flask(__name__)
api = Api(app)
api.add_resource(HelpRequestList, '/requests')
api.add_resource(HelpRequestListAsJSON, '/requests.json')
api.add_resource(HelpRequest, '/request/<string:helprequest_id>')
api.add_resource(HelpRequestAsJSON, '/request/<string:helprequest_id>.json')

# Redirect from the index to the list of help requests.
@app.route('/')
def index():
    return redirect(api.url_for(HelpRequestList), code=303)

# This is needed to load JSON from Javascript running in the browser.
@app.after_request
def after_request(response):
  response.headers.add('Access-Control-Allow-Origin', '*')
  response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
  response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
  return response

# Start the server.
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5555, debug=True)
<|endoftext|>