<commit_before>from core import modules
from core.modules.vistrails_module import ModuleConnector, ModuleError
from core.modules.module_utils import FilePool
from core.utils import withIndex, InstanceObject
import threading

################################################################################

_lock = threading.Lock()

class Interpreter(object):
    def __init__(self):
        pass

    typeConversion = {'Float': float,
                      'Integer': int,
                      'String': lambda x: x}

    def execute(self, pipeline, vistrailName, currentVersion, view,
                logger, useLock=True):
        try:
            if useLock:
                _lock.acquire()
            self.filePool = FilePool()
            objects = {}
            errors = {}
            executed = {}
            def addToExecuted(obj):
                executed[obj.id] = True
            def beginCompute(obj):
                view.setModuleComputing(obj.id)
            def beginUpdate(obj):
                view.setModuleActive(obj.id)
                reg = modules.module_registry.registry
                name = reg.getDescriptor(obj.__class__).name
                if logger:
                    logger.startModuleExecution(vistrailName,
                                                currentVersion, obj.id, name)
            def endUpdate(obj, error=''):
                if not error:
                    view.setModuleSuccess(obj.id)
                else:
                    view.setModuleError(obj.id, error)
                if logger:
                    logger.finishModuleExecution(vistrailName,
                                                 currentVersion, obj.id)
            def annotate(obj, d):
                if logger:
                    logger.insertAnnotationDB(vistrailName,
                                              currentVersion, obj.id, d)
            logging_obj = InstanceObject(signalSuccess=addToExecuted,
                                         beginUpdate=beginUpdate,
                                         beginCompute=beginCompute,
                                         endUpdate=endUpdate,
                                         annotate=annotate)
            # create objects
            for id, module in pipeline.modules.items():
    #            print "Will summon module", id, module
                objects[id] = module.summon()
                objects[id].interpreter = self
                objects[id].id = id
                if view:
                    objects[id].logging = logging_obj
                objects[id].vistrailName = vistrailName
                objects[id].currentVersion = currentVersion
    #            print "result: ", objects[id]
                for f in module.functions:
                    reg = modules.module_registry.registry
                    if len(f.params)==0:
                        nullObject = reg.getDescriptorByName('Null').module()
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(nullObject,
                                                                 'value'))
                    if len(f.params)==1:
                        p = f.params[0]
                        constant = reg.getDescriptorByName(p.type).module()
                        constant.setValue(p.evaluatedStrValue)
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(constant,
                                                                 'value'))
                    if len(f.params)>1:
                        value_list = []
                        tupleModule = reg.getDescriptorByName('Tuple').module()
                        tupleModule.length = len(f.params)
                        for (i,p) in withIndex(f.params):
                            constant = reg.getDescriptorByName(p.type).module()
                            constant.setValue(p.evaluatedStrValue)
                            tupleModule.setInputPort(i,
                                                     ModuleConnector(constant,
                                                                     'value'))
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(tupleModule,
                                                                 'value'))

            # create connections
            for id, conn in pipeline.connections.items():
    #             print "Will connect ",conn.sourceId,"and",conn.destinationId,
    #             print "with",conn
                src = objects[conn.sourceId]
                dst = objects[conn.destinationId]
    #             print "src:",src
    #             print "dst:",dst
                conn.makeConnection(src, dst)

            for v in pipeline.graph.sinks():
                try:
                    objects[v].update()
                except ModuleError, me:
                    me.module.logging.endUpdate(me.module, False)
#                     print "Module failed during execution."
#                     print "Module:",me.module
#                     print "Error:",me.msg
                    errors[me.module.id] = me
        finally:
            if useLock:
                _lock.release()
            self.filePool.cleanup()
            del self.filePool

        return (objects, errors, executed)

################################################################################
<commit_msg>Refactored Interpreter.execute() into Interpreter.locked_execute and unlocked_execute.<commit_after>############################################################################
##
## Copyright (C) 2006-2007 University of Utah. All rights reserved.
##
## This file is part of VisTrails.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following to ensure GNU General Public
## Licensing requirements will be met:
## http://www.opensource.org/licenses/gpl-license.php
##
## If you are unsure which license is appropriate for your use (for
## instance, you are interested in developing a commercial derivative
## of VisTrails), please contact us at vistrails@sci.utah.edu.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
############################################################################
from core import modules
from core.modules.vistrails_module import ModuleConnector, ModuleError
from core.modules.module_utils import FilePool
from core.utils import withIndex, InstanceObject, lock_method
import threading

################################################################################

_lock = threading.Lock()

class Interpreter(object):
    def __init__(self):
        pass

    typeConversion = {'Float': float,
                      'Integer': int,
                      'String': lambda x: x}

    @lock_method(_lock)
    def locked_execute(self, pipeline, vistrailName, currentVersion,
                       view, logger):
        return self.unlocked_execute(pipeline, vistrailName,
                                     currentVersion, view, logger)

    def unlocked_execute(self, pipeline, vistrailName, currentVersion,
                         view, logger):

        def addToExecuted(obj):
            executed[obj.id] = True
        def beginCompute(obj):
            view.setModuleComputing(obj.id)
        def beginUpdate(obj):
            view.setModuleActive(obj.id)
            reg = modules.module_registry.registry
            name = reg.getDescriptor(obj.__class__).name
            if logger:
                logger.startModuleExecution(vistrailName,
                                            currentVersion, obj.id, name)
        def endUpdate(obj, error=''):
            if not error:
                view.setModuleSuccess(obj.id)
            else:
                view.setModuleError(obj.id, error)
            if logger:
                logger.finishModuleExecution(vistrailName,
                                             currentVersion, obj.id)
        def annotate(obj, d):
            if logger:
                logger.insertAnnotationDB(vistrailName,
                                          currentVersion, obj.id, d)

        try:
            self.filePool = FilePool()
            objects = {}
            errors = {}
            executed = {}
            logging_obj = InstanceObject(signalSuccess=addToExecuted,
                                         beginUpdate=beginUpdate,
                                         beginCompute=beginCompute,
                                         endUpdate=endUpdate,
                                         annotate=annotate)
            # create objects
            for id, module in pipeline.modules.items():
                objects[id] = module.summon()
                objects[id].interpreter = self
                objects[id].id = id
                if view:
                    objects[id].logging = logging_obj
                objects[id].vistrailName = vistrailName
                objects[id].currentVersion = currentVersion
                for f in module.functions:
                    reg = modules.module_registry.registry
                    if len(f.params)==0:
                        nullObject = reg.getDescriptorByName('Null').module()
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(nullObject,
                                                                 'value'))
                    if len(f.params)==1:
                        p = f.params[0]
                        constant = reg.getDescriptorByName(p.type).module()
                        constant.setValue(p.evaluatedStrValue)
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(constant,
                                                                 'value'))
                    if len(f.params)>1:
                        value_list = []
                        tupleModule = reg.getDescriptorByName('Tuple').module()
                        tupleModule.length = len(f.params)
                        for (i,p) in withIndex(f.params):
                            constant = reg.getDescriptorByName(p.type).module()
                            constant.setValue(p.evaluatedStrValue)
                            tupleModule.setInputPort(i,
                                                     ModuleConnector(constant,
                                                                     'value'))
                        objects[id].setInputPort(f.name,
                                                 ModuleConnector(tupleModule,
                                                                 'value'))

            # create connections
            for id, conn in pipeline.connections.items():
                src = objects[conn.sourceId]
                dst = objects[conn.destinationId]
                conn.makeConnection(src, dst)

            for v in pipeline.graph.sinks():
                try:
                    objects[v].update()
                except ModuleError, me:
                    me.module.logging.endUpdate(me.module, False)
                    errors[me.module.id] = me
        finally:
            self.filePool.cleanup()
            del self.filePool

        return (objects, errors, executed)

    def execute(self, pipeline, vistrailName, currentVersion, view,
                logger, useLock=True):
        if useLock:
            method_call = self.locked_execute
        else:
            method_call = self.unlocked_execute

        return method_call(pipeline, vistrailName, currentVersion, view,
                           logger)

################################################################################

class CachedInterpreter(object):

    def __init__(self):
        self._file_pool = FilePool()
        self._persistent_pipeline = core.vistrail.Pipeline()

    def execute(self, pipeline, vistrailName, currentVersion,
                view, logger):
        self.add_to_persistent_pipeline(pipeline)

    def add_to_persistent_pipeline(self, pipeline):
        new_pipeline_sinks = pipeline.graph.sinks()

##############################################################################
<|endoftext|>


<commit_before>import numpy as np
from nose.tools import assert_true
from os.path import join
import numpy.testing as npt

import nibabel as nib
from dipy.io.peaks import load_peaks
from nibabel.tmpdirs import TemporaryDirectory

from dipy.data import get_data
from dipy.workflows.reconst import ReconstCSDFlow, ReconstCSAFlow

# Conditional import machinery for pytables
from dipy.utils.optpkg import optional_package

# Allow import, but disable doctests, if we don't have pytables
tables, have_tables, _ = optional_package('tables')

# Decorator to protect tests from being run without pytables present
iftables = npt.dec.skipif(not have_tables,
                          'Pytables does not appear to be installed')

@iftables
def test_reconst_csa():
    reconst_flow_core(ReconstCSAFlow)

