<commit_before>"""\
Package resource API
--------------------

A resource is a logical file contained within a package, or a logical
subdirectory thereof.  The package resource API expects resource names
to have their path parts separated with ``/``, *not* whatever the local
path separator is.  Do not use os.path operations to manipulate resource
names being passed into the API.

The package resource API is designed to work with normal filesystem packages,
.egg files, and unpacked .egg files.  It can also work in a limited way with
.zip files and with custom PEP 302 loaders that support the ``get_data()``
method.
"""

import sys
import os
import time
import zipimport
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

__all__ = [
    'register_loader_type', 'get_provider', 'IResourceProvider',
    'ResourceManager', 'iter_distributions', 'require', 'resource_string',
    'resource_stream', 'resource_filename', 'set_extraction_path',
    'cleanup_resources', # 'glob_resources'
]

_provider_factories = {}

def register_loader_type(loader_type, provider_factory):
    """Register `provider_factory` to make providers for `loader_type`

    `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
    and `provider_factory` is a function that, passed a *module* object,
    returns an ``IResourceProvider`` for that module.
    """
    _provider_factories[loader_type] = provider_factory

def get_provider(moduleName):
    """Return an IResourceProvider for the named module"""
    module = sys.modules[moduleName]
    loader = getattr(module, '__loader__', None)
    return _find_adapter(_provider_factories, loader)(module)

class IResourceProvider:
    """An object that provides access to package resources"""

    def get_resource_filename(manager, resource_name):
        """Return a true filesystem path for `resource_name`

        `manager` must be an ``IResourceManager``"""

    def get_resource_stream(manager, resource_name):
        """Return a readable file-like object for `resource_name`

        `manager` must be an ``IResourceManager``"""

    def get_resource_string(manager, resource_name):
        """Return a string containing the contents of `resource_name`

        `manager` must be an ``IResourceManager``"""

    def has_resource(resource_name):
        """Does the package contain the named resource?"""

    def has_metadata(name):
        """Does the package's distribution contain the named metadata?"""

    def get_metadata(name):
        """The named metadata resource as a string"""

    def get_metadata_lines(name):
        """The named metadata resource as a filtered iterator of stripped lines"""

    # XXX list_resources?  glob_resources?

class ResourceManager:
    """Manage resource extraction and packages"""

    extraction_path = None

    def __init__(self):
        self.cached_files = []

    def resource_exists(self, package_name, resource_name):
        """Does the named resource exist in the named package?"""
        return get_provider(package_name).has_resource(self, resource_name)

    def resource_filename(self, package_name, resource_name):
        """Return a true filesystem path for specified resource"""
        return get_provider(package_name).get_resource_filename(self, resource_name)

    def resource_stream(self, package_name, resource_name):
        """Return a readable file-like object for specified resource"""
        return get_provider(package_name).get_resource_stream(self, resource_name)

    def resource_string(self, package_name, resource_name):
        """Return specified resource as a string"""
        return get_provider(package_name).get_resource_string(self, resource_name)

    def get_cache_path(self, archive_name, names=()):
        """Return absolute location in cache for `archive_name` and `names`

        The parent directory of the resulting path will be created if it does
        not already exist.  `archive_name` should be the base filename of the
        enclosing egg (which may not be the name of the enclosing zipfile!),
        including the ".egg" extension.  `names`, if provided, should be a
        sequence of path name parts "under" the egg's extraction location.

        This method should only be called by resource providers that need to
        obtain an extraction location, and only for names they intend to
        extract, as it tracks the generated names for possible cleanup later.
        """
        extract_path = self.extraction_path
        extract_path = extract_path or os.path.expanduser('~/.python-eggs')
        target_path = os.path.join(extract_path, archive_name, *names)
        _ensure_directory(target_path)
        self.cached_files.append(target_path)
        return target_path

    def require(self, requirement, path=None):
        """Ensure a distribution matching `requirement` is on ``sys.path``

        The `requirement` and `path` arguments are the same as for
        the ``iter_distributions()`` method, but `requirement` is not optional
        for `require`, since you must specify the desired distribution name.
        """
        for dist in self.iter_distributions(requirement, path):
            dist.require()
            return
        else:
            pass
            #raise ImportError(
            #    "No distributions found matching " + repr(requirement)
            #)

    # XXX Not yet implemented

    def postprocess(self, filename):
        """Perform any platform-specific postprocessing of file `filename`

        This is where Mac header rewrites should be done; other platforms don't
        have anything special they should do.

        Resource providers should call this method after successfully
        extracting a compressed resource.  They should not call it on resources
        that are already in the filesystem.
        """
        # XXX
        # print "postprocessing", filename

    def iter_distributions(self, requirement=None, path=None):
        """Iterate over distributions in `path` matching `requirement`

        The `path` is a sequence of ``sys.path`` items.  If not supplied,
        ``sys.path`` is used.

        The `requirement` is an optional string specifying the name of the
        desired distribution.
        """
        # XXX
        return ()

    def set_extraction_path(self, path):
        """Set the base path where resources will be extracted to, if needed.

        If not set, this defaults to ``os.expanduser("~/.python-eggs")``.
        Resources are extracted to subdirectories of this path based upon
        information given by the ``IResourceProvider``.  You may set this to a
        temporary directory, but then you must call ``cleanup_resources()`` to
        delete the extracted files when done.  There is no guarantee that
        ``cleanup_resources()`` will be able to remove all extracted files.

        (Note: you may not change the extraction path for a given resource
        manager once resources have been extracted, unless you first call
        ``cleanup_resources()``.)
        """
        if self.cached_files:
            raise ValueError(
                "Can't change extraction path, files already extracted"
            )

        self.extraction_path = path

    def cleanup_resources(self, force=False):
        """
        Delete all extracted resource files and directories, returning a list
        of the file and directory names that could not be successfully removed.
        This function does not have any concurrency protection, so it should
        generally only be called when the extraction path is a temporary
        directory exclusive to a single process.  This method is not
        automatically called; you must call it explicitly or register it as an
        ``atexit`` function if you wish to ensure cleanup of a temporary
        directory used for extractions.
        """
        # XXX
        pass

class DefaultProvider:
    """Provides access to package resources in the filesystem"""

    egg_info = None

    def __init__(self, module):
        self.module = module
        self.loader = getattr(module, '__loader__',None)
        self.module_path = os.path.dirname(module.__file__)

    def get_resource_filename(self, manager, resource_name):
        return self._fn(resource_name)

    def get_resource_stream(self, manager, resource_name):
        return open(self._fn(resource_name), 'rb')

    def get_resource_string(self, manager, resource_name):
        return self._get(self._fn(resource_name))

    def has_resource(self, resource_name):
        return self._has(self._fn(resource_name))

    def has_metadata(self, name):
        if not self.egg_info:
            raise NotImplementedError("Only .egg supports metadata")
        return self._has(os.path.join(self.egg_info, *name.split('/')))

    def get_metadata(self, name):
        if not self.egg_info:
            raise NotImplementedError("Only .egg supports metadata")
        return self._get(os.path.join(self.egg_info, *name.split('/')))

    def get_metadata_lines(self, name):
        for line in self.get_metadata(name).splitlines():
            line = line.strip()
            if line:
                yield line

    def _has(self, path):
        return os.path.exists(path)

    def _get(self, path):
        stream = open(path, 'rb')
        try:
            return stream.read()
        finally:
            stream.close()

    def _fn(self, resource_name):
        return os.path.join(self.module_path, *resource_name.split('/'))

register_loader_type(type(None), DefaultProvider)

class NullProvider(DefaultProvider):
    """Try to implement resource support for arbitrary PEP 302 loaders"""

    def _has(self, path):
        raise NotImplementedError(
            "Can't perform this operation for unregistered loader type"
        )

    def _get(self, path):
        get_data = getattr(self.loader, 'get_data', None)
        if get_data is None:
            raise NotImplementedError(
                "Can't perform this operation for loaders without 'get_data()'"
            )
        return get_data(path)

register_loader_type(object, NullProvider)

