<commit_before>import copy
import system
import state

class Mission(object):
    """
    A mission is represented as a sequence of actions that are carried out in
    a given environment and initial state.
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a mission object from a given JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('environment' in jsn)
        assert('initialState' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['actions'], list))

        env = state.Environment.fromJSON(jsn['environment'])
        initialState = state.State.fromJSON(jsn['initialState'])
        actions = [Action.fromJSON(action) for action in jsn['actions']]

        return Mission(env, initialState, actions)

    def __init__(self, environment, initialState, actions):
        """
        Constructs a new Mission description.

        :param  environment:    a description of the environment
        :param  internal:       a description of the initial internal state
        :param  external:       a description of the initial external state
        :param  actions:        a list of actions
        """
        assert(actions != [])
        assert(isinstance(environment, state.Environment) and not environment is None)
        assert(isinstance(initialState, state.State) and not initialState is None)

        self.__environment = environment
        self.__initialState = initialState
        self.__actions = actions

    def getEnvironment(self):
        """
        Returns a description of the (initial) state of the environment
        in which this mission should be executed.
        """
        return self.__environment

    def getInitialState(self):
        return self.__initialState

    def getActions(self):
        return copy.copy(self.__actions)

    def getExpectedMissionDuration(self, system):
        """
        Returns the expected time that the mission is going to take based on each
        action timeout.
        """
        durationTime = 0.0
        schemas = system.getActionSchemas()
        for action in self.__actions:
            durationTime += schemas[action.getSchemaName()].computeTimeout()
        return durationTime

    def toJSON(self):
        """
        Returns a JSON description of this mission.
        """
        return {
            'environment': self.__environment.toJSON(),
            'initialState': self.__initialState.toJSON(),
            'actions': [a.toJSON() for a in self.__actions]
        }

#class MissionContext(object):
#    """
#    Mission contexts are used to describe a context in which a mission should
#    take place. Context is given by the initial state of the environment, and
#    the initial values of the internal and external system variables.
#    """

class MissionOutcome(object):
    """
    Mission outcomes are used to summarise and record the outcome of performing
    a mission.
    """
    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a MissionOutcome from a JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('passed' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['passed'], bool))
        assert(isinstance(jsn['actions'], list))
        actions = [ActionOutcome.fromJSON(a) for a in jsn['actions']]
        return MissionOutcome(jsn['passed'], actions)

    def __init__(self, passed, outcomes):
        """
        Constructs a MissionOutcome object.

        :param  passed      indicates the success (or failure) of the mission.
                            True if successful, False if not.
        :param  outcomes    a list containing the ActionOutcomes for the
                            each of the actions in the mission.
        """
        self.__passed = passed
        self.__outcomes  = outcomes

    def toJSON(self):
        """
        Returns a JSON description of the mission outcome.
        """
        return {
            'passed': self.__passed,
            'actions': [outcome.toJSON() for outcome in  self.__outcomes]
        }

    def __str__(self):
        return str(self.toJSON())

    def __repr__(self):
        return str(self)

class ActionOutcome(object):
    @staticmethod
    def fromJSON(jsn):
        """
        TODO
        """
        assert (isinstance(jsn, dict) and not jsn is None)
        assert ('successful' in jsn)
        assert ('action' in jsn)
        assert ('stateBefore' in jsn)
        assert ('stateAfter' in jsn)
        assert ('timeElapsed' in jsn)
        assert (isinstance(jsn['successful'], bool) and not jsn['successful'] is None)

        return ActionOutcome(Action.fromJSON(jsn['action']),
                             jsn['successful'],
                             state.State.fromJSON(jsn['stateBefore']),
                             state.State.fromJSON(jsn['stateAfter']),
                             jsn['timeElapsed'])

    """
    Used to describe the outcome of an action execution in terms of system state.
    """
    def __init__(self, action, successful, stateBefore, stateAfter, timeElapsed):
        """
        Constructs a ActionOutcome.

        :param  action      the action that was performed
        :param  stateBefore the state of the system immediately prior to execution
        :param  stateAfter  the state of the system immediately after execution
        """
        assert (isinstance(action, Action) and not action is None)
        assert (isinstance(successful, bool) and not successful is None)
        assert (isinstance(stateBefore, state.State) and not stateBefore is None)
        assert (isinstance(stateAfter, state.State) and not stateAfter is None)
        assert (isinstance(timeElapsed, float) and not timeElapsed is None)

        self.__action      = action
        self.__successful  = successful
        self.__stateBefore = stateBefore
        self.__stateAfter  = stateAfter
        self.__timeElapsed = timeElapsed

    def toJSON(self):
        """
        Returns a JSON description of this action outcome.
        """
        return {
            'action':       self.__action.toJSON(),
            'successful':   self.__successful,
            'stateBefore':  self.__stateBefore.toJSON(),
            'stateAfter':   self.__stateAfter.toJSON(),
            'timeElapsed':  self.__timeElapsed
        }

class Action(object):
    """
    Description of the concept of "Actions".
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs an Action object from its JSON description.
        """
        assert (isinstance(jsn, dict) and not jsn is None)
        assert ('kind' in jsn)
        assert ('parameters' in jsn)
        return Action(jsn['kind'], jsn['parameters'])

    def __init__(self, kind, values):
        """
        Constructs an Action description.

        :param  kind    the name of the schema to which the action belongs
        :param  values  a dictionary of parameter values for the action
        """
        assert ((isinstance(kind, str) or (isinstance(kind, unicode))) and not kind is None)
        assert (isinstance(values, dict) and not values is None)
        self.__kind = kind
        self.__values = copy.copy(values)

    def getSchemaName(self):
        """
        Returns the name of the schema to which this action belongs.
        """
        return self.__kind

    def read(self, value):
        """
        Returns the value for a specific parameter in this action.
        """
        return self.getValue(value)

    def getValue(self, value):
        """
        Returns the value for a specific parameter in this action.
        """
        return self.__values[value]

    def getValues(self):
        """
        Returns a copy of the parameters for this action.
        """
        return copy.copy(self.__values)

    def toJSON(self):
        """
        Returns a JSON description of this action.
        """
        return {
            'kind': self.__kind,
            'parameters': self.getValues()
        }

class Parameter(object):
    """
    Docstring.
    """

    def __init__(self, name, valueRange, description):
        """
        Constructs a Parameter object.

        :param  name:           the name of this parameter
        :param  valueRange:     the range of possible values for this parameter,\
                                given as a ValueRange object.
        :param  description:    a short description of the parameter
        """
        self.__name = name
        self.__valueRange = valueRange
        self.__description = description

    def getValueRange(self):
        """
        Returns the range of possible values for this parameter.
        """
        return self.__valueRange

    def generate(self):
        """
        Returns a sample (random)
        """
        return self.__valueRange.sample()

    def getType(self):
        """
        Returns the type of this parameter
        """
        return self.__valueRange.getType()

    def getDescription(self):
        """
        Returns a description of this parameter
        """
        return self.__description

    def getName(self):
        """
        Returns the name of this parameter.
        """
        return self.__name
<commit_msg>Added missing parameters in computeTimeout<commit_after>import copy
import system
import state

class Mission(object):
    """
    A mission is represented as a sequence of actions that are carried out in
    a given environment and initial state.
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a mission object from a given JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('environment' in jsn)
        assert('initialState' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['actions'], list))

        env = state.Environment.fromJSON(jsn['environment'])
        initialState = state.State.fromJSON(jsn['initialState'])
        actions = [Action.fromJSON(action) for action in jsn['actions']]

        return Mission(env, initialState, actions)

    def __init__(self, environment, initialState, actions):
        """
        Constructs a new Mission description.

        :param  environment:    a description of the environment
        :param  internal:       a description of the initial internal state
        :param  external:       a description of the initial external state
        :param  actions:        a list of actions
        """
        assert(actions != [])
        assert(isinstance(environment, state.Environment) and not environment is None)
        assert(isinstance(initialState, state.State) and not initialState is None)

        self.__environment = environment
        self.__initialState = initialState
        self.__actions = actions

    def getEnvironment(self):
        """
        Returns a description of the (initial) state of the environment
        in which this mission should be executed.
        """
        return self.__environment

    def getInitialState(self):
        return self.__initialState

    def getActions(self):
        return copy.copy(self.__actions)

    def getExpectedMissionDuration(self, system):
        """
        Returns the expected time that the mission is going to take based on each
        action timeout.
        """
        durationTime = 0.0
        schemas = system.getActionSchemas()
        for action in self.__actions:
            durationTime += schemas[action.getSchemaName()].computeTimeout(action, self.__initialState, self.__environment)
        return durationTime

    def toJSON(self):
        """
        Returns a JSON description of this mission.
        """
        return {
            'environment': self.__environment.toJSON(),
            'initialState': self.__initialState.toJSON(),
            'actions': [a.toJSON() for a in self.__actions]
        }

#class MissionContext(object):
#    """
#    Mission contexts are used to describe a context in which a mission should
#    take place. Context is given by the initial state of the environment, and
#    the initial values of the internal and external system variables.
#    """

class MissionOutcome(object):
    """
    Mission outcomes are used to summarise and record the outcome of performing
    a mission.
    """
    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a MissionOutcome from a JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('passed' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['passed'], bool))
        assert(isinstance(jsn['actions'], list))
        actions = [ActionOutcome.fromJSON(a) for a in jsn['actions']]
        return MissionOutcome(jsn['passed'], actions)

    def __init__(self, passed, outcomes):
        """
        Constructs a MissionOutcome object.

        :param  passed      indicates the success (or failure) of the mission.
                            True if successful, False if not.
        :param  outcomes    a list containing the ActionOutcomes for the
                            each of the actions in the mission.
        """
        self.__passed = passed
        self.__outcomes  = outcomes

    def toJSON(self):
        """
        Returns a JSON description of the mission outcome.
        """
        return {
            'passed': self.__passed,
            'actions': [outcome.toJSON() for outcome in  self.__outcomes]
        }

    def __str__(self):
        return str(self.toJSON())

    def __repr__(self):
        return str(self)

