<commit_before>"""
Backend for python-rtmidi:

http://pypi.python.org/pypi/python-rtmidi/
"""
from __future__ import absolute_import
import os
import time
import rtmidi
from ..ports import BaseInput, BaseOutput
from ..parser import parse

def _get_api_lookup():
    api_to_name = {}
    name_to_api = {}

    for name in dir(rtmidi):
        if name.startswith('API_'):
            value = getattr(rtmidi, name)
            name = name.replace('API_', '')
            name_to_api[name] = value
            api_to_name[value] = name

    return api_to_name, name_to_api

_api_to_name, _name_to_api = _get_api_lookup()

def _get_api_id(name=None):
    if name is None:
        return rtmidi.API_UNSPECIFIED

    try:
        api = _name_to_api[name]
    except KeyError:
        raise ValueError('unknown API {}'.format(name))    

    if name in get_api_names():
        return api
    else:
        raise ValueError('API {} not compiled in'.format(name))
    

def get_devices(api=None):
    devices = []

    api = _get_api_id(api)
    input_names = set(rtmidi.MidiIn(rtapi=api).get_ports())
    output_names = set(rtmidi.MidiOut(rtapi=api).get_ports())

    for name in sorted(input_names | output_names):
        devices.append({
                'name' : name,
                'is_input' : name in input_names,
                'is_output' : name in output_names,
                })

    return devices

def get_api_names():
    return [_api_to_name[n] for n in rtmidi.get_compiled_api()]

class PortCommon(object):
    def _open(self, api=None, virtual=False, callback=None):

        api = _get_api_id(api)
        opening_input = hasattr(self, 'receive')

        if opening_input:
            self._rt = rtmidi.MidiIn(rtapi=api)
            self._rt.ignore_types(False, False, False)<commit_msg>Now removed client:port from LINUX_ALSA port names.<commit_after>"""
Backend for python-rtmidi:

http://pypi.python.org/pypi/python-rtmidi/
"""
from __future__ import absolute_import
import os
import time
import rtmidi
from ..ports import BaseInput, BaseOutput
from ..parser import parse

def _get_api_lookup():
    api_to_name = {}
    name_to_api = {}

    for name in dir(rtmidi):
        if name.startswith('API_'):
            value = getattr(rtmidi, name)
            name = name.replace('API_', '')
            name_to_api[name] = value
            api_to_name[value] = name

    return api_to_name, name_to_api

_api_to_name, _name_to_api = _get_api_lookup()

def _get_api_id(name=None):
    if name is None:
        return rtmidi.API_UNSPECIFIED

    try:
        api = _name_to_api[name]
    except KeyError:
        raise ValueError('unknown API {}'.format(name))    

    if name in get_api_names():
        return api
    else:
        raise ValueError('API {} not compiled in'.format(name))
    
def _fix_name(name, is_input, api=None):
    """Remove client:port from LINUX_ALSA port name."""
    # RtMidi adds client:port numbers inconsistently.
    # For input ports:
    #     'Midi Through 14:0'
    # For output ports:
    #     'Midi Through:0'
    # Just remove them.

    if api is None:
        api = get_api_names()[0]

    if api == 'LINUX_ALSA':
        if is_input:
            if ':' in name:
                return name.rsplit(None, 1)[0]
        else:
            if ':' in name:
                return name.rsplit(':', 1)[0]
    
    return name

def get_devices(api=None):
    devices = []

    rtapi = _get_api_id(api)
    input_names = set(rtmidi.MidiIn(rtapi=rtapi).get_ports())
    output_names = set(rtmidi.MidiOut(rtapi=rtapi).get_ports())

    for name in sorted(input_names | output_names):
        devices.append({
                'name': _fix_name(name, (name in input_names), api),
                'is_input': name in input_names,
                'is_output': name in output_names,
                })

    return devices

def get_api_names():
    return [_api_to_name[n] for n in rtmidi.get_compiled_api()]

class PortCommon(object):
    def _open(self, api=None, virtual=False, callback=None):

        rtapi = _get_api_id(api)
        opening_input = hasattr(self, 'receive')

        self.name = _fix_name(self.name, opening_input, api)

        if opening_input:
            self._rt = rtmidi.MidiIn(rtapi=api)
            self._rt.ignore_types(False, False, False)
{'fraction_non_alphanumeric': 0.10943658706236958, 'fraction_numerical': 0.0020867145838163693, 'mean_word_length': 2.541871921182266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '82201', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1427, 'n_words': 332}