Skip to content

Commit

Permalink
Merge pull request #91 from ArcanaFramework/text-load-save
Browse files Browse the repository at this point in the history
added load save implementation for text files
  • Loading branch information
tclose authored Sep 30, 2024
2 parents 06eb686 + 2b8b53e commit b54e9df
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
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 @@ def load(self, **kwargs: ty.Any) -> ty.Any:
Any
the data loaded from the file in an type to the format
"""
raise NotImplementedError

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

@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

0 comments on commit b54e9df

Please sign in to comment.