File: compression.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (29 lines) | stat: -rw-r--r-- 722 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import commands, os

_uncompress_table = {
    ".bz": "bzip2",
    ".BZ": "bzip2",
    ".gz": "gzip",
    ".GZ": "gzip",
    ".Z": "compress",
    }

def open_file(filename, mode = "rb"):
    ext = os.path.splitext(filename)[1]
    type = _uncompress_table.get(ext)
    if type is None:
        return open(filename, mode)
    if type == "gzip":
        import gzip
        gzip.open(filename, mode)
    if type == "bzip2":
        cmd = "bzcat --decompress"
        cmd += commands.mkarg(filename)
        return os.popen(cmd, mode)
    if type == "compress":
        cmd = "zcat -d"
        cmd += commands.mkarg(filename)
        return os.popen(cmd, mode)
    raise AssertionError("What's a %r?" % type)