@iftables
def test_reconst_csd():
    reconst_flow_core(ReconstCSDFlow)

@iftables
def reconst_flow_core(flow):
    with TemporaryDirectory() as out_dir:
        data_path, bval_path, bvec_path = get_data('small_64D')
        vol_img = nib.load(data_path)
        volume = vol_img.get_data()
        mask = np.ones_like(volume[:, :, :, 0])
        mask_img = nib.Nifti1Image(mask.astype(np.uint8), vol_img.get_affine())
        mask_path = join(out_dir, 'tmp_mask.nii.gz')
        nib.save(mask_img, mask_path)

        reconst_flow = flow()

        reconst_flow.run(data_path, bval_path, bvec_path, mask_path,
                         out_dir=out_dir, extract_pam_values=True)

        gfa_path = reconst_flow.last_generated_outputs['out_gfa']
        gfa_data = nib.load(gfa_path).get_data()
        assert_true(gfa_data.shape == volume.shape[:-1])

        peaks_dir_path = reconst_flow.last_generated_outputs['out_peaks_dir']
        peaks_dir_data = nib.load(peaks_dir_path).get_data()
        assert_true(peaks_dir_data.shape[-1] == 15)
        assert_true(peaks_dir_data.shape[:-1] == volume.shape[:-1])

        peaks_idx_path = \
            reconst_flow.last_generated_outputs['out_peaks_indices']
        peaks_idx_data = nib.load(peaks_idx_path).get_data()
        assert_true(peaks_idx_data.shape[-1] == 5)
        assert_true(peaks_idx_data.shape[:-1] == volume.shape[:-1])

        peaks_vals_path = \
            reconst_flow.last_generated_outputs['out_peaks_values']
        peaks_vals_data = nib.load(peaks_vals_path).get_data()
        assert_true(peaks_vals_data.shape[-1] == 5)
        assert_true(peaks_vals_data.shape[:-1] == volume.shape[:-1])

        shm_path = reconst_flow.last_generated_outputs['out_shm']
        shm_data = nib.load(shm_path).get_data()
        assert_true(shm_data.shape[-1] == 45)
        assert_true(shm_data.shape[:-1] == volume.shape[:-1])

        pam = load_peaks(reconst_flow.last_generated_outputs['out_pam'])
        npt.assert_allclose(pam.peak_dirs.reshape(peaks_dir_data.shape),
                                   peaks_dir_data)
        npt.assert_allclose(pam.peak_values, peaks_vals_data)
        npt.assert_allclose(pam.peak_indices, peaks_idx_data)
        npt.assert_allclose(pam.shm_coeff, shm_data)
        npt.assert_allclose(pam.gfa, gfa_data)

if __name__ == '__main__':
    npt.run_module_suite()
<commit_msg>TEST: does not need to be skipped any more<commit_after>import numpy as np
from nose.tools import assert_true
from os.path import join
import numpy.testing as npt

import nibabel as nib
from dipy.io.peaks import load_peaks
from nibabel.tmpdirs import TemporaryDirectory

from dipy.data import get_data
from dipy.workflows.reconst import ReconstCSDFlow, ReconstCSAFlow

def test_reconst_csa():
    reconst_flow_core(ReconstCSAFlow)

def test_reconst_csd():
    reconst_flow_core(ReconstCSDFlow)

def reconst_flow_core(flow):
    with TemporaryDirectory() as out_dir:
        data_path, bval_path, bvec_path = get_data('small_64D')
        vol_img = nib.load(data_path)
        volume = vol_img.get_data()
        mask = np.ones_like(volume[:, :, :, 0])
        mask_img = nib.Nifti1Image(mask.astype(np.uint8), vol_img.get_affine())
        mask_path = join(out_dir, 'tmp_mask.nii.gz')
        nib.save(mask_img, mask_path)

        reconst_flow = flow()

        reconst_flow.run(data_path, bval_path, bvec_path, mask_path,
                         out_dir=out_dir, extract_pam_values=True)

        gfa_path = reconst_flow.last_generated_outputs['out_gfa']
        gfa_data = nib.load(gfa_path).get_data()
        assert_true(gfa_data.shape == volume.shape[:-1])

        peaks_dir_path = reconst_flow.last_generated_outputs['out_peaks_dir']
        peaks_dir_data = nib.load(peaks_dir_path).get_data()
        assert_true(peaks_dir_data.shape[-1] == 15)
        assert_true(peaks_dir_data.shape[:-1] == volume.shape[:-1])

        peaks_idx_path = \
            reconst_flow.last_generated_outputs['out_peaks_indices']
        peaks_idx_data = nib.load(peaks_idx_path).get_data()
        assert_true(peaks_idx_data.shape[-1] == 5)
        assert_true(peaks_idx_data.shape[:-1] == volume.shape[:-1])

        peaks_vals_path = \
            reconst_flow.last_generated_outputs['out_peaks_values']
        peaks_vals_data = nib.load(peaks_vals_path).get_data()
        assert_true(peaks_vals_data.shape[-1] == 5)
        assert_true(peaks_vals_data.shape[:-1] == volume.shape[:-1])

        shm_path = reconst_flow.last_generated_outputs['out_shm']
        shm_data = nib.load(shm_path).get_data()
        assert_true(shm_data.shape[-1] == 45)
        assert_true(shm_data.shape[:-1] == volume.shape[:-1])

        pam = load_peaks(reconst_flow.last_generated_outputs['out_pam'])
        npt.assert_allclose(pam.peak_dirs.reshape(peaks_dir_data.shape),
                            peaks_dir_data)
        npt.assert_allclose(pam.peak_values, peaks_vals_data)
        npt.assert_allclose(pam.peak_indices, peaks_idx_data)
        npt.assert_allclose(pam.shm_coeff, shm_data)
        npt.assert_allclose(pam.gfa, gfa_data)

