From: W. Trevor King Date: Sat, 18 Feb 2012 18:40:47 +0000 (-0500) Subject: Add apachelog.file.open, which makes it easy to ignore compressession in processors. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ea45cc5cf6ff5fdace7ced45b537d96213767dc8;p=apachelog.git Add apachelog.file.open, which makes it easy to ignore compressession in processors. --- diff --git a/apachelog/file.py b/apachelog/file.py new file mode 100644 index 0000000..5d0c95e --- /dev/null +++ b/apachelog/file.py @@ -0,0 +1,30 @@ +import gzip as _gzip +import os.path as _os_path + + +"""Openers by file extention. + +Values should be callables such that:: + + for line in opener(filename, mode): + ... + +will work. +""" +OPENERS = { + '.gz': _gzip.open, + } + + +def open(filename, openers=None): + """Utility method that decompresses files based on their extension. + + Uses ``OPENERS`` to determine the appropriate opener for the + file's extension. If the extension is not listed in ``OPENERS``, + fall back to the ``open`` builtin. + """ + if openers is None: + openers = OPENERS + extension = _os_path.splitext(filename)[-1] + opener = openers.get(extension, open) + return opener(filename, 'r')