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

# Copyright (c) <NAME>.
# See LICENSE for details.

"""
This module provides helper functions:

`convert2dbus` - converts from Python type to DBUS type according signature
`convert` - function to convert dbus object to python object.
`converter` - decorator to convert dbus object to python object.
`exception_wrapper` - decorator to convert dbus exception to pympris exception.
`available_players` - function searchs and returns unique names of objects
                      which implemented MPRIS2 interfaces.

"""

import sys
from functools import wraps
import dbus

MPRIS_NAME_PREFIX = "org.mpris.MediaPlayer2"


def convert2dbus(value, signature):
    """Convert Python type to dbus type according signature"""
    if len(signature) == 2 and signature.startswith('a'):
        return dbus.Array(value, signature=signature[-1])
    type_map = {
        'b': dbus.Boolean, 'y': dbus.Byte, 'n': dbus.Int16,
        'i': dbus.Int32, 'x': dbus.Int64, 'q': dbus.UInt16, 'u': dbus.UInt32,
        't': dbus.UInt64, 'd': dbus.Double, 'o': dbus.ObjectPath,
        'g': dbus.Signature, 's': dbus.UTF8String}
    return type_map[signature](value)

if sys.version_info[0] == 2:
    def convert(dbus_obj):
        if isinstance(dbus_obj, dbus.Boolean):
            return bool(dbus_obj)
        if filter(lambda obj_type: isinstance(dbus_obj, obj_type),
                  (dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64,
                   dbus.UInt16, dbus.UInt32, dbus.UInt64)):
            return int(dbus_obj)
        if isinstance(dbus_obj, dbus.Double):
            return float(dbus_obj)
        if filter(lambda obj_type: isinstance(dbus_obj, obj_type),
                 (dbus.ObjectPath, dbus.Signature, dbus.String, dbus.UTF8String)):
            return unicode(dbus_obj)
        if isinstance(dbus_obj, dbus.Array):
            return map(convert, dbus_obj)
        if isinstance(dbus_obj, dbus.Dictionary):
            return {convert(key): convert(value)
                    for key, value in dbus_obj.items()}
        if isinstance(dbus_obj, dbus.Struct):
            return tuple(map(convert, dbus_obj))
        return dbus_obj
else:
    def convert(dbus_obj):
        if isinstance(dbus_obj, dbus.Boolean):
            return bool(dbus_obj)
        if [obj_type for obj_type in (dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64,
                dbus.UInt16, dbus.UInt32, dbus.UInt64) if isinstance(dbus_obj, obj_type)]:
            return int(dbus_obj)
        if isinstance(dbus_obj, dbus.Double):
            return float(dbus_obj)
        if [obj_type for obj_type in (dbus.ObjectPath, dbus.Signature,
                dbus.String) if isinstance(dbus_obj, obj_type)]:
            return str(dbus_obj)
        if isinstance(dbus_obj, dbus.Array):
            return list(map(convert, dbus_obj))
        if isinstance(dbus_obj, dbus.Dictionary):
            return {convert(key): convert(value)
                    for key, value in dbus_obj.items()}
        if isinstance(dbus_obj, dbus.Struct):
            return tuple(map(convert, dbus_obj))
        return dbus_obj

def converter(f):
    """Decorator to convert from dbus type to Python type"""
    @wraps(f)
    def wrapper(*args, **kwds):
        return convert(f(*args, **kwds))
    return wrapper


def exception_wrapper(f):
    """Decorator to convert dbus exception to pympris exception"""
    @wraps(f)
    def wrapper(*args, **kwds):
        try:
            return f(*args, **kwds)
        except dbus.exceptions.DBusException as err:
            _args = err.args
            raise PyMPRISException(*_args)
    return wrapper


def available_players():
    """Search and return set of unique names of objects
    which implemented MPRIS2 interfaces."""
    bus = dbus.SessionBus()
    players = set()
    for name in [item for item in bus.list_names() if item.startswith(MPRIS_NAME_PREFIX)]:
        owner_name = bus.get_name_owner(name)
        players.add(convert(owner_name))
    return players


