<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

"""
This module contains the different priors to be used with ``keppy``.
"""

# Flat priors
def flat(theta, bounds):
    """
    Computes a flat prior probability for a given set of parameters theta.

    Parameters
    ----------
    theta : ``dict``
        The orbital and instrumental parameters. This dictionary must have
        keywords identical to the parameter names (which depend on the number
        of data sets, the parametrization option and if the additional
        uncertainties are also being estimated.

    bounds : ``dict``
        Bounds for the priors. This dictionary has the same keywords as
        ``theta``. Each entry has to be a sequence of size two, in which the
        first is the lower bound and the second is the higher bound. Example:
        ``bounds = {'log_k': (-1, 1), 'log_period': (1, 4)}``.

    Returns
    -------
    prob : ``float``
        The prior probability for a given set of orbital and instrumental
        parameters.
    """
    keys = theta.keys()

    # Compute the eccentricity beforehand to impose a prior of e < 1 on it
    try:
        ecc = 10 ** theta['log_ecc']
    except KeyError:
        try:
            ecc = (theta['sqe_cosw'] ** 2 + theta['sqe_sinw'] ** 2).value
        except AttributeError:
            ecc = theta['sqe_cosw'] ** 2 + theta['sqe_sinw'] ** 2

    check = [bounds[key][0] < theta[key] < bounds[key][1] for key in keys]
    if all(check) is True and ecc < 1:
        prob = 0.0
    else:
        prob = -np.inf
    return prob
<commit_msg>Removed support for astropy.units from prior<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

"""
This module contains the different priors to be used with ``keppy``.
"""

