Exemplo n.º 1
0
 def _get_model_path_and_name(self):
     model_path = self.config.get('model_path', '')
     if not model_path:
         raise ValueError('Model path should not be empty')
     model_name, extension = os_splitext(os_split(model_path)[1])
     if  extension!= '.h5':
         raise ValueError("This macro requires a .h5 file, {} given".format(extension))
     return model_path, model_name
Exemplo n.º 2
0
 def get_inputs(self):
     self.input_folder = Folder(
         get_input_names_for_role("input_folder_id")[0])
     output_folder_id = get_output_names_for_role("output_folder_id")[0]
     self.output_folder = Folder(output_folder_id)
     self.output_file_path = get_recipe_config()['output_model_path']
     self.batch_size = int(get_recipe_config()['batch_size'])
     if not get_recipe_config()['show_batch_size']:
         self.batch_size = -1
     self.overwrite_output_model = get_recipe_config(
     )['overwrite_output_model']
     self.model_path = get_recipe_config()['model_path']
     self.model_name = os_splitext(os_split(self.model_path)[1])[0]
     self.float_32 = get_recipe_config()["float_32"]
Exemplo n.º 3
0
def get_io(fileobj: AudioIO, mod_path: list = [], cached: bool = True, blacklist: list = []) -> DevIO:
    """ get_io(fileobj, mod_path=[], cached=True, blacklist=[]) -> Finds a
    audio device that can take the data read from fileobj and returns it.

    """

    # IO device cache dictionary
    global __io_cache

    from .import_util import load_lazy_import, unload_lazy_import

    # Get the file input data type.
    annotations = getattr(getattr(fileobj, "read"), "__annotations__", {})
    file_input = annotations.get("return", str)

    # Get the file output data type.
    annotations = getattr(getattr(fileobj, "write"), "__annotations__", {})
    file_output = annotations.get("data", str)

    if cached:
        # Load and already cached audio device.
        if file_input in __io_cache:
            return __io_cache[file_input]
        elif file_output in __io_cache:
            return __io_cache[file_output]

    # Get a list of modules ending in '_io.py'
    mod_list = _build_mod_list(mod_path, "_io.py", blacklist)

    device = None
    dummy = None

    # Make importing lazy.
    # load_lazy_import(mod_path=mod_path)

    # This packages name.
    this_pkgname = __name__.split(".", 1)[0]

    # Load the codec module that can handle file.
    for path, name in mod_list:
        # Get the package name from path.
        pkgname = os_basename(path.rstrip("/"))

        # Import the package if it is different from this one.
        if pkgname != this_pkgname and pkgname:
            try:
                __import__(pkgname)
            except ImportError as err:
                continue

        # Load the module.
        module = import_module(".%s" % os_splitext(name)[0], pkgname)

        # Get the filetypes and handler from module.
        supported_dict = getattr(module, "__supported_dict", {})

        handler = getattr(module, supported_dict.get("handler", "dummy"), None)

        if not handler:
            continue

        # Try not to use the dummy.
        if "dummy" in name:
            dummy = handler
            continue

        # Check the module dependencies.
        dependencies = supported_dict.get("dependencies", {})
        if not _check_dependencies(dependencies):
            continue

        input_t = supported_dict.get("input", [])
        output_t = supported_dict.get("output", [])

        default = supported_dict.get("default", False)

        # Add device input to io cache
        __io_cache.update(((key, handler) for key in input_t))

        # Add device output to io cache.
        __io_cache.update(((key, handler) for key in output_t))

        # Check if filename is supported.
        if "r" in fileobj.mode and file_input in output_t:
            device = handler
            if default:
                break
        elif "w" in fileobj.mode and file_output in input_t:
            device = handler
            if default:
                break

    # Turn off lazy imports.
    # unload_lazy_import()

    # No device was found so use the dummy_io.
    if not device:
        device = dummy

    return device
