Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit

Permalink
dffml: sources: file: Added bz2 support
Browse files Browse the repository at this point in the history
* dffml: sources: file: Added bz2 support
* tests: source: test_file: Added tests for bz2

Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
yashlamba authored and pdxjohnny committed Mar 24, 2019
1 parent 6e3167e commit c7c45c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New Model creation tutorial
- Added update functionality to the CSV source
- Added support for Gzip file source
- Added support for bz2 file source
### Changed
- Restructured documentation to docs folder and moved from rST to markdown
- Git feature cloc logs if no binaries are in path
Expand Down
7 changes: 6 additions & 1 deletion dffml/source/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import os
import abc
import asyncio
import gzip
import gzip
import bz2
from .source import Source
from .log import LOGGER

Expand Down Expand Up @@ -42,6 +43,8 @@ async def _open(self):
return
if self.filename[::-1].startswith(('.gz')[::-1]):
opener = gzip.open(self.filename, 'rt')
elif self.filename[::-1].startswith(('.bz2')[::-1]):
opener = bz2.open(self.filename, 'rt')
else:
opener = open(self.filename, 'r')
with opener as fd:
Expand All @@ -54,6 +57,8 @@ async def _close(self):
if not self.readonly:
if self.filename[::-1].startswith(('.gz')[::-1]):
close = gzip.open(self.filename, 'wt')
elif self.filename[::-1].startswith(('.bz2')[::-1]):
close = bz2.open(self.filename, 'wt')
else:
close = open(self.filename, 'w')
with close as fd:
Expand Down
15 changes: 15 additions & 0 deletions tests/source/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ async def test_open_gz(self):
await source.open()
m_open.assert_called_once_with('testfile.gz', 'rt')

async def test_open_bz2(self):
source = FakeFileSource('testfile.bz2')
m_open = mock_open()
with patch('os.path.exists', return_value=True), \
patch('bz2.open', m_open):
await source.open()
m_open.assert_called_once_with('testfile.bz2', 'rt')

async def test_open_no_file(self):
source = FakeFileSource('testfile')
with patch('os.path.isfile', return_value=False):
Expand All @@ -91,6 +99,13 @@ async def test_close_gz(self):
await source.close()
m_open.assert_called_once_with('testfile.gz', 'wt')

async def test_close_bz2(self):
source = FakeFileSource('testfile.bz2')
m_open = mock_open()
with patch('bz2.open', m_open):
await source.close()
m_open.assert_called_once_with('testfile.bz2', 'wt')

async def test_close_readonly(self):
source = FakeFileSource('testfile:ro')
m_open = mock_open()
Expand Down

0 comments on commit c7c45c1

Please sign in to comment.