# Flat priors
def flat(theta, bounds):
    """
    Computes a flat prior probability for a given set of parameters theta.

    Parameters
    ----------
    theta : ``dict``
        The orbital and instrumental parameters. This dictionary must have
        keywords identical to the parameter names (which depend on the number
        of data sets, the parametrization option and if the additional
        uncertainties are also being estimated.

    bounds : ``dict``
        Bounds for the priors. This dictionary has the same keywords as
        ``theta``. Each entry has to be a sequence of size two, in which the
        first is the lower bound and the second is the higher bound. Example:
        ``bounds = {'log_k': (-1, 1), 'log_period': (1, 4)}``.

    Returns
    -------
    prob : ``float``
        The prior probability for a given set of orbital and instrumental
        parameters.
    """
    keys = theta.keys()

    # Compute the eccentricity beforehand to impose a prior of e < 1 on it
    try:
        ecc = 10 ** theta['log_ecc']
    except KeyError:
        try:
            ecc = (theta['sqe_cosw'] ** 2 + theta['sqe_sinw'] ** 2)
        except AttributeError:
            ecc = theta['sqe_cosw'] ** 2 + theta['sqe_sinw'] ** 2

    check = [bounds[key][0] < theta[key] < bounds[key][1] for key in keys]
    if all(check) is True and ecc < 1:
        prob = 0.0
    else:
        prob = -np.inf
    return prob
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""Swagger generic API client. This client handles the client-
server communication, and is invariant across implementations. Specifics of
the methods and models for each application are generated from the Swagger
templates."""

import ast
import sys
import os
import re
import urllib
import urllib2
import httplib
import json
import datetime
import mimetypes
import random
import string

from models import *

class ApiClient(object):
  """Generic API client for Swagger client library builds

  Attributes:
    host: The base path for the server to call
    headerName: a header to pass when making calls to the API
    headerValue: a header value to pass when making calls to the API
  """
  def __init__(self, host=None, headerName=None, headerValue=None):
    self.defaultHeaders = {}
    if (headerName is not None):
      self.defaultHeaders[headerName] = headerValue
    self.host = host
    self.cookie = None
    self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30))
    # Set default User-Agent.
    self.user_agent = 'Python-Swagger'

  @property
  def user_agent(self):
    return self.defaultHeaders['User-Agent']

  @user_agent.setter
  def user_agent(self, value):
    self.defaultHeaders['User-Agent'] = value

  def setDefaultHeader(self, headerName, headerValue):
    self.defaultHeaders[headerName] = headerValue

  def callAPI(self, resourcePath, method, queryParams, postData,
              headerParams=None, files=None):

    url = self.host + resourcePath

    mergedHeaderParams = self.defaultHeaders.copy()
    mergedHeaderParams.update(headerParams)
    headers = {}
    if mergedHeaderParams:
      for param, value in mergedHeaderParams.iteritems():
        headers[param] = ApiClient.sanitizeForSerialization(value)

    if self.cookie:
      headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)

    data = None

    if queryParams:
      # Need to remove None values, these should not be sent
      sentQueryParams = {}
      for param, value in queryParams.items():
        if value is not None:
          sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
      url = url + '?' + urllib.urlencode(sentQueryParams)

    if method in ['GET']:
      #Options to add statements later on and for compatibility
      pass

    elif method in ['POST', 'PUT', 'DELETE']:
      if postData:
        postData = ApiClient.sanitizeForSerialization(postData)
        if 'Content-type' not in headers:
          headers['Content-type'] = 'application/json'
          data = json.dumps(postData)
        elif headers['Content-type'] == 'multipart/form-data':
          data = self.buildMultipartFormData(postData, files)
          headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary)
          headers['Content-length'] = str(len(data))
        else:
            data = urllib.urlencode(postData)

    else:
      raise Exception('Method ' + method + ' is not recognized.')

    request = MethodRequest(method=method, url=url, headers=headers,
                            data=data)

    # Make the request
    response = urllib2.urlopen(request)
    if 'Set-Cookie' in response.headers:
      self.cookie = response.headers['Set-Cookie']
    string = response.read()

    try:
      data = json.loads(string)
    except ValueError:  # PUT requests don't return anything
      data = None

    return data

  def toPathValue(self, obj):
    """Convert a string or object to a path-friendly value
    Args:
        obj -- object or string value
    Returns:
        string -- quoted value
    """
    if type(obj) == list:
      return ','.join(obj)
    else:
      return str(obj)

  @staticmethod
  def sanitizeForSerialization(obj):
    """
    Sanitize an object for Request.

    If obj is None, return None.
    If obj is str, int, long, float, bool, return directly.
    If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
    If obj is list, santize each element in the list.
    If obj is dict, return the dict.
    If obj is swagger model, return the properties dict.
    """
    if isinstance(obj, type(None)):
      return None
    elif isinstance(obj, (str, int, long, float, bool, file)):
      return obj
    elif isinstance(obj, list):
      return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj]
    elif isinstance(obj, (datetime.datetime, datetime.date)):
      return obj.isoformat()
    else:
      if isinstance(obj, dict):
        objDict = obj
      else:
        # Convert model obj to dict except attributes `swaggerTypes`, `attributeMap`
        # and attributes which value is not None.
        # Convert attribute name to json key in model definition for request.
        objDict = {obj.attributeMap[key]: val
                   for key, val in obj.__dict__.iteritems()
                   if key != 'swaggerTypes' and key != 'attributeMap' and val is not None}
      return {key: ApiClient.sanitizeForSerialization(val)
              for (key, val) in objDict.iteritems()}

  def buildMultipartFormData(self, postData, files):
    def escape_quotes(s):
      return s.replace('"', '\\"')

    lines = []

    for name, value in postData.items():
      lines.extend((
        '--{0}'.format(self.boundary),
        'Content-Disposition: form-data; name="{0}"'.format(escape_quotes(name)),
        '',
        str(value),
      ))

    for name, filepath in files.items():
      f = open(filepath, 'r')
      filename = filepath.split('/')[-1]
      mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
      lines.extend((
        '--{0}'.format(self.boundary),
        'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)),
        'Content-Type: {0}'.format(mimetype),
        '',
        f.read()
      ))

    lines.extend((
      '--{0}--'.format(self.boundary),
      ''
    ))
    return '\r\n'.join(lines)

  def deserialize(self, obj, objClass):
    """Derialize a JSON string into an object.

    Args:
        obj -- string or object to be deserialized
        objClass -- class literal for deserialzied object, or string
            of class name
    Returns:
        object -- deserialized object"""

    # Have to accept objClass as string or actual type. Type could be a
    # native Python type, or one of the model classes.
    if type(objClass) == str:
      if 'list[' in objClass:
        match = re.match('list\[(.*)\]', objClass)
        subClass = match.group(1)
        return [self.deserialize(subObj, subClass) for subObj in obj]

      if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']):
        objClass = ast.literal_eval(objClass)
      else:  # not a native type, must be model class
        objClass = ast.literal_eval(objClass + '.' + objClass)

    if objClass in [int, long, float, dict, list, str, bool]:
      return objClass(obj)
    elif objClass == datetime:
      return self.__parse_string_to_datetime(obj)

    instance = objClass()

    for attr, attrType in instance.swaggerTypes.iteritems():
        if obj is not None and instance.attributeMap[attr] in obj and type(obj) in [list, dict]:
          value = obj[instance.attributeMap[attr]]
          if attrType in ['str', 'int', 'long', 'float', 'bool']:
            attrType = ast.literal_eval(attrType)
            try:
              value = attrType(value)
            except UnicodeEncodeError:
              value = unicode(value)
            except TypeError:
              value = value
            setattr(instance, attr, value)
          elif (attrType == 'datetime'):
            setattr(instance, attr, self.__parse_string_to_datetime(value))
          elif 'list[' in attrType:
            match = re.match('list\[(.*)\]', attrType)
            subClass = match.group(1)
            subValues = []
            if not value:
              setattr(instance, attr, None)
            else:
              for subValue in value:
                subValues.append(self.deserialize(subValue, subClass))
            setattr(instance, attr, subValues)
          else:
            setattr(instance, attr, self.deserialize(value, attrType))

    return instance

  def __parse_string_to_datetime(self, string):
    """
    Parse datetime in string to datetime.

    The string should be in iso8601 datetime format.
    """
    try:
        from dateutil.parser import parse
        return parse(string)
    except ImportError:
        return string

class MethodRequest(urllib2.Request):
  def __init__(self, *args, **kwargs):
    """Construct a MethodRequest. Usage is the same as for
    `urllib2.Request` except it also takes an optional `method`
    keyword argument. If supplied, `method` will be used instead of
    the default."""

    if 'method' in kwargs:
      self.method = kwargs.pop('method')
    return urllib2.Request.__init__(self, *args, **kwargs)

  def get_method(self):
    return getattr(self, 'method', urllib2.Request.get_method(self))
<commit_msg>Validate scheme used in urlopen<commit_after>#!/usr/bin/env python
"""Swagger generic API client. This client handles the client-
server communication, and is invariant across implementations. Specifics of
the methods and models for each application are generated from the Swagger
templates."""

import ast
import sys
import os
import re
import urllib
import urllib2
import httplib
import json
import datetime
import mimetypes
import random
import string

from magnum import utils
from models import *

class ApiClient(object):
  """Generic API client for Swagger client library builds

  Attributes:
    host: The base path for the server to call
    headerName: a header to pass when making calls to the API
    headerValue: a header value to pass when making calls to the API
  """
  def __init__(self, host=None, headerName=None, headerValue=None):
    self.defaultHeaders = {}
    if (headerName is not None):
      self.defaultHeaders[headerName] = headerValue
    self.host = host
    self.cookie = None
    self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30))
    # Set default User-Agent.
    self.user_agent = 'Python-Swagger'

  @property
  def user_agent(self):
    return self.defaultHeaders['User-Agent']

  @user_agent.setter
  def user_agent(self, value):
    self.defaultHeaders['User-Agent'] = value

  def setDefaultHeader(self, headerName, headerValue):
    self.defaultHeaders[headerName] = headerValue

  def callAPI(self, resourcePath, method, queryParams, postData,
              headerParams=None, files=None):

    url = self.host + resourcePath

    mergedHeaderParams = self.defaultHeaders.copy()
    mergedHeaderParams.update(headerParams)
    headers = {}
    if mergedHeaderParams:
      for param, value in mergedHeaderParams.iteritems():
        headers[param] = ApiClient.sanitizeForSerialization(value)

    if self.cookie:
      headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)

    data = None

    if queryParams:
      # Need to remove None values, these should not be sent
      sentQueryParams = {}
      for param, value in queryParams.items():
        if value is not None:
          sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
      url = url + '?' + urllib.urlencode(sentQueryParams)

    if method in ['GET']:
      #Options to add statements later on and for compatibility
      pass

    elif method in ['POST', 'PUT', 'DELETE']:
      if postData:
        postData = ApiClient.sanitizeForSerialization(postData)
        if 'Content-type' not in headers:
          headers['Content-type'] = 'application/json'
          data = json.dumps(postData)
        elif headers['Content-type'] == 'multipart/form-data':
          data = self.buildMultipartFormData(postData, files)
          headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary)
          headers['Content-length'] = str(len(data))
        else:
            data = urllib.urlencode(postData)

    else:
      raise Exception('Method ' + method + ' is not recognized.')

    utils.raise_exception_invalid_scheme(url)

    request = MethodRequest(method=method, url=url, headers=headers,
                            data=data)

    # Make the request
    response = urllib2.urlopen(request) #nosec
    if 'Set-Cookie' in response.headers:
      self.cookie = response.headers['Set-Cookie']
    string = response.read()

    try:
      data = json.loads(string)
    except ValueError:  # PUT requests don't return anything
      data = None

    return data

  def toPathValue(self, obj):
    """Convert a string or object to a path-friendly value
    Args:
        obj -- object or string value
    Returns:
        string -- quoted value
    """
    if type(obj) == list:
      return ','.join(obj)
    else:
      return str(obj)

  @staticmethod
  def sanitizeForSerialization(obj):
    """
    Sanitize an object for Request.

    If obj is None, return None.
    If obj is str, int, long, float, bool, return directly.
    If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
    If obj is list, santize each element in the list.
    If obj is dict, return the dict.
    If obj is swagger model, return the properties dict.
    """
    if isinstance(obj, type(None)):
      return None
    elif isinstance(obj, (str, int, long, float, bool, file)):
      return obj
    elif isinstance(obj, list):
      return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj]
    elif isinstance(obj, (datetime.datetime, datetime.date)):
      return obj.isoformat()
    else:
      if isinstance(obj, dict):
        objDict = obj
      else:
        # Convert model obj to dict except attributes `swaggerTypes`, `attributeMap`
        # and attributes which value is not None.
        # Convert attribute name to json key in model definition for request.
        objDict = {obj.attributeMap[key]: val
                   for key, val in obj.__dict__.iteritems()
                   if key != 'swaggerTypes' and key != 'attributeMap' and val is not None}
      return {key: ApiClient.sanitizeForSerialization(val)
              for (key, val) in objDict.iteritems()}

  def buildMultipartFormData(self, postData, files):
    def escape_quotes(s):
      return s.replace('"', '\\"')

    lines = []

    for name, value in postData.items():
      lines.extend((
        '--{0}'.format(self.boundary),
        'Content-Disposition: form-data; name="{0}"'.format(escape_quotes(name)),
        '',
        str(value),
      ))

    for name, filepath in files.items():
      f = open(filepath, 'r')
      filename = filepath.split('/')[-1]
      mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
      lines.extend((
        '--{0}'.format(self.boundary),
        'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)),
        'Content-Type: {0}'.format(mimetype),
        '',
        f.read()
      ))

    lines.extend((
      '--{0}--'.format(self.boundary),
      ''
    ))
    return '\r\n'.join(lines)

  def deserialize(self, obj, objClass):
    """Derialize a JSON string into an object.

    Args:
        obj -- string or object to be deserialized
        objClass -- class literal for deserialzied object, or string
            of class name
    Returns:
        object -- deserialized object"""

    # Have to accept objClass as string or actual type. Type could be a
    # native Python type, or one of the model classes.
    if type(objClass) == str:
      if 'list[' in objClass:
        match = re.match('list\[(.*)\]', objClass)
        subClass = match.group(1)
        return [self.deserialize(subObj, subClass) for subObj in obj]

      if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']):
        objClass = ast.literal_eval(objClass)
      else:  # not a native type, must be model class
        objClass = ast.literal_eval(objClass + '.' + objClass)

    if objClass in [int, long, float, dict, list, str, bool]:
      return objClass(obj)
    elif objClass == datetime:
      return self.__parse_string_to_datetime(obj)

    instance = objClass()

    for attr, attrType in instance.swaggerTypes.iteritems():
        if obj is not None and instance.attributeMap[attr] in obj and type(obj) in [list, dict]:
          value = obj[instance.attributeMap[attr]]
          if attrType in ['str', 'int', 'long', 'float', 'bool']:
            attrType = ast.literal_eval(attrType)
            try:
              value = attrType(value)
            except UnicodeEncodeError:
              value = unicode(value)
            except TypeError:
              value = value
            setattr(instance, attr, value)
          elif (attrType == 'datetime'):
            setattr(instance, attr, self.__parse_string_to_datetime(value))
          elif 'list[' in attrType:
            match = re.match('list\[(.*)\]', attrType)
            subClass = match.group(1)
            subValues = []
            if not value:
              setattr(instance, attr, None)
            else:
              for subValue in value:
                subValues.append(self.deserialize(subValue, subClass))
            setattr(instance, attr, subValues)
          else:
            setattr(instance, attr, self.deserialize(value, attrType))

    return instance

  def __parse_string_to_datetime(self, string):
    """
    Parse datetime in string to datetime.

    The string should be in iso8601 datetime format.
    """
    try:
        from dateutil.parser import parse
        return parse(string)
    except ImportError:
        return string

class MethodRequest(urllib2.Request):
  def __init__(self, *args, **kwargs):
    """Construct a MethodRequest. Usage is the same as for
    `urllib2.Request` except it also takes an optional `method`
    keyword argument. If supplied, `method` will be used instead of
    the default."""

    if 'method' in kwargs:
      self.method = kwargs.pop('method')
    return urllib2.Request.__init__(self, *args, **kwargs)

  def get_method(self):
    return getattr(self, 'method', urllib2.Request.get_method(self))
<|endoftext|>


<commit_before>"""
:file: enum.py

