Skip to content

Commit

Permalink
Add read_file and write_file (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Sep 10, 2024
1 parent 6085c1b commit 8825dca
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 116 deletions.
2 changes: 1 addition & 1 deletion NOTICE-3RD-PARTY-CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Python
| Dependency | Version | License |
|:-----------|:-------:|--------:|
|types-requests|2.32.0.20240712|Apache 2.0|
|types-requests|2.32.0.20240907|Apache 2.0|
|urllib3|2.2.2|MIT|
## Workflows
| Dependency | Version | License |
Expand Down
7 changes: 5 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [`velocitas_lib`](./velocitas_lib.md#module-velocitas_lib)
- [`velocitas_lib.conan_utils`](./velocitas_lib.conan_utils.md#module-velocitas_libconan_utils)
- [`velocitas_lib.docker`](./velocitas_lib.docker.md#module-velocitas_libdocker)
- [`velocitas_lib.file_utils`](./velocitas_lib.file_utils.md#module-velocitas_libfile_utils)
- [`velocitas_lib.functional_interface`](./velocitas_lib.functional_interface.md#module-velocitas_libfunctional_interface)
- [`velocitas_lib.middleware`](./velocitas_lib.middleware.md#module-velocitas_libmiddleware)
- [`velocitas_lib.services`](./velocitas_lib.services.md#module-velocitas_libservices)
Expand Down Expand Up @@ -48,6 +49,10 @@
- [`docker.container_exists`](./velocitas_lib.docker.md#function-container_exists): Check if a container with a given name exists.
- [`docker.is_docker_image_build_locally`](./velocitas_lib.docker.md#function-is_docker_image_build_locally): Check if vehicle app docker image is locally available
- [`docker.push_docker_image_to_registry`](./velocitas_lib.docker.md#function-push_docker_image_to_registry): Push docker image to local image registry
- [`file_utils.capture_area_in_file`](./velocitas_lib.file_utils.md#function-capture_area_in_file): Capture an area of a textfile between a matching start line (exclusive) and the first line matching end_line (exclusive).
- [`file_utils.read_file`](./velocitas_lib.file_utils.md#function-read_file): Reads the file with the given file_path and returns it's content as a str.
- [`file_utils.replace_text_in_file`](./velocitas_lib.file_utils.md#function-replace_text_in_file): Replace all occurrences of text in a file with a replacement.
- [`file_utils.write_file`](./velocitas_lib.file_utils.md#function-write_file): Writes the content to the file_path and returns the success of the write operation.
- [`functional_interface.get_interfaces_for_type`](./velocitas_lib.functional_interface.md#function-get_interfaces_for_type): Return all interfaces for the given type.
- [`middleware.get_middleware_type`](./velocitas_lib.middleware.md#function-get_middleware_type): Return the current middleware type.
- [`services.get_service_port`](./velocitas_lib.services.md#function-get_service_port): Return the service port as string for the specified service.
Expand All @@ -56,11 +61,9 @@
- [`services.parse_service_config`](./velocitas_lib.services.md#function-parse_service_config): Parse service spec configuration and return it as an named tuple.
- [`services.resolve_functions`](./velocitas_lib.services.md#function-resolve_functions)
- [`templates.copy_templates`](./velocitas_lib.templates.md#function-copy_templates): Copy templates from the template dir to the target dir.
- [`text_utils.capture_area_in_file`](./velocitas_lib.text_utils.md#function-capture_area_in_file): Capture an area of a textfile between a matching start line (exclusive) and the first line matching end_line (exclusive).
- [`text_utils.create_truncated_string`](./velocitas_lib.text_utils.md#function-create_truncated_string): Create a truncated version of input if it is longer than length.
- [`text_utils.replace_item_in_list`](./velocitas_lib.text_utils.md#function-replace_item_in_list): Replace the whole line which matches the given text with a replacement.
- [`text_utils.replace_text_area`](./velocitas_lib.text_utils.md#function-replace_text_area): Replace all occurrences of all text areas matching the parameters with a replacement.
- [`text_utils.replace_text_in_file`](./velocitas_lib.text_utils.md#function-replace_text_in_file): Replace all occurrences of text in a file with a replacement.
- [`text_utils.to_camel_case`](./velocitas_lib.text_utils.md#function-to_camel_case): Return a camel case version of a snake case string.
- [`variables.json_obj_to_flat_map`](./velocitas_lib.variables.md#function-json_obj_to_flat_map): Flatten a JSON Object into a one dimensional dict by joining the keys

Expand Down
120 changes: 120 additions & 0 deletions docs/velocitas_lib.file_utils.md
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)._
62 changes: 4 additions & 58 deletions docs/velocitas_lib.text_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

---

<a href="../velocitas_lib/text_utils.py#L19"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/text_utils.py#L18"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `to_camel_case`

Expand All @@ -35,7 +35,7 @@ Return a camel case version of a snake case string.

---

<a href="../velocitas_lib/text_utils.py#L31"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/text_utils.py#L30"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `create_truncated_string`

Expand All @@ -61,28 +61,7 @@ Create a truncated version of input if it is longer than length. Will keep the r

---

<a href="../velocitas_lib/text_utils.py#L49"><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/text_utils.py#L68"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/text_utils.py#L48"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `replace_item_in_list`

Expand All @@ -109,7 +88,7 @@ Replace the whole line which matches the given text with a replacement.

---

<a href="../velocitas_lib/text_utils.py#L96"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/text_utils.py#L76"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `replace_text_area`

Expand All @@ -134,39 +113,6 @@ Replace all occurrences of all text areas matching the parameters with a replace
- <b>`replacement`</b> (str): The replacement for text area.


---

<a href="../velocitas_lib/text_utils.py#L130"><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.




---
Expand Down
58 changes: 58 additions & 0 deletions tests/test_file_utils.py
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
Loading

0 comments on commit 8825dca

Please sign in to comment.