Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run Linter

on:
pull_request:
branches:
- main
- develop

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
export PATH="$HOME/.local/bin:$PATH"

- name: Install dependencies
run: |
poetry install

- name: Run linter
run: |
poetry run lint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.coverage
venv
*.json
*.yaml
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]


### Changed

- Сoverage to display the percentage of coverage by unit tests.

## 0.1.0

### Added
Expand Down
Empty file added formatfusion/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions formatfusion/commands/cmd_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import typing as t
from pathlib import Path

from ..core.image import ConverterImage
from formatfusion.core.image import ConverterImage

logger = logging.getLogger(__name__)


def run(opts: t.Dict[str, t.Any]):
def run(opts: t.Dict[str, t.Any]) -> t.Any:
logger.info("Start converting..")
return run_convert(opts)

Expand Down
5 changes: 2 additions & 3 deletions formatfusion/commands/cmd_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import typing as t
from pathlib import Path

from formatfusion.core.json_and_yaml import ConverterYAMLandJSON
from formatfusion.helpers import validate_files

from ..core.json_and_yaml import ConverterYAMLandJSON

logger = logging.getLogger(__name__)


def run(opts: t.Dict[str, t.Any]):
def run(opts: t.Dict[str, t.Any]) -> t.Any:
logger.info("Start converting..")
return run_convert(opts)

Expand Down
1 change: 0 additions & 1 deletion formatfusion/core/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import typing as t
from pathlib import Path

logger = logging.getLogger(__name__)
Expand Down
9 changes: 6 additions & 3 deletions formatfusion/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import logging
import os
from pathlib import Path

logger = logging.getLogger(__name__)

VALID_EXTENSIONS = {"json", "yaml", "png", "jpg"}


def get_file_extension(filename: str):
return os.path.splitext(filename)[-1].lstrip(".")
def get_file_extension(filename: Path) -> str:
if not filename.name:
raise ValueError("Filename cannot be empty.")
return filename.suffix.lstrip(".")


def validate_files(input_file, output_file) -> bool:
def validate_files(input_file: Path, output_file: Path) -> bool:
if not os.path.isfile(input_file):
logger.error(f"The file '{input_file}' does not exist")
return False
Expand Down
7 changes: 4 additions & 3 deletions formatfusion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""
import json
import logging
import typing as t

from docopt import docopt

Expand All @@ -26,13 +27,13 @@
logger = logging.getLogger(__name__)


def run_command(opts):
def run_command(opts: t.Dict[str, t.Any]) -> t.Any:
command_name = opts["<command>"]
match command_name:
case "yaml":
cmd_module = commands.cmd_yaml
case "image":
cmd_module = commands.cmd_image
cmd_module = commands.cmd_image # type: ignore
case _:
raise RuntimeError(f"Invalid command <{command_name}>")

Expand All @@ -44,7 +45,7 @@ def run_command(opts):
return cmd_module.run(cmd_options)


def main(opts):
def main(opts: t.Dict[str, t.Any]) -> t.Any:
logger.debug("Run app with options: %s", json.dumps(opts))

try:
Expand Down
104 changes: 100 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ docopt = "^0.6.2"
black = "22.12.0"
isort = "5.11.5"
mypy = "1.11.2"
coverage = "^7.6.10"
types-docopt = "^0.6.11.20241107"
types-pyyaml = "^6.0.12.20241230"

[tool.isort]
multi_line_output = 3
Expand All @@ -37,9 +40,10 @@ line-length = 88
include = '\.pyi?$'

[tool.mypy]
python_version = "3.11"
#disallow_untyped_defs = true
python_version = "3.12"
disallow_untyped_defs = true
disable_error_code = "annotation-unchecked"
show_error_codes = true
no_implicit_optional = true
warn_unused_ignores = true
namespace_packages = true
20 changes: 16 additions & 4 deletions scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import pathlib
import sys
import unittest
import coverage

import black

from isort import main as isort_main
from mypy.main import main as mypy_main

Expand All @@ -14,6 +16,7 @@
)
logger = logging.getLogger(__name__)


def lint():
if len(sys.argv) > 1:
logger.warning("lint not support arguments")
Expand Down Expand Up @@ -61,6 +64,7 @@ def lint():

logger.info("Mypy check passed")


def format():
if len(sys.argv) > 1:
logger.warning("format not support arguments")
Expand All @@ -82,17 +86,25 @@ def format():
]
black.patched_main()


def test():
logging.disable(logging.INFO)

cov = coverage.Coverage(
source=[str(top_level_dir / "formatfusion")],
omit=[str(top_level_dir / "tests/*")]
)
cov.start()

test_dir = str(top_level_dir / "tests")
discover_pattern = "test*.py"
test_suite = unittest.TestLoader().discover(test_dir, pattern=discover_pattern)

runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(test_suite)

if result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
cov.stop()
cov.save()
cov.report()

sys.exit(0 if result.wasSuccessful() else 1)
27 changes: 27 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from pathlib import Path

from formatfusion.helpers import get_file_extension


class TestGetFileExtension(unittest.TestCase):
def test_valid_extensions(self):
self.assertEqual(get_file_extension(Path("example.txt")), "txt")
self.assertEqual(get_file_extension(Path("archive.tar.gz")), "gz")
self.assertEqual(get_file_extension(Path("photo.jpeg")), "jpeg")

def test_no_extension(self):
self.assertEqual(get_file_extension(Path("file")), "")

def test_hidden_file(self):
self.assertEqual(get_file_extension(Path(".hiddenfile")), "")

def test_complex_extensions(self):
self.assertEqual(get_file_extension(Path("complex.name.with.dots.csv")), "csv")

def test_directory(self):
self.assertEqual(get_file_extension(Path("folder/")), "")

def test_empty_filename(self):
with self.assertRaises(ValueError):
get_file_extension(Path(""))
Loading