Contains a more natural enum implementation.

:date: 10/06/2016
:authors:
    - Gilad Naaman <gilad@naaman.io>
"""

from enum import auto
from .base import *
from .scalars import *
import collections

__all__ = ('Enum', 'auto')

class auto:
    pass

class Literal:
    def __init__(self, enum, literal_name, value):
        self.enum = enum
        self.literal_name = literal_name
        self.value = value

    def __int__(self):
        return self.value

    def __repr__(self):
        return f'{get_type_name(self.enum)}.{self.literal_name}'

class EnumMetadata(SerializerMetadata):
    __slots__ = ('flags', 'serializer', 'literals')
    _VALID_UNDERLYING_TYPES = (
        u8, u16, u32, u64, i8, i16, i32, i64,
        u8_le, u16_le, u32_le, u64_le, i8_le, i16_le, i32_le, i64_le,
        u8_be, u16_be, u32_be, u64_be, i8_be, i16_be, i32_be, i64_be)

    def __init__(self, *, literals: collections.OrderedDict, underlying: Type['Scalar'], flags: bool = False):
        super().__init__(underlying.byte_size)

        if underlying not in self._VALID_UNDERLYING_TYPES:
            raise TypeError(f'Invalid underlying type for Enum: {get_type_name(underlying)}')
        serializer = get_as_value(underlying)
        try:
            for k, v in literals.items():
                if not serializer.validate(v):
                    raise ValueError('<unknown error>')
        except ValueError as e:
            raise ValueError(f'Invalid value for literal {v}: {e}')

        self.flags = flags
        self.serializer = serializer
        self.literals = literals

class EnumMeta(SerializerMeta):
    def __new__(mcs, name, bases, classdict: collections.OrderedDict, underlying_type=i32):
        if not hasattr(mcs, SerializerMeta.METAATTR):
            literals = (
                (k, v) for k, v in classdict.items()
                if isinstance(v, (int, auto)) and not k.startswith('_')
            )
            literals_dict = collections.OrderedDict()

            next_expected_value = 0
            for lit_name, literal in literals:
                # Replace `auto` instances with the correct values.
                if isinstance(literal, auto):
                    # Update the literal object before taking its value
                    literal = next_expected_value
                    classdict[lit_name] = literal

                next_expected_value = literal + 1
                literals_dict[lit_name] = literal

            for lit_name in literals_dict:
                del classdict[lit_name]

            classdict.update({
                SerializerMeta.METAATTR: EnumMetadata(literals=literals_dict, underlying=underlying_type)
            })

        return super(EnumMeta, mcs).__new__(mcs, name, bases, classdict)

    def __prepare__(cls, bases, **kwargs):
        return collections.OrderedDict()

    @property
    def literals(cls) -> collections.OrderedDict:
        return cls.__hydras_metadata__.literals

    def __getattr__(self, name):
        # Wrap literals in a `Literal` object
        if name in self.literals:
            return Literal(self, name, self.literals[name])
        return super().__getattr__(name)

class Enum(Serializer, metaclass=EnumMeta):
    @property
    def literals(self) -> collections.OrderedDict:
        return type(self).literals

    @property
    def serializer(self) -> Scalar:
        return self.__hydras_metadata__.serializer

    """ An enum formatter that can be shared between structs. """
    def __init__(self, default_value=None, *args, **kwargs):
        if type(self) is Enum:
            raise RuntimeError('Cannot instantiate `Enum` directly. Must subclass it.')
        elif len(self.literals) == 0:
            raise RuntimeError('Cannot instantiate an empty Enum')

        assert default_value is None or isinstance(default_value, (int, Literal))

        # Validate the default_value
        if default_value is None:
            default_value = self.get_literal_by_name(next(iter(self.literals)))
        elif isinstance(default_value, int):
            if not self.is_constant_valid(default_value):
                raise ValueError('Literal constant is not included in the enum: %d' % default_value)
            default_value = self.get_literal_by_value(default_value)
        elif isinstance(default_value, Literal):
            if default_value.enum is not type(self) or \
                    not self.is_constant_valid(default_value.value) or \
                    default_value.literal_name not in self.literals:
                raise ValueError('Invalid or corrupted literal')

        super(Enum, self).__init__(default_value, *args, **kwargs)

    def serialize(self, value: Literal, settings=None):
        assert (isinstance(value, Literal) and value.enum == type(self)) or \
               (isinstance(value, int) and self.is_constant_valid(value))

        return self.serializer.serialize(int(value), self.resolve_settings(settings))

    def deserialize(self, raw_data, settings=None):
        value = self.serializer.deserialize(raw_data, self.resolve_settings(settings))

        if not self.is_constant_valid(value):
            raise ValueError('Parsed enum value is unknown: %d' % value)

        return Literal(type(self), self.get_literal_name(value) or '<invalid>', value)

    def validate(self, value):
        """ Validate the given enum value. """
        if not self.is_constant_valid(int(value)):
            return False

        return super(Enum, self).validate(int(value))

    def is_constant_valid(self, num):
        """ Determine if the given number is a valid enum literal. """
        return num in self.literals.values()

    def get_literal_name(self, num):
        """ Get the name of the constant from a number or a Literal object. """
        return next((n for n, v in self.literals.items() if v == num), None)

    def get_literal_by_name(self, name):
        return Literal(type(self), name, self.literals[name])

    def get_literal_by_value(self, value):
        return Literal(type(self), self.get_literal_name(value), value)

    def values_equal(self, a, b):
        return int(a) == int(b)
<commit_msg>enum.auto satisfies pycharm's type checker a bit better<commit_after>"""
:file: enum.py

Contains a more natural enum implementation.

:date: 10/06/2016
:authors:
    - Gilad Naaman <gilad@naaman.io>
