Back to index

obnam  1.1
compression_plugin.py
Go to the documentation of this file.
00001 # Copyright (C) 2011  Lars Wirzenius
00002 #
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import logging
00018 import os
00019 import zlib
00020 
00021 import obnamlib
00022 
00023 class DeflateCompressionFilter(object):
00024 
00025     def __init__(self, app):
00026         self.tag = "deflate"
00027         self.app = app
00028         self.warned = False
00029 
00030     def filter_read(self, data, repo, toplevel):
00031         return zlib.decompress(data)
00032         
00033     def filter_write(self, data, repo, toplevel):
00034         how = self.app.settings['compress-with']
00035         if how == 'deflate':
00036             data = zlib.compress(data)
00037         elif how == 'gzip':
00038             if not self.warned:
00039                 self.app.ts.notify("--compress-with=gzip is deprecated.  " +
00040                                    "Use --compress-with=deflate instead")
00041                 self.warned = True
00042             data = zlib.compress(data)
00043 
00044         return data
00045     
00046 
00047 class CompressionPlugin(obnamlib.ObnamPlugin):
00048 
00049     def enable(self):
00050         self.app.settings.choice(['compress-with'],
00051                                  ['none', 'deflate', 'gzip'],
00052                                  'use PROGRAM to compress repository with '
00053                                     '(one of none, deflate)',
00054                                  metavar='PROGRAM')
00055         
00056         hooks = [
00057             ('repository-data', DeflateCompressionFilter(self.app),
00058              obnamlib.Hook.EARLY_PRIORITY),
00059         ]
00060         for name, callback, prio in hooks:
00061             self.app.hooks.add_callback(name, callback, prio)