if __name__ == '__main__':
    npt.run_module_suite()
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Authors: Fred Buchanan
License: Apache 2.0
Description: Integration tests for coconut
"""

from __future__ import print_function, absolute_import, unicode_literals, division

from coconut.root import *

import unittest
import sys
import os
import subprocess
import shutil
import functools

call = functools.partial(subprocess.check_call,stderr=sys.stdout.fileno())

class OldCocoTest(unittest.TestCase):

    def _compile_extras(self,extraCommands = []):
        src = os.path.join(self.src,"extras.coco")

        cmd = ["coconut"] + extraCommands + [src,self.bin]
        call(cmd)

    def _compile_runner(self,extraCommands = []):
        src = os.path.join(self.src,"src","runner.coco")

        cmd = ["coconut"] + extraCommands + [src,self.bin]
        call(cmd)

    def _compile_agnostic(self,extraCommands = []):
        src = os.path.join(self.src,"src","agnostic")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_2(self,extraCommands = []):
        src = os.path.join(self.src,"src","python2")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","2"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_3(self,extraCommands = []):
        src = os.path.join(self.src,"src","python3")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","3"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_35(self,extraCommands = []):
        src = os.path.join(self.src,"src","python35")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","35"] + extraCommands + [src,bin]
        call(cmd)

    def _run_source(self):
        call(["python",os.path.join(self.bin,"runner.py")])

    def _compile_source(self,agnosticTarget = None, stict = False, minify = False, line_numbers = False, keep_lines = False):

        agnosticCommands = []
        extraCommands = []
        if agnosticTarget != None:
            agnosticCommands += ["--target",agnosticTarget]
        if stict:
            extraCommands += ["--strict"]
        if line_numbers:
            extraCommands += ["--line-numbers"]
        if keep_lines:
            extraCommands += ["--keep-lines"]

        self._compile_runner(extraCommands+agnosticCommands)
        self._compile_agnostic(extraCommands+agnosticCommands)

        if sys.version_info >= (3,):
            self._compile_3(extraCommands)
        if sys.version_info >= (3,5):
            self._compile_35(extraCommands)
        if (sys.version_info >= (2,)) & (sys.version_info < (3,)):
            self._compile_2(extraCommands)

    def _clean(self):
        shutil.rmtree(self.bin)

    def setUp(self):
        self.src = os.path.join(os.path.abspath(os.path.dirname(__file__)),"src")
        self.bin = os.path.join(os.path.abspath(os.path.dirname(__file__)),"bin")

    def tearDown(self):
        try:
            self._clean()
        except:
            pass

    def test_normal(self):
        self._compile_source()
        self._run_source()
        self._clean()

    def test_strict(self):
        self._compile_source(stict = True)
        self._run_source()
        self._clean()

    def test_minify(self):
        self._compile_source(minify = True)
        self._run_source()
        self._clean()

    def test_line_numbers(self):
        self._compile_source(line_numbers = True)
        self._run_source()
        self._clean()

    def test_keep_lines(self):
        self._compile_source(keep_lines = True)
        self._run_source()
        self._clean()

    def test_version_target(self):
        version = "".join([str(sys.version_info.major),str(sys.version_info.minor)])
        self._compile_source(agnosticTarget = version)
        self._run_source()
        self._clean()

    def test_extra(self):
        self._compile_extras()
        call(["python",os.path.join(self.bin,"extras.py")])
        self._clean()

if __name__ == '__main__':
    unittest.main()<commit_msg>Removed extras tests. I'll put them back in when I can discover the problem. I think it has something to do with icoconut<commit_after>#!/usr/bin/env python
"""
Authors: Fred Buchanan
License: Apache 2.0
Description: Integration tests for coconut
"""

from __future__ import print_function, absolute_import, unicode_literals, division

from coconut.root import *

import unittest
import sys
import os
import subprocess
import shutil
import functools

call = functools.partial(subprocess.check_call,stderr=sys.stdout.fileno())

class OldCocoTest(unittest.TestCase):

    def _compile_extras(self,extraCommands = []):
        src = os.path.join(self.src,"extras.coco")

        cmd = ["coconut"] + extraCommands + [src,self.bin]
        call(cmd)

    def _compile_runner(self,extraCommands = []):
        src = os.path.join(self.src,"src","runner.coco")

        cmd = ["coconut"] + extraCommands + [src,self.bin]
        call(cmd)

    def _compile_agnostic(self,extraCommands = []):
        src = os.path.join(self.src,"src","agnostic")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_2(self,extraCommands = []):
        src = os.path.join(self.src,"src","python2")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","2"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_3(self,extraCommands = []):
        src = os.path.join(self.src,"src","python3")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","3"] + extraCommands + [src,bin]
        call(cmd)

    def _compile_35(self,extraCommands = []):
        src = os.path.join(self.src,"src","python35")
        bin = os.path.join(self.bin,"cocotest")

        cmd = ["coconut","--target","35"] + extraCommands + [src,bin]
        call(cmd)

    def _run_source(self):
        call(["python",os.path.join(self.bin,"runner.py")])

    def _compile_source(self,agnosticTarget = None, stict = False, minify = False, line_numbers = False, keep_lines = False):

        agnosticCommands = []
        extraCommands = []
        if agnosticTarget != None:
            agnosticCommands += ["--target",agnosticTarget]
        if stict:
            extraCommands += ["--strict"]
        if line_numbers:
            extraCommands += ["--line-numbers"]
        if keep_lines:
            extraCommands += ["--keep-lines"]

        self._compile_runner(extraCommands+agnosticCommands)
        self._compile_agnostic(extraCommands+agnosticCommands)

        if sys.version_info >= (3,):
            self._compile_3(extraCommands)
        if sys.version_info >= (3,5):
            self._compile_35(extraCommands)
        if (sys.version_info >= (2,)) & (sys.version_info < (3,)):
            self._compile_2(extraCommands)

    def _clean(self):
        shutil.rmtree(self.bin)

    def setUp(self):
        self.src = os.path.join(os.path.abspath(os.path.dirname(__file__)),"src")
        self.bin = os.path.join(os.path.abspath(os.path.dirname(__file__)),"bin")

    def tearDown(self):
        try:
            self._clean()
        except:
            pass

    def test_normal(self):
        self._compile_source()
        self._run_source()
        self._clean()

    def test_strict(self):
        self._compile_source(stict = True)
        self._run_source()
        self._clean()

    def test_minify(self):
        self._compile_source(minify = True)
        self._run_source()
        self._clean()

    def test_line_numbers(self):
        self._compile_source(line_numbers = True)
        self._run_source()
        self._clean()

    def test_keep_lines(self):
        self._compile_source(keep_lines = True)
        self._run_source()
        self._clean()

    def test_version_target(self):
        version = "".join([str(sys.version_info.major),str(sys.version_info.minor)])
        self._compile_source(agnosticTarget = version)
        self._run_source()
        self._clean()

    def test_extra(self):
        self._compile_extras()
        #call(["python",os.path.join(self.bin,"extras.py")])
        self._clean()

if __name__ == '__main__':
    unittest.main()<|endoftext|>


<commit_before>"""
Samba
"""
import contextlib
import logging
import sys

from teuthology import misc as teuthology
from ..orchestra import run
from ..orchestra.daemon import DaemonGroup

log = logging.getLogger(__name__)

def get_sambas(ctx, roles):
    """
    Scan for roles that are samba.  Yield the id of the the samba role
    (samba.0, samba.1...)  and the associated remote site

    :param ctx: Context
    :param roles: roles for this test (extracted from yaml files)
    """
    for role in roles:
        assert isinstance(role, basestring)
        PREFIX = 'samba.'
        assert role.startswith(PREFIX)
        id_ = role[len(PREFIX):]
        (remote,) = ctx.cluster.only(role).remotes.iterkeys()
        yield (id_, remote)

@contextlib.contextmanager
def task(ctx, config):
    """
    Setup samba smbd with ceph vfs module.  This task assumes the samba
    package has already been installed via the install task.

    The config is optional and defaults to starting samba on all nodes.
    If a config is given, it is expected to be a list of
    samba nodes to start smbd servers on.

    Example that starts smbd on all samba nodes::

        tasks:
        - install:
        - install:
            project: samba
            extra_packages: ['samba']
        - ceph:
        - samba:
        - interactive:

    Example that starts smbd on just one of the samba nodes and cifs on the other::

        tasks:
        - samba: [samba.0]
        - cifs: [samba.1]

    An optional backend can be specified, and requires a path which smbd will
    use as the backend storage location:

        roles:
            - [osd.0, osd.1, osd.2, mon.0, mon.1, mon.2, mds.a]
            - [client.0, samba.0]

        tasks:
        - ceph:
        - ceph-fuse: [client.0]
        - samba:
            samba.0:
              cephfuse: "{testdir}/mnt.0"

    This mounts ceph to {testdir}/mnt.0 using fuse, and starts smbd with
    a UNC of //localhost/cephfuse.  Access through that UNC will be on
    the ceph fuse mount point.

    If no arguments are specified in the samba
    role, the default behavior is to enable the ceph UNC //localhost/ceph
    and use the ceph vfs module as the smbd backend.

    :param ctx: Context
    :param config: Configuration
    """
    log.info("Setting up smbd with ceph vfs...")
    assert config is None or isinstance(config, list) or isinstance(config, dict), \
        "task samba got invalid config"

    if config is None:
        config = dict(('samba.{id}'.format(id=id_), None)
                  for id_ in teuthology.all_roles_of_type(ctx.cluster, 'samba'))
    elif isinstance(config, list):
        config = dict((name, None) for name in config)

    samba_servers = list(get_sambas(ctx=ctx, roles=config.keys()))

    testdir = teuthology.get_testdir(ctx)

    from teuthology.task.ceph import DaemonGroup
    if not hasattr(ctx, 'daemons'):
        ctx.daemons = DaemonGroup()

    for id_, remote in samba_servers:

        rolestr = "samba.{id_}".format(id_=id_)

        confextras = """vfs objects = ceph
  ceph:config_file = /etc/ceph/ceph.conf"""

        unc = "ceph"
        backend = "/"

        if config[rolestr] is not None:
            # verify that there's just one parameter in role
            if len(config[rolestr]) != 1:
                log.error("samba config for role samba.{id_} must have only one parameter".format(id_=id_))
                raise Exception('invalid config')
            confextras = ""
            (unc, backendstr) = config[rolestr].items()[0]
            backend = backendstr.format(testdir=testdir)

        # on first samba role, set ownership and permissions of ceph root
        # so that samba tests succeed
        if config[rolestr] is None and id_ == samba_servers[0][0]:
            remote.run(
                    args=[
                        'mkdir', '-p', '/tmp/cmnt', run.Raw('&&'),
                        'sudo', 'ceph-fuse', '/tmp/cmnt', run.Raw('&&'),
                        'sudo', 'chown', 'ubuntu:ubuntu', '/tmp/cmnt/', run.Raw('&&'),
                        'sudo', 'chmod', '1777', '/tmp/cmnt/', run.Raw('&&'),
                        'sudo', 'umount', '/tmp/cmnt/', run.Raw('&&'),
                        'rm', '-rf', '/tmp/cmnt',
                        ],
                    )
        else:
            remote.run(
                    args=[
                        'sudo', 'chown', 'ubuntu:ubuntu', backend, run.Raw('&&'),
                        'sudo', 'chmod', '1777', backend,
                        ],
                    )

        teuthology.sudo_write_file(remote, "/usr/local/samba/etc/smb.conf", """
[global]
  workgroup = WORKGROUP
  netbios name = DOMAIN

[{unc}]
  path = {backend}
  {extras}
  writeable = yes
  valid users = ubuntu
