comparison mercurial/util.py @ 30265:6a8aff737a17

util: put compression code next to each other ctxmanager was injecting itself between the compression and decompression code. Let's restore some order.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 15 Oct 2016 17:24:01 -0700
parents 7356e6b1f5b8
children 8321b083a83d
comparison
equal deleted inserted replaced
30264:dd3dd80fca10 30265:6a8aff737a17
2792 pos = path.rfind('/') 2792 pos = path.rfind('/')
2793 while pos != -1: 2793 while pos != -1:
2794 yield path[:pos] 2794 yield path[:pos]
2795 pos = path.rfind('/', 0, pos) 2795 pos = path.rfind('/', 0, pos)
2796 2796
2797 # compression utility
2798
2799 class nocompress(object):
2800 def compress(self, x):
2801 return x
2802 def flush(self):
2803 return ""
2804
2805 compressors = {
2806 None: nocompress,
2807 # lambda to prevent early import
2808 'BZ': lambda: bz2.BZ2Compressor(),
2809 'GZ': lambda: zlib.compressobj(),
2810 }
2811 # also support the old form by courtesies
2812 compressors['UN'] = compressors[None]
2813
2814 def _makedecompressor(decompcls):
2815 def generator(f):
2816 d = decompcls()
2817 for chunk in filechunkiter(f):
2818 yield d.decompress(chunk)
2819 def func(fh):
2820 return chunkbuffer(generator(fh))
2821 return func
2822
2823 class ctxmanager(object): 2797 class ctxmanager(object):
2824 '''A context manager for use in 'with' blocks to allow multiple 2798 '''A context manager for use in 'with' blocks to allow multiple
2825 contexts to be entered at once. This is both safer and more 2799 contexts to be entered at once. This is both safer and more
2826 flexible than contextlib.nested. 2800 flexible than contextlib.nested.
2827 2801
2878 del self._atexit 2852 del self._atexit
2879 if pending: 2853 if pending:
2880 raise exc_val 2854 raise exc_val
2881 return received and suppressed 2855 return received and suppressed
2882 2856
2857 # compression utility
2858
2859 class nocompress(object):
2860 def compress(self, x):
2861 return x
2862 def flush(self):
2863 return ""
2864
2865 compressors = {
2866 None: nocompress,
2867 # lambda to prevent early import
2868 'BZ': lambda: bz2.BZ2Compressor(),
2869 'GZ': lambda: zlib.compressobj(),
2870 }
2871 # also support the old form by courtesies
2872 compressors['UN'] = compressors[None]
2873
2874 def _makedecompressor(decompcls):
2875 def generator(f):
2876 d = decompcls()
2877 for chunk in filechunkiter(f):
2878 yield d.decompress(chunk)
2879 def func(fh):
2880 return chunkbuffer(generator(fh))
2881 return func
2882
2883 def _bz2(): 2883 def _bz2():
2884 d = bz2.BZ2Decompressor() 2884 d = bz2.BZ2Decompressor()
2885 # Bzip2 stream start with BZ, but we stripped it. 2885 # Bzip2 stream start with BZ, but we stripped it.
2886 # we put it back for good measure. 2886 # we put it back for good measure.
2887 d.decompress('BZ') 2887 d.decompress('BZ')