Skip to content

Commit

Permalink
Add Formats.list_extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Jul 20, 2017
1 parent cbee1e5 commit 38684fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v3.0.6 (dev)
------------
* Added 'list_extensions' method to xphyle.formats.Formats

v3.0.5 (2017.07.19)
-------------------
Expand Down
7 changes: 7 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def test_list_formats(self):
set(('gzip','gz','pigz')),
set(get_format('gzip').aliases))

def test_list_extensions(self):
self.assertSetEqual(
set((
'.gz', '.bgz', '.bz2', '.bzip', '.bzip2', '.xz', '.lzma',
'.7z', '.7zip')),
set(FORMATS.list_extensions(True)))

def test_guess_format(self):
self.assertEqual('gzip', FORMATS.guess_compression_format('gz'))
self.assertEqual('gzip', FORMATS.guess_compression_format('.gz'))
Expand Down
17 changes: 15 additions & 2 deletions xphyle/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from xphyle.progress import PROCESS_PROGRESS, iter_file_chunked
from xphyle.types import (
FileMode, ModeCoding, ModeArg, PathOrFile, FileLike, Union, Callable,
Iterable, Iterator, List, Tuple, ModuleType, PathLike, FileLikeInterface,
FileLikeBase, AnyStr, AnyChar, IO, cast)
Iterable, Iterator, List, Tuple, Set, ModuleType, PathLike,
FileLikeInterface, FileLikeBase, AnyStr, AnyChar, IO, cast)

class ThreadsVar(object):
"""Maintain ``threads`` variable.
Expand Down Expand Up @@ -1117,6 +1117,19 @@ def list_compression_formats(self) -> Tuple:
"""
return tuple(self.compression_formats.keys())

def list_extensions(self, with_sep: bool = False) -> Iterable[str]:
"""Returns an iterable with all valid extensions.
Args:
with_sep: Add separator prefix to each extension.
"""
exts = set() # type: Set[str]
for fmt in self.compression_formats.values():
exts.update(fmt.exts)
if with_sep:
exts = set("{}{}".format(os.extsep, ext) for ext in exts)
return exts

def get_compression_format(self, name: str) -> CompressionFormat:
"""Returns the CompressionFormat associated with the given name.
Expand Down

0 comments on commit 38684fd

Please sign in to comment.