""".format(extras=confextras, unc=unc, backend=backend))

        # create ubuntu user
        remote.run(
            args=[
                'sudo', '/usr/local/samba/bin/smbpasswd', '-e', 'ubuntu',
                run.Raw('||'),
                'printf', run.Raw('"ubuntu\nubuntu\n"'),
                run.Raw('|'),
                'sudo', '/usr/local/samba/bin/smbpasswd', '-s', '-a', 'ubuntu'
            ])

        smbd_cmd = [
                'sudo',
                'daemon-helper',
                'kill',
                'nostdin',
                '/usr/local/samba/sbin/smbd',
                '-F',
                ]
        ctx.daemons.add_daemon(remote, 'smbd', id_,
                               args=smbd_cmd,
                               logger=log.getChild("smbd.{id_}".format(id_=id_)),
                               stdin=run.PIPE,
                               wait=False,
                               )

        # let smbd initialize, probably a better way...
        import time
        seconds_to_sleep = 100
        log.info('Sleeping for %s  seconds...' % seconds_to_sleep)
        time.sleep(seconds_to_sleep)
        log.info('Sleeping stopped...')

    try:
        yield
    finally:
        log.info('Stopping smbd processes...')
        exc_info = (None, None, None)
        for d in ctx.daemons.iter_daemons_of_role('smbd'):
            try:
                d.stop()
            except (run.CommandFailedError,
                    run.CommandCrashedError,
                    run.ConnectionLostError):
                exc_info = sys.exc_info()
                log.exception('Saw exception from %s.%s', d.role, d.id_)
        if exc_info != (None, None, None):
            raise exc_info[0], exc_info[1], exc_info[2]

        for id_, remote in samba_servers:
            remote.run(
                args=[
                    'sudo',
                    'rm', '-rf',
                    '/usr/local/samba/etc/smb.conf',
                    '/usr/local/samba/private/*',
                    '/usr/local/samba/var/run/',
                    '/usr/local/samba/var/locks',
                    '/usr/local/samba/var/lock',
                    ],
                )
            # make sure daemons are gone
            try:
                remote.run(
                    args=[
                        'while',
                        'sudo', 'killall', '-9', 'smbd',
                        run.Raw(';'),
                        'do', 'sleep', '1',
                        run.Raw(';'),
                        'done',
                        ],
                    )

                remote.run(
                    args=[
                        'sudo',
                        'lsof',
                        backend,
                        ],
                    )
                remote.run(
                    args=[
                        'sudo',
                        'fuser',
                        '-M',
                        backend,
                        ],
                    )
            except Exception:
                log.exception("Saw exception")
                pass
<commit_msg>Remove oddly-placed, broken import<commit_after>"""
Samba
"""
import contextlib
import logging
import sys
import time

from teuthology import misc as teuthology
from ..orchestra import run
from ..orchestra.daemon import DaemonGroup

log = logging.getLogger(__name__)

def get_sambas(ctx, roles):
    """
    Scan for roles that are samba.  Yield the id of the the samba role
    (samba.0, samba.1...)  and the associated remote site

    :param ctx: Context
    :param roles: roles for this test (extracted from yaml files)
    """
    for role in roles:
        assert isinstance(role, basestring)
        PREFIX = 'samba.'
        assert role.startswith(PREFIX)
        id_ = role[len(PREFIX):]
        (remote,) = ctx.cluster.only(role).remotes.iterkeys()
        yield (id_, remote)

@contextlib.contextmanager
def task(ctx, config):
    """
    Setup samba smbd with ceph vfs module.  This task assumes the samba
    package has already been installed via the install task.

    The config is optional and defaults to starting samba on all nodes.
    If a config is given, it is expected to be a list of
    samba nodes to start smbd servers on.

    Example that starts smbd on all samba nodes::

        tasks:
        - install:
        - install:
            project: samba
            extra_packages: ['samba']
        - ceph:
        - samba:
        - interactive:

    Example that starts smbd on just one of the samba nodes and cifs on the other::

        tasks:
        - samba: [samba.0]
        - cifs: [samba.1]

    An optional backend can be specified, and requires a path which smbd will
    use as the backend storage location:

        roles:
            - [osd.0, osd.1, osd.2, mon.0, mon.1, mon.2, mds.a]
            - [client.0, samba.0]

        tasks:
        - ceph:
        - ceph-fuse: [client.0]
        - samba:
            samba.0:
              cephfuse: "{testdir}/mnt.0"

    This mounts ceph to {testdir}/mnt.0 using fuse, and starts smbd with
    a UNC of //localhost/cephfuse.  Access through that UNC will be on
    the ceph fuse mount point.

    If no arguments are specified in the samba
    role, the default behavior is to enable the ceph UNC //localhost/ceph
    and use the ceph vfs module as the smbd backend.

    :param ctx: Context
    :param config: Configuration
    """
    log.info("Setting up smbd with ceph vfs...")
    assert config is None or isinstance(config, list) or isinstance(config, dict), \
        "task samba got invalid config"

    if config is None:
        config = dict(('samba.{id}'.format(id=id_), None)
                  for id_ in teuthology.all_roles_of_type(ctx.cluster, 'samba'))
    elif isinstance(config, list):
        config = dict((name, None) for name in config)

    samba_servers = list(get_sambas(ctx=ctx, roles=config.keys()))

    testdir = teuthology.get_testdir(ctx)

    if not hasattr(ctx, 'daemons'):
        ctx.daemons = DaemonGroup()

    for id_, remote in samba_servers:

        rolestr = "samba.{id_}".format(id_=id_)

        confextras = """vfs objects = ceph
  ceph:config_file = /etc/ceph/ceph.conf"""

        unc = "ceph"
        backend = "/"

        if config[rolestr] is not None:
            # verify that there's just one parameter in role
            if len(config[rolestr]) != 1:
                log.error("samba config for role samba.{id_} must have only one parameter".format(id_=id_))
                raise Exception('invalid config')
            confextras = ""
            (unc, backendstr) = config[rolestr].items()[0]
            backend = backendstr.format(testdir=testdir)

        # on first samba role, set ownership and permissions of ceph root
        # so that samba tests succeed
        if config[rolestr] is None and id_ == samba_servers[0][0]:
            remote.run(
                    args=[
                        'mkdir', '-p', '/tmp/cmnt', run.Raw('&&'),
                        'sudo', 'ceph-fuse', '/tmp/cmnt', run.Raw('&&'),
                        'sudo', 'chown', 'ubuntu:ubuntu', '/tmp/cmnt/', run.Raw('&&'),
                        'sudo', 'chmod', '1777', '/tmp/cmnt/', run.Raw('&&'),
                        'sudo', 'umount', '/tmp/cmnt/', run.Raw('&&'),
                        'rm', '-rf', '/tmp/cmnt',
                        ],
                    )
        else:
            remote.run(
                    args=[
                        'sudo', 'chown', 'ubuntu:ubuntu', backend, run.Raw('&&'),
                        'sudo', 'chmod', '1777', backend,
                        ],
                    )

        teuthology.sudo_write_file(remote, "/usr/local/samba/etc/smb.conf", """
[global]
  workgroup = WORKGROUP
  netbios name = DOMAIN

[{unc}]
  path = {backend}
  {extras}
  writeable = yes
  valid users = ubuntu
""".format(extras=confextras, unc=unc, backend=backend))

        # create ubuntu user
        remote.run(
            args=[
                'sudo', '/usr/local/samba/bin/smbpasswd', '-e', 'ubuntu',
                run.Raw('||'),
                'printf', run.Raw('"ubuntu\nubuntu\n"'),
                run.Raw('|'),
                'sudo', '/usr/local/samba/bin/smbpasswd', '-s', '-a', 'ubuntu'
            ])

        smbd_cmd = [
                'sudo',
                'daemon-helper',
                'kill',
                'nostdin',
                '/usr/local/samba/sbin/smbd',
                '-F',
                ]
        ctx.daemons.add_daemon(remote, 'smbd', id_,
                               args=smbd_cmd,
                               logger=log.getChild("smbd.{id_}".format(id_=id_)),
                               stdin=run.PIPE,
                               wait=False,
                               )

        # let smbd initialize, probably a better way...
        seconds_to_sleep = 100
        log.info('Sleeping for %s  seconds...' % seconds_to_sleep)
        time.sleep(seconds_to_sleep)
        log.info('Sleeping stopped...')

    try:
        yield
    finally:
        log.info('Stopping smbd processes...')
        exc_info = (None, None, None)
        for d in ctx.daemons.iter_daemons_of_role('smbd'):
            try:
                d.stop()
            except (run.CommandFailedError,
                    run.CommandCrashedError,
                    run.ConnectionLostError):
                exc_info = sys.exc_info()
                log.exception('Saw exception from %s.%s', d.role, d.id_)
        if exc_info != (None, None, None):
            raise exc_info[0], exc_info[1], exc_info[2]

        for id_, remote in samba_servers:
            remote.run(
                args=[
                    'sudo',
                    'rm', '-rf',
                    '/usr/local/samba/etc/smb.conf',
                    '/usr/local/samba/private/*',
                    '/usr/local/samba/var/run/',
                    '/usr/local/samba/var/locks',
                    '/usr/local/samba/var/lock',
                    ],
                )
            # make sure daemons are gone
            try:
                remote.run(
                    args=[
                        'while',
                        'sudo', 'killall', '-9', 'smbd',
                        run.Raw(';'),
                        'do', 'sleep', '1',
                        run.Raw(';'),
                        'done',
                        ],
                    )

                remote.run(
                    args=[
                        'sudo',
                        'lsof',
                        backend,
                        ],
                    )
                remote.run(
                    args=[
                        'sudo',
                        'fuser',
                        '-M',
                        backend,
                        ],
                    )
            except Exception:
                log.exception("Saw exception")
                pass
<|endoftext|>


<commit_before>"""
.. module:: skrf.time
========================================
time (:mod:`skrf.time`)
========================================

Time domain functions

.. autosummary::
   :toctree: generated/

   time_gate
   detect_span
   find_n_peaks
   indexes