Exemplo n.º 4
0
def get_codec(filename: str, mod_path: list = [], cached: bool = True, blacklist: list = []) -> AudioIO:
    """ get_codec(filename, mod_path=[], cached=True, blacklist=[]) -> Load the
    codecs in the path and return the first one that can play the file, or the
    one with the default attribute set.

        filename        The file the codec needs to handle
        mod_path        Additional search paths for modules
        cached          Use cached codecs if available
        blacklist       Modules not to load

    """

    # Codec cache dictionary
    global __codec_cache

    from urllib.parse import urlparse

    from .import_util import load_lazy_import, unload_lazy_import

    # Get the file extension.
    file_ext = os_splitext(filename)[1].lower()

    # Get protocol.
    file_prot = urlparse(filename).scheme

    if cached:
        # Load and already cached codec.
        if file_ext in __codec_cache:
            return __codec_cache[file_ext]
        elif file_prot in __codec_cache:
            return __codec_cache[file_prot]

    # Get a list of modules ending in '_file.py'
    mod_list = _build_mod_list(mod_path, "_file.py", blacklist)

    codec = None
    dummy = None

    # Make importing lazy.
    # load_lazy_import(mod_path=mod_path)

    # This packages name.
    this_pkgname = __name__.split(".", 1)[0]

    # Load the codec module that can handle file.
    for path, name in mod_list:
        # Get the package name from path.
        pkgname = os_basename(path.rstrip("/"))

        # Import the package if it is different from this one.
        if pkgname != this_pkgname and pkgname:
            try:
                __import__(pkgname)
            except ImportError as err:
                continue

        # Load the module.
        try:
            module = import_module(".%s" % os_splitext(name)[0], pkgname)
        except ImportError as err:
            print("Skipping module: (%s) because of error: %s" % (name, err))
            continue

        # Get the filetypes and handler from module.
        supported_dict = getattr(module, "__supported_dict", {})

        # Get the handler.
        handler = getattr(module, supported_dict.get("handler", "dummy"), None)

        # Don't even check this module if it does not have a handler.
        if not handler:
            continue

        # Try not to use the dummy handler.
        if "dummy" in name:
            dummy = handler
            continue

        # Check the module dependencies.
        dependencies = supported_dict.get("dependencies", {})
        if not _check_dependencies(dependencies):
            continue

        issupported = supported_dict.get("issupported", lambda *a: False)
        ext = supported_dict.get("ext", [])
        protocol = supported_dict.get("protocol", [])

        default = supported_dict.get("default", False)

        # Add filetype handlers to the codec cache.
        __codec_cache.update(((key, handler) for key in ext))

        # Add protocol handlers to the codec cache.
        __codec_cache.update(((key, handler) for key in protocol))

        # Check if filename is supported.
        if issupported(filename) or file_ext in ext or file_prot in protocol:
            codec = handler
            if default:
                break
        elif not codec and ".*" in ext:
            codec = handler

    # Turn off lazy imports.
    # unload_lazy_import()

    # No codec was found so default to the dummy codec.
    if not codec:
        codec = dummy

    return codec