class ZipProvider(DefaultProvider):
    """Resource support for zips and eggs"""

    egg_name = None
    eagers = None

    def __init__(self, module):
        self.module = module
        self.loader = module.__loader__
        self.zipinfo = zipimport._zip_directory_cache[self.loader.archive]
        self.zip_pre = self.loader.archive + os.sep

        path = self.module_path = os.path.dirname(module.__file__)
        old = None
        self.prefix = []
        while path != old:
            if path.lower().endswith('.egg'):
                self.egg_name = os.path.basename(path)
                self.egg_info = os.path.join(path, 'EGG-INFO')
                break
            old = path
            path, base = os.path.split(path)
            self.prefix.append(base)

    def _short_name(self, path):
        if path.startswith(self.zip_pre):
            return path[len(self.zip_pre):]
        return path

    def _has(self, path):
        return self._short_name(path) in self.zipinfo

    def _get(self, path):
        return self.loader.get_data(path)

    def get_resource_stream(self, manager, resource_name):
        return StringIO(self.get_resource_string(manager, resource_name))

    def _extract_resource(self, manager, resource_name):
        parts = resource_name.split('/')
        zip_path = os.path.join(self.module_path, *parts)
        zip_stat = self.zipinfo[os.path.join(*(self.prefix + parts))]
        t, d, size = zip_stat[5], zip_stat[6], zip_stat[3]
        date_time = (
            (d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F,
            (t & 0xFFFF) >> 11, (t >> 5) & 0x3F,
            (t & 0x1F) * 2, 0, 0, -1
        )
        timestamp = time.mktime(date_time)
        real_path = manager.get_cache_path(self.egg_name, self.prefix+parts)

        if os.path.isfile(real_path):
            stat = os.stat(real_path)
            if stat.st_size == size and stat.st_mtime == timestamp:
                # size and stamp match, don't bother extracting
                return real_path

        # print "extracting", zip_path

        data = self.loader.get_data(zip_path)
        open(real_path, 'wb').write(data)
        os.utime(real_path, (timestamp, timestamp))
        manager.postprocess(real_path)
        return real_path

    def _get_eager_resources(self):
        if self.eagers is None:
            eagers = []
            for name in ('native_libs.txt', 'eager_resources.txt'):
                if self.has_metadata(name):
                    eagers.extend(self.get_metadata_lines(name))
            self.eagers = eagers
        return self.eagers

    def get_resource_filename(self, manager, resource_name):
        if not self.egg_name:
            raise NotImplementedError(
                "resource_filename() only supported for .egg, not .zip"
            )

        # should lock for extraction here
        eagers = self._get_eager_resources()
        if resource_name in eagers:
            for name in eagers:
                self._extract_resource(manager, name)

        return self._extract_resource(manager, resource_name)

register_loader_type(zipimport.zipimporter, ZipProvider)

def _get_mro(cls):
    """Get an mro for a type or classic class"""
    if not isinstance(cls, type):
        cls = type('', (cls, object), {})
        return cls.__mro__[1:]
    return cls.__mro__

def _find_adapter(registry, ob):
    """Return an adapter factory for `ob` from `registry`"""
    for t in _get_mro(getattr(ob, '__class__', type(ob))):
        if t in registry:
            return registry[t]

def _ensure_directory(path):
    dirname = os.path.dirname(path)
    if not os.path.isdir(dirname):
        os.makedirs(dirname)

# Set up global resource manager

_manager = ResourceManager()

def _initialize(g):
    for name in dir(_manager):
        if not name.startswith('_'):
            g[name] = getattr(_manager, name)
_initialize(globals())
<commit_msg>Specify and implement # comments in get_metadata_lines A few more PEP 8 formatting fixes for pkg_resources<commit_after>"""\
Package resource API
--------------------

A resource is a logical file contained within a package, or a logical
subdirectory thereof.  The package resource API expects resource names
to have their path parts separated with ``/``, *not* whatever the local
path separator is.  Do not use os.path operations to manipulate resource
names being passed into the API.

The package resource API is designed to work with normal filesystem packages,
.egg files, and unpacked .egg files.  It can also work in a limited way with
.zip files and with custom PEP 302 loaders that support the ``get_data()``
method.
"""

import sys
import os
import time
import zipimport
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

__all__ = [
    'register_loader_type', 'get_provider', 'IResourceProvider',
    'ResourceManager', 'iter_distributions', 'require', 'resource_string',
    'resource_stream', 'resource_filename', 'set_extraction_path',
    'cleanup_resources', # 'glob_resources'
]

_provider_factories = {}

def register_loader_type(loader_type, provider_factory):
    """Register `provider_factory` to make providers for `loader_type`

    `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
    and `provider_factory` is a function that, passed a *module* object,
    returns an ``IResourceProvider`` for that module.
    """
    _provider_factories[loader_type] = provider_factory

def get_provider(moduleName):
    """Return an IResourceProvider for the named module"""
    module = sys.modules[moduleName]
    loader = getattr(module, '__loader__', None)
    return _find_adapter(_provider_factories, loader)(module)

class IResourceProvider:
    """An object that provides access to package resources"""

    def get_resource_filename(manager, resource_name):
        """Return a true filesystem path for `resource_name`

        `manager` must be an ``IResourceManager``"""

    def get_resource_stream(manager, resource_name):
        """Return a readable file-like object for `resource_name`

        `manager` must be an ``IResourceManager``"""

    def get_resource_string(manager, resource_name):
        """Return a string containing the contents of `resource_name`

        `manager` must be an ``IResourceManager``"""

    def has_resource(resource_name):
        """Does the package contain the named resource?"""

    def has_metadata(name):
        """Does the package's distribution contain the named metadata?"""

    def get_metadata(name):
        """The named metadata resource as a string"""

    def get_metadata_lines(name):
        """The named metadata resource as a filtered iterator of
        stripped (of # comments and whitespace) lines
        """

    # XXX list_resources?  glob_resources?

class ResourceManager:
    """Manage resource extraction and packages"""

    extraction_path = None

    def __init__(self):
        self.cached_files = []

    def resource_exists(self, package_name, resource_name):
        """Does the named resource exist in the named package?"""
        return get_provider(package_name).has_resource(self, resource_name)

    def resource_filename(self, package_name, resource_name):
        """Return a true filesystem path for specified resource"""
        return get_provider(package_name).get_resource_filename(self,
            resource_name)

    def resource_stream(self, package_name, resource_name):
        """Return a readable file-like object for specified resource"""
        return get_provider(package_name).get_resource_stream(self,
            resource_name)

    def resource_string(self, package_name, resource_name):
        """Return specified resource as a string"""
        return get_provider(package_name).get_resource_string(self,
            resource_name)

    def get_cache_path(self, archive_name, names=()):
        """Return absolute location in cache for `archive_name` and `names`

        The parent directory of the resulting path will be created if it does
        not already exist.  `archive_name` should be the base filename of the
        enclosing egg (which may not be the name of the enclosing zipfile!),
        including the ".egg" extension.  `names`, if provided, should be a
        sequence of path name parts "under" the egg's extraction location.

        This method should only be called by resource providers that need to
        obtain an extraction location, and only for names they intend to
        extract, as it tracks the generated names for possible cleanup later.
        """
        extract_path = self.extraction_path
        extract_path = extract_path or os.path.expanduser('~/.python-eggs')
        target_path = os.path.join(extract_path, archive_name, *names)
        _ensure_directory(target_path)
        self.cached_files.append(target_path)
        return target_path

    def require(self, requirement, path=None):
        """Ensure a distribution matching `requirement` is on ``sys.path``

        The `requirement` and `path` arguments are the same as for
        the ``iter_distributions()`` method, but `requirement` is not optional
        for `require`, since you must specify the desired distribution name.
        """
        for dist in self.iter_distributions(requirement, path):
            dist.require()
            return
        else:
            pass
            #raise ImportError(
            #    "No distributions found matching " + repr(requirement)
            #)

    # XXX Not yet implemented

    def postprocess(self, filename):
        """Perform any platform-specific postprocessing of file `filename`

        This is where Mac header rewrites should be done; other platforms don't
        have anything special they should do.

        Resource providers should call this method after successfully
        extracting a compressed resource.  They should not call it on resources
        that are already in the filesystem.
        """
        # XXX
        # print "postprocessing", filename

    def iter_distributions(self, requirement=None, path=None):
        """Iterate over distributions in `path` matching `requirement`

        The `path` is a sequence of ``sys.path`` items.  If not supplied,
        ``sys.path`` is used.

        The `requirement` is an optional string specifying the name of the
        desired distribution.
        """
        # XXX
        return ()

    def set_extraction_path(self, path):
        """Set the base path where resources will be extracted to, if needed.

        If not set, this defaults to ``os.expanduser("~/.python-eggs")``.
        Resources are extracted to subdirectories of this path based upon
        information given by the ``IResourceProvider``.  You may set this to a
        temporary directory, but then you must call ``cleanup_resources()`` to
        delete the extracted files when done.  There is no guarantee that
        ``cleanup_resources()`` will be able to remove all extracted files.

        (Note: you may not change the extraction path for a given resource
        manager once resources have been extracted, unless you first call
        ``cleanup_resources()``.)
        """
        if self.cached_files:
            raise ValueError(
                "Can't change extraction path, files already extracted"
            )

        self.extraction_path = path

    def cleanup_resources(self, force=False):
        """
        Delete all extracted resource files and directories, returning a list
        of the file and directory names that could not be successfully removed.
        This function does not have any concurrency protection, so it should
        generally only be called when the extraction path is a temporary
        directory exclusive to a single process.  This method is not
        automatically called; you must call it explicitly or register it as an
        ``atexit`` function if you wish to ensure cleanup of a temporary
        directory used for extractions.
        """
        # XXX
        pass

class DefaultProvider:
    """Provides access to package resources in the filesystem"""

    egg_info = None

    def __init__(self, module):
        self.module = module
        self.loader = getattr(module, '__loader__', None)
        self.module_path = os.path.dirname(module.__file__)

    def get_resource_filename(self, manager, resource_name):
        return self._fn(resource_name)

    def get_resource_stream(self, manager, resource_name):
        return open(self._fn(resource_name), 'rb')

    def get_resource_string(self, manager, resource_name):
        return self._get(self._fn(resource_name))

    def has_resource(self, resource_name):
        return self._has(self._fn(resource_name))

    def has_metadata(self, name):
        if not self.egg_info:
            raise NotImplementedError("Only .egg supports metadata")
        return self._has(os.path.join(self.egg_info, *name.split('/')))

    def get_metadata(self, name):
        if not self.egg_info:
            raise NotImplementedError("Only .egg supports metadata")
        return self._get(os.path.join(self.egg_info, *name.split('/')))

    def get_metadata_lines(self, name):
        for line in self.get_metadata(name).splitlines():
            line = line.strip()
            if not line.startswith('#'):
                yield line

    def _has(self, path):
        return os.path.exists(path)

    def _get(self, path):
        stream = open(path, 'rb')
        try:
            return stream.read()
        finally:
            stream.close()

    def _fn(self, resource_name):
        return os.path.join(self.module_path, *resource_name.split('/'))

register_loader_type(type(None), DefaultProvider)

class NullProvider(DefaultProvider):
    """Try to implement resource support for arbitrary PEP 302 loaders"""

    def _has(self, path):
        raise NotImplementedError(
            "Can't perform this operation for unregistered loader type"
        )

    def _get(self, path):
        get_data = getattr(self.loader, 'get_data', None)
        if get_data is None:
            raise NotImplementedError(
                "Can't perform this operation for loaders without 'get_data()'"
            )
        return get_data(path)

register_loader_type(object, NullProvider)

class ZipProvider(DefaultProvider):
    """Resource support for zips and eggs"""

    egg_name = None
    eagers = None

    def __init__(self, module):
        self.module = module
        self.loader = module.__loader__
        self.zipinfo = zipimport._zip_directory_cache[self.loader.archive]
        self.zip_pre = self.loader.archive + os.sep

        path = self.module_path = os.path.dirname(module.__file__)
        old = None
        self.prefix = []
        while path != old:
            if path.lower().endswith('.egg'):
                self.egg_name = os.path.basename(path)
                self.egg_info = os.path.join(path, 'EGG-INFO')
                break
            old = path
            path, base = os.path.split(path)
            self.prefix.append(base)

    def _short_name(self, path):
        if path.startswith(self.zip_pre):
            return path[len(self.zip_pre):]
        return path

    def _has(self, path):
        return self._short_name(path) in self.zipinfo

    def _get(self, path):
        return self.loader.get_data(path)

    def get_resource_stream(self, manager, resource_name):
        return StringIO(self.get_resource_string(manager, resource_name))

    def _extract_resource(self, manager, resource_name):
        parts = resource_name.split('/')
        zip_path = os.path.join(self.module_path, *parts)
        zip_stat = self.zipinfo[os.path.join(*(self.prefix + parts))]
        t, d, size = zip_stat[5], zip_stat[6], zip_stat[3]
        date_time = (
            (d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F,
            (t & 0xFFFF) >> 11, (t >> 5) & 0x3F,
            (t & 0x1F) * 2, 0, 0, -1
        )
        timestamp = time.mktime(date_time)
        real_path = manager.get_cache_path(self.egg_name, self.prefix + parts)

        if os.path.isfile(real_path):
            stat = os.stat(real_path)
            if stat.st_size == size and stat.st_mtime == timestamp:
                # size and stamp match, don't bother extracting
                return real_path

        # print "extracting", zip_path

        data = self.loader.get_data(zip_path)
        open(real_path, 'wb').write(data)
        os.utime(real_path, (timestamp, timestamp))
        manager.postprocess(real_path)
        return real_path

    def _get_eager_resources(self):
        if self.eagers is None:
            eagers = []
            for name in ('native_libs.txt', 'eager_resources.txt'):
                if self.has_metadata(name):
                    eagers.extend(self.get_metadata_lines(name))
            self.eagers = eagers
        return self.eagers

    def get_resource_filename(self, manager, resource_name):
        if not self.egg_name:
            raise NotImplementedError(
                "resource_filename() only supported for .egg, not .zip"
            )

        # should lock for extraction here
        eagers = self._get_eager_resources()
        if resource_name in eagers:
            for name in eagers:
                self._extract_resource(manager, name)

        return self._extract_resource(manager, resource_name)

register_loader_type(zipimport.zipimporter, ZipProvider)

def _get_mro(cls):
    """Get an mro for a type or classic class"""
    if not isinstance(cls, type):
        cls = type('', (cls, object), {})
        return cls.__mro__[1:]
    return cls.__mro__

def _find_adapter(registry, ob):
    """Return an adapter factory for `ob` from `registry`"""
    for t in _get_mro(getattr(ob, '__class__', type(ob))):
        if t in registry:
            return registry[t]

def _ensure_directory(path):
    dirname = os.path.dirname(path)
    if not os.path.isdir(dirname):
        os.makedirs(dirname)

# Set up global resource manager

_manager = ResourceManager()

def _initialize(g):
    for name in dir(_manager):
        if not name.startswith('_'):
            g[name] = getattr(_manager, name)
_initialize(globals())
<|endoftext|>


<commit_before>#!/usr/bin/env python3
#
# usage: plash noroot CMD

import ctypes
import errno
import os
import sys
from getpass import getuser
from multiprocessing import Lock  # that takes way too long to load
from subprocess import CalledProcessError, check_call

from plash.utils import catch_and_die, die, get_plash_data

# I do believe this libc constants are stable.
CLONE_NEWNS = 0x00020000
CLONE_NEWUSER = 0x10000000
MS_REC = 0x4000
MS_PRIVATE = 1 << 18

# SystemExit because so the process dies if this is unhandled
class CouldNotSetupUnshareError(SystemExit):
    pass

def die_with_errno(hint):
    myerrno = ctypes.get_errno()
    errno_str = errno.errorcode.get(myerrno, myerrno)
    die('errno while {}: {}'.format(hint, errno_str))

def get_subs(query_user, subfile):
    'get subuids or subgids for a user'
    with open(subfile) as f:
        read = f.readline()
        user, start, count = read.split(':')
        if user == query_user:
            return int(start), int(count)
    die('The user {} does not havy any subuids or subgids, please add some'.
        format(query_user))

def unshare_if_user(extra_setup_cmd=None):

    if not os.getuid():
        return
    os.environ['PLASH_DATA'] = get_plash_data()
    uid_start, uid_count = get_subs(getuser(), '/etc/subuid')
    gid_start, gid_count = get_subs(getuser(), '/etc/subgid')

    setup_cmds = [[
        'newuidmap',
        str(os.getpid()), '0',
        str(os.getuid()), '1', '1',
        str(uid_start),
        str(uid_count)
    ], [
        'newgidmap',
        str(os.getpid()), '0',
        str(os.getgid()), '1', '1',
        str(gid_start),
        str(gid_count)
    ]]

    if extra_setup_cmd:
        setup_cmds.append(extra_setup_cmd)

    def prepare_unshared_proccess():
        for cmd in setup_cmds:
            with catch_and_die(
                [CalledProcessError, FileNotFoundError], debug='forked child'):
                check_call(cmd)

    # we need to call prepare_unshared_proccess
    # from outside of the unshared process
    lock = Lock()
    lock.acquire()
    child = os.fork()
    if not child:
        lock.acquire()
        prepare_unshared_proccess()
        sys.exit(0)
    # what the unshare binary does do
    libc = ctypes.CDLL('libc.so.6', use_errno=True)
    libc.unshare(CLONE_NEWNS
                 | CLONE_NEWUSER) != -1 or die_with_errno(hint='unsharing')
    libc.mount("none", "/", None, MS_REC | MS_PRIVATE,
               None) != -1 or die_with_errno(hint='mounting')

    lock.release()
    pid, raw_exit_status = os.wait()
    exit_status = raw_exit_status // 255
    if exit_status:
        raise CouldNotSetupUnshareError()

def unshare_if_root():
    if os.getuid():
        return
    libc = ctypes.CDLL('libc.so.6', use_errno=True)

    libc.unshare(CLONE_NEWNS) != -1 or die_with_errno(hint='unsharing')
    libc.mount("none", "/", None, MS_REC | MS_PRIVATE,
               None) != -1 or die_with_errno(hint='mounting')
<commit_msg>err msg in lower case<commit_after>#!/usr/bin/env python3
#
# usage: plash noroot CMD

import ctypes
import errno
import os
import sys
from getpass import getuser
from multiprocessing import Lock  # that takes way too long to load
from subprocess import CalledProcessError, check_call

from plash.utils import catch_and_die, die, get_plash_data

# I do believe this libc constants are stable.
CLONE_NEWNS = 0x00020000
CLONE_NEWUSER = 0x10000000
MS_REC = 0x4000
MS_PRIVATE = 1 << 18

# SystemExit because so the process dies if this is unhandled
class CouldNotSetupUnshareError(SystemExit):
    pass

def die_with_errno(hint):
    myerrno = ctypes.get_errno()
    errno_str = errno.errorcode.get(myerrno, myerrno)
    die('errno while {}: {}'.format(hint, errno_str))

def get_subs(query_user, subfile):
    'get subuids or subgids for a user'
    with open(subfile) as f:
        read = f.readline()
        user, start, count = read.split(':')
        if user == query_user:
            return int(start), int(count)
    die('the user {} does not havy any subuids or subgids, please add some'.
        format(query_user))

def unshare_if_user(extra_setup_cmd=None):

    if not os.getuid():
        return
    os.environ['PLASH_DATA'] = get_plash_data()
    uid_start, uid_count = get_subs(getuser(), '/etc/subuid')
    gid_start, gid_count = get_subs(getuser(), '/etc/subgid')

    setup_cmds = [[
        'newuidmap',
        str(os.getpid()), '0',
        str(os.getuid()), '1', '1',
        str(uid_start),
        str(uid_count)
    ], [
        'newgidmap',
        str(os.getpid()), '0',
        str(os.getgid()), '1', '1',
        str(gid_start),
        str(gid_count)
    ]]

    if extra_setup_cmd:
        setup_cmds.append(extra_setup_cmd)

    def prepare_unshared_proccess():
        for cmd in setup_cmds:
            with catch_and_die(
                [CalledProcessError, FileNotFoundError], debug='forked child'):
                check_call(cmd)

    # we need to call prepare_unshared_proccess
    # from outside of the unshared process
    lock = Lock()
    lock.acquire()
    child = os.fork()
    if not child:
        lock.acquire()
        prepare_unshared_proccess()
        sys.exit(0)
    # what the unshare binary does do
    libc = ctypes.CDLL('libc.so.6', use_errno=True)
    libc.unshare(CLONE_NEWNS
                 | CLONE_NEWUSER) != -1 or die_with_errno(hint='unsharing')
    libc.mount("none", "/", None, MS_REC | MS_PRIVATE,
               None) != -1 or die_with_errno(hint='mounting')

    lock.release()
    pid, raw_exit_status = os.wait()
    exit_status = raw_exit_status // 255
    if exit_status:
        raise CouldNotSetupUnshareError()

def unshare_if_root():
    if os.getuid():
        return
    libc = ctypes.CDLL('libc.so.6', use_errno=True)

    libc.unshare(CLONE_NEWNS) != -1 or die_with_errno(hint='unsharing')
    libc.mount("none", "/", None, MS_REC | MS_PRIVATE,
               None) != -1 or die_with_errno(hint='mounting')
<|endoftext|>


<commit_before>from pymel.all import mel as pm
from maya import OpenMayaMPx
from maya import OpenMaya
from maya import cmds
from maya import mel
import rush
import logging
import string
import random
import json
import sys
import os
import Qt

kPluginCmdName = "rush"
kVerboseFlag = "-v"
kVerboseLongFlag = "-verbose"

# Define mel procedure to call the previous function
mel.eval("""
global proc callLastCommand(string $function)
{
    repeatLast -ac $function -acl "blah-blah....";
}
""")

def setupLogger(verbose=False):
    """

    Args:
        verbose (bool): verbose mode

    Return:
        type: return value

    """
    if verbose:
        lv = logging.DEBUG
    else:
        lv = logging.CRITICAL

    tempLogName = 'Rush : ' + ''.join(
        random.SystemRandom().choice(
            string.ascii_uppercase + string.digits) for _ in range(10))

    logger = logging.getLogger(tempLogName)
    handler = logging.StreamHandler()
    logger.setLevel(lv)
    handler.setLevel(lv)
    logger.addHandler(handler)
    return logger

def loadStyle():
    """ Load stylesheet

    Return:
        contents(str): stylesheet info in string

    """
    mayaScriptDir = cmds.internalVar(userScriptDir=True)
    qss = os.path.normpath(
        os.path.join(mayaScriptDir, "rush", "style.qss"))
    f = file(qss, 'r')
    contents = f.read()
    f.close()
    return contents

def getMayaWindow():
    """ Return Maya's main window
    """

    for obj in Qt.QtWidgets.QApplication.topLevelWidgets():
        if obj.objectName() == 'MayaWindow':
            return obj
    raise RuntimeError('Could not find MayaWindow instance')

class CustomQLineEdit(Qt.QtWidgets.QLineEdit):
    """ Custom QLineEdit with custom events and signals"""

    escPressed = Qt.QtCore.Signal(str)
    tabPressed = Qt.QtCore.Signal(str)

    def __init__(self, parent=None):
        super(CustomQLineEdit, self).__init__(parent)
        self.setFocusPolicy(Qt.QtCore.Qt.StrongFocus)

    def focusOutEvent(self, event):
        # Emit signal to close the window when it gets out of focus
        self.escPressed.emit('esc')

    def keyPressEvent(self, event):
        if event.key() == Qt.QtCore.Qt.Key_Escape:
            self.escPressed.emit('esc')
        elif event.key() == Qt.QtCore.Qt.Key_Tab:
            self.tabPressed.emit('tab')
        else:
            super(CustomQLineEdit, self).keyPressEvent(event)

class Gui(rush.RushCommands, Qt.QtWidgets.QDialog):

    def closeExistingWindow(self):
        """ Close window if exists """

        for qt in Qt.QtWidgets.QApplication.topLevelWidgets():
            try:
                if qt.__class__.__name__ == self.__class__.__name__:
                    qt.close()
            except:
                pass

    def __init__(self, logger, parent=None):
        self.closeExistingWindow()
        super(Gui, self).__init__(parent)

        self.logger = logger

        self.setAttribute(Qt.QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("Rush")
        self.setWindowFlags(Qt.QtCore.Qt.Window)
        self.setWindowFlags(
            Qt.QtCore.Qt.Popup | Qt.QtCore.Qt.FramelessWindowHint)
        try:
            self.setWindowFlags(
                self.windowFlags() | Qt.QtCore.Qt.NoDropShadowWindowHint)
        except AttributeError:
            pass

        self.setAttribute(Qt.QtCore.Qt.WA_TranslucentBackground)

        self.setStyleSheet(loadStyle())

        # Create Data then UI
        self.createData()
        self.createUI()

    def createUI(self):
        self.LE = CustomQLineEdit(self)
        self.LE.setFixedWidth(200)
        self.layout = Qt.QtWidgets.QBoxLayout(
            Qt.QtWidgets.QBoxLayout.TopToBottom)
        self.layout.addWidget(self.LE)
        self.setLayout(self.layout)

        # Set up QCompleter
        self.completer = Qt.QtWidgets.QCompleter(self)
        self.completer.setCompletionMode(
            Qt.QtWidgets.QCompleter.UnfilteredPopupCompletion)
        self.completer.setModel(self.filteredModel)
        self.completer.setObjectName("commandCompleter")
        # self.completer.popup().setIconSize(self.iconSize)
        self.completer.popup().setStyleSheet(loadStyle())

        # Edit line Edit behavior
        self.LE.setCompleter(self.completer)
        self.LE.textEdited.connect(self.updateData)
        self.LE.returnPressed.connect(self.execute)
        self.LE.escPressed.connect(self.close)
        self.LE.tabPressed.connect(self.tabCompletion)
        self.LE.setFocus()

    def createData(self):
        """

        Return:
            QSortFilterProxyModel: data

        """

        model = Qt.QtGui.QStandardItemModel()

        # Load json files as dicrectory.
        # key is command name, and its item is icon path.
        mayaScriptDir = cmds.internalVar(userScriptDir=True)
        commandFile = os.path.normpath(
            os.path.join(mayaScriptDir, "rushCmds.json"))
        try:
            f = open(commandFile)
            jsonDict = json.load(f)
            f.close()
        except IOError:
            jsonDict = {}

        # Create a list of command names
        self.commands = [i for i in jsonDict]

        # Add all command names and icon paths to the the model(model)
        for num, command in enumerate(jsonDict):
            item = Qt.QtGui.QStandardItem(command)
            if os.path.isabs(jsonDict[command]) is True:
                iconPath = os.path.normpath(jsonDict[command])
                item.setIcon(Qt.QtGui.QIcon(iconPath))
            else:
                item.setIcon(Qt.QtGui.QIcon(":%s" % jsonDict[command]))
            model.setItem(num, 0, item)

        # Store the model(model) into the sortFilterProxy model
        self.filteredModel = Qt.QtCore.QSortFilterProxyModel(self)
        self.filteredModel.setFilterCaseSensitivity(
            Qt.QtCore.Qt.CaseInsensitive)
        self.filteredModel.setSourceModel(model)

    def updateData(self):
        """ Update current completion data

        Args:

        Return:

        """

        # command completer
        currentText = self.LE.text()
        if currentText == "":
            self.LE.setCompleter(self.completer)

        # Set commands to case insensitive
        regExp = Qt.QtCore.QRegExp(
            self.LE.text(),
            Qt.QtCore.Qt.CaseInsensitive,
            Qt.QtCore.QRegExp.RegExp)
        self.filteredModel.setFilterRegExp(regExp)

    def tabCompletion(self):
        text = self.LE.text().lower()
        currents = [i for i in self.commands if text in i.lower()]
        top = currents[0]
        self.LE.setText(top)

    def execute(self):
        cmd = self.LE.text()

        # Close gui first otherwise maya clashes(2017)
        self.close()

        try:
            f = getattr(self, "_%s" % cmd)
            f()
            self.logger.debug("Running command : %s" % cmd)

            # Add to repeatLast command so the comamnd can be repeatable
            # by G key
            pm.callLastCommand(
                """python(\"rush.RushCommands()._%s()\")""" % cmd)
        except AttributeError:
            pass

class Rush(OpenMayaMPx.MPxCommand):

    def __init__(self):
        super(Rush, self).__init__()

        self.verbose = False
        self.cmdArg = "Initial arg"

    def doIt(self, args):

        # Parse the arguments.
        argData = OpenMaya.MArgDatabase(self.syntax(), args)
        try:
            self.cmdArg = argData.commandArgumentString(0)
        except RuntimeError:
            pass
        if argData.isFlagSet(kVerboseFlag):
            self.verbose = argData.flagArgumentBool(kVerboseFlag, 0)

        logger = setupLogger(self.verbose)
        self.mw = Gui(logger, getMayaWindow())
        self.mw.show()

        pos = Qt.QtGui.QCursor.pos()
        self.mw.move(
            pos.x() - (self.mw.width() / 2),
            pos.y() - (self.mw.height() / 2))

        self.mw.raise_()
        self.mw.activateWindow()

    def undoIt(self):
        pass

    def redoIt(self):
        pass

    def isUndoable(self):
        return False

# Creator
def cmdCreator():
    # Create the command
    """

    Return:
        pointer to the command

    """
    ptr = OpenMayaMPx.asMPxPtr(Rush())
    return ptr

def syntaxCreator():
    """ Syntax creator

    Return:
        syntax (OpenMaya.MSyntax): return value

    """
    syntax = OpenMaya.MSyntax()
    syntax.addArg(OpenMaya.MSyntax.kString)
    syntax.addFlag(kVerboseFlag, kVerboseLongFlag, OpenMaya.MSyntax.kBoolean)
    return syntax

def initializePlugin(mobject):
    """ Initialize the script plug-in

    Args:
        mobject (OpenMaya.MObject):

    """
    mplugin = OpenMayaMPx.MFnPlugin(mobject, "Michitaka Inoue", "1.0", "Any")
    try:
        mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
    except:
        sys.stderr.write("Failed to register command: %s\n" % kPluginCmdName)
        raise

def uninitializePlugin(mobject):
    """ Uninitialize the script plug-in

    Args:
        mobject (OpenMaya.MObject):

    """
    mplugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mplugin.deregisterCommand(kPluginCmdName)
    except:
        sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
        raise
<commit_msg>reload rush module<commit_after>from pymel.all import mel as pm
from maya import OpenMayaMPx
from maya import OpenMaya
from maya import cmds
from maya import mel
import rush
import logging
import string
import random
import json
import sys
import os
import Qt
reload(rush)

kPluginCmdName = "rush"
kVerboseFlag = "-v"
kVerboseLongFlag = "-verbose"

# Define mel procedure to call the previous function
mel.eval("""
global proc callLastCommand(string $function)
{
    repeatLast -ac $function -acl "blah-blah....";
}
""")

def setupLogger(verbose=False):
    """

    Args:
        verbose (bool): verbose mode

    Return:
        type: return value

    """
    if verbose:
        lv = logging.DEBUG
    else:
        lv = logging.CRITICAL

    tempLogName = 'Rush : ' + ''.join(
        random.SystemRandom().choice(
            string.ascii_uppercase + string.digits) for _ in range(10))

    logger = logging.getLogger(tempLogName)
    handler = logging.StreamHandler()
    logger.setLevel(lv)
    handler.setLevel(lv)
    logger.addHandler(handler)
    return logger

def loadStyle():
    """ Load stylesheet

    Return:
        contents(str): stylesheet info in string

    """
    mayaScriptDir = cmds.internalVar(userScriptDir=True)
    qss = os.path.normpath(
        os.path.join(mayaScriptDir, "rush", "style.qss"))
    f = file(qss, 'r')
    contents = f.read()
    f.close()
    return contents

def getMayaWindow():
    """ Return Maya's main window
    """

    for obj in Qt.QtWidgets.QApplication.topLevelWidgets():
        if obj.objectName() == 'MayaWindow':
            return obj
    raise RuntimeError('Could not find MayaWindow instance')

class CustomQLineEdit(Qt.QtWidgets.QLineEdit):
    """ Custom QLineEdit with custom events and signals"""

    escPressed = Qt.QtCore.Signal(str)
    tabPressed = Qt.QtCore.Signal(str)

    def __init__(self, parent=None):
        super(CustomQLineEdit, self).__init__(parent)
        self.setFocusPolicy(Qt.QtCore.Qt.StrongFocus)

    def focusOutEvent(self, event):
        # Emit signal to close the window when it gets out of focus
        self.escPressed.emit('esc')

    def keyPressEvent(self, event):
        if event.key() == Qt.QtCore.Qt.Key_Escape:
            self.escPressed.emit('esc')
        elif event.key() == Qt.QtCore.Qt.Key_Tab:
            self.tabPressed.emit('tab')
        else:
            super(CustomQLineEdit, self).keyPressEvent(event)

class Gui(rush.RushCommands, Qt.QtWidgets.QDialog):

    def closeExistingWindow(self):
        """ Close window if exists """

        for qt in Qt.QtWidgets.QApplication.topLevelWidgets():
            try:
                if qt.__class__.__name__ == self.__class__.__name__:
                    qt.close()
            except:
                pass

    def __init__(self, logger, parent=None):
        self.closeExistingWindow()
        super(Gui, self).__init__(parent)

        self.logger = logger

        self.setAttribute(Qt.QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("Rush")
        self.setWindowFlags(Qt.QtCore.Qt.Window)
        self.setWindowFlags(
            Qt.QtCore.Qt.Popup | Qt.QtCore.Qt.FramelessWindowHint)
        try:
            self.setWindowFlags(
                self.windowFlags() | Qt.QtCore.Qt.NoDropShadowWindowHint)
        except AttributeError:
            pass

        self.setAttribute(Qt.QtCore.Qt.WA_TranslucentBackground)

        self.setStyleSheet(loadStyle())

        # Create Data then UI
        self.createData()
        self.createUI()

    def createUI(self):
        self.LE = CustomQLineEdit(self)
        self.LE.setFixedWidth(200)
        self.layout = Qt.QtWidgets.QBoxLayout(
            Qt.QtWidgets.QBoxLayout.TopToBottom)
        self.layout.addWidget(self.LE)
        self.setLayout(self.layout)

        # Set up QCompleter
        self.completer = Qt.QtWidgets.QCompleter(self)
        self.completer.setCompletionMode(
            Qt.QtWidgets.QCompleter.UnfilteredPopupCompletion)
        self.completer.setModel(self.filteredModel)
        self.completer.setObjectName("commandCompleter")
        # self.completer.popup().setIconSize(self.iconSize)
        self.completer.popup().setStyleSheet(loadStyle())

        # Edit line Edit behavior
        self.LE.setCompleter(self.completer)
        self.LE.textEdited.connect(self.updateData)
        self.LE.returnPressed.connect(self.execute)
        self.LE.escPressed.connect(self.close)
        self.LE.tabPressed.connect(self.tabCompletion)
        self.LE.setFocus()

    def createData(self):
        """

        Return:
            QSortFilterProxyModel: data

        """

        model = Qt.QtGui.QStandardItemModel()

        # Load json files as dicrectory.
        # key is command name, and its item is icon path.
        mayaScriptDir = cmds.internalVar(userScriptDir=True)
        commandFile = os.path.normpath(
            os.path.join(mayaScriptDir, "rushCmds.json"))
        try:
            f = open(commandFile)
            jsonDict = json.load(f)
            f.close()
        except IOError:
            jsonDict = {}

        # Create a list of command names
        self.commands = [i for i in jsonDict]

        # Add all command names and icon paths to the the model(model)
        for num, command in enumerate(jsonDict):
            item = Qt.QtGui.QStandardItem(command)
            if os.path.isabs(jsonDict[command]) is True:
                iconPath = os.path.normpath(jsonDict[command])
                item.setIcon(Qt.QtGui.QIcon(iconPath))
            else:
                item.setIcon(Qt.QtGui.QIcon(":%s" % jsonDict[command]))
            model.setItem(num, 0, item)

        # Store the model(model) into the sortFilterProxy model
        self.filteredModel = Qt.QtCore.QSortFilterProxyModel(self)
        self.filteredModel.setFilterCaseSensitivity(
            Qt.QtCore.Qt.CaseInsensitive)
        self.filteredModel.setSourceModel(model)

    def updateData(self):
        """ Update current completion data

        Args:

        Return:

        """

        # command completer
        currentText = self.LE.text()
        if currentText == "":
            self.LE.setCompleter(self.completer)

        # Set commands to case insensitive
        regExp = Qt.QtCore.QRegExp(
            self.LE.text(),
            Qt.QtCore.Qt.CaseInsensitive,
            Qt.QtCore.QRegExp.RegExp)
        self.filteredModel.setFilterRegExp(regExp)

    def tabCompletion(self):
        text = self.LE.text().lower()
        currents = [i for i in self.commands if text in i.lower()]
        top = currents[0]
        self.LE.setText(top)

    def execute(self):
        cmd = self.LE.text()

        # Close gui first otherwise maya clashes(2017)
        self.close()

        try:
            f = getattr(self, "_%s" % cmd)
            f()
            self.logger.debug("Running command : %s" % cmd)

            # Add to repeatLast command so the comamnd can be repeatable
            # by G key
            pm.callLastCommand(
                """python(\"rush.RushCommands()._%s()\")""" % cmd)
        except AttributeError:
            pass

class Rush(OpenMayaMPx.MPxCommand):

    def __init__(self):
        super(Rush, self).__init__()

        self.verbose = False
        self.cmdArg = "Initial arg"

    def doIt(self, args):

        # Parse the arguments.
        argData = OpenMaya.MArgDatabase(self.syntax(), args)
        try:
            self.cmdArg = argData.commandArgumentString(0)
        except RuntimeError:
            pass
        if argData.isFlagSet(kVerboseFlag):
            self.verbose = argData.flagArgumentBool(kVerboseFlag, 0)

        logger = setupLogger(self.verbose)
        self.mw = Gui(logger, getMayaWindow())
        self.mw.show()

        pos = Qt.QtGui.QCursor.pos()
        self.mw.move(
            pos.x() - (self.mw.width() / 2),
            pos.y() - (self.mw.height() / 2))

        self.mw.raise_()
        self.mw.activateWindow()

    def undoIt(self):
        pass

    def redoIt(self):
        pass

    def isUndoable(self):
        return False

# Creator
def cmdCreator():
    # Create the command
    """

    Return:
        pointer to the command

    """
    ptr = OpenMayaMPx.asMPxPtr(Rush())
    return ptr

def syntaxCreator():
    """ Syntax creator

    Return:
        syntax (OpenMaya.MSyntax): return value

    """
    syntax = OpenMaya.MSyntax()
    syntax.addArg(OpenMaya.MSyntax.kString)
    syntax.addFlag(kVerboseFlag, kVerboseLongFlag, OpenMaya.MSyntax.kBoolean)
    return syntax

def initializePlugin(mobject):
    """ Initialize the script plug-in

    Args:
        mobject (OpenMaya.MObject):

    """
    mplugin = OpenMayaMPx.MFnPlugin(mobject, "Michitaka Inoue", "1.0", "Any")
    try:
        mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
    except:
        sys.stderr.write("Failed to register command: %s\n" % kPluginCmdName)
        raise

def uninitializePlugin(mobject):
    """ Uninitialize the script plug-in

    Args:
        mobject (OpenMaya.MObject):

    """
    mplugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mplugin.deregisterCommand(kPluginCmdName)
    except:
        sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
        raise
<|endoftext|>


<commit_before>import django
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.db import models, IntegrityError, transaction
from django.template.defaultfilters import slugify as default_slugify
from django.utils.translation import ugettext_lazy as _, ugettext

class TagBase(models.Model):
    name = models.CharField(verbose_name=_('Name'), max_length=100)
    slug = models.SlugField(verbose_name=_('Slug'), unique=True, max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if not self.pk and not self.slug:
            self.slug = self.slugify(self.name)
            if django.VERSION >= (1, 2):
                from django.db import router
                using = kwargs.get("using") or router.db_for_write(
                    type(self), instance=self)
                # Make sure we write to the same db for all attempted writes,
                # with a multi-master setup, theoretically we could try to
                # write and rollback on different DBs
                kwargs["using"] = using
                trans_kwargs = {"using": using}
            else:
                trans_kwargs = {}
            i = 0
            while True:
                i += 1
                try:
                    sid = transaction.savepoint(**trans_kwargs)
                    res = super(TagBase, self).save(*args, **kwargs)
                    transaction.savepoint_commit(sid, **trans_kwargs)
                    return res
                except IntegrityError:
                    transaction.savepoint_rollback(sid, **trans_kwargs)
                    self.slug = self.slugify(self.name, i)
        else:
            return super(TagBase, self).save(*args, **kwargs)

    def slugify(self, tag, i=None):
        slug = default_slugify(tag)
        if i is not None:
            slug += "_%d" % i
        return slug

class Tag(TagBase):
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

class ItemBase(models.Model):
    def __unicode__(self):
        return ugettext("%(object)s tagged with %(tag)s") % {
            "object": self.content_object,
            "tag": self.tag
        }

    class Meta:
        abstract = True

    @classmethod
    def tag_model(cls):
        return cls._meta.get_field_by_name("tag")[0].rel.to

    @classmethod
    def tag_relname(cls):
        return cls._meta.get_field_by_name('tag')[0].rel.related_name

    @classmethod
    def lookup_kwargs(cls, instance):
        return {
            'content_object': instance
        }

    @classmethod
    def bulk_lookup_kwargs(cls, instances):
        return {
            "content_object__in": instances,
        }

class TaggedItemBase(ItemBase):
    if django.VERSION < (1, 2):
        tag = models.ForeignKey(Tag, related_name="%(class)s_items")
    else:
        tag = models.ForeignKey(Tag, related_name="%(app_label)s_%(class)s_items")

    class Meta:
        abstract = True

    @classmethod
    def tags_for(cls, model, instance=None):
        if instance is not None:
            return cls.tag_model().objects.filter(**{
                '%s__content_object' % cls.tag_relname(): instance
            })
        return cls.tag_model().objects.filter(**{
            '%s__content_object__isnull' % cls.tag_relname(): False
        }).distinct()

class GenericTaggedItemBase(ItemBase):
    object_id = models.IntegerField(verbose_name=_('Object id'), db_index=True)
    if django.VERSION < (1, 2):
        content_type = models.ForeignKey(
            ContentType,
            verbose_name=_('Content type'),
            related_name="%(class)s_tagged_items"
        )
    else:
        content_type = models.ForeignKey(
            ContentType,
            verbose_name=_('Content type'),
            related_name="%(app_label)s_%(class)s_tagged_items"
        )
    content_object = GenericForeignKey()

    class Meta:
        abstract=True

    @classmethod
    def lookup_kwargs(cls, instance):
        return {
            'object_id': instance.pk,
            'content_type': ContentType.objects.get_for_model(instance)
        }

    @classmethod
    def bulk_lookup_kwargs(cls, instances):
        # TODO: instances[0], can we assume there are instances.
        return {
            "object_id__in": [instance.pk for instance in instances],
            "content_type": ContentType.objects.get_for_model(instances[0]),
        }

    @classmethod
    def tags_for(cls, model, instance=None):
        ct = ContentType.objects.get_for_model(model)
        kwargs = {
            "%s__content_type" % cls.tag_relname(): ct
        }
        if instance is not None:
            kwargs["%s__object_id" % cls.tag_relname()] = instance.pk
        return cls.tag_model().objects.filter(**kwargs).distinct()

class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
    class Meta:
        verbose_name = _("Tagged Item")
        verbose_name_plural = _("Tagged Items")
<commit_msg>use get absolute url<commit_after>import django
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.db import models, IntegrityError, transaction
from django.template.defaultfilters import slugify as default_slugify
from django.utils.translation import ugettext_lazy as _, ugettext
from django.core.urlresolvers import reverse

class TagBase(models.Model):
    name = models.CharField(verbose_name=_('Name'), max_length=100)
    slug = models.SlugField(verbose_name=_('Slug'), unique=True, max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if not self.pk and not self.slug:
            self.slug = self.slugify(self.name)
            if django.VERSION >= (1, 2):
                from django.db import router
                using = kwargs.get("using") or router.db_for_write(
                    type(self), instance=self)
                # Make sure we write to the same db for all attempted writes,
                # with a multi-master setup, theoretically we could try to
                # write and rollback on different DBs
                kwargs["using"] = using
                trans_kwargs = {"using": using}
            else:
                trans_kwargs = {}
            i = 0
            while True:
                i += 1
                try:
                    sid = transaction.savepoint(**trans_kwargs)
                    res = super(TagBase, self).save(*args, **kwargs)
                    transaction.savepoint_commit(sid, **trans_kwargs)
                    return res
                except IntegrityError:
                    transaction.savepoint_rollback(sid, **trans_kwargs)
                    self.slug = self.slugify(self.name, i)
        else:
            return super(TagBase, self).save(*args, **kwargs)

    def slugify(self, tag, i=None):
        slug = default_slugify(tag)
        if i is not None:
            slug += "_%d" % i
        return slug

class Tag(TagBase):
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

    def get_absolute_url(self):
        return reverse('taggit_tag', kwargs={'tag_slug':self.slug})

class ItemBase(models.Model):
    def __unicode__(self):
        return ugettext("%(object)s tagged with %(tag)s") % {
            "object": self.content_object,
            "tag": self.tag
        }

    class Meta:
        abstract = True

    @classmethod
    def tag_model(cls):
        return cls._meta.get_field_by_name("tag")[0].rel.to

    @classmethod
    def tag_relname(cls):
        return cls._meta.get_field_by_name('tag')[0].rel.related_name

    @classmethod
    def lookup_kwargs(cls, instance):
        return {
            'content_object': instance
        }

    @classmethod
    def bulk_lookup_kwargs(cls, instances):
        return {
            "content_object__in": instances,
        }

class TaggedItemBase(ItemBase):
    if django.VERSION < (1, 2):
        tag = models.ForeignKey(Tag, related_name="%(class)s_items")
    else:
        tag = models.ForeignKey(Tag, related_name="%(app_label)s_%(class)s_items")

    class Meta:
        abstract = True

    @classmethod
    def tags_for(cls, model, instance=None):
        if instance is not None:
            return cls.tag_model().objects.filter(**{
                '%s__content_object' % cls.tag_relname(): instance
            })
        return cls.tag_model().objects.filter(**{
            '%s__content_object__isnull' % cls.tag_relname(): False
        }).distinct()

class GenericTaggedItemBase(ItemBase):
    object_id = models.IntegerField(verbose_name=_('Object id'), db_index=True)
    if django.VERSION < (1, 2):
        content_type = models.ForeignKey(
            ContentType,
            verbose_name=_('Content type'),
            related_name="%(class)s_tagged_items"
        )
    else:
        content_type = models.ForeignKey(
            ContentType,
            verbose_name=_('Content type'),
            related_name="%(app_label)s_%(class)s_tagged_items"
        )
    content_object = GenericForeignKey()

    class Meta:
        abstract=True

    @classmethod
    def lookup_kwargs(cls, instance):
        return {
            'object_id': instance.pk,
            'content_type': ContentType.objects.get_for_model(instance)
        }

    @classmethod
    def bulk_lookup_kwargs(cls, instances):
        # TODO: instances[0], can we assume there are instances.
        return {
            "object_id__in": [instance.pk for instance in instances],
            "content_type": ContentType.objects.get_for_model(instances[0]),
        }

    @classmethod
    def tags_for(cls, model, instance=None):
        ct = ContentType.objects.get_for_model(model)
        kwargs = {
            "%s__content_type" % cls.tag_relname(): ct
        }
        if instance is not None:
            kwargs["%s__object_id" % cls.tag_relname()] = instance.pk
        return cls.tag_model().objects.filter(**kwargs).distinct()

class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
    class Meta:
        verbose_name = _("Tagged Item")
        verbose_name_plural = _("Tagged Items")
<|endoftext|>


<commit_before>"""Menu_bar

Menu bar module for setting menu bar setup for OSX
"""
__author__ = 'ales lerch'

import rumps
import desktop
import wallpaper
from wall import HOME
from os.path import isfile
from database import DB_lite

class Bar(rumps.App):

    def __init__(self):
        super().__init__("WallpDesk")
        self.menu = ["About","Settings","Reset database", None,
                "Desktop daemon", "Wallpaper daemon", None
                ,"Next wallpaper", "Quit"]
        self.db = DB_lite()
        self.def_wallpaper = self.db.get_wall_path()
        self.quit_button = None
        self.template = True
        self.title = None
        if isfile("images/menu_bar.png"):
            self.icon = "images/menu_bar.png"
        else:
            self.title = "Icon not found"

        self.editor = wallpaper.Editor()
        self.desktop = desktop.Daemon()

    @rumps.clicked("About")
    def about(self, _):
        rumps.Window(message="\t   WallpDesk (0.0.4)", title="\tAbout WallpDesk",
                default_text="""\n\tVersion: 0.0.4\n\n\tLicence: MIT\n\n\tAuthor: Ales Lerch""",
            dimensions=(170, 130)).run()

    @rumps.clicked("Reset database")
    def reset_db(self, _):
        self.editor.reset_library()

    @rumps.clicked("Settings")
    def settings(self, _):
        #file_path = filedialog.askdirectory() NSError
        file_path = rumps.Window(message="Set wallpaper absolute path", title='Path',
                default_text=self.def_wallpaper,
                ok="Ok", cancel="Cancel",
                dimensions=(320, 120)).run()
        if self.def_wallpaper != file_path.text:
            print(file_path.text)
            if file_path.text != "None":
                self.def_wallpaper = file_path.text
                self.db.set_wall_path(file_path.text)
                self.editor.set_loading_dir(self.def_wallpaper)

    @rumps.clicked("Desktop daemon")
    def on_off_test(self, sender):
        """ notifications are so far broken in rumps
        rumps.notification(title='Hi', subtitle='There.', message='Friend!',
        sound=does_something.sound, data=my_data)"""
        sender.state = not sender.state
        if sender.state:
            rumps.alert(message='You are now running desktop daemon')
            self.desktop.interrupted = False
            self.desktop.run()
        else:
            self.desktop.interrupted = True

    @rumps.clicked("Wallpaper daemon")
    def wallpaper(self, sender):
        if self.def_wallpaper:
            sender.state = not sender.state
            if sender.state:
                rumps.alert(message='You are now running wallpaper daemon', ok='OK')
                print(self.def_wallpaper)
                self.editor.interrupted = False
                self.editor.run()
            else:
                self.editor.interrupted = True
        else:
            rumps.alert("No input path for images set!")

    @rumps.clicked("Next wallpaper")
    def next_wallpaper(self,_):
        self.editor.choose_random_image()

    @rumps.clicked("Quit")
    def clean_up_before_quit(self,_):
        print("Quit application")
        rumps.quit_application()

if __name__ == "__main__":
    Bar().run()
<commit_msg>menu support user folder input<commit_after>"""Menu_bar

Menu bar module for setting menu bar setup for OSX
"""
__author__ = 'ales lerch'

import rumps
import desktop
import wallpaper
from wall import HOME
from os.path import isfile
from database import DB_lite
from subprocess import Popen, PIPE

class Bar(rumps.App):

    def __init__(self):
        super().__init__("WallpDesk")
        self.menu = ["About","Settings","Reset database", None,
                "Desktop daemon", "Wallpaper daemon", None
                ,"Next wallpaper", "Quit"]
        self.db = DB_lite()
        self.def_wallpaper = self.db.get_wall_path()
        self.quit_button = None
        self.template = True
        self.title = None
        if isfile("images/menu_bar.png"):
            self.icon = "images/menu_bar.png"
        else:
            self.title = "Icon not found"

        self.editor = wallpaper.Editor()
        self.desktop = desktop.Daemon()

    @rumps.clicked("About")
    def about(self, _):
        rumps.Window(message="\t   WallpDesk (0.0.4)", title="\tAbout WallpDesk",
                default_text="""\n\tVersion: 0.0.4\n\n\tLicence: MIT\n\n\tAuthor: Ales Lerch""",
            dimensions=(170, 130)).run()

    @rumps.clicked("Reset database")
    def reset_db(self, _):
        self.editor.reset_library()

    @rumps.clicked("Settings")
    def settings(self, _):
        cmd = b"""choose folder with prompt "Please select an output folder:" """
        proc = Popen(["osascript", '-'], stdin=PIPE, stdout=PIPE)
        file_path, _ = proc.communicate(cmd)
        file_path = file_path.decode("utf-8").replace("alias Macintosh HD",'').replace('\n','').replace(':','/')
        if self.def_wallpaper != file_path and proc.returncode == 0:
            print(file_path)
            self.def_wallpaper = file_path
            self.db.set_wall_path(file_path)
            self.editor.set_loading_dir(self.def_wallpaper)

    @rumps.clicked("Desktop daemon")
    def on_off_test(self, sender):
        """ notifications are so far broken in rumps
        rumps.notification(title='Hi', subtitle='There.', message='Friend!',
        sound=does_something.sound, data=my_data)"""
        sender.state = not sender.state
        if sender.state:
            rumps.alert(message='You are now running desktop daemon')
            self.desktop.interrupted = False
            self.desktop.run()
        else:
            self.desktop.interrupted = True

    @rumps.clicked("Wallpaper daemon")
    def wallpaper(self, sender):
        if self.def_wallpaper:
            sender.state = not sender.state
            if sender.state:
                rumps.alert(message='You are now running wallpaper daemon', ok='OK')
                print(self.def_wallpaper)
                self.editor.interrupted = False
                self.editor.run()
            else:
                self.editor.interrupted = True
        else:
            rumps.alert("No input path for images set!")

    @rumps.clicked("Next wallpaper")
    def next_wallpaper(self,_):
        self.editor.choose_random_image()

    @rumps.clicked("Quit")
    def clean_up_before_quit(self,_):
        print("Quit application")
        rumps.quit_application()

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import math
import thread

import roslib; roslib.load_manifest('ev3_ros')
import rospy
from sensor_msgs.msg import JointState, Imu
from std_msgs.msg import Bool
from PyKDL import Rotation
import rpyc
import nxt.error

from nxt_msgs.msg import Range, Contact, JointCommand, Color, Gyro, Accelerometer

class Device(object):
    def __init__(self, params):
        self.desired_period = 1.0 / params['desired_frequency']
        self.period = self.desired_period
        self.initialized = False
        self.name = params['name']

    def needs_trigger(self):
        # initialize
        if not self.initialized:
            self.initialized = True
            self.last_run = rospy.Time.now()
            rospy.logdebug('Initializing %s', self.name)
            return False
        # compute frequency
        now = rospy.Time.now()
        period = 0.9 * self.period + 0.1 * (now - self.last_run).to_sec()

        # check period
        if period > self.desired_period * 1.2:
            rospy.logwarn('%s not reaching desired frequency: actual %f, desired %f',
                          self.name, 1.0 / period, 1.0 / self.desired_period)
        elif period > self.desired_period * 1.5:
            rospy.logerr('%s not reaching desired frequency: actual %f, desired %f',
                         self.name, 1.0 / period, 1.0 / self.desired_period)

        return period > self.desired_period

    def trigger(self):
        raise NotImplementedError()

    def do_trigger(self):
        try:
            rospy.logdebug('Trigger %s with current frequency %f',
                           self.name, 1.0/self.period)
            now = rospy.Time.now()
            self.period = 0.9 * self.period + 0.1 * (now - self.last_run).to_sec()
            self.last_run = now
            self.trigger()
            rospy.logdebug('Trigger %s took %f mili-seconds',
                           self.name, (rospy.Time.now() - now).to_sec() * 1000)
        except nxt.error.I2CError:
            rospy.logwarn('caught an exception nxt.error.I2CError')
        except nxt.error.DirProtError:
            rospy.logwarn('caught an exception nxt.error.DirProtError')

class Motor(Device):
    POWER_TO_NM = 0.01
    POWER_MAX = 125

    def __init__(self, params, lego):
        super(Motor, self).__init__(params)

        # create motor
        self.motor = lego.Motor(params['port'])
        self.cmd = 0  # default command

        # create publisher
        self.pub = rospy.Publisher('joint_state', JointState)
        self.last_js = None

        # create subscriber
        self.sub = rospy.Subscriber('joint_command', JointCommand, self.cmd_cb, None, 2)

    def cmd_cb(self, msg):
        if msg.name == self.name:
            cmd = msg.effort / self.POWER_TO_NM
            if cmd > self.POWER_MAX:
                cmd = self.POWER_MAX
            elif cmd < -self.POWER_MAX:
                cmd = -self.POWER_MAX
            self.cmd = cmd  # save command

    def trigger(self):
        js = JointState()
        js.header.stamp = rospy.Time.now()
        state = self.motor.get_output_state()
        js.name.append(self.name)
        js.position.append(state[9] * math.pi / 180.0)
        js.effort.append(state[1] * self.POWER_TO_NM)
        if self.last_js:
            vel = (js.position[0] - self.last_js.position[0]) / \
                  (js.header.stamp - self.last_js.header.stamp).to_sec()
            js.velocity.append(vel)
        else:
            vel = 0
            js.velocity.append(vel)
        self.pub.publish(js)
        self.last_js = js

        # send command
        self.motor.run_forever(int(self.cmd), regulation_mode=False)

class UltraSonicSensor(Device):
    def __init__(self, params, lego):
        super(UltraSonicSensor, self).__init__(params)
        # create ultrasonic sensor
        self.ultrasonic = lego.UltrasonicSensor(params['port'])
        self.frame_id = params['frame_id']
        self.spread = params['spread_angle']
        self.min_range = params['min_range']
        self.max_range = params['max_range']

        # create publisher
        self.pub = rospy.Publisher(params['name'], Range)

    def trigger(self):
        ds = Range()
        ds.header.frame_id = self.frame_id
        ds.header.stamp = rospy.Time.now()
        ds.range = self.ultrasonic.get_sample() / 100.0
        ds.spread_angle = self.spread
        ds.range_min = self.min_range
        ds.range_max = self.max_range
        self.pub.publish(ds)

class GyroSensor(Device):
    def __init__(self, params, lego):
        super(GyroSensor, self).__init__(params)
        # create gyro sensor
        self.gyro = lego.GyroSensor(params['port'])
        self.frame_id = params['frame_id']
        self.orientation = 0.0
        self.offset = 0.0
        self.prev_time = rospy.Time.now()

        # calibrate
        rospy.loginfo('Calibrating Gyro. Don\'t move the robot now')
        start_time = rospy.Time.now()
        cal_duration = rospy.Duration(2)
        offset = 0
        tmp_time = rospy.Time.now()
        while rospy.Time.now() < start_time + cal_duration:
            rospy.sleep(0.01)
            sample = self.gyro.get_sample()
            now = rospy.Time.now()
            offset += (sample * (now - tmp_time).to_sec())
            tmp_time = now
        self.offset = offset / (tmp_time - start_time).to_sec()
        rospy.loginfo('Gyro calibrated with offset %f', self.offset)

        # create publisher
        self.pub = rospy.Publisher(params['name'], Gyro)

        # create publisher
        self.pub2 = rospy.Publisher(params['name'] + '_imu', Imu)

    def trigger(self):
        sample = self.gyro.get_sample()
        gs = Gyro()
        gs.header.frame_id = self.frame_id
        gs.header.stamp = rospy.Time.now()
        gs.calibration_offset.x = 0.0
        gs.calibration_offset.y = 0.0
        gs.calibration_offset.z = self.offset
        gs.angular_velocity.x = 0.0
        gs.angular_velocity.y = 0.0
        gs.angular_velocity.z = (sample - self.offset) * math.pi / 180.0
        gs.angular_velocity_covariance = [0, 0, 0, 0, 0, 0, 0, 0, 1]
        self.pub.publish(gs)

        imu = Imu()
        imu.header.frame_id = self.frame_id
        imu.header.stamp = rospy.Time.now()
        imu.angular_velocity.x = 0.0
        imu.angular_velocity.y = 0.0
        imu.angular_velocity.z = (sample-self.offset) * math.pi / 180.0
        imu.angular_velocity_covariance = [0, 0, 0, 0, 0, 0, 0, 0, 1]
        imu.orientation_covariance = [0.001, 0, 0, 0, 0.001, 0, 0, 0, 0.1]
        self.orientation += imu.angular_velocity.z * (imu.header.stamp - self.prev_time).to_sec()
        self.prev_time = imu.header.stamp
        imu.orientation.x, imu.orientation.y, imu.orientation.z, imu.orientation.w = \
            Rotation.RotZ(self.orientation).GetQuaternion()
        self.pub2.publish(imu)

def main():
    rospy.init_node('ev3_ros')
    ns = 'ev3_robot'
    host = rospy.get_param('~host', None)
    conn = rpyc.classic.connect(host)
    lego = conn.modules.ev3.lego

    config = rospy.get_param('~' + ns)
    components = []
    for c in config:
        rospy.loginfo('Creating %s with name %s on %s',
                      c['type'], c['name'], c['port'])
        if c['type'] == 'motor':
            components.append(Motor(c, lego))
        elif c['type'] == 'ultrasonic':
            components.append(UltraSonicSensor(c, lego))
        elif c['type'] == 'gyro':
            components.append(GyroSensor(c, lego))
        else:
            rospy.logerr('Invalid sensor/actuator type %s', c['type'])

    callback_handle_frequency = 10.0
    last_callback_handle = rospy.Time.now()
    lock = thread.allocate_lock()

    while not rospy.is_shutdown():
        lock.acquire()
        triggered = False
        for c in components:
            if c.needs_trigger() and not triggered:
                c.do_trigger()
                triggered = True
        lock.release()
        now = rospy.Time.now()
        if (now - last_callback_handle).to_sec() > 1.0 / callback_handle_frequency:
            last_callback_handle = now
            rospy.sleep(0.01)

if __name__ == '__main__':
    main()
<commit_msg>fix ev3_ros by using python-ev3 commands<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import math
import thread

import roslib; roslib.load_manifest('ev3_ros')
import rospy
from sensor_msgs.msg import JointState, Imu
from std_msgs.msg import Bool
from PyKDL import Rotation
import rpyc
import nxt.error

from nxt_msgs.msg import Range, Contact, JointCommand, Color, Gyro, Accelerometer

class Device(object):
    def __init__(self, params):
        self.desired_period = 1.0 / params['desired_frequency']
        self.period = self.desired_period
        self.initialized = False
        self.name = params['name']

    def needs_trigger(self):
        # initialize
        if not self.initialized:
            self.initialized = True
            self.last_run = rospy.Time.now()
            rospy.logdebug('Initializing %s', self.name)
            return False
        # compute frequency
        now = rospy.Time.now()
        period = 0.9 * self.period + 0.1 * (now - self.last_run).to_sec()

        # check period
        if period > self.desired_period * 1.2:
            rospy.logwarn('%s not reaching desired frequency: actual %f, desired %f',
                          self.name, 1.0 / period, 1.0 / self.desired_period)
        elif period > self.desired_period * 1.5:
            rospy.logerr('%s not reaching desired frequency: actual %f, desired %f',
                         self.name, 1.0 / period, 1.0 / self.desired_period)

        return period > self.desired_period

    def trigger(self):
        raise NotImplementedError()

    def do_trigger(self):
        try:
            rospy.logdebug('Trigger %s with current frequency %f',
                           self.name, 1.0/self.period)
            now = rospy.Time.now()
            self.period = 0.9 * self.period + 0.1 * (now - self.last_run).to_sec()
            self.last_run = now
            self.trigger()
            rospy.logdebug('Trigger %s took %f mili-seconds',
                           self.name, (rospy.Time.now() - now).to_sec() * 1000)
        except nxt.error.I2CError:
            rospy.logwarn('caught an exception nxt.error.I2CError')
        except nxt.error.DirProtError:
            rospy.logwarn('caught an exception nxt.error.DirProtError')

class Motor(Device):
    POWER_TO_NM = 0.01
    POWER_MAX = 125

    def __init__(self, params, lego):
        super(Motor, self).__init__(params)

        # create motor
        self.motor = lego.Motor(params['port'])
        self.cmd = 0  # default command

        # create publisher
        self.pub = rospy.Publisher('joint_state', JointState, queue_size=5)
        self.last_js = None

        # create subscriber
        self.sub = rospy.Subscriber('joint_command', JointCommand, self.cmd_cb, None, 2)

    def cmd_cb(self, msg):
        if msg.name == self.name:
            cmd = msg.effort / self.POWER_TO_NM
            if cmd > self.POWER_MAX:
                cmd = self.POWER_MAX
            elif cmd < -self.POWER_MAX:
                cmd = -self.POWER_MAX
            self.cmd = cmd  # save command

    def trigger(self):
        js = JointState()
        js.header.stamp = rospy.Time.now()
        js.position.append(self.motor.position * math.pi / 180.0)
        if self.last_js:
            vel = (js.position[0] - self.last_js.position[0]) / \
                  (js.header.stamp - self.last_js.header.stamp).to_sec()
            js.velocity.append(vel)
        else:
            vel = 0
            js.velocity.append(vel)
        self.pub.publish(js)
        self.last_js = js

        # send command
        self.motor.run_forever(int(self.cmd), regulation_mode=False)

class UltraSonicSensor(Device):
    def __init__(self, params, lego):
        super(UltraSonicSensor, self).__init__(params)
        # create ultrasonic sensor
        self.ultrasonic = lego.UltrasonicSensor(params['port'])
        self.frame_id = params['frame_id']
        self.spread = params['spread_angle']
        self.min_range = params['min_range']
        self.max_range = params['max_range']

        # create publisher
        self.pub = rospy.Publisher(params['name'], Range, queue_size=5)

    def trigger(self):
        ds = Range()
        ds.header.frame_id = self.frame_id
        ds.header.stamp = rospy.Time.now()
        ds.range = self.ultrasonic.dist_cm / 100.0
        ds.spread_angle = self.spread
        ds.range_min = self.min_range
        ds.range_max = self.max_range
        self.pub.publish(ds)

class GyroSensor(Device):
    def __init__(self, params, lego):
        super(GyroSensor, self).__init__(params)
        # create gyro sensor
        self.gyro = lego.GyroSensor(params['port'])
        self.frame_id = params['frame_id']
        self.orientation = 0.0
        self.offset = 0.0
        self.prev_time = rospy.Time.now()

        # calibrate
        rospy.loginfo('Calibrating Gyro. Don\'t move the robot now')
        start_time = rospy.Time.now()
        cal_duration = rospy.Duration(2)
        offset = 0
        tmp_time = rospy.Time.now()
        while rospy.Time.now() < start_time + cal_duration:
            rospy.sleep(0.01)
            sample = self.gyro.ang
            now = rospy.Time.now()
            offset += (sample * (now - tmp_time).to_sec())
            tmp_time = now
        self.offset = offset / (tmp_time - start_time).to_sec()
        rospy.loginfo('Gyro calibrated with offset %f', self.offset)

        # create publisher
        self.pub = rospy.Publisher(params['name'], Gyro, queue_size=5)

        # create publisher
        self.pub2 = rospy.Publisher(params['name'] + '_imu', Imu, queue_size=5)

    def trigger(self):
        sample = self.gyro.ang
        gs = Gyro()
        gs.header.frame_id = self.frame_id
        gs.header.stamp = rospy.Time.now()
        gs.calibration_offset.x = 0.0
        gs.calibration_offset.y = 0.0
        gs.calibration_offset.z = self.offset
        gs.angular_velocity.x = 0.0
        gs.angular_velocity.y = 0.0
        gs.angular_velocity.z = (sample - self.offset) * math.pi / 180.0
        gs.angular_velocity_covariance = [0, 0, 0, 0, 0, 0, 0, 0, 1]
        self.pub.publish(gs)

        imu = Imu()
        imu.header.frame_id = self.frame_id
        imu.header.stamp = rospy.Time.now()
        imu.angular_velocity.x = 0.0
        imu.angular_velocity.y = 0.0
        imu.angular_velocity.z = (sample-self.offset) * math.pi / 180.0
        imu.angular_velocity_covariance = [0, 0, 0, 0, 0, 0, 0, 0, 1]
        imu.orientation_covariance = [0.001, 0, 0, 0, 0.001, 0, 0, 0, 0.1]
        self.orientation += imu.angular_velocity.z * (imu.header.stamp - self.prev_time).to_sec()
        self.prev_time = imu.header.stamp
        imu.orientation.x, imu.orientation.y, imu.orientation.z, imu.orientation.w = \
            Rotation.RotZ(self.orientation).GetQuaternion()
        self.pub2.publish(imu)

def main():
    rospy.init_node('ev3_ros')
    ns = 'ev3_robot'
    host = rospy.get_param('~host', None)
    conn = rpyc.classic.connect(host)
    lego = conn.modules.ev3.lego

    config = rospy.get_param('~' + ns)
    components = []
    for c in config:
        rospy.loginfo('Creating %s with name %s on %s',
                      c['type'], c['name'], c['port'])
        if c['type'] == 'motor':
            components.append(Motor(c, lego))
        elif c['type'] == 'ultrasonic':
            components.append(UltraSonicSensor(c, lego))
        elif c['type'] == 'gyro':
            components.append(GyroSensor(c, lego))
        else:
            rospy.logerr('Invalid sensor/actuator type %s', c['type'])

    callback_handle_frequency = 10.0
    last_callback_handle = rospy.Time.now()
    lock = thread.allocate_lock()

    while not rospy.is_shutdown():
        lock.acquire()
        triggered = False
        for c in components:
            if c.needs_trigger() and not triggered:
                c.do_trigger()
                triggered = True
        lock.release()
        now = rospy.Time.now()
        if (now - last_callback_handle).to_sec() > 1.0 / callback_handle_frequency:
            last_callback_handle = now
            rospy.sleep(0.01)

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


<commit_before># Bulk insert/update DB operations for the Django ORM. Useful when
# inserting/updating lots of objects where the bottleneck is overhead
# in talking to the database. Instead of doing this
#
#   for x in seq:
#       o = SomeObject()
#       o.foo = x
#       o.save()
#
# or equivalently this
#
#   for x in seq:
#       SomeObject.objects.create(foo=x)
#
# do this
#
#   l = []
#   for x in seq:
#       o = SomeObject()
#       o.foo = x
#       l.append(o)
#   insert_many(l)
#
# Note that these operations are really simple. They won't work with
# many-to-many relationships, and you may have to divide really big
# lists into smaller chunks before sending them through.

def insert_many(objects, using="default"):
    """Insert list of Django objects in one SQL query. Objects must be
    of the same Django model. Note that save is not called and signals
    on the model are not raised."""
    if not objects:
        return

    import django.db.models
    from django.db import connections
    con = connections[using]

    model = objects[0].__class__
#    print objects
#    assert False, model
    fields = [f for f in model._meta.fields if not isinstance(f, django.db.models.AutoField)]
    parameters = []
    for o in objects:
        parameters.append(tuple(f.get_db_prep_save(f.pre_save(o, True), connection=con) for f in fields))

    table = model._meta.db_table
    column_names = ",".join(f.column for f in fields)
    placeholders = ",".join(("%s",) * len(fields))
    con.cursor().executemany(
        "insert into %s (%s) values (%s)" % (table, column_names, placeholders),
        parameters)

def update_many(objects, fields=[], using="default"):
    """Update list of Django objects in one SQL query, optionally only
    overwrite the given fields (as names, e.g. fields=["foo"]).
    Objects must be of the same Django model. Note that save is not
    called and signals on the model are not raised."""
    if not objects:
        return

    import django.db.models
    from django.db import connections
    con = connections[using]

    names = fields
    meta = objects[0]._meta
    fields = [f for f in meta.fields if not isinstance(f, django.db.models.AutoField) and (not names or f.name in names)]

    if not fields:
        raise ValueError("No fields to update, field names are %s." % names)

    fields_with_pk = fields + [meta.pk]
    parameters = []
    for o in objects:
        parameters.append(tuple(f.get_db_prep_save(f.pre_save(o, True), connection=con) for f in fields_with_pk))

    table = meta.db_table
    assignments = ",".join(("%s=%%s"% f.column) for f in fields)
    con.cursor().executemany(
        "update %s set %s where %s=%%s" % (table, assignments, meta.pk.column),
        parameters)<commit_msg>Fixed an issue with bulk insert/update. Database columns should be quoted to avoid potential conflicts between column names and reserved database operators (like 'order')<commit_after># Bulk insert/update DB operations for the Django ORM. Useful when
# inserting/updating lots of objects where the bottleneck is overhead
# in talking to the database. Instead of doing this
#
#   for x in seq:
#       o = SomeObject()
#       o.foo = x
#       o.save()
#
# or equivalently this
#
#   for x in seq:
#       SomeObject.objects.create(foo=x)
#
# do this
#
#   l = []
#   for x in seq:
#       o = SomeObject()
#       o.foo = x
#       l.append(o)
#   insert_many(l)
#
# Note that these operations are really simple. They won't work with
# many-to-many relationships, and you may have to divide really big
# lists into smaller chunks before sending them through.

def insert_many(objects, using="default"):
    """Insert list of Django objects in one SQL query. Objects must be
    of the same Django model. Note that save is not called and signals
    on the model are not raised."""
    if not objects:
        return

    import django.db.models
    from django.db import connections
    con = connections[using]

    model = objects[0].__class__
    fields = [f for f in model._meta.fields if not isinstance(f, django.db.models.AutoField)]
    parameters = []
    for o in objects:
        parameters.append(tuple(f.get_db_prep_save(f.pre_save(o, True), connection=con) for f in fields))
    table = model._meta.db_table
    column_names = ",".join(con.ops.quote_name(f.column) for f in fields)
    placeholders = ",".join(("%s",) * len(fields))
    con.cursor().executemany(
        "insert into %s (%s) values (%s)" % (table, column_names, placeholders),
        parameters)

def update_many(objects, fields=[], using="default"):
    """Update list of Django objects in one SQL query, optionally only
    overwrite the given fields (as names, e.g. fields=["foo"]).
    Objects must be of the same Django model. Note that save is not
    called and signals on the model are not raised."""
    if not objects:
        return

    import django.db.models
    from django.db import connections
    con = connections[using]

    names = fields
    meta = objects[0]._meta
    fields = [f for f in meta.fields if not isinstance(f, django.db.models.AutoField) and (not names or f.name in names)]

    if not fields:
        raise ValueError("No fields to update, field names are %s." % names)

    fields_with_pk = fields + [meta.pk]
    parameters = []
    for o in objects:
        parameters.append(tuple(f.get_db_prep_save(f.pre_save(o, True), connection=con) for f in fields_with_pk))

    table = meta.db_table
    assignments = ",".join(("%s=%%s"% con.ops.quote_name(f.column)) for f in fields)
    con.cursor().executemany(
        "update %s set %s where %s=%%s" % (table, assignments, meta.pk.column),
        parameters)<|endoftext|>