-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
306 additions
and
116 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
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,120 @@ | ||
<!-- markdownlint-disable --> | ||
|
||
<a href="../velocitas_lib/file_utils.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
# <kbd>module</kbd> `velocitas_lib.file_utils` | ||
|
||
|
||
|
||
|
||
|
||
--- | ||
|
||
<a href="../velocitas_lib/file_utils.py#L21"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `replace_text_in_file` | ||
|
||
```python | ||
replace_text_in_file(file_path: str, text: str, replacement: str) → None | ||
``` | ||
|
||
Replace all occurrences of text in a file with a replacement. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`file_path`</b> (str): The path to the file. | ||
- <b>`text`</b> (str): The text to find. | ||
- <b>`replacement`</b> (str): The replacement for text. | ||
|
||
|
||
--- | ||
|
||
<a href="../velocitas_lib/file_utils.py#L40"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `capture_area_in_file` | ||
|
||
```python | ||
capture_area_in_file( | ||
file: TextIOWrapper, | ||
start_line: str, | ||
end_line: str, | ||
map_fn: Optional[Callable[[str], str]] = None | ||
) → List[str] | ||
``` | ||
|
||
Capture an area of a textfile between a matching start line (exclusive) and the first line matching end_line (exclusive). | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`file`</b> (TextIOWrapper): The text file to read from. | ||
- <b>`start_line`</b> (str): The line which triggers the capture (will not be part of the output) | ||
- <b>`end_line`</b> (str): The line which terminates the capture (will not be bart of the output) | ||
- <b>`map_fn`</b> (Optional[Callable[[str], str]], optional): An optional mapping function to transform captured lines. Defaults to None. | ||
|
||
|
||
|
||
**Returns:** | ||
|
||
- <b>`List[str]`</b>: A list of captured lines. | ||
|
||
|
||
--- | ||
|
||
<a href="../velocitas_lib/file_utils.py#L74"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `read_file` | ||
|
||
```python | ||
read_file(file_path: str) → Optional[str] | ||
``` | ||
|
||
Reads the file with the given file_path and returns it's content as a str. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`file_path`</b> (str): the file_path of the file to read. | ||
|
||
|
||
|
||
**Returns:** | ||
|
||
- <b>`str`</b>: the content of the specified file. | ||
|
||
|
||
--- | ||
|
||
<a href="../velocitas_lib/file_utils.py#L97"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a> | ||
|
||
## <kbd>function</kbd> `write_file` | ||
|
||
```python | ||
write_file(file_path: str, content: str) → bool | ||
``` | ||
|
||
Writes the content to the file_path and returns the success of the write operation. | ||
|
||
|
||
|
||
**Args:** | ||
|
||
- <b>`file_path`</b> (str): the file_path of the file to write. | ||
- <b>`content`</b> (str): the content to be written to the file. | ||
|
||
|
||
|
||
**Returns:** | ||
|
||
- <b>`bool`</b>: True if writing was successful, False otherwise. | ||
|
||
|
||
|
||
|
||
--- | ||
|
||
_This file was automatically generated via [lazydocs](https://github.com/ml-tooling/lazydocs)._ |
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,58 @@ | ||
# Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache License, Version 2.0 which is available at | ||
# https://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import tempfile | ||
|
||
import pytest | ||
|
||
from velocitas_lib.file_utils import ( | ||
read_file, | ||
write_file, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def temp_file(): | ||
temp_file = tempfile.NamedTemporaryFile(delete=False) | ||
yield temp_file | ||
|
||
if os.path.exists(temp_file.name): | ||
os.remove(temp_file.name) | ||
|
||
|
||
def test_write_file(temp_file): | ||
temp_file_path = temp_file.name | ||
write_file( | ||
temp_file_path, | ||
"Lorem ipsum dolor sit amet ...\n1...2...3...", | ||
) | ||
|
||
assert os.path.exists(temp_file.name) | ||
|
||
read_file_content = temp_file.readlines() | ||
assert read_file_content[0] == b"Lorem ipsum dolor sit amet ...\n" | ||
assert read_file_content[1] == b"1...2...3..." | ||
|
||
|
||
def test_read_file(temp_file): | ||
file_content = "Lorem ipsum dolor sit amet ...\n1...2...3..." | ||
temp_file.write(file_content.encode("utf-8")) | ||
temp_file.flush() | ||
temp_file.close() | ||
|
||
temp_file_path = temp_file.name | ||
read_file_content = read_file(temp_file_path) | ||
|
||
assert read_file_content == file_content |
Oops, something went wrong.