From ea45cc5cf6ff5fdace7ced45b537d96213767dc8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 18 Feb 2012 13:40:47 -0500 Subject: [PATCH] Add apachelog.file.open, which makes it easy to ignore compressession in processors. --- apachelog/file.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 apachelog/file.py 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') -- 2.26.2