class ActionOutcome(object):
    @staticmethod
    def fromJSON(jsn):
        """
        TODO
        """
        assert (isinstance(jsn, dict) and not jsn is None)
        assert ('successful' in jsn)
        assert ('action' in jsn)
        assert ('stateBefore' in jsn)
        assert ('stateAfter' in jsn)
        assert ('timeElapsed' in jsn)
        assert (isinstance(jsn['successful'], bool) and not jsn['successful'] is None)

        return ActionOutcome(Action.fromJSON(jsn['action']),
                             jsn['successful'],
                             state.State.fromJSON(jsn['stateBefore']),
                             state.State.fromJSON(jsn['stateAfter']),
                             jsn['timeElapsed'])

    """
    Used to describe the outcome of an action execution in terms of system state.
    """
    def __init__(self, action, successful, stateBefore, stateAfter, timeElapsed):
        """
        Constructs a ActionOutcome.

        :param  action      the action that was performed
        :param  stateBefore the state of the system immediately prior to execution
        :param  stateAfter  the state of the system immediately after execution
        """
        assert (isinstance(action, Action) and not action is None)
        assert (isinstance(successful, bool) and not successful is None)
        assert (isinstance(stateBefore, state.State) and not stateBefore is None)
        assert (isinstance(stateAfter, state.State) and not stateAfter is None)
        assert (isinstance(timeElapsed, float) and not timeElapsed is None)

        self.__action      = action
        self.__successful  = successful
        self.__stateBefore = stateBefore
        self.__stateAfter  = stateAfter
        self.__timeElapsed = timeElapsed

    def toJSON(self):
        """
        Returns a JSON description of this action outcome.
        """
        return {
            'action':       self.__action.toJSON(),
            'successful':   self.__successful,
            'stateBefore':  self.__stateBefore.toJSON(),
            'stateAfter':   self.__stateAfter.toJSON(),
            'timeElapsed':  self.__timeElapsed
        }

class Action(object):
    """
    Description of the concept of "Actions".
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs an Action object from its JSON description.
        """
        assert (isinstance(jsn, dict) and not jsn is None)
        assert ('kind' in jsn)
        assert ('parameters' in jsn)
        return Action(jsn['kind'], jsn['parameters'])

    def __init__(self, kind, values):
        """
        Constructs an Action description.

        :param  kind    the name of the schema to which the action belongs
        :param  values  a dictionary of parameter values for the action
        """
        assert ((isinstance(kind, str) or (isinstance(kind, unicode))) and not kind is None)
        assert (isinstance(values, dict) and not values is None)
        self.__kind = kind
        self.__values = copy.copy(values)

    def getSchemaName(self):
        """
        Returns the name of the schema to which this action belongs.
        """
        return self.__kind

    def read(self, value):
        """
        Returns the value for a specific parameter in this action.
        """
        return self.getValue(value)

    def getValue(self, value):
        """
        Returns the value for a specific parameter in this action.
        """
        return self.__values[value]

    def getValues(self):
        """
        Returns a copy of the parameters for this action.
        """
        return copy.copy(self.__values)

    def toJSON(self):
        """
        Returns a JSON description of this action.
        """
        return {
            'kind': self.__kind,
            'parameters': self.getValues()
        }

class Parameter(object):
    """
    Docstring.
    """

    def __init__(self, name, valueRange, description):
        """
        Constructs a Parameter object.

        :param  name:           the name of this parameter
        :param  valueRange:     the range of possible values for this parameter,\
                                given as a ValueRange object.
        :param  description:    a short description of the parameter
        """
        self.__name = name
        self.__valueRange = valueRange
        self.__description = description

    def getValueRange(self):
        """
        Returns the range of possible values for this parameter.
        """
        return self.__valueRange

    def generate(self):
        """
        Returns a sample (random)
        """
        return self.__valueRange.sample()

    def getType(self):
        """
        Returns the type of this parameter
        """
        return self.__valueRange.getType()

    def getDescription(self):
        """
        Returns a description of this parameter
        """
        return self.__description

    def getName(self):
        """
        Returns the name of this parameter.
        """
        return self.__name
<|endoftext|>


<commit_before>__all__ = ['logspace', 'linspace']

import numeric as _nx
from numeric import array

def linspace(start, stop, num=50, endpoint=True, retstep=False, maskna=False):
    """
    Return evenly spaced numbers over a specified interval.

    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop` ].

    The endpoint of the interval can optionally be excluded.

    Parameters
    ----------
    start : scalar
        The starting value of the sequence.
    stop : scalar
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
    num : int, optional
        Number of samples to generate. Default is 50.
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
    maskna : boolean
        If this is true, the returned array will have an NA mask.

    Returns
    -------
    samples : ndarray
        There are `num` equally spaced samples in the closed interval
        ``[start, stop]`` or the half-open interval ``[start, stop)``
        (depending on whether `endpoint` is True or False).
    step : float (only if `retstep` is True)
        Size of spacing between samples.

    See Also
    --------
    arange : Similiar to `linspace`, but uses a step size (instead of the
             number of samples).
    logspace : Samples uniformly distributed in log space.

    Examples
    --------
    >>> np.linspace(2.0, 3.0, num=5)
        array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
    >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
        array([ 2. ,  2.2,  2.4,  2.6,  2.8])
    >>> np.linspace(2.0, 3.0, num=5, retstep=True)
        (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

    Graphical illustration:

    >>> import matplotlib.pyplot as plt
    >>> N = 8
    >>> y = np.zeros(N)
    >>> x1 = np.linspace(0, 10, N, endpoint=True)
    >>> x2 = np.linspace(0, 10, N, endpoint=False)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

    """
    num = int(num)
    if num <= 0:
        return array([], float, maskna=maskna)
    if endpoint:
        if num == 1:
            return array([float(start)], maskna=maskna)
        step = (stop-start)/float((num-1))
        y = _nx.arange(0, num, maskna=maskna) * step + start
        y[-1] = stop
    else:
        step = (stop-start)/float(num)
        y = _nx.arange(0, num, maskna=maskna) * step + start
    if retstep:
        return y, step
    else:
        return y

def logspace(start,stop,num=50,endpoint=True,base=10.0, maskna=False):
    """
    Return numbers spaced evenly on a log scale.

    In linear space, the sequence starts at ``base ** start``
    (`base` to the power of `start`) and ends with ``base ** stop``
    (see `endpoint` below).

    Parameters
    ----------
    start : float
        ``base ** start`` is the starting value of the sequence.
    stop : float
        ``base ** stop`` is the final value of the sequence, unless `endpoint`
        is False.  In that case, ``num + 1`` values are spaced over the
        interval in log-space, of which all but the last (a sequence of
        length ``num``) are returned.
    num : integer, optional
        Number of samples to generate.  Default is 50.
    endpoint : boolean, optional
        If true, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    base : float, optional
        The base of the log space. The step size between the elements in
        ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
        Default is 10.0.
    maskna : boolean
        If this is true, the returned array will have an NA mask.

    Returns
    -------
    samples : ndarray
        `num` samples, equally spaced on a log scale.

    See Also
    --------
    arange : Similiar to linspace, with the step size specified instead of the
             number of samples. Note that, when used with a float endpoint, the
             endpoint may or may not be included.
    linspace : Similar to logspace, but with the samples uniformly distributed
               in linear space, instead of log space.

    Notes
    -----
    Logspace is equivalent to the code

    >>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
    ... # doctest: +SKIP
    >>> power(base, y)
    ... # doctest: +SKIP

    Examples
    --------
    >>> np.logspace(2.0, 3.0, num=4)
        array([  100.        ,   215.443469  ,   464.15888336,  1000.        ])
    >>> np.logspace(2.0, 3.0, num=4, endpoint=False)
        array([ 100.        ,  177.827941  ,  316.22776602,  562.34132519])
    >>> np.logspace(2.0, 3.0, num=4, base=2.0)
        array([ 4.        ,  5.0396842 ,  6.34960421,  8.        ])

    Graphical illustration:

    >>> import matplotlib.pyplot as plt
    >>> N = 10
    >>> x1 = np.logspace(0.1, 1, N, endpoint=True)
    >>> x2 = np.logspace(0.1, 1, N, endpoint=False)
    >>> y = np.zeros(N)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

    """
    y = linspace(start,stop,num=num,endpoint=endpoint,maskna=maskna)
    return _nx.power(base,y)

<commit_msg>fix typos<commit_after>__all__ = ['logspace', 'linspace']

import numeric as _nx
from numeric import array

def linspace(start, stop, num=50, endpoint=True, retstep=False, maskna=False):
    """
    Return evenly spaced numbers over a specified interval.

    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop` ].

    The endpoint of the interval can optionally be excluded.

    Parameters
    ----------
    start : scalar
        The starting value of the sequence.
    stop : scalar
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
    num : int, optional
        Number of samples to generate. Default is 50.
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
    maskna : boolean
        If this is true, the returned array will have an NA mask.

    Returns
    -------
    samples : ndarray
        There are `num` equally spaced samples in the closed interval
        ``[start, stop]`` or the half-open interval ``[start, stop)``
        (depending on whether `endpoint` is True or False).
    step : float (only if `retstep` is True)
        Size of spacing between samples.

    See Also
    --------
    arange : Similar to `linspace`, but uses a step size (instead of the
             number of samples).
    logspace : Samples uniformly distributed in log space.

    Examples
    --------
    >>> np.linspace(2.0, 3.0, num=5)
        array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
    >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
        array([ 2. ,  2.2,  2.4,  2.6,  2.8])
    >>> np.linspace(2.0, 3.0, num=5, retstep=True)
        (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

    Graphical illustration:

    >>> import matplotlib.pyplot as plt
    >>> N = 8
    >>> y = np.zeros(N)
    >>> x1 = np.linspace(0, 10, N, endpoint=True)
    >>> x2 = np.linspace(0, 10, N, endpoint=False)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

    """
    num = int(num)
    if num <= 0:
        return array([], float, maskna=maskna)
    if endpoint:
        if num == 1:
            return array([float(start)], maskna=maskna)
        step = (stop-start)/float((num-1))
        y = _nx.arange(0, num, maskna=maskna) * step + start
        y[-1] = stop
    else:
        step = (stop-start)/float(num)
        y = _nx.arange(0, num, maskna=maskna) * step + start
    if retstep:
        return y, step
    else:
        return y