"""
import scipy.signal

from .util import find_nearest_index
from scipy import signal
import numpy as npy
from numpy.fft import fft, rfft, fftshift, ifft, irfft, ifftshift
from scipy.ndimage import convolve1d
import warnings

# imports for type hinting
from typing import List, TYPE_CHECKING
if TYPE_CHECKING:
    from .network import Network

def indexes(y: npy.ndarray, thres: float = 0.3, min_dist: int = 1) -> npy.ndarray:
    """
    Peak detection routine.

    Finds the numeric index of the peaks in *y* by taking its first order difference. By using
    *thres* and *min_dist* parameters, it is possible to reduce the number of
    detected peaks. *y* must be signed.

    Parameters
    ----------
    y : ndarray (signed)
        1D amplitude data to search for peaks.
    thres : float between [0., 1.], optional
        Normalized threshold. Only the peaks with amplitude higher than the
        threshold will be detected. Default is 0.3
    min_dist : int, optional
        Minimum distance between each detected peak. The peak with the highest
        amplitude is preferred to satisfy this constraint. Default is 1

    Returns
    -------
    ndarray
        Array containing the numeric indexes of the peaks that were detected

    Notes
    -----
    This function was taken from peakutils-1.1.0
    http://pythonhosted.org/PeakUtils/index.html

    """
    #This function was taken from peakutils, and is covered
    # by the MIT license, included below:

    #The MIT License (MIT)

    #Copyright (c) 2014 Lucas Hermann Negri

    #Permission is hereby granted, free of charge, to any person obtaining a copy
    #of this software and associated documentation files (the "Software"), to deal
    #in the Software without restriction, including without limitation the rights
    #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    #copies of the Software, and to permit persons to whom the Software is
    #furnished to do so, subject to the following conditions:

    #The above copyright notice and this permission notice shall be included in
    #all copies or substantial portions of the Software.

    #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    #THE SOFTWARE.

    if isinstance(y, npy.ndarray) and npy.issubdtype(y.dtype, npy.unsignedinteger):
        raise ValueError("y must be signed")

    thres = thres * (npy.max(y) - npy.min(y)) + npy.min(y)
    min_dist = int(min_dist)

    # compute first order difference
    dy = npy.diff(y)

    # propagate left and right values successively to fill all plateau pixels (0-value)
    zeros, = npy.where(dy == 0)

    # check if the singal is totally flat
    if len(zeros) == len(y) - 1:
        return npy.array([])

    while len(zeros):
        # add pixels 2 by 2 to propagate left and right value onto the zero-value pixel
        zerosr = npy.hstack([dy[1:], 0.])
        zerosl = npy.hstack([0., dy[:-1]])

        # replace 0 with right value if non zero
        dy[zeros]=zerosr[zeros]
        zeros, = npy.where(dy == 0)

        # replace 0 with left value if non zero
        dy[zeros] = zerosl[zeros]
        zeros, = npy.where(dy == 0)

    # find the peaks by using the first order difference
    peaks = npy.where((npy.hstack([dy, 0.]) < 0.)
                     & (npy.hstack([0., dy]) > 0.)
                     & (y > thres))[0]

    # handle multiple peaks, respecting the minimum distance
    if peaks.size > 1 and min_dist > 1:
        highest = peaks[npy.argsort(y[peaks])][::-1]
        rem = npy.ones(y.size, dtype=bool)
        rem[peaks] = False

        for peak in highest:
            if not rem[peak]:
                sl = slice(max(0, peak - min_dist), peak + min_dist + 1)
                rem[sl] = True
                rem[peak] = False

        peaks = npy.arange(y.size)[~rem]

    return peaks

def find_n_peaks(x: npy.ndarray, n: int, thres: float = 0.9, **kwargs) -> List[int]:
    """
    Find a given number of peaks in a signal.

    Parameters
    ----------
    x : npy.ndarray
        signal
    n : int
        number of peaks to search for
    thres : float, optional
        threshold, default is 0.9
    **kwargs : optional keyword arguments passed to :func:`indexes`

    Returns
    -------
    peak_idxs : list of int
        List containing the numeric indexes of the peaks that were detected

    Raises
    ------
    ValueError
        If no peaks are found.
    """
    for dummy in range(10):

        idx = indexes(x, **kwargs)
        if len(idx) < n:
            thres *= .5

        else:
            peak_vals = sorted(x[idx], reverse=True)[:n]
            peak_idxs = [x.tolist().index(k) for k in peak_vals]

            return peak_idxs
    raise ValueError('Couldnt find %i peaks' % n)

def detect_span(ntwk: 'Network') -> float:
    """
    Detect the correct time-span between two largest peaks.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to get data from

    Returns
    -------
    span : float
    """
    x = ntwk.s_time_db.flatten()
    p1, p2 = find_n_peaks(x, n=2)
    # distance to nearest neighbor peak
    span = abs(ntwk.frequency.t_ns[p1]-ntwk.frequency.t_ns[p2])
    return span

time_lookup_dict = {
    "s": 1,
    "ms": 1e-3,
    "us": 1e-6,
    "µs": 1e-6,
    "ns": 1e-9,
    "ps": 1e-12
}

def time_gate(ntwk: 'Network', start: float = None, stop: float = None, center: float = None, span: float = None,
              mode: str = 'bandpass', window=('kaiser', 6),
              method: str ='fft', fft_window: str='hann', conv_mode: str='wrap', t_unit: str = "") -> 'Network':
    """
    Time-domain gating of one-port s-parameters with a window function from scipy.signal.windows.

    The gate can be defined with start/stop times, or by center/span. All times are in units of nanoseconds but
    can be changed using the `t_unit` parameter. The default unit will change to `s` in **scikit-rf** version 1.0.
    Common windows are:

    * ('kaiser', 6)
    * 6 # integers are interpreted as kaiser beta-values
    * 'hamming'
    * 'boxcar'  # a straight up rect

    If no parameters are passed this will try to auto-gate the largest
    peak.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to operate on
    start : number, or None
        start of time gate in t_unit.
    stop : number, or None
        stop of time gate in t_unit.
    center : number, or None
        center of time gate, in t_unit. If None, and span is given,
        the gate will be centered on the peak.
    span : number, or None
        span of time gate, in t_unit.  If None span will be half of the
        distance to the second tallest peak
    mode : ['bandpass', 'bandstop']
        mode of gate
    window : string, float, or tuple
        passed to `window` arg of `scipy.signal.get_window()`
    method : str
        Gating method. There are 3 option: 'convolution', 'fft', 'rfft'.

        With *'convolution'*, the time-domain gate gets transformed into frequency-domain using inverse FFT and the
        gating is then achieved by convolution with the frequency-domain data.

        With *'fft'* (default), the data gets transformed into time-domain using inverse FFT and the gating is achieved
        by multiplication with the time-domain gate. The gated time-domain signal is then transformed back into
        frequency-domain using inverse FFT. As only positive signal frequencies are considered for the inverse FFT
        (with or without a dc component), the resulting time-domain signal has the same number of samples as in the
        frequency-domain, but is complex-valued. This method is also know as *time-domain band-pass mode*.

        With *'rfft'*, the procedure is the same as with *'fft'*, but the inverse FFT uses a complex-conjugate copy of
        the positive signal frequencies for the negative frequencies (Hermitian frequency response). A dc sample is
        also required. The resulting time-domain signal is real-valued and has twice the number of samples, which gives
        an improved time resolution. This method is also known as *time-domain low-pass mode*.

    fft_window : str or tuple or None
        Frequency-domain window applied before the inverse FFT in case of the (R)FFT method.
        This parameter takes the same values as the `window` parameter.
        Example: `window='hann` (default), or `window=('kaiser', 5)`, or `window=None`.
        The window helps to remove artefacts such as time-domain sidelobes of the pulses, but it is a trade-off with
        the achievable pulse width. The window is removed when the gated time-domain signals is transformed back into
        frequency-domain.

    conv_mode : str
        Extension mode for the convolution (if selected) determining how the frequency-domain gate is extended beyond
        the boundaries. This has a large effect on the generation of gating artefacts due to boundary effects. The
        optimal mode depends on the data. See the parameter description of `scipy.ndimage.convolve1d` for the available
        options.

    t_unit : str
        Time unit for start, stop, center and span arguments, defaults to nanoseconds (ns).
        Possible values:
            's': seconds
            'ms': milliseconds
            'µs' or 'us': microseconds
            'ns': nanoseconds (default)
            'ps': picoseconds

    Note
    ----
    You cant gate things that are 'behind' strong reflections. This
    is due to the multiple reflections that occur.

    If you need to time-gate an N-port network, then you should
    gate each s-parameter independently.

    Returns
    -------
    ntwk : Network
        copy of ntwk with time-gated s-parameters

    .. warning::
        Depending on sharpness of the gate, the band edges may be
        inaccurate, due to properties of FFT. We do not re-normalize
        anything.
    """

    if ntwk.nports >1:
        raise ValueError('Time-gating only works on one-ports. Try passing `ntwk.s11` or `ntwk.s21`.')

    if t_unit == "":
        if not all([e is None for e in [start, stop, center, span]]):
            # Do not raise in autogate mode, where all parameters are None
            warnings.warn('''
                            Time unit not passed: currently uses 'ns' per default.
                            The future versions of scikit-rf will use 's' per default instead,
                            so it is recommended to specify explicitly the time unit
                            to obtain similar results with future versions.
                            ''',
                            DeprecationWarning, stacklevel=2)
        t_unit = 'ns'

    t_mult = time_lookup_dict[t_unit]

    if start is not None and stop is not None:
        start *= t_mult
        stop *= t_mult
        span = abs(stop-start)
        center = (stop+start)/2.

    else:
        if center is None:
            # they didnt provide center, so find the peak
            n = ntwk.s_time_mag.argmax()
            center = ntwk.frequency.t_ns[n]

        if span is None:
            span = detect_span(ntwk)

        center *= t_mult
        span *= t_mult
        start = center - span / 2.
        stop = center + span / 2.

    ntwk_gated = ntwk.copy()
    method = method.lower()
    n_fd = ntwk.frequency.npoints
    df = ntwk.frequency.step

    if method == 'convolution':
        # frequency-domain gating
        n_td = n_fd
        # create dummy-window
        window_fd = npy.ones(n_fd)

    elif method == 'fft':
        # time-domain band-pass mode
        n_td = n_fd
        if fft_window is not None:
            # create band-pass window (zero on both lower and upper limit, one at center)
            window_fd = signal.get_window(window, n_fd)
        else:
            # create dummy-window
            window_fd = npy.ones(n_fd)

    elif method == 'rfft':
        # time-domain low-pass mode
        if ntwk.f[0] > 0.0:
            # no dc point included
            warnings.warn('The network data to be gated does not contain the dc point (0 Hz). This is required for the '
                          'selected low-pass gating mode. Please consider to include the dc point if the results are '
                          'inaccurate, either by direct measurement of by extrapolation using '
                          'skrf.Network.extrapolate_to_dc().', UserWarning, stacklevel=2)
        n_td = 2 * n_fd - 1
        if fft_window is not None:
            # create low-pass window (one at lower limit at f=0, zero on upper limit)
            window_fd = signal.get_window(window, 2 * n_fd)
            window_fd = window_fd[n_fd:]
        else:
            # create dummy-window
            window_fd = npy.ones(n_fd)

    else:
        raise ValueError('Invalid parameter method=`{}`'.format(method))

    # apply frequency-domain window
    ntwk_gated.s[:, 0, 0] = ntwk_gated.s[:, 0, 0] * window_fd

    # create time vector
    t = npy.linspace(-0.5 / df, 0.5 / df, n_td)

    # find start/stop gate indices
    start_idx = find_nearest_index(t, start)
    stop_idx = find_nearest_index(t, stop)

    # create gating window
    window_width = abs(stop_idx - start_idx)
    window = signal.get_window(window, window_width)

    # create the gate by padding the window with zeros
    gate = npy.zeros_like(t)
    gate[start_idx:stop_idx] = window

    if method == 'convolution':
        # frequency-domain gating
        kernel = fftshift(fft(ifftshift(gate), norm='forward'))
        ntwk_gated.s[:, 0, 0] = convolve1d(ntwk_gated.s[:, 0, 0], kernel, mode=conv_mode)
    elif method == 'fft':
        # time-domain band-pass mode
        s_td = fftshift(ifft(ntwk_gated.s[:, 0, 0]))
        s_td_g = s_td * gate
        ntwk_gated.s[:, 0, 0] = fft(ifftshift(s_td_g))
    elif method == 'rfft':
        # time-domain low-pass mode
        s_td = fftshift(irfft(ntwk_gated.s[:, 0, 0], n=len(t)))
        s_td_g = s_td * gate
        ntwk_gated.s[:, 0, 0] = rfft(ifftshift(s_td_g))

    # remove frequency-domain window
    ntwk_gated.s[:, 0, 0] = ntwk_gated.s[:, 0, 0] / window_fd

    if mode == 'bandstop':
        ntwk_gated = ntwk - ntwk_gated
    elif mode == 'bandpass':
        pass
    else:
        raise ValueError('mode should be \'bandpass\' or \'bandstop\'')

    return ntwk_gated
<commit_msg>Add warning for detect span too<commit_after>"""
.. module:: skrf.time
========================================
time (:mod:`skrf.time`)
========================================

