Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added load save implementation for text files #91

Merged
merged 3 commits into from
Sep 30, 2024
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
1 change: 1 addition & 0 deletions extras/fileformats/extras/text/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import load_save # noqa: F401
35 changes: 35 additions & 0 deletions extras/fileformats/extras/text/load_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import typing as ty
from fileformats.core import extra_implementation, FileSet
from fileformats.text import Plain # , Csv, Tsv

# import pandas as pd


@extra_implementation(FileSet.load)
def load_text_file(text: Plain, **kwargs: ty.Any) -> Plain:
return text.raw_contents # type: ignore[no-any-return]


@extra_implementation(FileSet.save)
def save_text_file(text: Plain, data: ty.Any, **kwargs: ty.Any) -> None:
text.fspath.write_text(data)


# @extra_implementation(FileSet.load)
# def load_csv_file(csv_file: Csv, **kwargs: ty.Any) -> pd.DataFrame:
# return pd.read_csv(csv_file.fspath, **kwargs)


# @extra_implementation(FileSet.save)
# def save_csv_file(csv_file: Csv, data: pd.DataFrame, **kwargs: ty.Any) -> None:
# data.to_csv(csv_file.fspath, index=False, **kwargs)


# @extra_implementation(FileSet.load)
# def load_tsv_file(tsv_file: Tsv, **kwargs: ty.Any) -> pd.DataFrame:
# return pd.read_csv(tsv_file.fspath, sep="\t", **kwargs)


# @extra_implementation(FileSet.save)
# def save_tsv_file(tsv_file: Tsv, data: pd.DataFrame, **kwargs: ty.Any) -> None:
# data.to_csv(tsv_file.fspath, sep="\t", index=False, **kwargs)
2 changes: 2 additions & 0 deletions fileformats/core/fileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
Any
the data loaded from the file in an type to the format
"""
raise NotImplementedError

Check warning on line 197 in fileformats/core/fileset.py

View check run for this annotation

Codecov / codecov/patch

fileformats/core/fileset.py#L197

Added line #L197 was not covered by tests

@extra
def save(self, data: ty.Any, **kwargs: ty.Any) -> None:
Expand All @@ -207,6 +208,7 @@
**kwargs : Any
any format-specific keyword arguments to pass to the saver
"""
raise NotImplementedError

Check warning on line 211 in fileformats/core/fileset.py

View check run for this annotation

Codecov / codecov/patch

fileformats/core/fileset.py#L211

Added line #L211 was not covered by tests

@classmethod
def new(cls, fspath: ty.Union[str, Path], data: ty.Any, **kwargs: ty.Any) -> Self:
Expand Down
8 changes: 4 additions & 4 deletions fileformats/core/tests/test_fs_mount_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ def test_copy_constraints(tmp_path):
mode=copy_modes,
)

assert new_ext4_file.contents == ext4_file.contents
assert new_ext4_file.raw_contents == ext4_file.raw_contents
assert os.path.islink(new_ext4_file)

# Symlinks not supported on CIFS
new_cifs_file = cifs_file.copy(
cifs_mnt / "dest",
mode=copy_modes,
)
assert new_cifs_file.contents == cifs_file.contents
assert new_cifs_file.raw_contents == cifs_file.raw_contents
assert not os.path.islink(new_cifs_file)
assert os.stat(new_cifs_file).st_ino == os.stat(cifs_file).st_ino # Hardlink

# Hardlinks not supported across logical volumes
new_ext4_file2 = ext4_file.copy(
ext4_mnt2 / "dest", mode=File.CopyMode.copy | File.CopyMode.hardlink
)
assert new_ext4_file2.contents == ext4_file.contents
assert new_ext4_file2.raw_contents == ext4_file.raw_contents
assert not os.path.islink(new_ext4_file2)
assert (
os.stat(ext4_file).st_ino != os.stat(new_ext4_file2).st_ino
Expand All @@ -351,7 +351,7 @@ def test_copy_constraints(tmp_path):
cifs_mnt / "dest",
mode=copy_modes,
)
assert ext4_file_on_cifs.contents == ext4_file.contents
assert ext4_file_on_cifs.raw_contents == ext4_file.raw_contents
assert not os.path.islink(ext4_file_on_cifs)
assert (
os.stat(ext4_file).st_ino != os.stat(ext4_file_on_cifs).st_ino
Expand Down
4 changes: 3 additions & 1 deletion fileformats/field/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def __init__(self, value: ty.Any):
if isinstance(value, Decimal):
self.value = value.value
try:
self.value = decimal.Decimal(value)
self.value = (
value.value if isinstance(value, Decimal) else decimal.Decimal(value)
)
except decimal.InvalidOperation as e:
raise FormatMismatchError(str(e)) from None

Expand Down
10 changes: 5 additions & 5 deletions fileformats/testing/basic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from fileformats.generic import UnicodeFile
from fileformats.text import Plain


class Foo(UnicodeFile):
class Foo(Plain):

ext = ".foo"


class Bar(UnicodeFile):
class Bar(Plain):

ext = ".bar"


class Baz(UnicodeFile):
class Baz(Plain):

ext = ".baz"


class Qux(UnicodeFile):
class Qux(Plain):

ext = ".qux"
2 changes: 1 addition & 1 deletion fileformats/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Plain(Text, UnicodeFile):
iana_mime = "text/plain"


class TextFile(Text, UnicodeFile):
class TextFile(Plain, UnicodeFile):
ext = ".txt"


Expand Down
Loading