def logspace(start,stop,num=50,endpoint=True,base=10.0, maskna=False):
    """
    Return numbers spaced evenly on a log scale.

    In linear space, the sequence starts at ``base ** start``
    (`base` to the power of `start`) and ends with ``base ** stop``
    (see `endpoint` below).

    Parameters
    ----------
    start : float
        ``base ** start`` is the starting value of the sequence.
    stop : float
        ``base ** stop`` is the final value of the sequence, unless `endpoint`
        is False.  In that case, ``num + 1`` values are spaced over the
        interval in log-space, of which all but the last (a sequence of
        length ``num``) are returned.
    num : integer, optional
        Number of samples to generate.  Default is 50.
    endpoint : boolean, optional
        If true, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    base : float, optional
        The base of the log space. The step size between the elements in
        ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
        Default is 10.0.
    maskna : boolean
        If this is true, the returned array will have an NA mask.

    Returns
    -------
    samples : ndarray
        `num` samples, equally spaced on a log scale.

    See Also
    --------
    arange : Similar to linspace, with the step size specified instead of the
             number of samples. Note that, when used with a float endpoint, the
             endpoint may or may not be included.
    linspace : Similar to logspace, but with the samples uniformly distributed
               in linear space, instead of log space.

    Notes
    -----
    Logspace is equivalent to the code

    >>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
    ... # doctest: +SKIP
    >>> power(base, y)
    ... # doctest: +SKIP

    Examples
    --------
    >>> np.logspace(2.0, 3.0, num=4)
        array([  100.        ,   215.443469  ,   464.15888336,  1000.        ])
    >>> np.logspace(2.0, 3.0, num=4, endpoint=False)
        array([ 100.        ,  177.827941  ,  316.22776602,  562.34132519])
    >>> np.logspace(2.0, 3.0, num=4, base=2.0)
        array([ 4.        ,  5.0396842 ,  6.34960421,  8.        ])

    Graphical illustration:

    >>> import matplotlib.pyplot as plt
    >>> N = 10
    >>> x1 = np.logspace(0.1, 1, N, endpoint=True)
    >>> x2 = np.logspace(0.1, 1, N, endpoint=False)
    >>> y = np.zeros(N)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

    """
    y = linspace(start,stop,num=num,endpoint=endpoint,maskna=maskna)
    return _nx.power(base,y)

<|endoftext|>


<commit_before>import state
import branch
import action

class Mission(object):
    """
    A mission is represented as a sequence of actions that are carried out in
    a given environment and initial state.
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a mission object from a given JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('environment' in jsn)
        assert('initialState' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['actions'], list))

        env = state.Environment.fromJSON(jsn['environment'])
        initialState = state.State.fromJSON(jsn['initialState'])
        actions = [action.Action.fromJSON(a) for a in jsn['actions']]

        return Mission(env, initialState, actions)

    def __init__(self, environment, initialState, actions):
        """
        Constructs a new Mission description.

        :param  environment:    a description of the environment
        :param  internal:       a description of the initial internal state
        :param  external:       a description of the initial external state
        :param  actions:        a list of actions
        """
        assert(isinstance(environment, state.Environment) and not environment is None)
        assert(isinstance(initialState, state.State) and not initialState is None)

        self.__environment = environment
        self.__initialState = initialState
        self.__actions = actions

    def getEnvironment(self):
        """
        Returns a description of the (initial) state of the environment
        in which this mission should be executed.
        """
        return self.__environment

    def getInitialState(self):
        return self.__initialState

    def getActions(self):
        return self.__actions[:]

    def size(self):
        """
        Returns the number of actions in this mission.
        """
        return len(self.__actions)

    def getExpectedMissionDuration(self, system):
        """
        Returns the expected time that the mission is going to take based on each
        action timeout.
        """
        durationTime = 0.0
        schemas = system.getActionSchemas()
        for a in self.__actions:
            durationTime += schemas[a.getSchemaName()].computeTimeout(a, self.__initialState, self.__environment)
        return durationTime

    def toJSON(self):
        """
        Returns a JSON description of this mission.
        """
        return {
            'environment': self.__environment.toJSON(),
            'initialState': self.__initialState.toJSON(),
            'actions': [a.toJSON() for a in self.__actions]
        }

class MissionOutcome(object):
    """
    Mission outcomes are used to summarise and record the outcome of performing
    a mission.
    """
    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a MissionOutcome from a JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('passed' in jsn)
        assert('actions' in jsn)
        assert('setupTime' in jsn)
        assert('totalTime' in jsn)
        assert(isinstance(jsn['passed'], bool))
        assert(isinstance(jsn['actions'], list))
        assert(isinstance(jsn['setupTime'], float))
        assert(isinstance(jsn['totalTime'], float))
        actions = [action.ActionOutcome.fromJSON(a) for a in jsn['actions']]
        return MissionOutcome(jsn['passed'], actions, jsn['setupTime'], jsn['totalTime'])

    def __init__(self, passed, outcomes, setupTime, totalTime):
        """
        Constructs a MissionOutcome object.

        :param  passed      indicates the success (or failure) of the mission.
                            True if successful, False if not.
        :param  outcomes    a list containing the ActionOutcomes for the
                            each of the actions in the mission.
        """
        self.__passed = passed
        self.__outcomes  = outcomes
        self.__setupTime = setupTime
        self.__totalTime = totalTime

    def toJSON(self):
        """
        Returns a JSON description of the mission outcome.
        """
        return {
            'passed': self.__passed,
            'actions': [outcome.toJSON() for outcome in  self.__outcomes],
            'setupTime': self.__setupTime,
            'totalTime': self.__totalTime
        }

    def __str__(self):
        return str(self.toJSON())

    def __repr__(self):
        return str(self)

    def getExecutedPath(self):
        """
        Returns the branch path that was taken by this mission execution.
        """
        return branch.BranchPath([a.getBranchID() for a in self.__outcomes])

    def getEndState(self):
        """
        Returns a description of the state of the system immediately after the
        execution of this mission.
        """
        return self.__outcomes[-1].getEndState()

    def getStartState(self):
        """
        Returns a description of the state of the system immediately before the
        execution of this mission.
        """
        return self.__outcomes[0].getStartState()

    def passed(self):
        """
        :see `successful`
        """
        return self.__passed

    def failed(self):
        """
        Returns true if this mission was unsuccessful.
        """
        return not self.passed()
<commit_msg>added Mission::isEmpty<commit_after>import state
import branch
import action

class Mission(object):
    """
    A mission is represented as a sequence of actions that are carried out in
    a given environment and initial state.
    """

    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a mission object from a given JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('environment' in jsn)
        assert('initialState' in jsn)
        assert('actions' in jsn)
        assert(isinstance(jsn['actions'], list))

        env = state.Environment.fromJSON(jsn['environment'])
        initialState = state.State.fromJSON(jsn['initialState'])
        actions = [action.Action.fromJSON(a) for a in jsn['actions']]

        return Mission(env, initialState, actions)

    def __init__(self, environment, initialState, actions):
        """
        Constructs a new Mission description.

        :param  environment:    a description of the environment
        :param  internal:       a description of the initial internal state
        :param  external:       a description of the initial external state
        :param  actions:        a list of actions
        """
        assert(isinstance(environment, state.Environment) and not environment is None)
        assert(isinstance(initialState, state.State) and not initialState is None)

        self.__environment = environment
        self.__initialState = initialState
        self.__actions = actions

    def getEnvironment(self):
        """
        Returns a description of the (initial) state of the environment
        in which this mission should be executed.
        """
        return self.__environment

    def isEmpty(self):
        """
        Returns True if this mission contains no actions.
        """
        return self.__actions == []

    def getInitialState(self):
        return self.__initialState

    def getActions(self):
        return self.__actions[:]

    def size(self):
        """
        Returns the number of actions in this mission.
        """
        return len(self.__actions)

    def getExpectedMissionDuration(self, system):
        """
        Returns the expected time that the mission is going to take based on each
        action timeout.
        """
        durationTime = 0.0
        schemas = system.getActionSchemas()
        for a in self.__actions:
            durationTime += schemas[a.getSchemaName()].computeTimeout(a, self.__initialState, self.__environment)
        return durationTime

    def toJSON(self):
        """
        Returns a JSON description of this mission.
        """
        return {
            'environment': self.__environment.toJSON(),
            'initialState': self.__initialState.toJSON(),
            'actions': [a.toJSON() for a in self.__actions]
        }

class MissionOutcome(object):
    """
    Mission outcomes are used to summarise and record the outcome of performing
    a mission.
    """
    @staticmethod
    def fromJSON(jsn):
        """
        Constructs a MissionOutcome from a JSON description.
        """
        assert(isinstance(jsn, dict) and not jsn is None)
        assert('passed' in jsn)
        assert('actions' in jsn)
        assert('setupTime' in jsn)
        assert('totalTime' in jsn)
        assert(isinstance(jsn['passed'], bool))
        assert(isinstance(jsn['actions'], list))
        assert(isinstance(jsn['setupTime'], float))
        assert(isinstance(jsn['totalTime'], float))
        actions = [action.ActionOutcome.fromJSON(a) for a in jsn['actions']]
        return MissionOutcome(jsn['passed'], actions, jsn['setupTime'], jsn['totalTime'])

    def __init__(self, passed, outcomes, setupTime, totalTime):
        """
        Constructs a MissionOutcome object.

        :param  passed      indicates the success (or failure) of the mission.
                            True if successful, False if not.
        :param  outcomes    a list containing the ActionOutcomes for the
                            each of the actions in the mission.
        """
        self.__passed = passed
        self.__outcomes  = outcomes
        self.__setupTime = setupTime
        self.__totalTime = totalTime

    def toJSON(self):
        """
        Returns a JSON description of the mission outcome.
        """
        return {
            'passed': self.__passed,
            'actions': [outcome.toJSON() for outcome in  self.__outcomes],
            'setupTime': self.__setupTime,
            'totalTime': self.__totalTime
        }

    def __str__(self):
        return str(self.toJSON())

    def __repr__(self):
        return str(self)

    def getExecutedPath(self):
        """
        Returns the branch path that was taken by this mission execution.
        """
        return branch.BranchPath([a.getBranchID() for a in self.__outcomes])

    def getEndState(self):
        """
        Returns a description of the state of the system immediately after the
        execution of this mission.
        """
        return self.__outcomes[-1].getEndState()

    def getStartState(self):
        """
        Returns a description of the state of the system immediately before the
        execution of this mission.
        """
        return self.__outcomes[0].getStartState()

    def passed(self):
        """
        :see `successful`
        """
        return self.__passed

    def failed(self):
        """
        Returns true if this mission was unsuccessful.
        """
        return not self.passed()
<|endoftext|>


<commit_before>import glob
import os
import sys
import logging
import hashlib

logger = logging.getLogger(__name__)

import yaml

from nymms import exceptions
from nymms.utils import logutil

class EmptyConfig(exceptions.NymmsException):
    def __init__(self, filename):
        self.filename = filename

    def __str__(self):
        return "Config file %s resulted in an empty config." % (self.filename)

def open_config_file(config_file):
    """ Opens a config file, logging common IOError exceptions.
    """
    try:
        return open(config_file)
    except IOError, e:
        # This should only happen with the top level config file, since
        # we use glob.glob on includes
        if e.errno == 2:
            logger.error("Could not find file '%s'." % (
                config_file))
        elif e.errno == 13:
            logger.error("Invalid permissions to open '%s'." % (config_file))
        raise

def load_config(config_file):
    stack = []
    root = os.path.split(os.path.abspath(os.path.expanduser(config_file)))[0]
    def recursive_preprocess(filename):
        filename = os.path.expanduser(filename)
        stack.append(os.path.abspath(filename))
        c = []

        with open_config_file(filename) as fd:
            for lineno, line in enumerate(fd):
                line = line.rstrip()
                if line.startswith('!include '):
                    path = line.split(None, 1)[1]
                    # if the include doesn't have a fully qualified path then
                    # assume the relative path is based off the directory of
                    # the initial config file
                    print path
                    print root
                    if not path.startswith('/'):
                        path = os.path.join(root, path)
                    print path
                    files = glob.glob(path)
                    print files
                    if not files:
                        logger.warning("Include statement '%s' at %s:%d did "
                                "not match any files.  Skipping." % (line,
                                        filename, lineno))
                        continue
                    for f in files:
                        f = os.path.abspath(f)
                        if f in stack:
                            logger.warning("Already parsed %s, skipping "
                                    "(%s:%d) to avoid infinite loop." % (f,
                                            filename, lineno))
                            continue
                        if os.path.isfile(f):
                            logger.debug("Parsing include (%s:%d): %s" % (
                                    filename, lineno, f))
                            c.extend(recursive_preprocess(f))
                        else:
                            logger.warning("%s is not a regular file, "
                                    "skipping (%s:%d)." % (f, filename,
                                            lineno))
                    continue
                c.append(line)
        return c
    logger.debug("Loading config file: %s" % (config_file))
    config = recursive_preprocess(config_file)
    if not config:
        raise EmptyConfig(config_file)
    config = os.linesep.join(config)
    version = hashlib.sha512(config).hexdigest()
    return (version, yaml.safe_load(config))
<commit_msg>Remove debug print statements<commit_after>import glob
import os
import sys
import logging
import hashlib

logger = logging.getLogger(__name__)

import yaml

from nymms import exceptions
from nymms.utils import logutil

class EmptyConfig(exceptions.NymmsException):
    def __init__(self, filename):
        self.filename = filename

    def __str__(self):
        return "Config file %s resulted in an empty config." % (self.filename)

def open_config_file(config_file):
    """ Opens a config file, logging common IOError exceptions.
    """
    try:
        return open(config_file)
    except IOError, e:
        # This should only happen with the top level config file, since
        # we use glob.glob on includes
        if e.errno == 2:
            logger.error("Could not find file '%s'." % (
                config_file))
        elif e.errno == 13:
            logger.error("Invalid permissions to open '%s'." % (config_file))
        raise

def load_config(config_file):
    stack = []
    root = os.path.split(os.path.abspath(os.path.expanduser(config_file)))[0]
    def recursive_preprocess(filename):
        filename = os.path.expanduser(filename)
        stack.append(os.path.abspath(filename))
        c = []

        with open_config_file(filename) as fd:
            for lineno, line in enumerate(fd):
                line = line.rstrip()
                if line.startswith('!include '):
                    path = line.split(None, 1)[1]
                    # if the include doesn't have a fully qualified path then
                    # assume the relative path is based off the directory of
                    # the initial config file
                    if not path.startswith('/'):
                        path = os.path.join(root, path)
                    files = glob.glob(path)
                    if not files:
                        logger.warning("Include statement '%s' at %s:%d did "
                                "not match any files.  Skipping." % (line,
                                        filename, lineno))
                        continue
                    for f in files:
                        f = os.path.abspath(f)
                        if f in stack:
                            logger.warning("Already parsed %s, skipping "
                                    "(%s:%d) to avoid infinite loop." % (f,
                                            filename, lineno))
                            continue
                        if os.path.isfile(f):
                            logger.debug("Parsing include (%s:%d): %s" % (
                                    filename, lineno, f))
                            c.extend(recursive_preprocess(f))
                        else:
                            logger.warning("%s is not a regular file, "
                                    "skipping (%s:%d)." % (f, filename,
                                            lineno))
                    continue
                c.append(line)
        return c
    logger.debug("Loading config file: %s" % (config_file))
    config = recursive_preprocess(config_file)
    if not config:
        raise EmptyConfig(config_file)
    config = os.linesep.join(config)
    version = hashlib.sha512(config).hexdigest()
    return (version, yaml.safe_load(config))
<|endoftext|>


<commit_before>from models import *
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from cStringIO import StringIO
from PIL import Image as PILImage
import urllib

def fixAllImagePaths():
    for image in Image.objects.all():
        if image.path[-1] != '/':
            image.path += '/'
            image.save()

#pass request.scheme, request.get_host() from view in same directory as images
def updateAllImageSizes(scheme, host):
    url_check = URLValidator()
    url = str(scheme + '://' + host)
    for image in Image.objects.all():
        try:
            url_check(image.path)
            im_url = image.path + image.name
        except ValidationError, e:
            im_url = url + image.path + '/' + image.name
        image.width, image.height = PILImage.open(StringIO(urllib.urlopen(im_url).read())).size
        image.save()
<commit_msg>Added file<commit_after>from models import *
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from cStringIO import StringIO
from PIL import Image as PILImage
import urllib

def fixAllImagePaths():
    for image in Image.objects.all():
        if image.path[-1] != '/':
            image.path += '/'
            image.save()

#pass request.scheme, request.get_host() from view in same directory as images
def updateAllImageSizes(scheme, host):
    url_check = URLValidator()
    url = str(scheme + '://' + host)
    for image in Image.objects.all():
        try:
            url_check(image.path)
            im_url = image.path + image.name
        except ValidationError, e:
            im_url = url + image.path + '/' + image.name
        image.width, image.height = PILImage.open(StringIO(urllib.urlopen(im_url).read())).size
        image.save()
<|endoftext|>


<commit_before>"""
Custom Authenticator to use OpenShift OAuth with JupyterHub.

