# Path: concert/coroutines/base.py
# def background(coroutine):
#     """Same as :func:`.start`, just meant to be used as a decorator."""
#     @functools.wraps(coroutine)
#     def inner(*args, **kwargs):
#         return start(coroutine(*args, **kwargs))
# 
#     return inner
# 
# async def feed_queue(producer, func, *args):
#     """Feed function *func* with items from *producer* in a separete thread. The signatute must be
#     func(queue, \*args) where elements in the queue are instances of
#     :class:`concert.helpers.PrioItem`.
#     """
#     loop = asyncio.get_running_loop()
#     pqueue = queue.PriorityQueue()
# 
#     try:
#         future = loop.run_in_executor(None, func, pqueue, *args)
#         prio = 1
#         async for item in producer:
#             pqueue.put(PrioItem(priority=prio, data=item))
#             prio += 1
#     except (asyncio.CancelledError, KeyboardInterrupt) as e:
#         LOG.log(concert.config.AIODEBUG, f'feed_queue cancelled by exception {type(e)}')
#         # Highest priority, effectively cancel processing of everything in the queue
#         prio = 0
#         raise
#     finally:
#         LOG.log(concert.config.AIODEBUG, f'feed_queue finished with priority {prio}')
#         pqueue.put(PrioItem(priority=prio, data=None))
#         await future
# 
# Path: concert/writers.py
# class TiffWriter(ImageWriter):
#     def __init__(self, filename, bytes_per_file, append=False):
#         import tifffile
#         # 2 ** 25 from tifffile
#         self._writer = tifffile.TiffWriter(filename, append=append,
#                                            bigtiff=bytes_per_file >= 2 ** 32 - 2 ** 25)
# 
#     def write(self, image):
#         self._writer.save(image)


import contextlib
import os
import logging
import tifffile
    import fabio
from logging import FileHandler, Formatter
from concert.coroutines.base import background, feed_queue
from concert.writers import TiffWriter
    from libtiff import TIFF

    def exists(self, *paths):
        return os.path.join(*paths) in self._paths

    def _descend(self, name):
        self._current = os.path.join(self._current, name)
        self._paths.add(self._current)

    def _ascend(self):
        if self._current != self._root:
            self._current = os.path.dirname(self._current)

    @background
    async def write(self, producer, dsetname=None):
        dsetname = dsetname or self.dsetname
        path = os.path.join(self._current, dsetname)

        i = 0
        async for item in producer:
            self._paths.add(os.path.join(path, str(i)))
            i += 1


class DirectoryWalker(Walker):

    """
    A DirectoryWalker moves through a file system and writes flat files using a
    specific filename template.
    """


# Path: concert/coroutines/base.py
# def background(coroutine):
#     """Same as :func:`.start`, just meant to be used as a decorator."""
#     @functools.wraps(coroutine)
#     def inner(*args, **kwargs):
#         return start(coroutine(*args, **kwargs))
# 
#     return inner
# 
# async def feed_queue(producer, func, *args):
#     """Feed function *func* with items from *producer* in a separete thread. The signatute must be
#     func(queue, \*args) where elements in the queue are instances of
#     :class:`concert.helpers.PrioItem`.
#     """
#     loop = asyncio.get_running_loop()
#     pqueue = queue.PriorityQueue()
# 
#     try:
#         future = loop.run_in_executor(None, func, pqueue, *args)
#         prio = 1
#         async for item in producer:
#             pqueue.put(PrioItem(priority=prio, data=item))
#             prio += 1
#     except (asyncio.CancelledError, KeyboardInterrupt) as e:
#         LOG.log(concert.config.AIODEBUG, f'feed_queue cancelled by exception {type(e)}')
#         # Highest priority, effectively cancel processing of everything in the queue
#         prio = 0
#         raise
#     finally:
#         LOG.log(concert.config.AIODEBUG, f'feed_queue finished with priority {prio}')
#         pqueue.put(PrioItem(priority=prio, data=None))
#         await future
# 
# Path: concert/writers.py
# class TiffWriter(ImageWriter):
#     def __init__(self, filename, bytes_per_file, append=False):
#         import tifffile
#         # 2 ** 25 from tifffile
#         self._writer = tifffile.TiffWriter(filename, append=append,
#                                            bigtiff=bytes_per_file >= 2 ** 32 - 2 ** 25)
# 
#     def write(self, image):
#         self._writer.save(image)


# Path: concert/storage.py
import contextlib
import os
import logging
import tifffile
    import fabio
from logging import FileHandler, Formatter
from concert.coroutines.base import background, feed_queue
from concert.writers import TiffWriter
    from libtiff import TIFF


    def exists(self, *paths):
        return os.path.join(*paths) in self._paths

    def _descend(self, name):
        self._current = os.path.join(self._current, name)
        self._paths.add(self._current)

    def _ascend(self):
        if self._current != self._root:
            self._current = os.path.dirname(self._current)

    @background
    async def write(self, producer, dsetname=None):
        dsetname = dsetname or self.dsetname
        path = os.path.join(self._current, dsetname)

        i = 0
        async for item in producer:
            self._paths.add(os.path.join(path, str(i)))
            i += 1


class DirectoryWalker(Walker):

    """
    A DirectoryWalker moves through a file system and writes flat files using a
    specific filename template.
    """


    def __init__(self, writer=TiffWriter, dsetname='frame_{:>06}.tif', start_index=0,