Completed
Pull Request — master (#305)
by Bart
01:24
created

fuel.utils.open_()   A

Complexity

Conditions 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 23
rs 9.0856
1
"""Low-level utilities for reading a variety of source formats."""
2
import codecs
3
import gzip
4
import io
5
import tarfile
6
import six
7
8
9
def open_(filename, mode='r', encoding='utf-8'):
10
    """Open a text file with UTF-8 and optional gzip compression.
11
12
    This function is useful when dealing with text files.
13
14
    Parameters
15
    ----------
16
    filename : str
17
        The filename to read.
18
    mode : str, optional
19
        The mode with which to open the file. Defaults to `r`.
20
    encoding : str, optional
21
        The encoding to use (see the codecs documentation_ for supported
22
        values). Defaults to `utf-8`.
23
24
    .. _documentation:
25
    https://docs.python.org/3/library/codecs.html#standard-encodings
26
27
    """
28
    if filename.endswith('.gz.'):
29
        zf = io.BufferedReader(gzip.open(filename, mode))
30
        return codecs.getreader(encoding)(zf)
31
    return codecs.open(filename, mode, encoding=encoding)
32
33
34
def tar_open(f):
35
    """Open either a filename or a file-like object as a TarFile.
36
37
    Parameters
38
    ----------
39
    f : str or file-like object
40
        The filename or file-like object from which to read.
41
42
    Returns
43
    -------
44
    TarFile
45
        A `TarFile` instance.
46
47
    """
48
    if isinstance(f, six.string_types):
49
        return tarfile.open(name=f)
50
    else:
51
        return tarfile.open(fileobj=f)
52