Derived from the GitHub OAuth authenticator.
"""

import json
import os

from tornado.auth import OAuth2Mixin
from tornado import gen, web

from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from jupyterhub.auth import LocalAuthenticator

from .oauth2 import OAuthLoginHandler, OAuthenticator

OPENSHIFT_URL = os.environ.get('OPENSHIFT_URL') or 'https://localhost:8443'

class OpenShiftMixin(OAuth2Mixin):
    _OAUTH_AUTHORIZE_URL = "%s/oauth/authorize" % OPENSHIFT_URL
    _OAUTH_ACCESS_TOKEN_URL = "%s/oauth/token" % OPENSHIFT_URL

class OpenShiftLoginHandler(OAuthLoginHandler, OpenShiftMixin):
    # This allows `Service Accounts as OAuth Clients` scenario
    # https://docs.openshift.org/latest/architecture/additional_concepts/authentication.html#service-accounts-as-oauth-clients
    @property
    def scope(self):
        return self.authenticator.scope

class OpenShiftOAuthenticator(OAuthenticator):

    login_service = "OpenShift"

    login_handler = OpenShiftLoginHandler

    scope = ['user:info']

    users_rest_api_path = '/apis/user.openshift.io/v1/users/~'

    @gen.coroutine
    def authenticate(self, handler, data=None):
        code = handler.get_argument("code")
        # TODO: Configure the curl_httpclient for tornado
        http_client = AsyncHTTPClient()

        # Exchange the OAuth code for a OpenShift Access Token
        #
        # See: https://docs.openshift.org/latest/architecture/additional_concepts/authentication.html#api-authentication

        params = dict(
            client_id=self.client_id,
            client_secret=self.client_secret,
            grant_type="authorization_code",
            code=code
        )

        url = url_concat("%s/oauth/token" % OPENSHIFT_URL, params)

        req = HTTPRequest(url,
                          method="POST",
                          validate_cert=False,
                          headers={"Accept": "application/json"},
                          body='' # Body is required for a POST...
                          )

        resp = yield http_client.fetch(req)

        resp_json = json.loads(resp.body.decode('utf8', 'replace'))

        access_token = resp_json['access_token']

        # Determine who the logged in user is
        headers={"Accept": "application/json",
                 "User-Agent": "JupyterHub",
                 "Authorization": "Bearer {}".format(access_token)
        }

        req = HTTPRequest("%s%s" % (OPENSHIFT_URL, self.users_rest_api_path),
                          method="GET",
                          validate_cert=False,
                          headers=headers)

        resp = yield http_client.fetch(req)
        resp_json = json.loads(resp.body.decode('utf8', 'replace'))

        return {
            'name': resp_json['metadata']['name'],
            'auth_state': {
                'access_token': access_token,
                'openshift_user': resp_json,
            }
        }

class LocalOpenShiftOAuthenticator(LocalAuthenticator, OpenShiftOAuthenticator):

    """A version that mixes in local system user creation"""
    pass
<commit_msg>Separate environment variables for Kubernetes server and OAuth server.<commit_after>"""
Custom Authenticator to use OpenShift OAuth with JupyterHub.

Derived from the GitHub OAuth authenticator.
"""

import json
import os

from tornado.auth import OAuth2Mixin
from tornado import gen, web

from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from jupyterhub.auth import LocalAuthenticator

from .oauth2 import OAuthLoginHandler, OAuthenticator

OPENSHIFT_URL = os.environ.get('OPENSHIFT_URL') or 'https://localhost:8443'
OPENSHIFT_AUTH_API_URL = os.environ.get('OPENSHIFT_AUTH_API_URL') or OPENSHIFT_URL
OPENSHIFT_REST_API_URL = os.environ.get('OPENSHIFT_REST_API_URL') or OPENSHIFT_URL

class OpenShiftMixin(OAuth2Mixin):
    _OAUTH_AUTHORIZE_URL = "%s/oauth/authorize" % OPENSHIFT_AUTH_API_URL
    _OAUTH_ACCESS_TOKEN_URL = "%s/oauth/token" % OPENSHIFT_AUTH_API_URL

class OpenShiftLoginHandler(OAuthLoginHandler, OpenShiftMixin):
    # This allows `Service Accounts as OAuth Clients` scenario
    # https://docs.openshift.org/latest/architecture/additional_concepts/authentication.html#service-accounts-as-oauth-clients
    @property
    def scope(self):
        return self.authenticator.scope

class OpenShiftOAuthenticator(OAuthenticator):

    login_service = "OpenShift"

    login_handler = OpenShiftLoginHandler

    scope = ['user:info']

    users_rest_api_path = '/apis/user.openshift.io/v1/users/~'

    @gen.coroutine
    def authenticate(self, handler, data=None):
        code = handler.get_argument("code")
        # TODO: Configure the curl_httpclient for tornado
        http_client = AsyncHTTPClient()

        # Exchange the OAuth code for a OpenShift Access Token
        #
        # See: https://docs.openshift.org/latest/architecture/additional_concepts/authentication.html#api-authentication

        params = dict(
            client_id=self.client_id,
            client_secret=self.client_secret,
            grant_type="authorization_code",
            code=code
        )

        url = url_concat(self.login_handler._OAUTH_ACCESS_TOKEN_URL, params)

        req = HTTPRequest(url,
                          method="POST",
                          validate_cert=False,
                          headers={"Accept": "application/json"},
                          body='' # Body is required for a POST...
                          )

        resp = yield http_client.fetch(req)

        resp_json = json.loads(resp.body.decode('utf8', 'replace'))

        access_token = resp_json['access_token']

        # Determine who the logged in user is
        headers={"Accept": "application/json",
                 "User-Agent": "JupyterHub",
                 "Authorization": "Bearer {}".format(access_token)
        }

        req = HTTPRequest("%s%s" % (OPENSHIFT_REST_API_URL, self.users_rest_api_path),
                          method="GET",
                          validate_cert=False,
                          headers=headers)

        resp = yield http_client.fetch(req)
        resp_json = json.loads(resp.body.decode('utf8', 'replace'))

        return {
            'name': resp_json['metadata']['name'],
            'auth_state': {
                'access_token': access_token,
                'openshift_user': resp_json,
            }
        }

class LocalOpenShiftOAuthenticator(LocalAuthenticator, OpenShiftOAuthenticator):

    """A version that mixes in local system user creation"""
    pass
<|endoftext|>


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

"""
oauthlib.oauth1
~~~~~~~~~~~~~~

