Skip to content

Commit

Permalink
renamed class CppFormatter to NativeFormatter and class `CppParse…
Browse files Browse the repository at this point in the history
…r` to `NativeParser`
  • Loading branch information
ClaasRostock committed Nov 10, 2024
1 parent f22c668 commit f9fb6c6
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 72 deletions.
11 changes: 6 additions & 5 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ select = [
]
ignore = [
# Ruff lint rules temporarily ignored, but which should be reactivated and resolved in the future.
"D100", # Missing docstring in public module <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D104", # Missing docstring in public package <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D105", # Missing docstring in magic method <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D107", # Missing docstring in __init__ <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"N999", # Invalid module name <- @TODO: reactivate and resolve @CLAROS, 2024-10-11
"D100", # Missing docstring in public module <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D104", # Missing docstring in public package <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D105", # Missing docstring in magic method <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
"D107", # Missing docstring in __init__ <- @TODO: reactivate and resolve docstring issues @CLAROS, 2024-10-21
# "N999", # Invalid module name <- @TODO: reactivate and resolve @CLAROS, 2024-10-11
"C901", # Function is too complex <- @TODO: reactivate and resolve print statements @CLAROS, 2024-09-22
"PLR0911", # Too many return statements <- @TODO: reactivate and resolve @CLAROS, 2024-10-11
"PLR0912", # Too many branches <- @TODO: reactivate and resolve @CLAROS, 2024-10-11
"PLR0915", # Too many statements <- @TODO: reactivate and resolve @CLAROS, 2024-10-11

# Ruff lint rules considered as too strict and hence ignored
"ANN101", # Missing type annotation for `self` argument in instance methods (NOTE: also listed as deprecated by Ruff)
"ANN102", # Missing type annotation for `cls` argument in class methods (NOTE: also listed as deprecated by Ruff)
Expand Down
4 changes: 2 additions & 2 deletions src/dictIO/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

from dictIO.formatter import (
Formatter,
CppFormatter,
NativeFormatter,
FoamFormatter,
JsonFormatter,
XmlFormatter,
)
from dictIO.parser import (
Parser,
CppParser,
NativeParser,
FoamParser,
JsonParser,
XmlParser,
Expand Down
8 changes: 4 additions & 4 deletions src/dictIO/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ def include(self, dict_to_include: SDict[_K, _V]) -> None:
f"Cannot include {dict_to_include.name}. Relative path to {dict_to_include.name} could not be resolved."
) from e

from dictIO import CppFormatter
from dictIO import NativeFormatter

formatter = CppFormatter()
formatter = NativeFormatter()
include_file_name = str(relative_file_path)
include_file_name = include_file_name.replace("\\", "\\\\")
include_file_name = formatter.format_value(include_file_name)
Expand Down Expand Up @@ -786,10 +786,10 @@ def __str__(self) -> str:
the string representation
"""
from dictIO import (
CppFormatter, # __str__ shall be formatted in dictIO native file format
NativeFormatter, # __str__ shall be formatted in dictIO native file format
)

formatter = CppFormatter()
formatter = NativeFormatter()
return formatter.to_string(cast(SDict[TKey, TValue], self))

def __repr__(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/dictIO/dictWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import MutableMapping, MutableSequence
from pathlib import Path

from dictIO import CppParser, Formatter, SDict, order_keys
from dictIO import Formatter, NativeParser, SDict, order_keys
from dictIO.types import TKey, TValue

__ALL__ = ["DictWriter", "create_target_file_name"]
Expand Down Expand Up @@ -78,7 +78,7 @@ def write(
formatter = formatter or Formatter.get_formatter(target_file)

# Before writing the dict, doublecheck once again that all of its elements are correctly typed.
parser = CppParser()
parser = NativeParser()
parser.parse_values(source_dict)

# If mode is set to 'a' (append) and target_file exists:
Expand Down
14 changes: 7 additions & 7 deletions src/dictIO/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

__ALL__ = [
"Formatter",
"CppFormatter",
"NativeFormatter",
"FoamFormatter",
"JsonFormatter",
"XmlFormatter",
Expand Down Expand Up @@ -71,8 +71,8 @@ def get_formatter(cls, target_file: Path | None = None) -> Formatter:
]: # .xml or OSP .ssd -> XmlFormatter
return XmlFormatter()

# 2. If no target file is passed, return CppFormatter as default / fallback
return CppFormatter() # default
# 2. If no target file is passed, return NativeFormatter as default / fallback
return NativeFormatter() # default

@abstractmethod
def to_string(
Expand Down Expand Up @@ -447,11 +447,11 @@ def add_double_quotes(self, arg: str) -> str:
return f'"{arg}"'


class CppFormatter(Formatter):
class NativeFormatter(Formatter):
"""Formatter to serialize a dict into a string in dictIO native file format."""

def __init__(self) -> None:
"""Define default configuration for CppFormatter."""
"""Define default configuration for NativeFormatter."""
# Invoke base class constructor
super().__init__()

Expand Down Expand Up @@ -850,7 +850,7 @@ def remove_trailing_spaces(self, s: str) -> str:
return ns


class FoamFormatter(CppFormatter):
class FoamFormatter(NativeFormatter):
"""Formatter to serialize a dict into a string in OpenFOAM dictionary format."""

def __init__(self) -> None:
Expand Down Expand Up @@ -893,7 +893,7 @@ def remove_underscore_keys_recursive(
dict_adapted_for_foam = deepcopy(arg)
remove_underscore_keys_recursive(dict_adapted_for_foam)

# Call base class implementation (CppFormatter)
# Call base class implementation (NativeFormatter)
s = super().to_string(dict_adapted_for_foam)

# Substitute all remeining single quotes, if any, by double quotes:
Expand Down
20 changes: 10 additions & 10 deletions src/dictIO/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if TYPE_CHECKING:
import os

__ALL__ = ["Parser", "CppParser", "FoamParser", "JsonParser", "XmlParser"]
__ALL__ = ["Parser", "NativeParser", "FoamParser", "JsonParser", "XmlParser"]

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,8 +66,8 @@ def get_parser(cls, source_file: Path | None = None) -> Parser:
]: # .xml or OSP .ssd -> XmlParser
return XmlParser()

# 2. If no source file is passed, return CppParser as default / fallback
return CppParser() # default
# 2. If no source file is passed, return NativeParser as default / fallback
return NativeParser() # default

def parse_file(
self,
Expand Down Expand Up @@ -382,11 +382,11 @@ def remove_quotes_from_strings(
return arg


class CppParser(Parser):
class NativeParser(Parser):
"""Parser to deserialize a string in dictIO native file format into a SDict."""

def __init__(self) -> None:
"""Define default configuration for CppParser."""
"""Define default configuration for NativeParser."""
# Invoke base class constructor
super().__init__()

Expand Down Expand Up @@ -1082,12 +1082,12 @@ def _parse_tokenized_dict(
+ "/"
)
logger.warning(
f"CppParser._parse_tokenized_dict(): tokens skipped: {key_value_pair_tokens} inside {context}"
f"NativeParser._parse_tokenized_dict(): tokens skipped: {key_value_pair_tokens} inside {context}"
)
else:
if len(key_value_pair_tokens) > 3: # noqa: PLR2004
logger.warning(
"CppParser._parse_tokenized_dict(): "
"NativeParser._parse_tokenized_dict(): "
f"more tokens in key-value pair than expected: {key_value_pair_tokens!s}"
)
# read the key (name) (first token, by convention)
Expand Down Expand Up @@ -1292,9 +1292,9 @@ def _clean(
self,
s_dict: SDict[TKey, TValue],
) -> None:
"""Remove CppFormatter / CppParser specific internal keys from dict.
"""Remove NativeFormatter / NativeParser specific internal keys from dict.
Removes keys written by CppFormatter for documentation purposes
Removes keys written by NativeFormatter for documentation purposes
but which shall not be created as keys in dict.data.
In specific, it is the following two keys that get deleted if existing:
_variables
Expand All @@ -1306,7 +1306,7 @@ def _clean(
del s_dict["_includes"]


class FoamParser(CppParser):
class FoamParser(NativeParser):
"""Parser to deserialize a string in OpenFOAM dictionary format into a SDict."""

def __init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cppdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

from dictIO import (
CppDict,
CppParser,
DictParser,
DictReader,
DictWriter,
NativeParser,
SDict,
find_global_key,
order_keys,
Expand All @@ -22,7 +22,7 @@

@pytest.fixture
def test_dict() -> SDict[TKey, TValue]:
parser = CppParser()
parser = NativeParser()
return parser.parse_file(Path("test_dict_dict"))


Expand Down
4 changes: 2 additions & 2 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

from dictIO import (
CppDict,
CppParser,
DictParser,
DictReader,
DictWriter,
NativeParser,
SDict,
find_global_key,
order_keys,
Expand All @@ -23,7 +23,7 @@

@pytest.fixture
def test_dict() -> SDict[TKey, TValue]:
parser = CppParser()
parser = NativeParser()
return parser.parse_file(Path("test_dict_dict"))


Expand Down
4 changes: 2 additions & 2 deletions tests/test_dictReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
from numpy.testing import assert_array_equal

from dictIO import CppParser, DictReader, DictWriter, SDict
from dictIO import DictReader, DictWriter, NativeParser, SDict
from dictIO.types import TKey, TValue

WindowsOnly: pytest.MarkDecorator = pytest.mark.skipif(not sys.platform.startswith("win"), reason="windows only test")
Expand Down Expand Up @@ -619,7 +619,7 @@ def prepare_dict_until(
) -> None:
file_name = Path.cwd() / file_to_read

parser = CppParser()
parser = NativeParser()
_ = parser.parse_file(file_name, dict_to_prepare)

funcs = [
Expand Down
24 changes: 12 additions & 12 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from dictIO import CppFormatter, DictReader, FoamFormatter, SDict, XmlFormatter
from dictIO import DictReader, FoamFormatter, NativeFormatter, SDict, XmlFormatter

if TYPE_CHECKING:
from dictIO.types import TKey, TValue
Expand Down Expand Up @@ -36,7 +36,7 @@ class TestCppFormatter:
)
def test_format_type_string_no_additional_quotes_expected(self, str_in: str) -> None:
# Prepare
formatter = CppFormatter()
formatter = NativeFormatter()
str_expected = str_in
# Execute
str_out = formatter.format_value(str_in)
Expand All @@ -56,7 +56,7 @@ def test_format_type_string_no_additional_quotes_expected(self, str_in: str) ->
)
def test_format_type_string_additional_single_quotes_expected(self, str_in: str) -> None:
# Prepare
formatter = CppFormatter()
formatter = NativeFormatter()
str_expected = f"'{str_in}'"
# Execute
str_out = formatter.format_value(str_in)
Expand All @@ -75,7 +75,7 @@ def test_format_type_string_additional_single_quotes_expected(self, str_in: str)
)
def test_format_type_string_additional_double_quotes_expected(self, str_in: str) -> None:
# Prepare
formatter = CppFormatter()
formatter = NativeFormatter()
str_expected = f'"{str_in}"'
# Execute
str_out = formatter.format_value(str_in)
Expand All @@ -84,7 +84,7 @@ def test_format_type_string_additional_double_quotes_expected(self, str_in: str)

def test_format_type_float(self) -> None:
# sourcery skip: extract-duplicate-method, inline-variable
formatter = CppFormatter()
formatter = NativeFormatter()
float_in = 1.23
str_out = formatter.format_value(float_in)
assert isinstance(str_out, str)
Expand All @@ -101,7 +101,7 @@ def test_format_type_float(self) -> None:
def test_insert_block_comments(self) -> None:
# sourcery skip: class-extract-method
# Prepare
formatter = CppFormatter()
formatter = NativeFormatter()
as_is_block_comment = (
"/*---------------------------------*- C++ -*----------------------------------*\\\n"
"This is a block comment; coding utf-8; version 0.1;\n"
Expand All @@ -122,7 +122,7 @@ def test_insert_block_comments(self) -> None:

@staticmethod
def run_block_comment_tests(
formatter: CppFormatter,
formatter: NativeFormatter,
as_is_block_comment: str,
default_block_comment: str,
) -> None:
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_insert_includes(self) -> None:
include_placeholder = "INCLUDE000102 INCLUDE000102;"
str_in = blockcomment_placeholder + "\n" + include_placeholder + "\n"
str_expected = str_in.replace(include_placeholder, include_directive_in.replace('"', "'"))
formatter = CppFormatter()
formatter = NativeFormatter()
# Execute
str_out = formatter.insert_includes(s_dict, str_in)
# Assert
Expand All @@ -230,7 +230,7 @@ def test_insert_line_comments(self) -> None:
# Prepare
s_dict: SDict[TKey, TValue] = SDict()
line_comment_in = "// This is a line comment"
formatter = CppFormatter()
formatter = NativeFormatter()
str_in_template = formatter.format_dict(s_dict)
s_dict.line_comments = {103: line_comment_in}
placeholder1 = "BLOCKCOMMENT000101 BLOCKCOMMENT000101;"
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_remove_trailing_spaces(self) -> None:
multi_line_str_expected += str_expected_6 + "\n"
multi_line_str_expected += str_expected_7

formatter = CppFormatter()
formatter = NativeFormatter()

# Execute 1
multi_line_str_out = formatter.remove_trailing_spaces(multi_line_str_in)
Expand Down Expand Up @@ -324,7 +324,7 @@ def test_list_with_nested_list(self) -> None:
s_dict: SDict[TKey, TValue] = SDict()
dict_in = deepcopy(s_dict)
dict_in.update(test_obj)
formatter = CppFormatter()
formatter = NativeFormatter()
assert test_obj == dict_in
# Execute
str_in: str = formatter.format_dict(test_obj)
Expand All @@ -335,7 +335,7 @@ def test_list_with_nested_list(self) -> None:
def test_to_string_does_not_alter_original(self) -> None:
# Prepare
dict_in = DictReader.read(Path("test_formatter_dict"))
formatter = CppFormatter()
formatter = NativeFormatter()
dict_in_reference = dict_in
dict_in_shallowcopy = copy(dict_in)
# Execute
Expand Down
Loading

0 comments on commit f9fb6c6

Please sign in to comment.