"""

from enum import auto
from .base import *
from .scalars import *
import collections

__all__ = ('Enum', 'auto')

class auto:
    # This is implemented solely to satisfy PyCharm's type-checker.
    # In reality this function will never be called
    def __int__(self):
        raise RuntimeError('This is not supposed to happen')

class Literal:
    def __init__(self, enum, literal_name, value):
        self.enum = enum
        self.literal_name = literal_name
        self.value = value

    def __int__(self):
        return self.value

    def __repr__(self):
        return f'{get_type_name(self.enum)}.{self.literal_name}'

class EnumMetadata(SerializerMetadata):
    __slots__ = ('flags', 'serializer', 'literals')
    _VALID_UNDERLYING_TYPES = (
        u8, u16, u32, u64, i8, i16, i32, i64,
        u8_le, u16_le, u32_le, u64_le, i8_le, i16_le, i32_le, i64_le,
        u8_be, u16_be, u32_be, u64_be, i8_be, i16_be, i32_be, i64_be)

    def __init__(self, *, literals: collections.OrderedDict, underlying: Type['Scalar'], flags: bool = False):
        super().__init__(underlying.byte_size)

        if underlying not in self._VALID_UNDERLYING_TYPES:
            raise TypeError(f'Invalid underlying type for Enum: {get_type_name(underlying)}')
        serializer = get_as_value(underlying)
        try:
            for k, v in literals.items():
                if not serializer.validate(v):
                    raise ValueError('<unknown error>')
        except ValueError as e:
            raise ValueError(f'Invalid value for literal {v}: {e}')

        self.flags = flags
        self.serializer = serializer
        self.literals = literals

class EnumMeta(SerializerMeta):
    def __new__(mcs, name, bases, classdict: collections.OrderedDict, underlying_type=i32):
        if not hasattr(mcs, SerializerMeta.METAATTR):
            literals = (
                (k, v) for k, v in classdict.items()
                if isinstance(v, (int, auto)) and not k.startswith('_')
            )
            literals_dict = collections.OrderedDict()

            next_expected_value = 0
            for lit_name, literal in literals:
                # Replace `auto` instances with the correct values.
                if isinstance(literal, auto):
                    # Update the literal object before taking its value
                    literal = next_expected_value
                    classdict[lit_name] = literal

                next_expected_value = literal + 1
                literals_dict[lit_name] = literal

            for lit_name in literals_dict:
                del classdict[lit_name]

            classdict.update({
                SerializerMeta.METAATTR: EnumMetadata(literals=literals_dict, underlying=underlying_type)
            })

        return super(EnumMeta, mcs).__new__(mcs, name, bases, classdict)

    def __prepare__(cls, bases, **kwargs):
        return collections.OrderedDict()

    @property
    def literals(cls) -> collections.OrderedDict:
        return cls.__hydras_metadata__.literals

    def __getattr__(self, name):
        # Wrap literals in a `Literal` object
        if name in self.literals:
            return Literal(self, name, self.literals[name])
        return super().__getattr__(name)

class Enum(Serializer, metaclass=EnumMeta):
    @property
    def literals(self) -> collections.OrderedDict:
        return type(self).literals

    @property
    def serializer(self) -> Scalar:
        return self.__hydras_metadata__.serializer

    """ An enum formatter that can be shared between structs. """
    def __init__(self, default_value=None, *args, **kwargs):
        if type(self) is Enum:
            raise RuntimeError('Cannot instantiate `Enum` directly. Must subclass it.')
        elif len(self.literals) == 0:
            raise RuntimeError('Cannot instantiate an empty Enum')

        assert default_value is None or isinstance(default_value, (int, Literal))

        # Validate the default_value
        if default_value is None:
            default_value = self.get_literal_by_name(next(iter(self.literals)))
        elif isinstance(default_value, int):
            if not self.is_constant_valid(default_value):
                raise ValueError('Literal constant is not included in the enum: %d' % default_value)
            default_value = self.get_literal_by_value(default_value)
        elif isinstance(default_value, Literal):
            if default_value.enum is not type(self) or \
                    not self.is_constant_valid(default_value.value) or \
                    default_value.literal_name not in self.literals:
                raise ValueError('Invalid or corrupted literal')

        super(Enum, self).__init__(default_value, *args, **kwargs)

    def serialize(self, value: Literal, settings=None):
        assert (isinstance(value, Literal) and value.enum == type(self)) or \
               (isinstance(value, int) and self.is_constant_valid(value))

        return self.serializer.serialize(int(value), self.resolve_settings(settings))

    def deserialize(self, raw_data, settings=None):
        value = self.serializer.deserialize(raw_data, self.resolve_settings(settings))

        if not self.is_constant_valid(value):
            raise ValueError('Parsed enum value is unknown: %d' % value)

        return Literal(type(self), self.get_literal_name(value) or '<invalid>', value)

    def validate(self, value):
        """ Validate the given enum value. """
        if not self.is_constant_valid(int(value)):
            return False

        return super(Enum, self).validate(int(value))

    def is_constant_valid(self, num):
        """ Determine if the given number is a valid enum literal. """
        return num in self.literals.values()

    def get_literal_name(self, num):
        """ Get the name of the constant from a number or a Literal object. """
        return next((n for n, v in self.literals.items() if v == num), None)

    def get_literal_by_name(self, name):
        return Literal(type(self), name, self.literals[name])

    def get_literal_by_value(self, value):
        return Literal(type(self), self.get_literal_name(value), value)

    def values_equal(self, a, b):
        return int(a) == int(b)
<|endoftext|>


<commit_before>from gbdxtools.images.base import RDABaseImage
from gbdxtools.images.drivers import RDADaskImageDriver
from gbdxtools.images.util.image import reproject_params
from gbdxtools.rda.interface import RDA
rda = RDA()

class Sentinel2Driver(RDADaskImageDriver):
    image_option_support = ["spec", "proj"]
    __image_option_defaults__ = {"spec": "10m", "proj": None}

class Sentinel2(RDABaseImage):
    """
     Dask based access to Sentinel2 images backed by rda Graphs.

     Args:
        prefix (str): The Sentinel data location from Catalog metadata item 'bucketPrefix'
        spec (str): Sensor group to use, values are '10m' (default), '20m', and '60m'
        proj (str): EPSG code for the resulting image, defaults to EPSG:4326 (WGS 84 Lat/Lon)
    """
    __Driver__ = Sentinel2Driver

    @property
    def _id(self):
        return self.__rda_id__

    @property
    def _spec(self):
        return self.options["spec"]

    @property
    def _rgb_bands(self):
        return [2,1,0]

    @property
    def _ndvi_bands(self):
         return [1,3]

    @classmethod
    def _build_graph(cls, prefix, spec="10m", proj=None, **kwargs):
        sentinel = rda.SentinelRead(SentinelId=prefix, sentinelProductSpec=spec)
        if proj is not None:
            sentinel = rda.Reproject(sentinel, **reproject_params(proj))
        return sentinel

class Sentinel1Driver(RDADaskImageDriver):
    image_option_support = ["polarization", "proj"]
    __image_option_defaults__ = {"polarization": "VH", "proj": None}

class Sentinel1(RDABaseImage):
    """
     Dask based access to Sentinel1 images backed by rda Graphs.

     Args:
        catID (str): The Sentinel CatalogID from the Sentinel1 Catalog Item
        polarization (str): The Polarization type. Defaults to 'VH' (default), 'VV', 'HH', or 'VV'
        proj (str): EPSG code for the resulting image.
    """
    __Driver__ = Sentinel2Driver

    @property
    def _id(self):
        return self.__rda_id__

    @property
    def polarization(self):
        return self.options["polarization"]

    @property
    def _rgb_bands(self):
        return [2,1,0]

    @property
    def _ndvi_bands(self):
         return [1,3]

    @classmethod
    def _build_graph(cls, catID, polarization="VH", proj=None, **kwargs):
        sentinel = rda.Sentinel1Read(SentinelId=catID, sentinel1Polarization=polarization)
        if proj is not None:
            sentinel = rda.Reproject(sentinel, **reproject_params(proj))
        return sentinel
<commit_msg>remove rgb and ndvi band def in sentinel1<commit_after>from gbdxtools.images.base import RDABaseImage
from gbdxtools.images.drivers import RDADaskImageDriver
from gbdxtools.images.util.image import reproject_params
from gbdxtools.rda.interface import RDA
rda = RDA()

class Sentinel2Driver(RDADaskImageDriver):
    image_option_support = ["spec", "proj"]
    __image_option_defaults__ = {"spec": "10m", "proj": None}

class Sentinel2(RDABaseImage):
    """
     Dask based access to Sentinel2 images backed by rda Graphs.

     Args:
        prefix (str): The Sentinel data location from Catalog metadata item 'bucketPrefix'
        spec (str): Sensor group to use, values are '10m' (default), '20m', and '60m'
        proj (str): EPSG code for the resulting image, defaults to EPSG:4326 (WGS 84 Lat/Lon)
    """
    __Driver__ = Sentinel2Driver

    @property
    def _id(self):
        return self.__rda_id__

    @property
    def _spec(self):
        return self.options["spec"]

    @property
    def _rgb_bands(self):
        return [2,1,0]

    @property
    def _ndvi_bands(self):
         return [1,3]

    @classmethod
    def _build_graph(cls, prefix, spec="10m", proj=None, **kwargs):
        sentinel = rda.SentinelRead(SentinelId=prefix, sentinelProductSpec=spec)
        if proj is not None:
            sentinel = rda.Reproject(sentinel, **reproject_params(proj))
        return sentinel

class Sentinel1Driver(RDADaskImageDriver):
    image_option_support = ["polarization", "proj"]
    __image_option_defaults__ = {"polarization": "VH", "proj": None}

class Sentinel1(RDABaseImage):
    """
     Dask based access to Sentinel1 images backed by rda Graphs.

     Args:
        catID (str): The Sentinel CatalogID from the Sentinel1 Catalog Item
        polarization (str): The Polarization type. Defaults to 'VH' (default), 'VV', 'HH', or 'VV'
        proj (str): EPSG code for the resulting image.
    """
    __Driver__ = Sentinel2Driver

    @property
    def _id(self):
        return self.__rda_id__

    @property
    def polarization(self):
        return self.options["polarization"]

    @classmethod
    def _build_graph(cls, catID, polarization="VH", proj=None, **kwargs):
        sentinel = rda.Sentinel1Read(SentinelId=catID, sentinel1Polarization=polarization)
        if proj is not None:
            sentinel = rda.Reproject(sentinel, **reproject_params(proj))
        return sentinel
<|endoftext|>


<commit_before>import logging
import requests

__version__ = '1.0'

RELS = {
    'activity_streams': 'http://activitystrea.ms/spec/1.0',
    'avatar': 'http://webfinger.net/rel/avatar',
    'hcard': 'http://microformats.org/profile/hcard',
    'open_id': 'http://specs.openid.net/auth/2.0/provider',
    'opensocial': 'http://ns.opensocial.org/2008/opensocial/activitystreams',
    'portable_contacts': 'http://portablecontacts.net/spec/1.0',
    'profile': 'http://webfinger.net/rel/profile-page',
    'xfn': 'http://gmpg.org/xfn/11',
}

WEBFINGER_TYPE = 'application/jrd+json'

UNOFFICIAL_ENDPOINTS = {
    'facebook.com': 'facebook-webfinger.appspot.com',
    'twitter.com': 'twitter-webfinger.appspot.com',
}

logger = logging.getLogger("webfinger")

class WebFingerException(Exception):
    pass

class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

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

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get('subject')

    @property
    def aliases(self):
        return self.jrd.get('aliases')

    @property
    def properties(self):
        return self.jrd.get('properties')

    @property
    def links(self):
        return self.jrd.get('links')

    def rel(self, relation, attr='href'):
        links = self.links
        if links:
            for link in links:
                if link.get('rel') == relation:
                    return link.get(attr)

class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def jrd(self, host, resource, rel, raw=False):
        """ Load resource at given URL and attempt to parse either XRD or JRD
            based on HTTP response Content-Type header.
        """

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            'User-Agent': 'python-webfinger/%s' % __version__,
            'Accept': WEBFINGER_TYPE,
        }

        params = {'resource': resource}
        if rel:
            params['rel'] = rel

        resp = requests.get(url, params=params, headers=headers, timeout=self.timeout, verify=True)

        logging.debug('fetching JRD from %s' % resp.url)

        content = resp.content
        content_type = resp.headers.get('Content-Type')

        logging.debug('response content type: %s' % content_type)

        if content_type != WEBFINGER_TYPE:
            raise WebFingerException('Invalid response type from server')

        if raw:
            return content

        return resp.json()

    def finger(self, resource, rel=None):
        """ Perform a WebFinger query based on the given subject.
            The `rel` parameter, if specified, will be passed to the provider,
            but be aware that providers are not required to implement the
            rel filter.
        """
        host = resource.split("@")[-1]
        jrd = self.jrd(host, resource, rel)
        return WebFingerResponse(jrd)

def finger(resource, rel=None, timeout=None, official=False):
    """ Shortcut method for invoking WebFingerClient.
    """

    if ":" not in resource:
        raise WebFingerException("scheme is required in subject URI")

    client = WebFingerClient(timeout=timeout, official=official)
    return client.finger(resource, rel=rel)

if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser(description="Simple webfinger client.")
    parser.add_argument("acct", metavar="URI", help="account URI")
    parser.add_argument("-d", "--debug", dest="debug", action="store_true", help="print debug logging output to console")
    parser.add_argument("-r", "--rel", metavar="REL", dest="rel", help="desired relation")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    wf = finger(args.acct, rel=args.rel)

    if args.rel:

        link = wf.find_link(args.rel)

        if link is None:
            print "*** Link not found for rel=%s" % args.rel

        print "%s:\n\t%s" % (link.rel, link.href)

    else:

        print "Activity Streams:  ", wf.activity_streams
        print "Avatar:            ", wf.avatar
        print "HCard:             ", wf.hcard
        print "OpenID:            ", wf.open_id
        print "Open Social:       ", wf.opensocial
        print "Portable Contacts: ", wf.portable_contacts
        print "Profile:           ", wf.profile
        print "XFN:               ", wf.rel("http://gmpg.org/xfn/11")
<commit_msg>Re-enable unofficial Facebook and Twitter endpoints, add WebFist rel<commit_after>import logging
import requests

__version__ = '1.0'

RELS = {
    'activity_streams': 'http://activitystrea.ms/spec/1.0',
    'avatar': 'http://webfinger.net/rel/avatar',
    'hcard': 'http://microformats.org/profile/hcard',
    'open_id': 'http://specs.openid.net/auth/2.0/provider',
    'opensocial': 'http://ns.opensocial.org/2008/opensocial/activitystreams',
    'portable_contacts': 'http://portablecontacts.net/spec/1.0',
    'profile': 'http://webfinger.net/rel/profile-page',
    'webfist': 'http://webfist.org/spec/rel',
    'xfn': 'http://gmpg.org/xfn/11',
}

WEBFINGER_TYPE = 'application/jrd+json'

UNOFFICIAL_ENDPOINTS = {
    'facebook.com': 'facebook-webfinger.appspot.com',
    'twitter.com': 'twitter-webfinger.appspot.com',
}

logger = logging.getLogger("webfinger")

class WebFingerException(Exception):
    pass

class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

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

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get('subject')

    @property
    def aliases(self):
        return self.jrd.get('aliases')

    @property
    def properties(self):
        return self.jrd.get('properties')

    @property
    def links(self):
        return self.jrd.get('links')

    def rel(self, relation, attr='href'):
        links = self.links
        if links:
            for link in links:
                if link.get('rel') == relation:
                    return link.get(attr)

class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def jrd(self, host, resource, rel, raw=False):
        """ Load resource at given URL and attempt to parse either XRD or JRD
            based on HTTP response Content-Type header.
        """

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            'User-Agent': 'python-webfinger/%s' % __version__,
            'Accept': WEBFINGER_TYPE,
        }

        params = {'resource': resource}
        if rel:
            params['rel'] = rel

        resp = requests.get(url, params=params, headers=headers, timeout=self.timeout, verify=True)

        logging.debug('fetching JRD from %s' % resp.url)

        content = resp.content
        content_type = resp.headers.get('Content-Type')

        logging.debug('response content type: %s' % content_type)

        # if content_type != WEBFINGER_TYPE:
        #     raise WebFingerException('Invalid response type from server')

        if raw:
            return content

        return resp.json()

    def finger(self, resource, rel=None):
        """ Perform a WebFinger query based on the given subject.
            The `rel` parameter, if specified, will be passed to the provider,
            but be aware that providers are not required to implement the
            rel filter.
        """
        host = resource.split("@")[-1]

        if host in UNOFFICIAL_ENDPOINTS and not self.official:
            unofficial_host = UNOFFICIAL_ENDPOINTS[host]
            logging.debug('host %s is not supported, using unofficial endpoint %s' % (host, unofficial_host))
            host = unofficial_host

        jrd = self.jrd(host, resource, rel)
        return WebFingerResponse(jrd)

def finger(resource, rel=None, timeout=None, official=False):
    """ Shortcut method for invoking WebFingerClient.
    """

    if ":" not in resource:
        raise WebFingerException("scheme is required in subject URI")

    client = WebFingerClient(timeout=timeout, official=official)
    return client.finger(resource, rel=rel)

if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser(description="Simple webfinger client.")
    parser.add_argument("acct", metavar="URI", help="account URI")
    parser.add_argument("-d", "--debug", dest="debug", action="store_true", help="print debug logging output to console")
    parser.add_argument("-r", "--rel", metavar="REL", dest="rel", help="desired relation")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    wf = finger(args.acct, rel=args.rel)

    if args.rel:

        link = wf.find_link(args.rel)

        if link is None:
            print "*** Link not found for rel=%s" % args.rel

        print "%s:\n\t%s" % (link.rel, link.href)

    else:

        print "Activity Streams:  ", wf.activity_streams
        print "Avatar:            ", wf.avatar
        print "HCard:             ", wf.hcard
        print "OpenID:            ", wf.open_id
        print "Open Social:       ", wf.opensocial
        print "Portable Contacts: ", wf.portable_contacts
        print "Profile:           ", wf.profile
        print "WebFist:           ", wf.webfist
        print "XFN:               ", wf.rel("http://gmpg.org/xfn/11")
<|endoftext|>


<commit_before>from unittest import TestCase, main
from os.path import exists, join
from os import remove, rename

from qiita_core.util import qiita_test_checker
from qiita_db.analysis import Analysis
from qiita_db.job import Job
from qiita_db.util import get_db_files_base_dir
from qiita_ware import r_server
from qiita_ware.analysis_pipeline import (
    RunAnalysis, _build_analysis_files, _job_comm_wrapper, _finish_analysis)

# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

@qiita_test_checker()
class TestRun(TestCase):
    def setUp(self):
        self._del_files = []

    def tearDown(self):
        for delfile in self._del_files:
            remove(delfile)

    def test_finish_analysis(self):
        pubsub = r_server.pubsub()
        pubsub.subscribe("demo@microbio.me")
        msgs = []

        _finish_analysis("demo@microbio.me", Analysis(1))
        for msg in pubsub.listen():
            if msg['type'] == 'message':
                msgs.append(msg['data'])
                if "allcomplete" in msg['data']:
                    pubsub.unsubscribe("demo@microbio.me")
                    break
        self.assertEqual(msgs, ['{"msg": "allcomplete", "analysis": 1}'])

    def test_failure_callback(self):
        """Make sure failure at file creation step doesn't hang everything"""
        # rename a needed file for creating the biom table
        base = get_db_files_base_dir()
        rename(join(base, "processed_data",
                    "1_study_1001_closed_reference_otu_table.biom"),
               join(base, "processed_data", "1_study_1001.bak"))

        try:
            app = RunAnalysis()
            app("demo@microbio.me", Analysis(2), [], rarefaction_depth=100)
            # make sure analysis set to error
            analysis = Analysis(2)
            self.assertEqual(analysis.status, 'error')
            for job_id in analysis.jobs:
                self.assertEqual(Job(job_id).status, 'error')
        finally:
            rename(join(base, "processed_data", "1_study_1001.bak"),
                   join(base, "processed_data",
                        "1_study_1001_closed_reference_otu_table.biom"))

    def test_build_files_job_comm_wrapper(self):
        # basic setup needed for test
        job = Job(3)

        # create the files needed for job, testing _build_analysis_files
        analysis = Analysis(2)
        _build_analysis_files(analysis, 100)
        self._del_files.append(join(get_db_files_base_dir(), "analysis",
                                    "2_analysis_mapping.txt"))
        self._del_files.append(join(get_db_files_base_dir(), "analysis",
                                    "2_analysis_18S.biom"))
        self.assertTrue(exists(join(get_db_files_base_dir(), "analysis",
                                    "2_analysis_mapping.txt")))
        self.assertTrue(exists(join(get_db_files_base_dir(), "analysis",
                                    "2_analysis_18S.biom")))
        self.assertEqual([3], analysis.jobs)

        _job_comm_wrapper("demo@microbio.me", 2, job)

        self.assertEqual(job.status, "error")

    def test_redis_comms(self):
        """Make sure redis communication happens"""
        msgs = []
        pubsub = r_server.pubsub()
        pubsub.subscribe("demo@microbio.me")

        app = RunAnalysis()
        app("demo@microbio.me", Analysis(2), [], rarefaction_depth=100)
        for msg in pubsub.listen():
            if msg['type'] == 'message':
                msgs.append(msg['data'])
                if "allcomplete" in msg['data']:
                    pubsub.unsubscribe("demo@microbio.me")
                    break
        self.assertEqual(
            msgs,
            ['{"msg": "Running", "command": "18S: Beta Diversity", '
             '"analysis": 2}',
             '{"msg": "ERROR", "command": "18S: Beta Diversity", '
             '"analysis": 2}',
             '{"msg": "allcomplete", "analysis": 2}'])
        log = self.conn_handler.execute_fetchall(
            "SELECT * from qiita.logging")
        self.assertEqual(1, len(log))
        log = log[0]
        self.assertEqual(1, log[0])
        self.assertEqual(2, log[2])
        self.assertTrue(len(log[3]) > 0)
        self.assertTrue('[{"job": 3, "analysis": 2}]')

    def test_add_jobs_in_construct_job_graphs(self):
        analysis = Analysis(2)
        RunAnalysis()._construct_job_graph(
            "demo@microbio.me", analysis, [('18S', 'Summarize Taxa')],
            comm_opts={'Summarize Taxa': {'opt1': 5}})
        self.assertEqual(analysis.jobs, [3, 4])
        job = Job(4)
        self.assertEqual(job.datatype, '18S')
        self.assertEqual(job.command,
                         ['Summarize Taxa', 'summarize_taxa_through_plots.py'])
        expopts = {
            '--output_dir': join(
                get_db_files_base_dir(), 'job',
                '4_summarize_taxa_through_plots.py_output_dir'),
            'opt1': 5}
        self.assertEqual(job.options, expopts)