This module is a wrapper for the most recent implementation of OAuth 1.0 Client
and Server classes.
"""

from .rfc5849 import Client, Server
from .rfc5849 import SIGNATURE_HMAC, SIGNATURE_RSA, SIGNATURE_PLAINTEXT
from .rfc5849 import SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_QUERY
from .rfc5849 import SIGNATURE_TYPE_BODY
<commit_msg>Convenience imports for the new OAuth 1 provider.<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

"""
oauthlib.oauth1
~~~~~~~~~~~~~~

This module is a wrapper for the most recent implementation of OAuth 1.0 Client
and Server classes.
"""

from .rfc5849 import Client
from .rfc5849 import SIGNATURE_HMAC, SIGNATURE_RSA, SIGNATURE_PLAINTEXT
from .rfc5849 import SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_QUERY
from .rfc5849 import SIGNATURE_TYPE_BODY
from .rfc5849.request_validator import RequestValidator
from .rfc5849.endpoints import RequestTokenEndpoint, AuthorizationEndpoint
from .rfc5849.endpoints import AccessTokenEndpoint, ResourceEndpoint
from .rfc5849.endpoints import WebApplicationServer
<|endoftext|>


<commit_before>class StyleOption(object):
	def __init__(self, name, options):
		self.name = name
		self.options = options

# The top-level styles that clang-format supports.
BASE_STYLE_TYPES = [
    'LLVM',
    'Google',
    'Chromium',
    'Mozilla',
    'WebKit',
]

# Keys that we explicitly ignore, either because they don't really apply to what
# we're doing or they are very obscure.
SKIP_STYLE_OPTIONS = [
    'Language',
    'CommentPragmas',
    'DisableFormat',
    'ForEachMacros',
    'JavaScriptQuotes',
    'JavaScriptWrapImports',
    'MacroBlockEnd',
    'MacroBlockBegin',
    'BreakAfterJavaFieldAnnotations',
]

# Set of all styling options.
# The keys of this dictionary represent each setting that can be tweaked.
# The values are arrays of the options for each setting. Each "option" is a dictionary of key-value pairs
# where the key usually repeats the style's top-level name. This is so that some styles (like the UseTab ones)
# can be coupled and handled together.
STYLE_OPTIONS = {}

# Set up the stylings that are just on or off.
STYLE_OPTIONS.update({
    key: StyleOption(key, [
    	{key:True},
    	{key:False},
    ])
    for key in [
        "AlignConsecutiveAssignments",
        "AlignConsecutiveDeclarations",
        "AlignEscapedNewlinesLeft",
        "AlignOperands",
        "AlignTrailingComments",
        "AllowAllParametersOfDeclarationOnNextLine",
        "AllowShortBlocksOnASingleLine",
        "AllowShortCaseLabelsOnASingleLine",
        "AllowShortIfStatementsOnASingleLine",
        "AllowShortLoopsOnASingleLine",
        "AlwaysBreakBeforeMultilineStrings",
        "AlwaysBreakTemplateDeclarations",
        "BinPackArguments",
        "BinPackParameters",
        "BreakBeforeTernaryOperators",
        "BreakConstructorInitializersBeforeComma",
        "BreakStringLiterals",
        "ConstructorInitializerAllOnOneLineOrOnePerLine",
        "Cpp11BracedListStyle",
        "DerivePointerAlignment",
        "ExperimentalAutoDetectBinPacking",
        "IndentCaseLabels",
        "IndentWrappedFunctionNames",
        "KeepEmptyLinesAtTheStartOfBlocks",
        "ObjCSpaceAfterProperty",
        "ObjCSpaceBeforeProtocolList",
        "ReflowComments",
        "SortIncludes",
        "SpaceAfterCStyleCast",
        "SpaceAfterTemplateKeyword",
        "SpaceBeforeAssignmentOperators",
        "SpaceInEmptyParentheses",
        "SpacesInAngles",
        "SpacesInContainerLiterals",
        "SpacesInCStyleCastParentheses",
        "SpacesInParentheses",
        "SpacesInSquareBrackets",
    ]
})

# Set up the stylings that have specific options.
STYLE_OPTIONS.update({
    key: StyleOption(key, [
    	{key:option} for option in options
    ])
    for key,options in {
        'AccessModifierOffset': [-4, -2, -1],
        'AlignAfterOpenBracket': ['Align', 'DontAlign', 'AlwaysBreak'],
        'AllowShortFunctionsOnASingleLine': ['All', 'Inline', 'None', 'Empty'],
        'AlwaysBreakAfterDefinitionReturnType': ['TopLevel', 'None'],
        'AlwaysBreakAfterReturnType': ['None', 'TopLevelDefinitions'],
        'BasedOnStyle': ['WebKit', 'Mozilla', 'Chromium', 'LLVM', 'Google'],
        'BreakBeforeBinaryOperators': ['None', 'NonAssignment', 'All'],
        'BreakBeforeBraces': ['GNU', 'Allman', 'Mozilla', 'Attach', 'Stroustrup', 'Linux', 'WebKit'],
        'ColumnLimit': [0, 80, 90, 100, 110, 120],
        'ConstructorInitializerIndentWidth': [2, 4],
        'ContinuationIndentWidth': [2, 4],
        'IncludeCategories': [[{'Regex': '^"(llvm|llvm-c|clang|clang-c)/', 'Priority': 2}, {'Regex': '^(<|"(gtest|isl|json)/)', 'Priority': 3}, {'Regex': '.*', 'Priority': 1}], [{'Regex': '^<.*\\.h>', 'Priority': 1}, {'Regex': '^<.*', 'Priority': 2}, {'Regex': '.*', 'Priority': 3}]],
        'IncludeIsMainRegex': ['$', '([-_](test|unittest))?$'],
        'IndentWidth': [2, 3, 4, 8],
        "MaxEmptyLinesToKeep": [0, 1, 2, 3, 4],
        'NamespaceIndentation': ['All', 'None', 'Inner'],
        'ObjCBlockIndentWidth': [2, 4],
        'PenaltyBreakBeforeFirstCallParameter': [1, 19],
        'PenaltyBreakComment': [300, 150],
        'PenaltyBreakFirstLessLess': [120, 60],
        'PenaltyBreakString': [1000, 500],
        'PenaltyExcessCharacter': [1000000, 500000],
        'PenaltyReturnTypeOnItsOwnLine': [200, 60],
        'PointerAlignment': ['Middle', 'Right', 'Left'],
        'SpaceBeforeParens': ['Always', 'Never', 'ControlStatements'],
        'SpacesBeforeTrailingComments': [1, 2],
        'Standard': ['Auto','Cpp03','Cpp11'],
        'Standard': ['Cpp11', 'Cpp03', 'Auto'],
    }.iteritems()
})

# The UseTab & TabWidth are coupled.
STYLE_OPTIONS['UseTab'] = StyleOption('UseTab', [
    {'UseTab': 'Never', 'TabWidth': 8},
    {'UseTab': 'ForIndentation', 'TabWidth': 4},
    {'UseTab': 'ForIndentation', 'TabWidth': 8},
    {'UseTab': 'Always', 'TabWidth': 4},
    {'UseTab': 'Always', 'TabWidth': 8},
])

<commit_msg>Style: add base implementation<commit_after>class StyleOption(object):
	def __init__(self, name, options):
		self.name = name
		self.options = options

class Style(object):
    def __init__(self, base=None, style=None):
        if style is None:
            if base is not None:
                style = {'BasedOnStyle': base}
            elif style is None:
                style = {}
        elif base is None:
            base = style['BasedOnStyle']

        self.base = base
        self.style = style

# The top-level styles that clang-format supports.
BASE_STYLE_TYPES = [
    'LLVM',
    'Google',
    'Chromium',
    'Mozilla',
    'WebKit',
]

# Keys that we explicitly ignore, either because they don't really apply to what
# we're doing or they are very obscure.
SKIP_STYLE_OPTIONS = [
    'Language',
    'CommentPragmas',
    'DisableFormat',
    'ForEachMacros',
    'JavaScriptQuotes',
    'JavaScriptWrapImports',
    'MacroBlockEnd',
    'MacroBlockBegin',
    'BreakAfterJavaFieldAnnotations',
]

# Set of all styling options.
# The keys of this dictionary represent each setting that can be tweaked.
# The values are arrays of the options for each setting. Each "option" is a dictionary of key-value pairs
# where the key usually repeats the style's top-level name. This is so that some styles (like the UseTab ones)
# can be coupled and handled together.
STYLE_OPTIONS = {}

# Set up the stylings that are just on or off.
STYLE_OPTIONS.update({
    key: StyleOption(key, [
    	{key:True},
    	{key:False},
    ])
    for key in [
        "AlignConsecutiveAssignments",
        "AlignConsecutiveDeclarations",
        "AlignEscapedNewlinesLeft",
        "AlignOperands",
        "AlignTrailingComments",
        "AllowAllParametersOfDeclarationOnNextLine",
        "AllowShortBlocksOnASingleLine",
        "AllowShortCaseLabelsOnASingleLine",
        "AllowShortIfStatementsOnASingleLine",
        "AllowShortLoopsOnASingleLine",
        "AlwaysBreakBeforeMultilineStrings",
        "AlwaysBreakTemplateDeclarations",
        "BinPackArguments",
        "BinPackParameters",
        "BreakBeforeTernaryOperators",
        "BreakConstructorInitializersBeforeComma",
        "BreakStringLiterals",
        "ConstructorInitializerAllOnOneLineOrOnePerLine",
        "Cpp11BracedListStyle",
        "DerivePointerAlignment",
        "ExperimentalAutoDetectBinPacking",
        "IndentCaseLabels",
        "IndentWrappedFunctionNames",
        "KeepEmptyLinesAtTheStartOfBlocks",
        "ObjCSpaceAfterProperty",
        "ObjCSpaceBeforeProtocolList",
        "ReflowComments",
        "SortIncludes",
        "SpaceAfterCStyleCast",
        "SpaceAfterTemplateKeyword",
        "SpaceBeforeAssignmentOperators",
        "SpaceInEmptyParentheses",
        "SpacesInAngles",
        "SpacesInContainerLiterals",
        "SpacesInCStyleCastParentheses",
        "SpacesInParentheses",
        "SpacesInSquareBrackets",
    ]
})

# Set up the stylings that have specific options.
STYLE_OPTIONS.update({
    key: StyleOption(key, [
    	{key:option} for option in options
    ])
    for key,options in {
        'AccessModifierOffset': [-4, -2, -1],
        'AlignAfterOpenBracket': ['Align', 'DontAlign', 'AlwaysBreak'],
        'AllowShortFunctionsOnASingleLine': ['All', 'Inline', 'None', 'Empty'],
        'AlwaysBreakAfterDefinitionReturnType': ['TopLevel', 'None'],
        'AlwaysBreakAfterReturnType': ['None', 'TopLevelDefinitions'],
        'BasedOnStyle': ['WebKit', 'Mozilla', 'Chromium', 'LLVM', 'Google'],
        'BreakBeforeBinaryOperators': ['None', 'NonAssignment', 'All'],
        'BreakBeforeBraces': ['GNU', 'Allman', 'Mozilla', 'Attach', 'Stroustrup', 'Linux', 'WebKit'],
        'ColumnLimit': [0, 80, 90, 100, 110, 120],
        'ConstructorInitializerIndentWidth': [2, 4],
        'ContinuationIndentWidth': [2, 4],
        'IncludeCategories': [[{'Regex': '^"(llvm|llvm-c|clang|clang-c)/', 'Priority': 2}, {'Regex': '^(<|"(gtest|isl|json)/)', 'Priority': 3}, {'Regex': '.*', 'Priority': 1}], [{'Regex': '^<.*\\.h>', 'Priority': 1}, {'Regex': '^<.*', 'Priority': 2}, {'Regex': '.*', 'Priority': 3}]],
        'IncludeIsMainRegex': ['$', '([-_](test|unittest))?$'],
        'IndentWidth': [2, 3, 4, 8],
        "MaxEmptyLinesToKeep": [0, 1, 2, 3, 4],
        'NamespaceIndentation': ['All', 'None', 'Inner'],
        'ObjCBlockIndentWidth': [2, 4],
        'PenaltyBreakBeforeFirstCallParameter': [1, 19],
        'PenaltyBreakComment': [300, 150],
        'PenaltyBreakFirstLessLess': [120, 60],
        'PenaltyBreakString': [1000, 500],
        'PenaltyExcessCharacter': [1000000, 500000],
        'PenaltyReturnTypeOnItsOwnLine': [200, 60],
        'PointerAlignment': ['Middle', 'Right', 'Left'],
        'SpaceBeforeParens': ['Always', 'Never', 'ControlStatements'],
        'SpacesBeforeTrailingComments': [1, 2],
        'Standard': ['Auto','Cpp03','Cpp11'],
        'Standard': ['Cpp11', 'Cpp03', 'Auto'],
    }.iteritems()
})

# The UseTab & TabWidth are coupled.
STYLE_OPTIONS['UseTab'] = StyleOption('UseTab', [
    {'UseTab': 'Never', 'TabWidth': 8},
    {'UseTab': 'ForIndentation', 'TabWidth': 4},
    {'UseTab': 'ForIndentation', 'TabWidth': 8},
    {'UseTab': 'Always', 'TabWidth': 4},
    {'UseTab': 'Always', 'TabWidth': 8},
])

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test scriptharness/commands/__init__.py
"""
from __future__ import absolute_import, division, print_function, \
                       unicode_literals