Time domain functions

.. autosummary::
   :toctree: generated/

   time_gate
   detect_span
   find_n_peaks
   indexes

"""
import scipy.signal

from .util import find_nearest_index
from scipy import signal
import numpy as npy
from numpy.fft import fft, rfft, fftshift, ifft, irfft, ifftshift
from scipy.ndimage import convolve1d
import warnings

# imports for type hinting
from typing import List, TYPE_CHECKING
if TYPE_CHECKING:
    from .network import Network

def indexes(y: npy.ndarray, thres: float = 0.3, min_dist: int = 1) -> npy.ndarray:
    """
    Peak detection routine.

    Finds the numeric index of the peaks in *y* by taking its first order difference. By using
    *thres* and *min_dist* parameters, it is possible to reduce the number of
    detected peaks. *y* must be signed.

    Parameters
    ----------
    y : ndarray (signed)
        1D amplitude data to search for peaks.
    thres : float between [0., 1.], optional
        Normalized threshold. Only the peaks with amplitude higher than the
        threshold will be detected. Default is 0.3
    min_dist : int, optional
        Minimum distance between each detected peak. The peak with the highest
        amplitude is preferred to satisfy this constraint. Default is 1

    Returns
    -------
    ndarray
        Array containing the numeric indexes of the peaks that were detected

    Notes
    -----
    This function was taken from peakutils-1.1.0
    http://pythonhosted.org/PeakUtils/index.html

    """
    #This function was taken from peakutils, and is covered
    # by the MIT license, included below:

    #The MIT License (MIT)

    #Copyright (c) 2014 Lucas Hermann Negri

    #Permission is hereby granted, free of charge, to any person obtaining a copy
    #of this software and associated documentation files (the "Software"), to deal
    #in the Software without restriction, including without limitation the rights
    #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    #copies of the Software, and to permit persons to whom the Software is
    #furnished to do so, subject to the following conditions:

    #The above copyright notice and this permission notice shall be included in
    #all copies or substantial portions of the Software.

    #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    #THE SOFTWARE.

    if isinstance(y, npy.ndarray) and npy.issubdtype(y.dtype, npy.unsignedinteger):
        raise ValueError("y must be signed")

    thres = thres * (npy.max(y) - npy.min(y)) + npy.min(y)
    min_dist = int(min_dist)

    # compute first order difference
    dy = npy.diff(y)

    # propagate left and right values successively to fill all plateau pixels (0-value)
    zeros, = npy.where(dy == 0)

    # check if the singal is totally flat
    if len(zeros) == len(y) - 1:
        return npy.array([])

    while len(zeros):
        # add pixels 2 by 2 to propagate left and right value onto the zero-value pixel
        zerosr = npy.hstack([dy[1:], 0.])
        zerosl = npy.hstack([0., dy[:-1]])

        # replace 0 with right value if non zero
        dy[zeros]=zerosr[zeros]
        zeros, = npy.where(dy == 0)

        # replace 0 with left value if non zero
        dy[zeros] = zerosl[zeros]
        zeros, = npy.where(dy == 0)

    # find the peaks by using the first order difference
    peaks = npy.where((npy.hstack([dy, 0.]) < 0.)
                     & (npy.hstack([0., dy]) > 0.)
                     & (y > thres))[0]

    # handle multiple peaks, respecting the minimum distance
    if peaks.size > 1 and min_dist > 1:
        highest = peaks[npy.argsort(y[peaks])][::-1]
        rem = npy.ones(y.size, dtype=bool)
        rem[peaks] = False

        for peak in highest:
            if not rem[peak]:
                sl = slice(max(0, peak - min_dist), peak + min_dist + 1)
                rem[sl] = True
                rem[peak] = False

        peaks = npy.arange(y.size)[~rem]

    return peaks

def find_n_peaks(x: npy.ndarray, n: int, thres: float = 0.9, **kwargs) -> List[int]:
    """
    Find a given number of peaks in a signal.

    Parameters
    ----------
    x : npy.ndarray
        signal
    n : int
        number of peaks to search for
    thres : float, optional
        threshold, default is 0.9
    **kwargs : optional keyword arguments passed to :func:`indexes`

    Returns
    -------
    peak_idxs : list of int
        List containing the numeric indexes of the peaks that were detected

    Raises
    ------
    ValueError
        If no peaks are found.
    """
    for dummy in range(10):

        idx = indexes(x, **kwargs)
        if len(idx) < n:
            thres *= .5

        else:
            peak_vals = sorted(x[idx], reverse=True)[:n]
            peak_idxs = [x.tolist().index(k) for k in peak_vals]

            return peak_idxs
    raise ValueError('Couldnt find %i peaks' % n)

time_lookup_dict = {
    "s": 1,
    "ms": 1e-3,
    "us": 1e-6,
    "µs": 1e-6,
    "ns": 1e-9,
    "ps": 1e-12
}

def detect_span(ntwk: 'Network', t_unit: str = "") -> float:
    """
    Detect the correct time-span between two largest peaks.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to get data from

    t_unit : str
        Time unit for start, stop, center and span arguments, defaults to nanoseconds (ns).
        Possible values:

            * 's': seconds
            * 'ms': milliseconds
            * 'µs' or 'us': microseconds
            * 'ns': nanoseconds (default)
            * 'ps': picoseconds

    Returns
    -------
    span : float in unit t_unit
    """

    if t_unit == "":
        # Do not raise in autogate mode, where all parameters are None
        warnings.warn('''
                        Time unit not passed: currently uses 'ns' per default.
                        The future versions of scikit-rf will use 's' per default instead,
                        so it is recommended to specify explicitly the time unit
                        to obtain similar results with future versions.
                        ''',
                        DeprecationWarning, stacklevel=2)
        t_unit = 'ns'

    x = ntwk.s_time_db.flatten()
    p1, p2 = find_n_peaks(x, n=2)
    print(p1, p2)
    # distance to nearest neighbor peak
    span = abs(ntwk.frequency.t[p1]-ntwk.frequency.t[p2])

    return span / time_lookup_dict[t_unit]

def time_gate(ntwk: 'Network', start: float = None, stop: float = None, center: float = None, span: float = None,
              mode: str = 'bandpass', window=('kaiser', 6),
              method: str ='fft', fft_window: str='hann', conv_mode: str='wrap', t_unit: str = "") -> 'Network':
    """
    Time-domain gating of one-port s-parameters with a window function from scipy.signal.windows.

    The gate can be defined with start/stop times, or by center/span. All times are in units of nanoseconds but
    can be changed using the `t_unit` parameter. The default unit will change to `s` in **scikit-rf** version 1.0.
    Common windows are:

    * ('kaiser', 6)
    * 6 # integers are interpreted as kaiser beta-values
    * 'hamming'
    * 'boxcar'  # a straight up rect

    If no parameters are passed this will try to auto-gate the largest
    peak.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to operate on
    start : number, or None
        start of time gate in t_unit.
    stop : number, or None
        stop of time gate in t_unit.
    center : number, or None
        center of time gate, in t_unit. If None, and span is given,
        the gate will be centered on the peak.
    span : number, or None
        span of time gate, in t_unit.  If None span will be half of the
        distance to the second tallest peak
    mode : ['bandpass', 'bandstop']
        mode of gate
    window : string, float, or tuple
        passed to `window` arg of `scipy.signal.get_window()`
    method : str
        Gating method. There are 3 option: 'convolution', 'fft', 'rfft'.

        With *'convolution'*, the time-domain gate gets transformed into frequency-domain using inverse FFT and the
        gating is then achieved by convolution with the frequency-domain data.

        With *'fft'* (default), the data gets transformed into time-domain using inverse FFT and the gating is achieved
        by multiplication with the time-domain gate. The gated time-domain signal is then transformed back into
        frequency-domain using inverse FFT. As only positive signal frequencies are considered for the inverse FFT
        (with or without a dc component), the resulting time-domain signal has the same number of samples as in the
        frequency-domain, but is complex-valued. This method is also know as *time-domain band-pass mode*.

        With *'rfft'*, the procedure is the same as with *'fft'*, but the inverse FFT uses a complex-conjugate copy of
        the positive signal frequencies for the negative frequencies (Hermitian frequency response). A dc sample is
        also required. The resulting time-domain signal is real-valued and has twice the number of samples, which gives
        an improved time resolution. This method is also known as *time-domain low-pass mode*.

    fft_window : str or tuple or None
        Frequency-domain window applied before the inverse FFT in case of the (R)FFT method.
        This parameter takes the same values as the `window` parameter.
        Example: `window='hann` (default), or `window=('kaiser', 5)`, or `window=None`.
        The window helps to remove artefacts such as time-domain sidelobes of the pulses, but it is a trade-off with
        the achievable pulse width. The window is removed when the gated time-domain signals is transformed back into
        frequency-domain.

    conv_mode : str
        Extension mode for the convolution (if selected) determining how the frequency-domain gate is extended beyond
        the boundaries. This has a large effect on the generation of gating artefacts due to boundary effects. The
        optimal mode depends on the data. See the parameter description of `scipy.ndimage.convolve1d` for the available
        options.

    t_unit : str
        Time unit for start, stop, center and span arguments, defaults to nanoseconds (ns).
        Possible values:

            * 's': seconds
            * 'ms': milliseconds
            * 'µs' or 'us': microseconds
            * 'ns': nanoseconds (default)
            * 'ps': picoseconds

    Note
    ----
    You cant gate things that are 'behind' strong reflections. This
    is due to the multiple reflections that occur.

    If you need to time-gate an N-port network, then you should
    gate each s-parameter independently.

    Returns
    -------
    ntwk : Network
        copy of ntwk with time-gated s-parameters

    .. warning::
        Depending on sharpness of the gate, the band edges may be
        inaccurate, due to properties of FFT. We do not re-normalize
        anything.
    """

    if ntwk.nports >1:
        raise ValueError('Time-gating only works on one-ports. Try passing `ntwk.s11` or `ntwk.s21`.')

    if t_unit == "":
        if not all([e is None for e in [start, stop, center, span]]):
            # Do not raise in autogate mode, where all parameters are None
            warnings.warn('''
                            Time unit not passed: currently uses 'ns' per default.
                            The future versions of scikit-rf will use 's' per default instead,
                            so it is recommended to specify explicitly the time unit
                            to obtain similar results with future versions.
                            ''',
                            DeprecationWarning, stacklevel=2)
        t_unit = 'ns'

    t_mult = time_lookup_dict[t_unit]

    if start is not None and stop is not None:
        start *= t_mult
        stop *= t_mult
        span = abs(stop-start)
        center = (stop+start)/2.

    else:
        if center is None:
            # they didnt provide center, so find the peak
            n = ntwk.s_time_mag.argmax()
            center = ntwk.frequency.t_ns[n]

        if span is None:
            span = detect_span(ntwk, t_unit='ns')

        center *= t_mult
        span *= t_mult
        start = center - span / 2.
        stop = center + span / 2.

    ntwk_gated = ntwk.copy()
    method = method.lower()
    n_fd = ntwk.frequency.npoints
    df = ntwk.frequency.step

    if method == 'convolution':
        # frequency-domain gating
        n_td = n_fd
        # create dummy-window
        window_fd = npy.ones(n_fd)

    elif method == 'fft':
        # time-domain band-pass mode
        n_td = n_fd
        if fft_window is not None:
            # create band-pass window (zero on both lower and upper limit, one at center)
            window_fd = signal.get_window(window, n_fd)
        else:
            # create dummy-window
            window_fd = npy.ones(n_fd)

    elif method == 'rfft':
        # time-domain low-pass mode
        if ntwk.f[0] > 0.0:
            # no dc point included
            warnings.warn('The network data to be gated does not contain the dc point (0 Hz). This is required for the '
                          'selected low-pass gating mode. Please consider to include the dc point if the results are '
                          'inaccurate, either by direct measurement of by extrapolation using '
                          'skrf.Network.extrapolate_to_dc().', UserWarning, stacklevel=2)
        n_td = 2 * n_fd - 1
        if fft_window is not None:
            # create low-pass window (one at lower limit at f=0, zero on upper limit)
            window_fd = signal.get_window(window, 2 * n_fd)
            window_fd = window_fd[n_fd:]
        else:
            # create dummy-window
            window_fd = npy.ones(n_fd)

    else:
        raise ValueError('Invalid parameter method=`{}`'.format(method))

    # apply frequency-domain window
    ntwk_gated.s[:, 0, 0] = ntwk_gated.s[:, 0, 0] * window_fd

    # create time vector
    t = npy.linspace(-0.5 / df, 0.5 / df, n_td)

    # find start/stop gate indices
    start_idx = find_nearest_index(t, start)
    stop_idx = find_nearest_index(t, stop)

    # create gating window
    window_width = abs(stop_idx - start_idx)
    window = signal.get_window(window, window_width)

    # create the gate by padding the window with zeros
    gate = npy.zeros_like(t)
    gate[start_idx:stop_idx] = window

    if method == 'convolution':
        # frequency-domain gating
        kernel = fftshift(fft(ifftshift(gate), norm='forward'))
        ntwk_gated.s[:, 0, 0] = convolve1d(ntwk_gated.s[:, 0, 0], kernel, mode=conv_mode)
    elif method == 'fft':
        # time-domain band-pass mode
        s_td = fftshift(ifft(ntwk_gated.s[:, 0, 0]))
        s_td_g = s_td * gate
        ntwk_gated.s[:, 0, 0] = fft(ifftshift(s_td_g))
    elif method == 'rfft':
        # time-domain low-pass mode
        s_td = fftshift(irfft(ntwk_gated.s[:, 0, 0], n=len(t)))
        s_td_g = s_td * gate
        ntwk_gated.s[:, 0, 0] = rfft(ifftshift(s_td_g))

    # remove frequency-domain window
    ntwk_gated.s[:, 0, 0] = ntwk_gated.s[:, 0, 0] / window_fd

    if mode == 'bandstop':
        ntwk_gated = ntwk - ntwk_gated
    elif mode == 'bandpass':
        pass
    else:
        raise ValueError('mode should be \'bandpass\' or \'bandstop\'')

    return ntwk_gated
<|endoftext|>


<commit_before>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<commit_msg>Fixed #8359 -- Add an admin log entry when creating a new user. Patch from evan_schulz.<commit_after>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                self.log_addition(request, new_user)
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<|endoftext|>


<commit_before># Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from . import WireframeView

from UM.i18n import i18nCatalog
i18n_catalog = i18nCatalog("uranium")

def getMetaData():
    return {
        "type": "view",
        "plugin": {
            "name": i18n_catalog.i18nc("@label", "Wireframe View"),
            "author": "Ultimaker",
            "version": "1.0",
            "description": i18n_catalog.i18nc("@info:whatsthis", "Provides a simple wireframe view"),
            "api": 2
        },
        "view": {
            "name": "Wireframe",
            "visible": False
        }
    }

def register(app):
    return { "view": WireframeView.WireframeView() }
<commit_msg>Add period at end of plug-in description<commit_after># Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from . import WireframeView

from UM.i18n import i18nCatalog
i18n_catalog = i18nCatalog("uranium")

def getMetaData():
    return {
        "type": "view",
        "plugin": {
            "name": i18n_catalog.i18nc("@label", "Wireframe View"),
            "author": "Ultimaker",
            "version": "1.0",
            "description": i18n_catalog.i18nc("@info:whatsthis", "Provides a simple wireframe view."),
            "api": 2
        },
        "view": {
            "name": "Wireframe",
            "visible": False
        }
    }

def register(app):
    return { "view": WireframeView.WireframeView() }
<|endoftext|>


<commit_before>from django.core import signals
from django.dispatch import dispatcher
from django import http
import sys

class BaseHandler(object):
    def __init__(self):
        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None

    def load_middleware(self):
        """
        Populate middleware lists from settings.MIDDLEWARE_CLASSES.

        Must be called after the environment is fixed (see __call__).
        """
        from django.conf import settings
        from django.core import exceptions
        self._request_middleware = []
        self._view_middleware = []
        self._response_middleware = []
        self._exception_middleware = []
        for middleware_path in settings.MIDDLEWARE_CLASSES:
            try:
                dot = middleware_path.rindex('.')
            except ValueError:
                raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path
            mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
            try:
                mod = __import__(mw_module, {}, {}, [''])
            except ImportError, e:
                raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
            try:
                mw_class = getattr(mod, mw_classname)
            except AttributeError:
                raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)

            try:
                mw_instance = mw_class()
            except exceptions.MiddlewareNotUsed:
                continue

            if hasattr(mw_instance, 'process_request'):
                self._request_middleware.append(mw_instance.process_request)
            if hasattr(mw_instance, 'process_view'):
                self._view_middleware.append(mw_instance.process_view)
            if hasattr(mw_instance, 'process_response'):
                self._response_middleware.insert(0, mw_instance.process_response)
            if hasattr(mw_instance, 'process_exception'):
                self._exception_middleware.insert(0, mw_instance.process_exception)

    def get_response(self, request):
        "Returns an HttpResponse object for the given HttpRequest"
        from django.core import exceptions, urlresolvers
        from django.core.mail import mail_admins
        from django.conf import settings

        # Apply request middleware
        for middleware_method in self._request_middleware:
            response = middleware_method(request)
            if response:
                return response

        resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
        try:
            callback, callback_args, callback_kwargs = resolver.resolve(request.path)

            # Apply view middleware
            for middleware_method in self._view_middleware:
                response = middleware_method(request, callback, callback_args, callback_kwargs)
                if response:
                    return response

            try:
                response = callback(request, *callback_args, **callback_kwargs)
            except Exception, e:
                # If the view raised an exception, run it through exception
                # middleware, and if the exception middleware returns a
                # response, use that. Otherwise, reraise the exception.
                for middleware_method in self._exception_middleware:
                    response = middleware_method(request, e)
                    if response:
                        return response
                raise

            # Complain if the view returned None (a common error).
            if response is None:
                raise ValueError, "The view %s.%s didn't return an HttpResponse object." % (callback.__module__, callback.func_name)

            return response
        except http.Http404, e:
            if settings.DEBUG:
                from django.views import debug
                return debug.technical_404_response(request, e)
            else:
                callback, param_dict = resolver.resolve404()
                return callback(request, **param_dict)
        except exceptions.PermissionDenied:
            return http.HttpResponseForbidden('
Permission denied
')
        except SystemExit:
            pass # See http://code.djangoproject.com/ticket/1023
        except: # Handle everything else, including SuspiciousOperation, etc.
            if settings.DEBUG:
                from django.views import debug
                return debug.technical_500_response(request, *sys.exc_info())
            else:
                # Get the exception info now, in case another exception is thrown later.
                exc_info = sys.exc_info()
                receivers = dispatcher.send(signal=signals.got_request_exception)
                # When DEBUG is False, send an error message to the admins.
                subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
                try:
                    request_repr = repr(request)
                except:
                    request_repr = "Request repr() unavailable"
                message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
                mail_admins(subject, message, fail_silently=True)
                # Return an HttpResponse that displays a friendly error message.
                callback, param_dict = resolver.resolve500()
                return callback(request, **param_dict)

    def _get_traceback(self, exc_info=None):
        "Helper function to return the traceback as a string"
        import traceback
        return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))
<commit_msg>Fixed small bug in 'The view ____ didn't return an HttpResponse object' message -- it assumed the view was a function, whereas it can be any callable object<commit_after>from django.core import signals
from django.dispatch import dispatcher
from django import http
import sys

class BaseHandler(object):
    def __init__(self):
        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None

    def load_middleware(self):
        """
        Populate middleware lists from settings.MIDDLEWARE_CLASSES.

        Must be called after the environment is fixed (see __call__).
        """
        from django.conf import settings
        from django.core import exceptions
        self._request_middleware = []
        self._view_middleware = []
        self._response_middleware = []
        self._exception_middleware = []
        for middleware_path in settings.MIDDLEWARE_CLASSES:
            try:
                dot = middleware_path.rindex('.')
            except ValueError:
                raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path
            mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
            try:
                mod = __import__(mw_module, {}, {}, [''])
            except ImportError, e:
                raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
            try:
                mw_class = getattr(mod, mw_classname)
            except AttributeError:
                raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)

            try:
                mw_instance = mw_class()
            except exceptions.MiddlewareNotUsed:
                continue

            if hasattr(mw_instance, 'process_request'):
                self._request_middleware.append(mw_instance.process_request)
            if hasattr(mw_instance, 'process_view'):
                self._view_middleware.append(mw_instance.process_view)
            if hasattr(mw_instance, 'process_response'):
                self._response_middleware.insert(0, mw_instance.process_response)
            if hasattr(mw_instance, 'process_exception'):
                self._exception_middleware.insert(0, mw_instance.process_exception)

    def get_response(self, request):
        "Returns an HttpResponse object for the given HttpRequest"
        from django.core import exceptions, urlresolvers
        from django.core.mail import mail_admins
        from django.conf import settings

        # Apply request middleware
        for middleware_method in self._request_middleware:
            response = middleware_method(request)
            if response:
                return response

        resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
        try:
            callback, callback_args, callback_kwargs = resolver.resolve(request.path)

            # Apply view middleware
            for middleware_method in self._view_middleware:
                response = middleware_method(request, callback, callback_args, callback_kwargs)
                if response:
                    return response

            try:
                response = callback(request, *callback_args, **callback_kwargs)
            except Exception, e:
                # If the view raised an exception, run it through exception
                # middleware, and if the exception middleware returns a
                # response, use that. Otherwise, reraise the exception.
                for middleware_method in self._exception_middleware:
                    response = middleware_method(request, e)
                    if response:
                        return response
                raise

            # Complain if the view returned None (a common error).
            if response is None:
                try:
                    view_name = callback.func_name # If it's a function
                except AttributeError:
                    view_name = callback.__class__.__name__ + '.__call__' # If it's a class
                raise ValueError, "The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)

            return response
        except http.Http404, e:
            if settings.DEBUG:
                from django.views import debug
                return debug.technical_404_response(request, e)
            else:
                callback, param_dict = resolver.resolve404()
                return callback(request, **param_dict)
        except exceptions.PermissionDenied:
            return http.HttpResponseForbidden('
Permission denied
')
        except SystemExit:
            pass # See http://code.djangoproject.com/ticket/1023
        except: # Handle everything else, including SuspiciousOperation, etc.
            if settings.DEBUG:
                from django.views import debug
                return debug.technical_500_response(request, *sys.exc_info())
            else:
                # Get the exception info now, in case another exception is thrown later.
                exc_info = sys.exc_info()
                receivers = dispatcher.send(signal=signals.got_request_exception)
                # When DEBUG is False, send an error message to the admins.
                subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
                try:
                    request_repr = repr(request)
                except:
                    request_repr = "Request repr() unavailable"
                message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
                mail_admins(subject, message, fail_silently=True)
                # Return an HttpResponse that displays a friendly error message.
                callback, param_dict = resolver.resolve500()
                return callback(request, **param_dict)

    def _get_traceback(self, exc_info=None):
        "Helper function to return the traceback as a string"
        import traceback
        return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))
<|endoftext|>