Skip to content

Commit

Permalink
Formatando codigo e configurando linter
Browse files Browse the repository at this point in the history
  • Loading branch information
JN513 committed Nov 20, 2024
1 parent e8f40f7 commit b2ce1f8
Show file tree
Hide file tree
Showing 15 changed files with 835 additions and 266 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -18,6 +18,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
pylint $(git ls-files '*.py') --max-public-methods=30 --ignore=tests
4 changes: 3 additions & 1 deletion README.md
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)
92 changes: 73 additions & 19 deletions core/file.py
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
Loading

0 comments on commit b2ce1f8

Please sign in to comment.