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

Commit

Permalink
dffml: source: file: Added Gzip support
Browse files Browse the repository at this point in the history
* dffml: source: file: Added Gzip support
* tests: source: file: Added test for gzip file source
* CHANGELOG: Added Gzip support
  • Loading branch information
yashlamba authored and pdxjohnny committed Mar 22, 2019
1 parent f2a42ca commit 784a1b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New Feature creation tutorial
- New Model creation tutorial
- Added update functionality to the CSV source
- Added support for Gzip 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
14 changes: 11 additions & 3 deletions dffml/source/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import abc
import asyncio

import gzip
from .source import Source
from .log import LOGGER

Expand Down Expand Up @@ -40,15 +40,23 @@ async def _open(self):
self.filename)
self.mem = {}
return
with open(self.filename, 'r') as fd:
if self.filename[::-1].startswith(('.gz')[::-1]):
opener = gzip.open(self.filename, 'rt')
else:
opener = open(self.filename, 'r')
with opener as fd:
await self.load_fd(fd)

async def close(self):
await asyncio.shield(self._close())

async def _close(self):
if not self.readonly:
with open(self.filename, 'w') as fd:
if self.filename[::-1].startswith(('.gz')[::-1]):
close = gzip.open(self.filename, 'wt')
else:
close = open(self.filename, 'w')
with close as fd:
await self.dump_fd(fd)

@abc.abstractmethod
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 @@ -63,6 +63,14 @@ async def test_open(self):
await source.open()
m_open.assert_called_once_with('testfile', 'r')

async def test_open_gz(self):
source = FakeFileSource('testfile.gz')
m_open = mock_open()
with patch('os.path.exists', return_value=True), \
patch('builtins.open', m_open):
await source.open()
m_open.assert_called_once_with('testfile.gz', 'rb')

async def test_open_no_file(self):
source = FakeFileSource('testfile')
with patch('os.path.isfile', return_value=False):
Expand All @@ -76,6 +84,13 @@ async def test_close(self):
await source.close()
m_open.assert_called_once_with('testfile', 'w')

async def test_close_gz(self):
source = FakeFileSource('testfile.gz')
m_open = mock_open()
with patch('builtins.open', m_open):
await source.close()
m_open.assert_called_once_with('testfile.gz', 'wb')

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

0 comments on commit 784a1b5

Please sign in to comment.