Exemplo n.º 5
0
def get_io(fileobj, mod_path=[], cached=True, blacklist=[]):
    """ get_io(fileobj, mod_path=[], cached=True, blacklist=[]) -> Finds a
    audio device that can take the data read from fileobj and returns it.

    """

    # IO device cache dictionary
    global __io_cache

    from .import_util import load_lazy_import, unload_lazy_import

    # Get the file input data type.
    annotations = getattr(getattr(fileobj, 'read'), '__annotations__', {})
    file_input = annotations.get('return', unicode)

    # Get the file output data type.
    annotations = getattr(getattr(fileobj, 'write'), '__annotations__', {})
    file_output = annotations.get('data', unicode)

    if cached:
        # Load and already cached audio device.
        if file_input in __io_cache:
            return __io_cache[file_input]
        elif file_output in __io_cache:
            return __io_cache[file_output]

    # Get a list of modules ending in '_io.py'
    mod_list = _build_mod_list(mod_path, '_io.py', blacklist)

    device = None
    dummy = None

    # Make importing lazy.
    # load_lazy_import(mod_path=mod_path)

    # This packages name.
    this_pkgname = __name__.split('.', 1)[0]

    # Load the codec module that can handle file.
    for path, name in mod_list:
        # Get the package name from path.
        pkgname = os_basename(path.rstrip('/'))

        # Import the package if it is different from this one.
        if pkgname != this_pkgname and pkgname:
            try:
                __import__(pkgname)
            except ImportError as err:
                continue

        # Load the module.
        module = import_module('.%s' % os_splitext(name)[0], pkgname)

        # Get the filetypes and handler from module.
        supported_dict = getattr(module, '__supported_dict', {})

        handler = getattr(module, supported_dict.get('handler', 'dummy'), None)

        if not handler:
            continue

        # Try not to use the dummy.
        if 'dummy' in name:
            dummy = handler
            continue

        # Check the module dependencies.
        dependencies = supported_dict.get('dependencies', {})
        if not _check_dependencies(dependencies):
            continue

        input_t = supported_dict.get('input', [])
        output_t = supported_dict.get('output', [])

        default = supported_dict.get('default', False)

        # Add device input to io cache
        __io_cache.update(((key, handler) for key in input_t))

        # Add device output to io cache.
        __io_cache.update(((key, handler) for key in output_t))

        # Check if filename is supported.
        if 'r' in fileobj.mode and file_input in output_t:
            device = handler
            if default: break
        elif 'w' in fileobj.mode and file_output in input_t:
            device = handler
            if default: break

    # Turn off lazy imports.
    # unload_lazy_import()

    # No device was found so use the dummy_io.
    if not device: device = dummy

    return device
Exemplo n.º 6
0
def get_codec(filename, mod_path=[], cached=True, blacklist=[]):
    """ get_codec(filename, mod_path=[], cached=True, blacklist=[]) -> Load the
    codecs in the path and return the first one that can play the file, or the
    one with the default attribute set.

        filename        The file the codec needs to handle
        mod_path        Additional search paths for modules
        cached          Use cached codecs if available
        blacklist       Modules not to load

    """

    # Codec cache dictionary
    global __codec_cache

    from urlparse import urlparse

    from .import_util import load_lazy_import, unload_lazy_import

    # Get the file extension.
    file_ext = os_splitext(filename)[1].lower()

    # Get protocol.
    file_prot = urlparse(filename).scheme

    if cached:
        # Load and already cached codec.
        if file_ext in __codec_cache:
            return __codec_cache[file_ext]
        elif file_prot in __codec_cache:
            return __codec_cache[file_prot]

    # Get a list of modules ending in '_file.py'
    mod_list = _build_mod_list(mod_path, '_file.py', blacklist)

    codec = None
    dummy = None

    # Make importing lazy.
    # load_lazy_import(mod_path=mod_path)

    # This packages name.
    this_pkgname = __name__.split('.', 1)[0]

    # Load the codec module that can handle file.
    for path, name in mod_list:
        # Get the package name from path.
        pkgname = os_basename(path.rstrip('/'))

        # Import the package if it is different from this one.
        if pkgname != this_pkgname and pkgname:
            try:
                __import__(pkgname)
            except ImportError as err:
                continue

        # Load the module.
        try:
            module = import_module('.%s' % os_splitext(name)[0], pkgname)
        except ImportError as err:
            print("Skipping module: (%s) because of error: %s" % (name, err))
            continue

        # Get the filetypes and handler from module.
        supported_dict = getattr(module, '__supported_dict', {})

        # Get the handler.
        handler = getattr(module, supported_dict.get('handler', 'dummy'), None)

        # Don't even check this module if it does not have a handler.
        if not handler:
            continue

        # Try not to use the dummy handler.
        if 'dummy' in name:
            dummy = handler
            continue

        # Check the module dependencies.
        dependencies = supported_dict.get('dependencies', {})
        if not _check_dependencies(dependencies):
            continue

        issupported = supported_dict.get('issupported', lambda *a: False)
        ext = supported_dict.get('ext', [])
        protocol = supported_dict.get('protocol', [])

        default = supported_dict.get('default', False)

        # Add filetype handlers to the codec cache.
        __codec_cache.update(((key, handler) for key in ext))

        # Add protocol handlers to the codec cache.
        __codec_cache.update(((key, handler) for key in protocol))

        # Check if filename is supported.
        if issupported(filename) or file_ext in ext or file_prot in protocol:
            codec = handler
            if default: break
        elif not codec and '.*' in ext:
            codec = handler

    # Turn off lazy imports.
    # unload_lazy_import()

    # No codec was found so default to the dummy codec.
    if not codec: codec = dummy

    return codec