if __name__ == "__main__":
    main()
<commit_msg>TST: tests reflect moi<commit_after>from unittest import TestCase, main
from os.path import join
from os import remove, rename

from moi.group import get_id_from_user
from moi import ctx_default

from qiita_core.util import qiita_test_checker
from qiita_db.analysis import Analysis
from qiita_db.job import Job
from qiita_db.util import get_db_files_base_dir
from qiita_ware.analysis_pipeline import RunAnalysis

# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

@qiita_test_checker()
class TestRun(TestCase):
    def setUp(self):
        self._del_files = []

    def tearDown(self):
        for delfile in self._del_files:
            remove(delfile)

    def test_failure_callback(self):
        """Make sure failure at file creation step doesn't hang everything"""
        # rename a needed file for creating the biom table
        base = get_db_files_base_dir()
        rename(join(base, "processed_data",
                    "1_study_1001_closed_reference_otu_table.biom"),
               join(base, "processed_data", "1_study_1001.bak"))
        analysis = Analysis(2)
        group = get_id_from_user("demo@microbio.me")
        try:
            app = RunAnalysis(moi_context=ctx_default,
                              moi_parent_id=group)
            app(analysis, [], rarefaction_depth=100)
            self.assertEqual(analysis.status, 'error')
            for job_id in analysis.jobs:
                self.assertEqual(Job(job_id).status, 'error')
        finally:
            rename(join(base, "processed_data", "1_study_1001.bak"),
                   join(base, "processed_data",
                        "1_study_1001_closed_reference_otu_table.biom"))

    def test_add_jobs_in_construct_job_graphs(self):
        analysis = Analysis(2)
        RunAnalysis()._construct_job_graph(
            analysis, [('18S', 'Summarize Taxa')],
            comm_opts={'Summarize Taxa': {'opt1': 5}})
        self.assertEqual(analysis.jobs, [3, 4])
        job = Job(4)
        self.assertEqual(job.datatype, '18S')
        self.assertEqual(job.command,
                         ['Summarize Taxa', 'summarize_taxa_through_plots.py'])
        expopts = {
            '--output_dir': join(
                get_db_files_base_dir(), 'job',
                '4_summarize_taxa_through_plots.py_output_dir'),
            'opt1': 5}
        self.assertEqual(job.options, expopts)

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os,random,time
from widgets import widgets
import PyRSS2Gen
from datetime import datetime
from linkify import linkify

