# Path: muninn/tools/utils.py
# class Processor(object):
# 
#     def __init__(self, archive_name):
#         self._archive_name = archive_name
#         self._archive = None
# 
#     def perform_operation(self, archive, item):
#         pass
# 
#     def __call__(self, item):
#         try:
#             if self._archive is None:
#                 self._archive = muninn.open(self._archive_name)
#             return self.perform_operation(self._archive, item)
#         except KeyboardInterrupt:
#             # don't capture keyboard interrupts inside sub-processes (only the main process should handle it)
#             pass
# 
# def create_parser(*args, **kwargs):
#     parser = argparse.ArgumentParser(*args, parents=[version_parser], **kwargs)
#     parser.add_argument("--verbose", action="store_true", help="display debug information")
#     return parser
# 
# def parse_args_and_run(parser, func):
#     args, unused_args = version_parser.parse_known_args()
#     if args.version:
#         version(os.path.basename(sys.argv[0]))
#         sys.exit(0)
# 
#     args = parser.parse_args(unused_args)
# 
#     log_level = logging.DEBUG if args.verbose else logging.INFO
#     logging.basicConfig(level=log_level, format="%(levelname)s: %(message)s")
# 
#     try:
#         return func(args)
#     except SystemExit:
#         raise
#     except KeyboardInterrupt:
#         sys.exit(1)
#     except muninn.Error as error:
#         logging.error(error)
#         sys.exit(1)
#     except Exception:
#         log_internal_error()
#         sys.exit(1)
#     finally:
#         logging.shutdown()


import argparse
import fnmatch
import glob
import logging
import multiprocessing
import os
import sys
import muninn
    from tqdm import tqdm as bar
from .utils import Processor, create_parser, parse_args_and_run
    except EnvironmentError as error:
        raise Error("unable to expand enclosing directory \"%s\" [%s]" % (path, error))


def expand_identity(path):
    return [path]


def filter_paths(paths, patterns):
    for pattern in patterns:
        paths = filter(lambda path: not fnmatch.fnmatch(os.path.basename(path), pattern), paths)
        # paths = [path for path in path if not fnmatch.fnmatch(os.path.basename(path), pattern)]
    return paths


def get_path_expansion_function(is_stem=False, is_enclosing_directory=False):
    assert not (is_stem and is_enclosing_directory)
    if is_stem:
        return expand_stem
    if is_enclosing_directory:
        return expand_enclosing_directory
    return expand_identity


class IngestProcessor(Processor):

    def __init__(self, args):
        super(IngestProcessor, self).__init__(args.archive)
        assert not args.link or not args.keep
        self.path_expansion_function = get_path_expansion_function(args.path_is_stem, args.path_is_enclosing_directory)

# Path: muninn/tools/utils.py
# class Processor(object):
# 
#     def __init__(self, archive_name):
#         self._archive_name = archive_name
#         self._archive = None
# 
#     def perform_operation(self, archive, item):
#         pass
# 
#     def __call__(self, item):
#         try:
#             if self._archive is None:
#                 self._archive = muninn.open(self._archive_name)
#             return self.perform_operation(self._archive, item)
#         except KeyboardInterrupt:
#             # don't capture keyboard interrupts inside sub-processes (only the main process should handle it)
#             pass
# 
# def create_parser(*args, **kwargs):
#     parser = argparse.ArgumentParser(*args, parents=[version_parser], **kwargs)
#     parser.add_argument("--verbose", action="store_true", help="display debug information")
#     return parser
# 
# def parse_args_and_run(parser, func):
#     args, unused_args = version_parser.parse_known_args()
#     if args.version:
#         version(os.path.basename(sys.argv[0]))
#         sys.exit(0)
# 
#     args = parser.parse_args(unused_args)
# 
#     log_level = logging.DEBUG if args.verbose else logging.INFO
#     logging.basicConfig(level=log_level, format="%(levelname)s: %(message)s")
# 
#     try:
#         return func(args)
#     except SystemExit:
#         raise
#     except KeyboardInterrupt:
#         sys.exit(1)
#     except muninn.Error as error:
#         logging.error(error)
#         sys.exit(1)
#     except Exception:
#         log_internal_error()
#         sys.exit(1)
#     finally:
#         logging.shutdown()


# Path: muninn/tools/ingest.py
import argparse
import fnmatch
import glob
import logging
import multiprocessing
import os
import sys
import muninn
    from tqdm import tqdm as bar
from .utils import Processor, create_parser, parse_args_and_run

    except EnvironmentError as error:
        raise Error("unable to expand enclosing directory \"%s\" [%s]" % (path, error))


def expand_identity(path):
    return [path]


def filter_paths(paths, patterns):
    for pattern in patterns:
        paths = filter(lambda path: not fnmatch.fnmatch(os.path.basename(path), pattern), paths)
        # paths = [path for path in path if not fnmatch.fnmatch(os.path.basename(path), pattern)]
    return paths


def get_path_expansion_function(is_stem=False, is_enclosing_directory=False):
    assert not (is_stem and is_enclosing_directory)
    if is_stem:
        return expand_stem
    if is_enclosing_directory:
        return expand_enclosing_directory
    return expand_identity


class IngestProcessor(Processor):

    def __init__(self, args):
        super(IngestProcessor, self).__init__(args.archive)
        assert not args.link or not args.keep
        self.path_expansion_function = get_path_expansion_function(args.path_is_stem, args.path_is_enclosing_directory)

        self.use_symlinks = args.link