-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatando codigo e configurando linter
- Loading branch information
Showing
15 changed files
with
835 additions
and
266 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 +1,3 @@ | ||
# processor-ci-communication | ||
# Processor CI Communication | ||
|
||
[![Pylint](https://github.com/LSC-Unicamp/processor_ci_communication/actions/workflows/pylint.yml/badge.svg)](https://github.com/LSC-Unicamp/processor_ci_communication/actions/workflows/pylint.yml) |
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,47 +1,101 @@ | ||
""" | ||
This module contains utility functions for handling files and directories. | ||
It includes functions for: | ||
- Reading files and returning their content. | ||
- Listing files in directories. | ||
- Opening files in a directory and reading their content. | ||
""" | ||
|
||
import os | ||
import glob | ||
|
||
|
||
def read_file(path: str) -> tuple[list[str], int]: | ||
def read_file(path: str) -> tuple[list[int], int]: | ||
"""Reads a file and returns its content and the number of lines. | ||
Args: | ||
path (str): Path to the file. | ||
Returns: | ||
tuple[list[int], int]: Tuple with the file content (as integers) and the number of lines. | ||
Raises: | ||
FileNotFoundError: If the specified file does not exist. | ||
ValueError: If a line in the file cannot be converted to an integer. | ||
""" | ||
if not os.path.isfile(path): | ||
FileNotFoundError(f"Erro: O arquivo '{path}' não foi encontrado.") | ||
raise FileNotFoundError(f"Error: The file '{path}' was not found.") | ||
|
||
file = open(path) | ||
data = [] | ||
|
||
for line in file.readlines(): | ||
data.append(int(line.replace("\n", ""), 16)) | ||
with open(path, 'r', encoding='utf-8') as file: | ||
for line in file: | ||
try: | ||
data.append(int(line.strip(), 16)) | ||
except ValueError as e: | ||
raise ValueError( | ||
f'Error converting line to integer: {e}' | ||
) from e | ||
|
||
return data, len(data) | ||
|
||
|
||
def list_files_in_dir(path: str) -> list[str]: | ||
"""List all files in a directory. | ||
Args: | ||
path (str): Path to the directory. | ||
Returns: | ||
list[str]: List with the files in the directory. | ||
Raises: | ||
FileNotFoundError: If the directory does not exist. | ||
OSError: For other errors related to accessing the directory. | ||
""" | ||
if not os.path.isdir(path): | ||
raise FileNotFoundError( | ||
f"Error: The directory '{path}' was not found." | ||
) | ||
|
||
try: | ||
return [ | ||
file | ||
for file in os.listdir(path) | ||
if os.path.isfile(os.path.join(path, file)) | ||
] | ||
except FileNotFoundError: | ||
print(f"Erro: O diretório '{path}' não foi encontrado.") | ||
return [] | ||
except Exception as e: | ||
print(f"Erro: {e}") | ||
return [] | ||
except OSError as e: | ||
raise OSError(f'Error accessing the directory: {e}') from e | ||
|
||
|
||
def open_files_in_dir(path: str) -> list[dict[str, list[str]]]: | ||
"""Open all files in a directory and return their content. | ||
Args: | ||
path (str): Path to the directory. | ||
Returns: | ||
list[dict[str, list[str]]]: List with the files' content. | ||
Raises: | ||
FileNotFoundError: If the directory does not exist. | ||
OSError: For other errors related to accessing the directory or files. | ||
""" | ||
if not os.path.isdir(path): | ||
raise FileNotFoundError( | ||
f"Error: The directory '{path}' was not found." | ||
) | ||
|
||
files_content = [] | ||
try: | ||
for file_name in os.listdir(path): | ||
file_path = os.path.join(path, file_name) | ||
if os.path.isfile(file_path): | ||
with open(file_path, "r", encoding="utf-8") as f: | ||
with open(file_path, 'r', encoding='utf-8') as f: | ||
file_data = f.readlines() | ||
files_content.append({file_name: [line.strip() for line in file_data]}) | ||
except FileNotFoundError: | ||
print(f"Erro: O diretório '{path}' não foi encontrado.") | ||
except Exception as e: | ||
print(f"Erro: {e}") | ||
files_content.append( | ||
{file_name: [line.strip() for line in file_data]} | ||
) | ||
except OSError as e: | ||
raise OSError(f'Error processing files in the directory: {e}') from e | ||
|
||
return files_content |
Oops, something went wrong.