Exemplo n.º 7
0
def main(args: dict) -> None:
    """ Encode args['filename'] times.

    """

    from os.path import basename as os_basename
    from os.path import isfile as os_isfile
    from os.path import splitext as os_splitext
    from sys import stdin as sys_stdin
    from select import select
    from time import sleep as time_sleep
    from termios import tcgetattr, tcsetattr, ECHO, ICANON, TCSANOW
    from termios import VMIN, VTIME

    from musio import open_file, open_device

    if args['debug']:
        from musio import io_util
        io_util.DEBUG = True

    filename = args['filename']
    output = os_splitext(os_basename(filename))[0] + '.' + args['filetype']
    output_bytes = output.encode('utf-8', 'surrogateescape')
    output_printable = output_bytes.decode('utf-8', 'ignore')
    if os_isfile(output):
        if input("Overwrite %s (y/n): " % output_printable).lower().startswith('n'):
            return

    # Save the current terminal state.
    normal = tcgetattr(sys_stdin)
    quiet = tcgetattr(sys_stdin)

    # Do not wait for key press and don't echo.
    quiet[3] &= ~(ECHO | ICANON)
    quiet[6][VMIN] = 0
    quiet[6][VTIME] = 0

    # Set the new terminal state.
    tcsetattr(sys_stdin, TCSANOW, quiet)

    # Value returned to tell the calling function whether to quit or
    # not.
    quit_val = True

    if args['filetype'].lower() == 'ogg':
        quality = args['quality'] / 10 if args['quality'] in range(-1, 11) else 0.5
    elif args['filetype'].lower() == 'mp3':
        quality = args['quality'] if args['quality'] in range(0, 10) else 2

    try:
        with open_file(**args) as in_file:
            in_file_title = in_file._info_dict.get('title',
                                                in_file._info_dict['name'])
            comment_dict = {'title': in_file_title}
            comment_dict.update(in_file._info_dict)
            for i in ['title', 'artist', 'album', 'year', 'comment',
                      'track', 'genre']:
                if args.get(i, ''):
                    comment_dict[i] = args[i]

            with open_file(output, 'w', depth=in_file.depth, rate=in_file.rate,
                        channels=in_file.channels, quality=quality,
                        comment_dict=comment_dict) as out_file:
                in_file.loops = 0

                if args['show_position']:
                    filename_bytes = filename.encode('utf-8', 'surrogateescape')
                    filename_printable = filename_bytes.decode('utf-8', 'ignore')
                    print("Encoding: %s to %s" % (filename_printable, output_printable))
                    print(in_file)

                for data in in_file:
                    if args['show_position']:
                        if in_file.length > 0:
                            # Calculate the percentage played.
                            pos = (in_file.position * 100) / in_file.length

                            # Make the string.
                            pos_str = 'Position: %.2f%%' % pos

                            # Find the length of the string.
                            format_len = len(pos_str) + 2

                            # Print the string and after erasing the old
                            # one using ansi escapes.
                            print('\033[%dD\033[K%s' % (format_len, pos_str),
                                  end='', flush=True)
                    out_file.write(data)

                    # Check for input.
                    r, _, _ = select([sys_stdin], [], [], 0)

                    # Get input if there was any otherwise continue.
                    if r:
                        command = r[0].readline().lower()
                        # Handle input commands.
                        if command.startswith('q'):
                            quit_val = False
                            break
                        elif command == '\n':
                            break

    except Exception as err:
        print("Error: %s" % err, flush=True)
        raise(err)
    finally:
        # Re-set the terminal state.
        tcsetattr(sys_stdin, TCSANOW, normal)

    if args['show_position']:
        print("\nDone.")

    return quit_val