from xml.sax import saxutils

try:
    XMLGenerator = saxutils.LexicalXMLGenerator
except:
    XMLGenerator = saxutils.XMLGenerator

class BnwRSSFeed(PyRSS2Gen.RSS2):
    rss_attrs = {
        "version":"2.0",
        "xmlns:atom":"http://www.w3.org/2005/Atom",
        "xmlns:slash":"http://purl.org/rss/1.0/modules/slash/",
        "xmlns:rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "xmlns:image":"http://purl.org/rss/1.0/modules/image/",
    }
    def write_xml(self, outfile, encoding = "iso-8859-1"):
        handler = XMLGenerator(outfile, encoding)
        handler.startDocument()
        self.publish(handler)
        handler.endDocument()
    def publish_extensions(self,handler):
        if self.selflink:
            PyRSS2Gen._element(handler,'atom:link',None,
                {'rel':'self',
                 'type':'application/rss+xml',
                 'href':self.selflink,
                })

class BnwDescription(object):
    def __init__(self,text):
        self.text=text or ''
    def publish(self,handler):
        handler.startElement('description',{})
        #handler.startCDATA()
        handler._write('<![CDATA[')
        #handler.characters(self.text)
        handler._write(self.text.replace(']]>',']]>'))
        handler._write(']]>')
        #handler.endCDATA()
        handler.endElement('description')

