Add apachelog.file.open, which makes it easy to ignore compressession in processors.
authorW. Trevor King <wking@drexel.edu>
Sat, 18 Feb 2012 18:40:47 +0000 (13:40 -0500)
committerW. Trevor King <wking@drexel.edu>
Sat, 18 Feb 2012 18:40:47 +0000 (13:40 -0500)
apachelog/file.py [new file with mode: 0644]

diff --git a/apachelog/file.py b/apachelog/file.py
new file mode 100644 (file)
index 0000000..5d0c95e
--- /dev/null
@@ -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')