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

Commit c7c45c1

Browse files
yashlambapdxjohnny
authored andcommitted
dffml: sources: file: Added bz2 support
* dffml: sources: file: Added bz2 support * tests: source: test_file: Added tests for bz2 Signed-off-by: John Andersen <[email protected]>
1 parent 6e3167e commit c7c45c1

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- New Model creation tutorial
1717
- Added update functionality to the CSV source
1818
- Added support for Gzip file source
19+
- Added support for bz2 file source
1920
### Changed
2021
- Restructured documentation to docs folder and moved from rST to markdown
2122
- Git feature cloc logs if no binaries are in path

dffml/source/file.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import os
44
import abc
55
import asyncio
6-
import gzip
6+
import gzip
7+
import bz2
78
from .source import Source
89
from .log import LOGGER
910

@@ -42,6 +43,8 @@ async def _open(self):
4243
return
4344
if self.filename[::-1].startswith(('.gz')[::-1]):
4445
opener = gzip.open(self.filename, 'rt')
46+
elif self.filename[::-1].startswith(('.bz2')[::-1]):
47+
opener = bz2.open(self.filename, 'rt')
4548
else:
4649
opener = open(self.filename, 'r')
4750
with opener as fd:
@@ -54,6 +57,8 @@ async def _close(self):
5457
if not self.readonly:
5558
if self.filename[::-1].startswith(('.gz')[::-1]):
5659
close = gzip.open(self.filename, 'wt')
60+
elif self.filename[::-1].startswith(('.bz2')[::-1]):
61+
close = bz2.open(self.filename, 'wt')
5762
else:
5863
close = open(self.filename, 'w')
5964
with close as fd:

tests/source/test_file.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ async def test_open_gz(self):
7171
await source.open()
7272
m_open.assert_called_once_with('testfile.gz', 'rt')
7373

74+
async def test_open_bz2(self):
75+
source = FakeFileSource('testfile.bz2')
76+
m_open = mock_open()
77+
with patch('os.path.exists', return_value=True), \
78+
patch('bz2.open', m_open):
79+
await source.open()
80+
m_open.assert_called_once_with('testfile.bz2', 'rt')
81+
7482
async def test_open_no_file(self):
7583
source = FakeFileSource('testfile')
7684
with patch('os.path.isfile', return_value=False):
@@ -91,6 +99,13 @@ async def test_close_gz(self):
9199
await source.close()
92100
m_open.assert_called_once_with('testfile.gz', 'wt')
93101

102+
async def test_close_bz2(self):
103+
source = FakeFileSource('testfile.bz2')
104+
m_open = mock_open()
105+
with patch('bz2.open', m_open):
106+
await source.close()
107+
m_open.assert_called_once_with('testfile.bz2', 'wt')
108+
94109
async def test_close_readonly(self):
95110
source = FakeFileSource('testfile:ro')
96111
m_open = mock_open()

0 commit comments

Comments
 (0)