Exemplo n.º 8
0
def main(args):
    """ Encode args['filename'] times.

    """

    from os.path import basename as os_basename
    from os.path import isfile as os_isfile
    from os.path import splitext as os_splitext
    from sys import stdin as sys_stdin
    from sys import stdout as sys_stdout
    from select import select
    from time import sleep as time_sleep
    from termios import tcgetattr, tcsetattr, ECHO, ICANON, TCSANOW
    from termios import VMIN, VTIME

    from musio import open_file, open_device

    if args['debug']:
        from musio import io_util
        io_util.DEBUG = True

    filename = args['filename']
    output = os_splitext(os_basename(filename))[0] + '.' + args['filetype']
    output_bytes = output.encode('utf-8', 'surrogateescape')
    output_printable = output_bytes.decode('utf-8', 'ignore')
    if os_isfile(output):
        if raw_input("Overwrite %s (y/n): " % output_printable).lower().startswith('n'):
            return

    # Save the current terminal state.
    normal = tcgetattr(sys_stdin)
    quiet = tcgetattr(sys_stdin)

    # Do not wait for key press and don't echo.
    quiet[3] &= ~(ECHO | ICANON)
    quiet[6][VMIN] = 0
    quiet[6][VTIME] = 0

    # Set the new terminal state.
    tcsetattr(sys_stdin, TCSANOW, quiet)

    # Value returned to tell the calling function whether to quit or
    # not.
    quit_val = True

    if args['filetype'].lower() == 'ogg':
        quality = args['quality'] / 10 if args['quality'] in range(-1, 11) else 0.5
    elif args['filetype'].lower() == 'mp3':
        quality = args['quality'] if args['quality'] in range(0, 10) else 2

    try:
        with open_file(**args) as in_file:
            in_file_title = in_file._info_dict.get('title',
                                                in_file._info_dict['name'])
            comment_dict = {'title': in_file_title}
            comment_dict.update(in_file._info_dict)
            for i in ['title', 'artist', 'album', 'year', 'comment',
                      'track', 'genre']:
                if args.get(i, ''):
                    comment_dict[i] = args[i]

            with open_file(output, 'w', depth=in_file.depth, rate=in_file.rate,
                        channels=in_file.channels, quality=quality,
                        comment_dict=comment_dict) as out_file:
                in_file.loops = 0

                if args['show_position']:
                    filename_bytes = filename.encode('utf-8', 'surrogateescape')
                    filename_printable = filename_bytes.decode('utf-8', 'ignore')
                    print("Encoding: %s to %s" % (filename, output))
                    print(in_file)

                for data in in_file:
                    if args['show_position']:
                        if in_file.length > 0:
                            # Calculate the percentage played.
                            pos = (in_file.position * 100) / float(in_file.length)

                            # Make the string.
                            pos_str = 'Position: %.2f%%' % pos

                            # Find the length of the string.
                            format_len = len(pos_str) + 2

                            # Print the string and after erasing the old
                            # one using ansi escapes.
                            print('\033[%dD\033[K%s' % (format_len, pos_str),
                                  end='')
                            sys_stdout.flush()
                    out_file.write(data)

                    # Check for input.
                    r, _, _ = select([sys_stdin], [], [], 0)

                    # Get input if there was any otherwise continue.
                    if r:
                        command = r[0].readline().lower()
                        # Handle input commands.
                        if command.startswith('q'):
                            quit_val = False
                            break
                        elif command == '\n':
                            break

    except Exception as err:
        print("Error: %s" % err)
        raise(err)
    finally:
        # Re-set the terminal state.
        tcsetattr(sys_stdin, TCSANOW, normal)

    if args['show_position']:
        print("\nDone.")

    return quit_val