class AtomSelf(object):
    def __init__(self,link):
        self.link=link
    def publish(self,handler):
        handler.startElement('atom:link',
                {'rel':'self',
                 'type':'application/rss+xml',
                 'href':self.link,
                })
        handler.endElement('atom:link')

def message_feed(messages,link,title,*args,**kwargs):
    rss_items=[PyRSS2Gen.RSSItem(#author=msg['user'],
                        link=widgets.post_url(msg['id']),
                        guid=widgets.post_url(msg['id']),
                        pubDate=datetime.utcfromtimestamp(msg['date']),
                        categories=set(msg['tags'])|set(msg['clubs']),
                        title='@%s: #%s' % (msg['user'],msg['id']),
                        description=BnwDescription(linkify(msg['text'],shorten=False).replace('\n','
'))) for msg in messages]

    rss_feed=BnwRSSFeed(title=title,
                        link=link,
                        description=None,
                        docs=None,
                        items=rss_items,
                        *args,**kwargs)
    rss_feed.selflink=link+'/?format=rss'
    return rss_feed.to_xml(encoding='utf-8')
<commit_msg>Fix linkify execution in rss module<commit_after># -*- coding: utf-8 -*-

import os,random,time
from widgets import widgets
import PyRSS2Gen
from datetime import datetime
from linkify import linkify

from xml.sax import saxutils

try:
    XMLGenerator = saxutils.LexicalXMLGenerator
except:
    XMLGenerator = saxutils.XMLGenerator

class BnwRSSFeed(PyRSS2Gen.RSS2):
    rss_attrs = {
        "version":"2.0",
        "xmlns:atom":"http://www.w3.org/2005/Atom",
        "xmlns:slash":"http://purl.org/rss/1.0/modules/slash/",
        "xmlns:rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "xmlns:image":"http://purl.org/rss/1.0/modules/image/",
    }
    def write_xml(self, outfile, encoding = "iso-8859-1"):
        handler = XMLGenerator(outfile, encoding)
        handler.startDocument()
        self.publish(handler)
        handler.endDocument()
    def publish_extensions(self,handler):
        if self.selflink:
            PyRSS2Gen._element(handler,'atom:link',None,
                {'rel':'self',
                 'type':'application/rss+xml',
                 'href':self.selflink,
                })

class BnwDescription(object):
    def __init__(self,text):
        self.text=text or ''
    def publish(self,handler):
        handler.startElement('description',{})
        #handler.startCDATA()
        handler._write('<![CDATA[')
        #handler.characters(self.text)
        handler._write(self.text.replace(']]>',']]>'))
        handler._write(']]>')
        #handler.endCDATA()
        handler.endElement('description')

class AtomSelf(object):
    def __init__(self,link):
        self.link=link
    def publish(self,handler):
        handler.startElement('atom:link',
                {'rel':'self',
                 'type':'application/rss+xml',
                 'href':self.link,
                })
        handler.endElement('atom:link')

def message_feed(messages,link,title,*args,**kwargs):
    rss_items=[PyRSS2Gen.RSSItem(#author=msg['user'],
                        link=widgets.post_url(msg['id']),
                        guid=widgets.post_url(msg['id']),
                        pubDate=datetime.utcfromtimestamp(msg['date']),
                        categories=set(msg['tags'])|set(msg['clubs']),
                        title='@%s: #%s' % (msg['user'],msg['id']),
                        description=BnwDescription(linkify(msg['text']).replace('\n','
'))) for msg in messages]

    rss_feed=BnwRSSFeed(title=title,
                        link=link,
                        description=None,
                        docs=None,
                        items=rss_items,
                        *args,**kwargs)
    rss_feed.selflink=link+'/?format=rss'
    return rss_feed.to_xml(encoding='utf-8')
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#/usr/bin/python2
'''
By kyubyong park. kbpark.linguist@gmail.com.
https://www.github.com/kyubyong/tacotron
'''

class Hyperparams:
    '''Hyper parameters'''
    # data
    text_file = 'WEB/text.csv'
    sound_fpath = 'WEB'
    max_len = 150 # maximum length of text
    max_duration = 5.0 # maximum duration of a sound file. seconds.

    # signal processing
    sr = 22050 # Sampling rate. Paper => 24000
    n_fft = 2048 # fft points (samples)
    frame_shift = 0.0125 # seconds
    frame_length = 0.05 # seconds
    hop_length = int(sr*frame_shift) # samples  This is dependent on the frame_shift.
    win_length = int(sr*frame_length) # samples This is dependent on the frame_length.
    n_mels = 80 # Number of Mel banks to generate
    power = 1.2 # Exponent for amplifying the predicted magnitude
    n_iter = 30 # Number of inversion iterations

    # model
    embed_size = 256 # alias = E
    encoder_num_banks = 16
    decoder_num_banks = 8
    num_highwaynet_blocks = 4
    r = 5 # Reduction factor. Paper => 2, 3, 5
	norm_type = "bn"  # a normalizer function. value: bn, ln, or ins

    # training scheme
    lr = 0.001 # Paper => Exponential decay
    logdir = "logdir"
    batch_size = 32
    num_epochs = 200 # Paper => 2M global steps!
    loss_type = "l1" # Or you can test "l2"

    # etc
    num_gpus = 1 # If you have multiple gpus, adjust this option, and increase the batch size
                 # and run `train_multiple_gpus.py` instead of `train.py`.
    target_zeros_masking = False # If True, we mask zero padding on the target,
                                 # so exclude them from the loss calculation.
<commit_msg>indent<commit_after># -*- coding: utf-8 -*-
#/usr/bin/python2
'''
By kyubyong park. kbpark.linguist@gmail.com.
https://www.github.com/kyubyong/tacotron
'''

class Hyperparams:
    '''Hyper parameters'''
    # data
    text_file = 'WEB/text.csv'
    sound_fpath = 'WEB'
    max_len = 150 # maximum length of text
    max_duration = 5.0 # maximum duration of a sound file. seconds.

    # signal processing
    sr = 22050 # Sampling rate. Paper => 24000
    n_fft = 2048 # fft points (samples)
    frame_shift = 0.0125 # seconds
    frame_length = 0.05 # seconds
    hop_length = int(sr*frame_shift) # samples  This is dependent on the frame_shift.
    win_length = int(sr*frame_length) # samples This is dependent on the frame_length.
    n_mels = 80 # Number of Mel banks to generate
    power = 1.2 # Exponent for amplifying the predicted magnitude
    n_iter = 30 # Number of inversion iterations

    # model
    embed_size = 256 # alias = E
    encoder_num_banks = 16
    decoder_num_banks = 8
    num_highwaynet_blocks = 4
    r = 5 # Reduction factor. Paper => 2, 3, 5
    norm_type = "bn"  # a normalizer function. value: bn, ln, or ins

    # training scheme
    lr = 0.001 # Paper => Exponential decay
    logdir = "logdir"
    batch_size = 32
    num_epochs = 200 # Paper => 2M global steps!
    loss_type = "l1" # Or you can test "l2"

    # etc
    num_gpus = 1 # If you have multiple gpus, adjust this option, and increase the batch size
                 # and run `train_multiple_gpus.py` instead of `train.py`.
    target_zeros_masking = False # If True, we mask zero padding on the target,
                                 # so exclude them from the loss calculation.
<|endoftext|>


<commit_before>from __future__ import print_function
import logging

__version__ = '1.0'

RELS = {
    'activity_streams': 'http://activitystrea.ms/spec/1.0',
    'avatar': 'http://webfinger.net/rel/avatar',
    'hcard': 'http://microformats.org/profile/hcard',
    'open_id': 'http://specs.openid.net/auth/2.0/provider',
    'opensocial': 'http://ns.opensocial.org/2008/opensocial/activitystreams',
    'portable_contacts': 'http://portablecontacts.net/spec/1.0',
    'profile': 'http://webfinger.net/rel/profile-page',
    'webfist': 'http://webfist.org/spec/rel',
    'xfn': 'http://gmpg.org/xfn/11',
}

WEBFINGER_TYPE = 'application/jrd+json'
LEGACY_WEBFINGER_TYPES = ['application/json']

UNOFFICIAL_ENDPOINTS = {
    'facebook.com': 'facebook-webfinger.appspot.com',
    'twitter.com': 'twitter-webfinger.appspot.com',
}

logger = logging.getLogger("webfinger")

class WebFingerException(Exception):
    pass

class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

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

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get('subject')

    @property
    def aliases(self):
        return self.jrd.get('aliases', [])

    @property
    def properties(self):
        return self.jrd.get('properties', {})

    @property
    def links(self):
        return self.jrd.get('links', [])

    def rel(self, relation, attr='href'):
        for link in self.links:
            if link.get('rel') == relation:
                if attr:
                    return link.get(attr)
                return link

class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def _parse_host(self, resource):

        host = resource.split("@")[-1]

        if host in UNOFFICIAL_ENDPOINTS and not self.official:
            unofficial_host = UNOFFICIAL_ENDPOINTS[host]
            logging.debug('host %s is not supported, using unofficial endpoint %s' % (host, unofficial_host))
            host = unofficial_host

        return host

    def finger(self, resource, host=None, rel=None, raw=False):

        import requests

        if not host:
            host = self._parse_host(resource)

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            'User-Agent': 'python-webfinger/%s' % __version__,
            'Accept': WEBFINGER_TYPE,
        }

        params = {'resource': resource}
        if rel:
            params['rel'] = rel

        resp = requests.get(url, params=params, headers=headers, timeout=self.timeout, verify=True)
        logging.debug('fetching JRD from %s' % resp.url)

        content_type = resp.headers.get('Content-Type', '').split(';', 1)[0].strip()
        logging.debug('response content type: %s' % content_type)

        if content_type != WEBFINGER_TYPE and content_type not in LEGACY_WEBFINGER_TYPES:
            raise WebFingerException('Invalid response type from server')

        if raw:
            return resp.json()

        return WebFingerResponse(resp.json())

def finger(resource, rel=None):
    """ Convenience method for invoking WebFingerClient.
    """
    return WebFingerClient().finger(resource, rel=rel)

if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser(description="Simple webfinger client.")
    parser.add_argument("acct", metavar="URI", help="account URI")
    parser.add_argument("-d", "--debug", dest="debug", action="store_true", help="print debug logging output to console")
    parser.add_argument("-r", "--rel", metavar="REL", dest="rel", help="desired relation")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    wf = finger(args.acct, rel=args.rel)

    print("--- %s ---" % wf.subject)

    if args.rel:

        link = wf.rel(args.rel)

        if link is None:
            print("*** Link not found for rel=%s" % args.rel)

        print("%s:\n\t%s" % (args.rel, link))

    else:

        print("Activity Streams:  ", wf.activity_streams)
        print("Avatar:            ", wf.avatar)
        print("HCard:             ", wf.hcard)
        print("OpenID:            ", wf.open_id)
        print("Open Social:       ", wf.opensocial)
        print("Portable Contacts: ", wf.portable_contacts)
        print("Profile:           ", wf.profile)
        print("WebFist:           ", wf.webfist)
        print("XFN:               ", wf.rel("http://gmpg.org/xfn/11"))
<commit_msg>Use quotes consistently<commit_after>from __future__ import print_function
import logging

__version__ = "1.0"

RELS = {
    "activity_streams": "http://activitystrea.ms/spec/1.0",
    "avatar": "http://webfinger.net/rel/avatar",
    "hcard": "http://microformats.org/profile/hcard",
    "open_id": "http://specs.openid.net/auth/2.0/provider",
    "opensocial": "http://ns.opensocial.org/2008/opensocial/activitystreams",
    "portable_contacts": "http://portablecontacts.net/spec/1.0",
    "profile": "http://webfinger.net/rel/profile-page",
    "webfist": "http://webfist.org/spec/rel",
    "xfn": "http://gmpg.org/xfn/11",
}

WEBFINGER_TYPE = "application/jrd+json"
LEGACY_WEBFINGER_TYPES = ["application/json"]

UNOFFICIAL_ENDPOINTS = {
    "facebook.com": "facebook-webfinger.appspot.com",
    "twitter.com": "twitter-webfinger.appspot.com",
}

logger = logging.getLogger("webfinger")

class WebFingerException(Exception):
    pass

class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

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

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get("subject")

    @property
    def aliases(self):
        return self.jrd.get("aliases", [])

    @property
    def properties(self):
        return self.jrd.get("properties", {})

    @property
    def links(self):
        return self.jrd.get("links", [])

    def rel(self, relation, attr="href"):
        for link in self.links:
            if link.get("rel") == relation:
                if attr:
                    return link.get(attr)
                return link

class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def _parse_host(self, resource):

        host = resource.split("@")[-1]

        if host in UNOFFICIAL_ENDPOINTS and not self.official:
            unofficial_host = UNOFFICIAL_ENDPOINTS[host]
            logging.debug("host %s is not supported, using unofficial endpoint %s" % (host, unofficial_host))
            host = unofficial_host

        return host

    def finger(self, resource, host=None, rel=None, raw=False):

        import requests

        if not host:
            host = self._parse_host(resource)

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            "User-Agent": "python-webfinger/%s" % __version__,
            "Accept": WEBFINGER_TYPE,
        }

        params = {"resource": resource}
        if rel:
            params["rel"] = rel

        resp = requests.get(url, params=params, headers=headers, timeout=self.timeout, verify=True)
        logging.debug("fetching JRD from %s" % resp.url)

        content_type = resp.headers.get("Content-Type", "").split(";", 1)[0].strip()
        logging.debug("response content type: %s" % content_type)

        if content_type != WEBFINGER_TYPE and content_type not in LEGACY_WEBFINGER_TYPES:
            raise WebFingerException("Invalid response type from server")

        if raw:
            return resp.json()

        return WebFingerResponse(resp.json())

def finger(resource, rel=None):
    """ Convenience method for invoking WebFingerClient.
    """
    return WebFingerClient().finger(resource, rel=rel)

if __name__ == "__main__":

    import argparse

    parser = argparse.ArgumentParser(description="Simple webfinger client.")
    parser.add_argument("acct", metavar="URI", help="account URI")
    parser.add_argument("-d", "--debug", dest="debug", action="store_true", help="print debug logging output to console")
    parser.add_argument("-r", "--rel", metavar="REL", dest="rel", help="desired relation")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    wf = finger(args.acct, rel=args.rel)

    print("--- %s ---" % wf.subject)

    if args.rel:

        link = wf.rel(args.rel)

        if link is None:
            print("*** Link not found for rel=%s" % args.rel)

        print("%s:\n\t%s" % (args.rel, link))

    else:

        print("Activity Streams:  ", wf.activity_streams)
        print("Avatar:            ", wf.avatar)
        print("HCard:             ", wf.hcard)
        print("OpenID:            ", wf.open_id)
        print("Open Social:       ", wf.opensocial)
        print("Portable Contacts: ", wf.portable_contacts)
        print("Profile:           ", wf.profile)
        print("WebFist:           ", wf.webfist)
        print("XFN:               ", wf.rel("http://gmpg.org/xfn/11"))
<|endoftext|>


<commit_before>"""
Django settings for stagecraft project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

from common import *

import os

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^10-$qwyu##ivl7f48^mit5e8a-8q#6ceb5i5&zk86)$^(^rmn'

DEBUG = True
TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []  # required if DEBUG is False

# Application definition

INSTALLED_APPS += (
)

MIDDLEWARE_CLASSES = (
)

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
<commit_msg>In development settings, don't nuke middleware<commit_after>"""
Django settings for stagecraft project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

from common import *

import os

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^10-$qwyu##ivl7f48^mit5e8a-8q#6ceb5i5&zk86)$^(^rmn'

DEBUG = True
TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []  # required if DEBUG is False

# Application definition

INSTALLED_APPS += (
)

MIDDLEWARE_CLASSES += (
)

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
<|endoftext|>