-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actionpack#80: "generalizes ReadBytes and WriteBytes" (#81)
* updates @synchronized nested function name * WriteBytes -> Write * ReadBytes -> Read * updates ProcedureTest
- Loading branch information
1 parent
6148eaf
commit 5633bce
Showing
16 changed files
with
260 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
from actionpack.actions.call import Call | ||
from actionpack.actions.pipeline import Pipeline | ||
from actionpack.actions.make_request import MakeRequest | ||
from actionpack.actions.read_bytes import ReadBytes | ||
from actionpack.actions.read import Read | ||
from actionpack.actions.read_input import ReadInput | ||
from actionpack.actions.retry_policy import RetryPolicy | ||
from actionpack.actions.serialization import Serialization | ||
from actionpack.actions.write_bytes import WriteBytes | ||
from actionpack.actions.write import Write | ||
|
||
|
||
__all__ = [ | ||
'Call', | ||
'MakeRequest', | ||
'Pipeline', | ||
'ReadBytes', | ||
'Read', | ||
'ReadInput', | ||
'RetryPolicy', | ||
'Serialization', | ||
'WriteBytes' | ||
'Write', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
from pathlib import Path | ||
|
||
from actionpack import Action | ||
from actionpack.action import Name | ||
|
||
|
||
class Read(Action[Name, bytes]): | ||
def __init__(self, filename: str, output_type: type = bytes): | ||
if output_type not in [bytes, str]: | ||
raise TypeError(f'Must be of type bytes or str: {output_type}') | ||
self.output_type = output_type | ||
self.path = Path(filename) | ||
|
||
def instruction(self) -> bytes: | ||
return self.path.read_bytes() if self.output_type is bytes else self.path.read_text() | ||
|
||
def validate(self) -> Read[Name, bytes]: | ||
if not self.path.exists(): | ||
raise FileNotFoundError(str(self.path)) | ||
if self.path.is_dir(): | ||
raise IsADirectoryError(str(self.path)) | ||
return self |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from __future__ import annotations | ||
from pathlib import Path | ||
|
||
from actionpack import Action | ||
from actionpack.action import Name | ||
|
||
|
||
class Write(Action[Name, int]): | ||
def __init__( | ||
self, | ||
filename: str, | ||
to_write: str, | ||
prefix: str = None, | ||
overwrite: bool = False, | ||
append: bool = False, | ||
): | ||
prefix_type, to_write_type = type(prefix), type(to_write) | ||
if prefix_type != to_write_type and to_write_type not in [bytes, str]: | ||
raise TypeError(f'Must be of type {to_write_type}: {prefix_type}') | ||
|
||
self.path = Path(filename) | ||
self.to_write = to_write | ||
self.prefix = prefix | ||
self.append = append | ||
self.overwrite = overwrite | ||
|
||
def instruction(self) -> str: | ||
if isinstance(self.to_write, bytes): | ||
mode, msg = 'ab', self.prefix if self.prefix else b'' + self.to_write | ||
elif isinstance(self.to_write, str): | ||
mode, msg = 'a', f"{self.prefix if self.prefix else ''}{self.to_write}" | ||
else: | ||
raise TypeError(f'Must be of str or bytes: {self.to_write}') | ||
|
||
if self.append: | ||
rchar = b'\n' if mode == 'ab' else '\n' | ||
self.path.open(mode).write(msg + rchar) | ||
else: | ||
self.path.write_bytes(msg) if isinstance(msg, bytes) else self.path.write_text(self.to_write) | ||
return str(self.path.absolute()) | ||
|
||
def validate(self) -> Write[Name, int]: | ||
if self.overwrite and self.append: | ||
raise ValueError('Cannot overwrite and append simultaneously') | ||
if self.path.exists() and not self.overwrite and not self.append: | ||
raise FileExistsError(f'Cannot {str(self)} to {str(self.path)}') | ||
if self.path.is_dir(): | ||
raise IsADirectoryError(str(self.path)) | ||
return self |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pickle | ||
|
||
from pathlib import Path | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from actionpack.action import Result | ||
from actionpack.actions import Read | ||
from actionpack.utils import pickleable | ||
|
||
|
||
class ReadTest(TestCase): | ||
|
||
contents = 'some file contents.' | ||
|
||
@patch('pathlib.Path.read_bytes') | ||
def test_can_Read_bytes(self, mock_input): | ||
contents = self.contents.encode() | ||
mock_input.return_value = contents | ||
action = Read(__file__, output_type=bytes) | ||
result = action.perform() | ||
self.assertIsInstance(result, Result) | ||
self.assertEqual(result.value, contents) | ||
|
||
@patch('pathlib.Path.read_text') | ||
def test_can_Read_string(self, mock_input): | ||
mock_input.return_value = self.contents | ||
action = Read(__file__, output_type=str) | ||
result = action.perform() | ||
self.assertIsInstance(result, Result) | ||
self.assertEqual(result.value, self.contents) | ||
|
||
@patch('pathlib.Path.read_bytes') | ||
def test_can_ReadBytes_even_if_failure(self, mock_input): | ||
contents = self.contents.encode() | ||
mock_input.return_value = contents | ||
|
||
invalid_file_result = Read('some/invalid/filepath').perform() | ||
self.assertIsInstance(invalid_file_result, Result) | ||
self.assertIsInstance(invalid_file_result.value, FileNotFoundError) | ||
|
||
directory_result = Read(Path(__file__).parent).perform() | ||
self.assertIsInstance(directory_result, Result) | ||
self.assertIsInstance(directory_result.value, IsADirectoryError) | ||
|
||
def test_can_pickle(self): | ||
action = Read(__file__) | ||
pickled = pickleable(action) | ||
unpickled = pickle.loads(pickled) | ||
|
||
self.assertTrue(pickleable(action)) | ||
self.assertEqual(unpickled.__dict__, action.__dict__) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.