class PyMPRISException(Exception):
    """docstring for PyMPRISException"""
    def __init__(self, *args):
        super(PyMPRISException, self).__init__(*args)<commit_msg>function convert: one code for python2 and python3.<commit_after>#!/usr/bin/env python
# coding=utf-8

# Copyright (c) <NAME>.
# See LICENSE for details.

"""
This module provides helper functions:

`convert2dbus` - converts from Python type to DBUS type according signature
`convert` - function to convert dbus object to python object.
`converter` - decorator to convert dbus object to python object.
`exception_wrapper` - decorator to convert dbus exception to pympris exception.
`available_players` - function searchs and returns unique names of objects
                      which implemented MPRIS2 interfaces.

"""

import sys
from functools import wraps, partial
from collections import namedtuple
import dbus

PY3 = (sys.version_info[0] == 3)
MPRIS_NAME_PREFIX = "org.mpris.MediaPlayer2"


def convert2dbus(value, signature):
    """Convert Python type to dbus type according signature"""
    if len(signature) == 2 and signature.startswith('a'):
        return dbus.Array(value, signature=signature[-1])
    dbus_string_type = dbus.String if PY3 else dbus.UTF8String
    type_map = {
        'b': dbus.Boolean, 'y': dbus.Byte, 'n': dbus.Int16,
        'i': dbus.Int32, 'x': dbus.Int64, 'q': dbus.UInt16, 'u': dbus.UInt32,
        't': dbus.UInt64, 'd': dbus.Double, 'o': dbus.ObjectPath,
        'g': dbus.Signature, 's': dbus_string_type}
    return type_map[signature](value)


def convert(dbus_obj):
    """Convert dbus_obj from dbus type to python type"""
    _isinstance = partial(isinstance, dbus_obj)
    ConvertType = namedtuple('ConvertType', 'pytype dbustypes')

    pyint = ConvertType(int, (dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64,
                              dbus.UInt16, dbus.UInt32, dbus.UInt64))
    pybool = ConvertType(bool, (dbus.Boolean, ))
    pyfloat = ConvertType(float, (dbus.Double, ))
    pylist = ConvertType(lambda _obj: list(map(convert, dbus_obj)),
                         (dbus.Array, ))
    pytuple = ConvertType(lambda _obj: tuple(map(convert, dbus_obj)),
                          (dbus.Struct, ))
    types_str = (dbus.ObjectPath, dbus.Signature, dbus.String)
    if not PY3:
        types_str += (dbus.UTF8String,)
    pystr = ConvertType(str if PY3 else unicode, types_str)

    pydict = ConvertType(
        lambda _obj: dict(zip(map(convert, dbus_obj.keys()),
                              map(convert, dbus_obj.values())
                              )
                          ),
        (dbus.Dictionary, )
    )

    for conv in (pyint, pybool, pyfloat, pylist, pytuple, pystr, pydict):
        if any(map(_isinstance, conv.dbustypes)):
            return conv.pytype(dbus_obj)
    else:
        return dbus_obj


def converter(f):
    """Decorator to convert from dbus type to Python type"""
    @wraps(f)
    def wrapper(*args, **kwds):
        return convert(f(*args, **kwds))
    return wrapper


def exception_wrapper(f):
    """Decorator to convert dbus exception to pympris exception"""
    @wraps(f)
    def wrapper(*args, **kwds):
        try:
            return f(*args, **kwds)
        except dbus.exceptions.DBusException as err:
            _args = err.args
            raise PyMPRISException(*_args)
    return wrapper


def available_players():
    """Search and return set of unique names of objects
    which implemented MPRIS2 interfaces."""
    bus = dbus.SessionBus()
    players = set()
    for name in filter(lambda item: item.startswith(MPRIS_NAME_PREFIX),
                       bus.list_names()):
        owner_name = bus.get_name_owner(name)
        players.add(convert(owner_name))
    return players


class PyMPRISException(Exception):

    """docstring for PyMPRISException"""

    def __init__(self, *args):
        super(PyMPRISException, self).__init__(*args)
{'fraction_non_alphanumeric': 0.11057994716316517, 'fraction_numerical': 0.011322178890426469, 'mean_word_length': 2.8038277511961724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64378', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2492, 'n_words': 663}