Add apachelog.file.open, which makes it easy to ignore compressession in processors.
[apachelog.git] / apachelog / file.py
1 import gzip as _gzip
2 import os.path as _os_path
3
4
5 """Openers by file extention.
6
7 Values should be callables such that::
8
9   for line in opener(filename, mode):
10       ...
11
12 will work.
13 """
14 OPENERS = {
15     '.gz': _gzip.open,
16     }
17
18
19 def open(filename, openers=None):
20     """Utility method that decompresses files based on their extension.
21
22     Uses ``OPENERS`` to determine the appropriate opener for the
23     file's extension.  If the extension is not listed in ``OPENERS``,
24     fall back to the ``open`` builtin.
25     """
26     if openers is None:
27         openers = OPENERS
28     extension = _os_path.splitext(filename)[-1]
29     opener = openers.get(extension, open)
30     return opener(filename, 'r')