import logging
import mock
import os
import pprint
import scriptharness.commands as commands
from scriptharness.exceptions import ScriptHarnessError, \
    ScriptHarnessException, ScriptHarnessFatal, ScriptHarnessTimeout
import scriptharness.log as log
import scriptharness.status as status
import shutil
import six
import subprocess
import sys
import time
import unittest
from . import LoggerReplacement

TEST_JSON = os.path.join(os.path.dirname(__file__), 'http', 'test_config.json')
TEST_DIR = "this_dir_should_not_exist"

# Helper functions {{{1
def cleanup():
    """Cleanliness"""
    if os.path.exists(TEST_DIR):
        shutil.rmtree(TEST_DIR)

def get_command(command=None, **kwargs):
    """Create a Command for testing
    """
    if command is None:
        command = [
            sys.executable, "-c",
            'from __future__ import print_function; print("hello");'
        ]
    kwargs.setdefault('logger', LoggerReplacement())
    return commands.Command(command, **kwargs)

def get_parsed_command(command=None, **kwargs):
    """Create a ParsedCommand for testing
    """
    if command is None:
        command = [
            sys.executable, "-c",
            'from __future__ import print_function; print("hello");'
        ]
    kwargs.setdefault('logger', LoggerReplacement())
    return commands.ParsedCommand(command, **kwargs)

def get_timeout_cmdlns():
    """Create a list of commandline commands to run to test timeouts.
    """
    cmdlns = []
    if os.name != "nt":
        cmdlns += [["sleep", "300"], "echo -n 'foo' && sleep 300"]
    cmdlns += [
        [sys.executable, "-c",
         "from __future__ import print_function; import time;"
         "time.sleep(300);"],
        [sys.executable, "-c",
         "from __future__ import print_function; import time;"
         "print('foo', end=' ');"
         "time.sleep(300);"],
    ]
    return cmdlns

# TestFunctions {{{1
class TestFunctions(unittest.TestCase):
    """Test the command functions
    """
    def setUp(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def tearDown(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    @mock.patch('scriptharness.commands.logging')
    def test_check_output(self, mock_logging):
        """test_commands | check_output()
        """
        logger = LoggerReplacement()
        mock_logging.getLogger.return_value = logger
        command = [sys.executable, "-mjson.tool", TEST_JSON]
        output = commands.check_output(command)
        self.assertEqual(
            logger.all_messages[0][1],
            commands.STRINGS["check_output"]["pre_msg"]
        )
        output2 = subprocess.check_output(command)
        self.assertEqual(output, output2)

    @mock.patch('scriptharness.commands.logging')
    def test_check_output_nolog(self, mock_logging):
        """test_commands | check_output() with no logging
        """
        logger = LoggerReplacement()
        mock_logging.getLogger.return_value = logger
        command = [sys.executable, "-mjson.tool", TEST_JSON]
        commands.check_output(command, log_output=False)
        self.assertEqual(
            logger.all_messages[0][1],
            commands.STRINGS["check_output"]["pre_msg"]
        )
        self.assertEqual(len(logger.all_messages), 1)

# TestDetectErrors {{{1
class TestDetectErrors(unittest.TestCase):
    """Test detect_errors()
    """
    def test_success(self):
        """test_commands | detect_errors() success
        """
        command = get_command()
        command.history['return_value'] = 0
        self.assertEqual(commands.detect_errors(command), status.SUCCESS)

    def test_failure(self):
        """test_commands | detect_errors() failure
        """
        command = get_command()
        for value in (1, None):
            command.history['return_value'] = value
            self.assertEqual(commands.detect_errors(command), status.ERROR)

# TestDetectParsedErrors {{{1
class TestDetectParsedErrors(unittest.TestCase):
    """Test detect_parsed_errors()
    """
    def test_success(self):
        """test_commands | detect_parsed_errors() success
        """
        error_list = log.ErrorList([])
        command = get_parsed_command(error_list=error_list)
        command.parser.history['num_errors'] = 0
        self.assertEqual(commands.detect_parsed_errors(command),
                         status.SUCCESS)

    def test_failure(self):
        """test_commands | detect_parsed_errors() failure
        """
        error_list = log.ErrorList([])
        command = get_parsed_command(error_list=error_list)
        for value in (1, 20):
            command.parser.history['num_errors'] = value
            self.assertEqual(commands.detect_parsed_errors(command),
                             status.ERROR)

# TestCommand {{{1
class TestCommand(unittest.TestCase):
    """Test Command()
    """
    def setUp(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def tearDown(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def test_simple_command(self):
        """test_commands | simple Command.run()
        """
        command = get_command()
        command.run()
        pprint.pprint(command.logger.all_messages)
        self.assertEqual(command.logger.all_messages[-1][2][0], "hello")

    def test_log_env(self):
        """test_commands | Command.log_env()
        """
        env = {"foo": "bar"}
        command = get_command(
            env=env,
        )
        command.log_env(env)
        command.run()
        env_line = "'foo': 'bar'"
        if os.name != 'nt' and six.PY2:
            env_line = "u'foo': u'bar'"
        count = 0
        for line in command.logger.all_messages:
            if line[1] == commands.STRINGS['command']['env']:
                print(line)
                self.assertTrue(env_line in line[2][0]["env"])
                count += 1
        self.assertEqual(count, 2)

    def test_bad_cwd(self):
        """test_commands | Command bad cwd
        """
        command = get_command(cwd=TEST_DIR)
        self.assertRaises(ScriptHarnessException, command.run)

    def test_good_cwd(self):
        """test_commands | Command good cwd
        """
        os.makedirs(TEST_DIR)
        command = get_command(cwd=TEST_DIR)
        command.log_start()
        self.assertEqual(
            command.logger.all_messages[0][1],
            command.strings["start_with_cwd"],
        )

    def test_output_timeout(self):
        """test_commands | Command output_timeout
        """
        for cmdln in get_timeout_cmdlns():
            now = time.time()
            command = get_command(command=cmdln, output_timeout=.5)
            print(cmdln)
            self.assertRaises(ScriptHarnessTimeout, command.run)
            self.assertTrue(now + 1 > time.time())

    def test_timeout(self):
        """test_commands | Command timeout
        """
        for cmdln in get_timeout_cmdlns():
            now = time.time()
            command = get_command(command=cmdln, timeout=.5)
            print(cmdln)
            self.assertRaises(ScriptHarnessTimeout, command.run)
            self.assertTrue(now + 1 > time.time())

    def test_command_error(self):
        """test_commands | Command.run() with error
        """
        command = get_command(
            command=[sys.executable, "-c", 'import sys; sys.exit(1)']
        )
        self.assertRaises(ScriptHarnessError, command.run)

    @mock.patch('scriptharness.commands.os')
    def test_fix_env(self, mock_os):
        """test_commands | Command.fix_env()
        """
        mock_os.name = 'nt'
        mock_os.environ = {u'SystemRoot': u'FakeSystemRoot'}
        command = get_command()
        env = {u'foo': u'bar', b'x': b'y'}
        if six.PY3:
            expected_env = {u'foo': u'bar', b'x': b'y',
                            u'SystemRoot': u'FakeSystemRoot'}
        else:
            expected_env = {b'foo': b'bar', b'x': b'y',
                            b'SystemRoot': b'FakeSystemRoot'}
        self.assertEqual(command.fix_env(env), expected_env)

# TestRun {{{1
class TestRun(unittest.TestCase):
    """test commands.run()
    """
    @mock.patch('scriptharness.commands.multiprocessing')
    def test_error(self, mock_multiprocessing):
        """test_commands | run() error
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessError"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessError("foo")
        mock_multiprocessing.Process = raise_error
        self.assertRaises(
            ScriptHarnessFatal, commands.run,
            "echo", halt_on_failure=True
        )

    @mock.patch('scriptharness.commands.multiprocessing')
    def test_timeout(self, mock_multiprocessing):
        """test_commands | run() timeout
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessTimeout"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessTimeout("foo")
        mock_multiprocessing.Process = raise_error
        self.assertRaises(
            ScriptHarnessFatal, commands.run,
            "echo", halt_on_failure=True
        )

    @mock.patch('scriptharness.commands.multiprocessing')
    def test_no_halt(self, mock_multiprocessing):
        """test_commands | run() halt_on_error=False
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessTimeout"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessTimeout("foo")
        mock_multiprocessing.Process = raise_error
        cmd = commands.run("echo", halt_on_failure=False)
        self.assertEqual(cmd.history['status'], status.TIMEOUT)

# TestParsedCommand {{{1
class TestParsedCommand(unittest.TestCase):
    """ParsedCommand()
    """
    def test_parser_kwarg(self):
        """test_commands | ParsedCommand() parser kwarg
        """
        error_list = log.ErrorList([
            {'substr': 'ell', 'level': logging.WARNING}
        ])
        logger = LoggerReplacement()
        parser = log.OutputParser(error_list, logger=logger)
        cmd = get_parsed_command(parser=parser)
        cmd.run()
        self.assertEqual(parser.history['num_warnings'], 1)
        pprint.pprint(logger.all_messages)
        self.assertEqual(
            logger.all_messages,
            [(logging.WARNING, ' hello', ())]
        )

    def test_bad_errorlist(self):
        """test_commands | ParsedCommand bad error_list
        """
        for error_list in (None, {'substr': 'asdf', 'level': logging.ERROR}):
            self.assertRaises(
                ScriptHarnessException, get_parsed_command,
                error_list
            )

    def test_parse(self):
        """test_commands | parse()
        """
        error_list = log.ErrorList([
            {'substr': 'ell', 'level': logging.WARNING}
        ])
        logger = LoggerReplacement()
        parser = log.OutputParser(error_list, logger=logger)
        cmd = commands.parse(
            [sys.executable, "-c",
             'from __future__ import print_function; print("hello");'],
            parser=parser
        )
        self.assertEqual(parser.history['num_warnings'], 1)
        pprint.pprint(logger.all_messages)
        self.assertEqual(
            logger.all_messages,
            [(logging.WARNING, ' hello', ())]
        )
<commit_msg>test_commands pylint<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test scriptharness/commands/__init__.py
"""
from __future__ import absolute_import, division, print_function, \
                       unicode_literals
import logging
import mock
import os
import pprint
import scriptharness.commands as commands
from scriptharness.exceptions import ScriptHarnessError, \
    ScriptHarnessException, ScriptHarnessFatal, ScriptHarnessTimeout
import scriptharness.log as log
import scriptharness.status as status
import shutil
import six
import subprocess
import sys
import time
import unittest
from . import LoggerReplacement

TEST_JSON = os.path.join(os.path.dirname(__file__), 'http', 'test_config.json')
TEST_DIR = "this_dir_should_not_exist"

# Helper functions {{{1
def cleanup():
    """Cleanliness"""
    if os.path.exists(TEST_DIR):
        shutil.rmtree(TEST_DIR)

def get_command(command=None, **kwargs):
    """Create a Command for testing
    """
    if command is None:
        command = [
            sys.executable, "-c",
            'from __future__ import print_function; print("hello");'
        ]
    kwargs.setdefault('logger', LoggerReplacement())
    return commands.Command(command, **kwargs)

def get_parsed_command(command=None, **kwargs):
    """Create a ParsedCommand for testing
    """
    if command is None:
        command = [
            sys.executable, "-c",
            'from __future__ import print_function; print("hello");'
        ]
    kwargs.setdefault('logger', LoggerReplacement())
    return commands.ParsedCommand(command, **kwargs)

def get_timeout_cmdlns():
    """Create a list of commandline commands to run to test timeouts.
    """
    cmdlns = []
    if os.name != "nt":
        cmdlns += [["sleep", "300"], "echo -n 'foo' && sleep 300"]
    cmdlns += [
        [sys.executable, "-c",
         "from __future__ import print_function; import time;"
         "time.sleep(300);"],
        [sys.executable, "-c",
         "from __future__ import print_function; import time;"
         "print('foo', end=' ');"
         "time.sleep(300);"],
    ]
    return cmdlns

# TestFunctions {{{1
class TestFunctions(unittest.TestCase):
    """Test the command functions
    """
    def setUp(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def tearDown(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    @mock.patch('scriptharness.commands.logging')
    def test_check_output(self, mock_logging):
        """test_commands | check_output()
        """
        logger = LoggerReplacement()
        mock_logging.getLogger.return_value = logger
        command = [sys.executable, "-mjson.tool", TEST_JSON]
        output = commands.check_output(command)
        self.assertEqual(
            logger.all_messages[0][1],
            commands.STRINGS["check_output"]["pre_msg"]
        )
        output2 = subprocess.check_output(command)
        self.assertEqual(output, output2)

    @mock.patch('scriptharness.commands.logging')
    def test_check_output_nolog(self, mock_logging):
        """test_commands | check_output() with no logging
        """
        logger = LoggerReplacement()
        mock_logging.getLogger.return_value = logger
        command = [sys.executable, "-mjson.tool", TEST_JSON]
        commands.check_output(command, log_output=False)
        self.assertEqual(
            logger.all_messages[0][1],
            commands.STRINGS["check_output"]["pre_msg"]
        )
        self.assertEqual(len(logger.all_messages), 1)

# TestDetectErrors {{{1
class TestDetectErrors(unittest.TestCase):
    """Test detect_errors()
    """
    def test_success(self):
        """test_commands | detect_errors() success
        """
        command = get_command()
        command.history['return_value'] = 0
        self.assertEqual(commands.detect_errors(command), status.SUCCESS)

    def test_failure(self):
        """test_commands | detect_errors() failure
        """
        command = get_command()
        for value in (1, None):
            command.history['return_value'] = value
            self.assertEqual(commands.detect_errors(command), status.ERROR)

# TestDetectParsedErrors {{{1
class TestDetectParsedErrors(unittest.TestCase):
    """Test detect_parsed_errors()
    """
    def test_success(self):
        """test_commands | detect_parsed_errors() success
        """
        error_list = log.ErrorList([])
        command = get_parsed_command(error_list=error_list)
        command.parser.history['num_errors'] = 0
        self.assertEqual(commands.detect_parsed_errors(command),
                         status.SUCCESS)

    def test_failure(self):
        """test_commands | detect_parsed_errors() failure
        """
        error_list = log.ErrorList([])
        command = get_parsed_command(error_list=error_list)
        for value in (1, 20):
            command.parser.history['num_errors'] = value
            self.assertEqual(commands.detect_parsed_errors(command),
                             status.ERROR)

# TestCommand {{{1
class TestCommand(unittest.TestCase):
    """Test Command()
    """
    def setUp(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def tearDown(self):
        """Cleanliness"""
        assert self  # silence pylint
        cleanup()

    def test_simple_command(self):
        """test_commands | simple Command.run()
        """
        command = get_command()
        command.run()
        pprint.pprint(command.logger.all_messages)
        self.assertEqual(command.logger.all_messages[-1][2][0], "hello")

    def test_log_env(self):
        """test_commands | Command.log_env()
        """
        env = {"foo": "bar"}
        command = get_command(
            env=env,
        )
        command.log_env(env)
        command.run()
        env_line = "'foo': 'bar'"
        if os.name != 'nt' and six.PY2:
            env_line = "u'foo': u'bar'"
        count = 0
        for line in command.logger.all_messages:
            if line[1] == commands.STRINGS['command']['env']:
                print(line)
                self.assertTrue(env_line in line[2][0]["env"])
                count += 1
        self.assertEqual(count, 2)

    def test_bad_cwd(self):
        """test_commands | Command bad cwd
        """
        command = get_command(cwd=TEST_DIR)
        self.assertRaises(ScriptHarnessException, command.run)

    def test_good_cwd(self):
        """test_commands | Command good cwd
        """
        os.makedirs(TEST_DIR)
        command = get_command(cwd=TEST_DIR)
        command.log_start()
        self.assertEqual(
            command.logger.all_messages[0][1],
            command.strings["start_with_cwd"],
        )

    def test_output_timeout(self):
        """test_commands | Command output_timeout
        """
        for cmdln in get_timeout_cmdlns():
            now = time.time()
            command = get_command(command=cmdln, output_timeout=.5)
            print(cmdln)
            self.assertRaises(ScriptHarnessTimeout, command.run)
            self.assertTrue(now + 1 > time.time())

    def test_timeout(self):
        """test_commands | Command timeout
        """
        for cmdln in get_timeout_cmdlns():
            now = time.time()
            command = get_command(command=cmdln, timeout=.5)
            print(cmdln)
            self.assertRaises(ScriptHarnessTimeout, command.run)
            self.assertTrue(now + 1 > time.time())

    def test_command_error(self):
        """test_commands | Command.run() with error
        """
        command = get_command(
            command=[sys.executable, "-c", 'import sys; sys.exit(1)']
        )
        self.assertRaises(ScriptHarnessError, command.run)

    @mock.patch('scriptharness.commands.os')
    def test_fix_env(self, mock_os):
        """test_commands | Command.fix_env()
        """
        mock_os.name = 'nt'
        mock_os.environ = {u'SystemRoot': u'FakeSystemRoot'}
        command = get_command()
        env = {u'foo': u'bar', b'x': b'y'}
        if six.PY3:
            expected_env = {u'foo': u'bar', b'x': b'y',
                            u'SystemRoot': u'FakeSystemRoot'}
        else:
            expected_env = {b'foo': b'bar', b'x': b'y',
                            b'SystemRoot': b'FakeSystemRoot'}
        self.assertEqual(command.fix_env(env), expected_env)

# TestRun {{{1
class TestRun(unittest.TestCase):
    """test commands.run()
    """
    @mock.patch('scriptharness.commands.multiprocessing')
    def test_error(self, mock_multiprocessing):
        """test_commands | run() error
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessError"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessError("foo")
        mock_multiprocessing.Process = raise_error
        self.assertRaises(
            ScriptHarnessFatal, commands.run,
            "echo", halt_on_failure=True
        )

    @mock.patch('scriptharness.commands.multiprocessing')
    def test_timeout(self, mock_multiprocessing):
        """test_commands | run() timeout
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessTimeout"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessTimeout("foo")
        mock_multiprocessing.Process = raise_error
        self.assertRaises(
            ScriptHarnessFatal, commands.run,
            "echo", halt_on_failure=True
        )

    @mock.patch('scriptharness.commands.multiprocessing')
    def test_no_halt(self, mock_multiprocessing):
        """test_commands | run() halt_on_error=False
        """
        def raise_error(*args, **kwargs):
            """raise ScriptHarnessTimeout"""
            if args or kwargs:  # silence pylint
                pass
            raise ScriptHarnessTimeout("foo")
        mock_multiprocessing.Process = raise_error
        cmd = commands.run("echo", halt_on_failure=False)
        self.assertEqual(cmd.history['status'], status.TIMEOUT)

# TestParsedCommand {{{1
class TestParsedCommand(unittest.TestCase):
    """ParsedCommand()
    """
    def test_parser_kwarg(self):
        """test_commands | ParsedCommand() parser kwarg
        """
        error_list = log.ErrorList([
            {'substr': 'ell', 'level': logging.WARNING}
        ])
        logger = LoggerReplacement()
        parser = log.OutputParser(error_list, logger=logger)
        cmd = get_parsed_command(parser=parser)
        cmd.run()
        self.assertEqual(parser.history['num_warnings'], 1)
        pprint.pprint(logger.all_messages)
        self.assertEqual(
            logger.all_messages,
            [(logging.WARNING, ' hello', ())]
        )

    def test_bad_errorlist(self):
        """test_commands | ParsedCommand bad error_list
        """
        for error_list in (None, {'substr': 'asdf', 'level': logging.ERROR}):
            self.assertRaises(
                ScriptHarnessException, get_parsed_command,
                error_list
            )

    def test_parse(self):
        """test_commands | parse()
        """
        error_list = log.ErrorList([
            {'substr': 'ell', 'level': logging.WARNING}
        ])
        logger = LoggerReplacement()
        parser = log.OutputParser(error_list, logger=logger)
        cmd = commands.parse(
            [sys.executable, "-c",
             'from __future__ import print_function; print("hello");'],
            parser=parser
        )
        self.assertTrue(isinstance(cmd, commands.ParsedCommand))
        self.assertEqual(parser.history['num_warnings'], 1)
        pprint.pprint(logger.all_messages)
        self.assertEqual(
            logger.all_messages,
            [(logging.WARNING, ' hello', ())]
        )
<|endoftext|>


<commit_before>import sublime, sublime_plugin;
import time, os.path, json, threading, sys
import subprocess
from os.path import expanduser
from threading import Thread
import json
import re
import signal

try:
  import BaseHTTPServer
except ImportError:
  import http.server as BaseHTTPServer

CACHE_DIR = expanduser("~") + '/.subhub'
PORT      = 48666
def open(directory):
  subprocess.Popen([sublime.executable_path(), '.'], cwd=directory, shell=True)

def clone(url):
  print('Cloning ' + url)
  m = re.search('.*github.com[\/|:]([^\/]+)\/([^\/|\.]+)', url)
  user = m.group(1)
  repo = m.group(2)

  local_repo = CACHE_DIR + '/' + user + '/' + repo

  if not os.path.isdir(local_repo):
    call(['mkdir', '-p', CACHE_DIR + '/' + user])

    if sublime.platform() == 'windows':
      call(['git', 'clone', "https://github.com/"+ user +"/" + repo + ".git", local_repo, '--depth', '1'])
    else:
      call(['git', 'clone', url, local_repo, '--depth', '1'])

  else:
    settings = sublime.load_settings("subhub.sublime-settings")
    if settings.get('if_exists') == 'pull':
      process = subprocess.Popen(['git', 'pull'], cwd=local_repo, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
      stdout = [x.decode('unicode_escape').rstrip() for x in process.stdout.readlines()]
      print(stdout)
    print('Repository is already checked out')

  return local_repo

class ConnectionHandler(BaseHTTPServer.BaseHTTPRequestHandler):

  def do_GET(self):
    print('Received: heartbeat1')
    self.send_response(200)
    self.send_header('Content-type',  'application/json')
    self.send_header('Access-Control-Allow-Origin',  '*')
    self.end_headers()

    return

  def do_POST(self):
    self.data_string = self.rfile.read(int(self.headers['Content-Length']))
    self.send_response(201)
    self.send_header('Content-type',  'application/json')
    self.send_header('Access-Control-Allow-Origin',  '*')
    self.end_headers()
    data = json.loads(self.data_string.decode("utf-8"))
    print("Cloning" + data['url'])
    open(clone(data['url']))

    return

print("Staring subhub server on port " + str(PORT))

server = BaseHTTPServer.HTTPServer(('', PORT), ConnectionHandler)
Thread(target=server.serve_forever).start()

def plugin_unloaded():
  print("Closing subhub server")
  server.shutdown()
  server.server_close()

#curl -X POST -H "Content-Type: application/json" -d '{"url":"https://github.com/nodejitsu/nssocket"}' http://localhost:48666
<commit_msg>fix broken mac support<commit_after>import sublime, sublime_plugin;
import time, os.path, json, threading, sys
import subprocess
from os.path import expanduser
from threading import Thread
import json
import re
import signal

try:
  import BaseHTTPServer
except ImportError:
  import http.server as BaseHTTPServer

CACHE_DIR = expanduser("~") + '/.subhub'
PORT      = 48666
def open(directory):
  if sublime.platform() == 'windows':
    subprocess.Popen([sublime.executable_path(), '.'], cwd=directory, shell=True)
  else:
    subprocess.call([os.path.join(os.path.dirname(sublime.executable_path()), '../SharedSupport/bin/subl'), directory])

def clone(url):
  print('Cloning ' + url)
  m = re.search('.*github.com[\/|:]([^\/]+)\/([^\/|\.]+)', url)
  user = m.group(1)
  repo = m.group(2)

  local_repo = CACHE_DIR + '/' + user + '/' + repo

  if not os.path.isdir(local_repo):
    call(['mkdir', '-p', CACHE_DIR + '/' + user])

    if sublime.platform() == 'windows':
      call(['git', 'clone', "https://github.com/"+ user +"/" + repo + ".git", local_repo, '--depth', '1'])
    else:
      call(['git', 'clone', url, local_repo, '--depth', '1'])

  else:
    settings = sublime.load_settings("subhub.sublime-settings")
    if settings.get('if_exists') == 'pull':
      process = subprocess.Popen(['git', 'pull'], cwd=local_repo, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
      stdout = [x.decode('unicode_escape').rstrip() for x in process.stdout.readlines()]
      print(stdout)
    print('Repository is already checked out')

  return local_repo

class ConnectionHandler(BaseHTTPServer.BaseHTTPRequestHandler):

  def do_GET(self):
    print('Received: heartbeat1')
    self.send_response(200)
    self.send_header('Content-type',  'application/json')
    self.send_header('Access-Control-Allow-Origin',  '*')
    self.end_headers()

    return

  def do_POST(self):
    self.data_string = self.rfile.read(int(self.headers['Content-Length']))
    self.send_response(201)
    self.send_header('Content-type',  'application/json')
    self.send_header('Access-Control-Allow-Origin',  '*')
    self.end_headers()
    data = json.loads(self.data_string.decode("utf-8"))
    print("Cloning" + data['url'])
    open(clone(data['url']))

    return

print("Staring subhub server on port " + str(PORT))

server = BaseHTTPServer.HTTPServer(('', PORT), ConnectionHandler)
Thread(target=server.serve_forever).start()

def plugin_unloaded():
  print("Closing subhub server")
  server.shutdown()
  server.server_close()

#curl -X POST -H "Content-Type: application/json" -d '{"url":"https://github.com/nodejitsu/nssocket"}' http://localhost:48666
<|endoftext|>


<commit_before>import ignorance

def test_rule_from_pattern_basics():
    to_test = [
        'foo', 'ba[rz]', 'ba?', '*.foo', 'foo/bar', 'foo/*/bar', '/foo',
        '/foo/bar', '**/foo/bar', 'foo/**/bar', 'foo/bar/**'
    ]
    negations = ['!' + pattern for pattern in to_test]
    for pattern in to_test + negations:
        rule = ignorance.git.rule_from_pattern(pattern)
        assert rule.pattern == pattern
        if pattern[0] == '!':
            assert rule.negation is True
            pattern = pattern[1:]
        else:
            assert rule.negation is False
        expected_rexp = ignorance.utils.fnmatch_pathname_to_regex(pattern)
        if '**' in pattern:
            pass
        elif '/' not in pattern:
            assert rule.regex == expected_rexp
            assert not rule.anchored
        else:
            if pattern[0] == '/':
                expected_rexp = expected_rexp[1:]
            assert rule.regex == '^' + expected_rexp
            assert rule.anchored
<commit_msg>Adding quicky directory_only rule parsing test<commit_after>import ignorance
import itertools

def test_rule_from_pattern_basics():
    to_test = [
        'foo', 'ba[rz]', 'ba?', '*.foo', 'foo/bar', 'foo/*/bar', '/foo',
        '/foo/bar', '**/foo/bar', 'foo/**/bar', 'foo/bar/**'
    ]
    for pattern in to_test:
        for negation, dir_only in itertools.product(
                (True, False), (True, False)):
            if negation:
                pattern = '!' + pattern
            if dir_only:
                pattern = pattern + '/'

            rule = ignorance.git.rule_from_pattern(pattern)
            assert rule.pattern == pattern
            if pattern[0] == '!':
                assert rule.negation is True
                pattern = pattern[1:]
            else:
                assert rule.negation is False
            if pattern[-1] == '/':
                assert rule.directory_only is True
                pattern = pattern[0:-1]
            else:
                assert rule.directory_only is False
            expected_rexp = ignorance.utils.fnmatch_pathname_to_regex(pattern)
            if '**' in pattern:
                pass
            elif '/' not in pattern:
                assert rule.regex == expected_rexp
                assert not rule.anchored
            else:
                if pattern[0] == '/':
                    expected_rexp = expected_rexp[1:]
                assert rule.regex == '^' + expected_rexp
                assert rule.anchored
<|endoftext|>


<commit_before><commit_msg>katakana tests similar to hiragana tests<commit_after><|endoftext|>