Skip to content

Commit b54e9df

Browse files
authored
Merge pull request #91 from ArcanaFramework/text-load-save
added load save implementation for text files
2 parents 06eb686 + 2b8b53e commit b54e9df

File tree

7 files changed

+51
-11
lines changed

7 files changed

+51
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import load_save # noqa: F401
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import typing as ty
2+
from fileformats.core import extra_implementation, FileSet
3+
from fileformats.text import Plain # , Csv, Tsv
4+
5+
# import pandas as pd
6+
7+
8+
@extra_implementation(FileSet.load)
9+
def load_text_file(text: Plain, **kwargs: ty.Any) -> Plain:
10+
return text.raw_contents # type: ignore[no-any-return]
11+
12+
13+
@extra_implementation(FileSet.save)
14+
def save_text_file(text: Plain, data: ty.Any, **kwargs: ty.Any) -> None:
15+
text.fspath.write_text(data)
16+
17+
18+
# @extra_implementation(FileSet.load)
19+
# def load_csv_file(csv_file: Csv, **kwargs: ty.Any) -> pd.DataFrame:
20+
# return pd.read_csv(csv_file.fspath, **kwargs)
21+
22+
23+
# @extra_implementation(FileSet.save)
24+
# def save_csv_file(csv_file: Csv, data: pd.DataFrame, **kwargs: ty.Any) -> None:
25+
# data.to_csv(csv_file.fspath, index=False, **kwargs)
26+
27+
28+
# @extra_implementation(FileSet.load)
29+
# def load_tsv_file(tsv_file: Tsv, **kwargs: ty.Any) -> pd.DataFrame:
30+
# return pd.read_csv(tsv_file.fspath, sep="\t", **kwargs)
31+
32+
33+
# @extra_implementation(FileSet.save)
34+
# def save_tsv_file(tsv_file: Tsv, data: pd.DataFrame, **kwargs: ty.Any) -> None:
35+
# data.to_csv(tsv_file.fspath, sep="\t", index=False, **kwargs)

fileformats/core/fileset.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def load(self, **kwargs: ty.Any) -> ty.Any:
194194
Any
195195
the data loaded from the file in an type to the format
196196
"""
197+
raise NotImplementedError
197198

198199
@extra
199200
def save(self, data: ty.Any, **kwargs: ty.Any) -> None:
@@ -207,6 +208,7 @@ def save(self, data: ty.Any, **kwargs: ty.Any) -> None:
207208
**kwargs : Any
208209
any format-specific keyword arguments to pass to the saver
209210
"""
211+
raise NotImplementedError
210212

211213
@classmethod
212214
def new(cls, fspath: ty.Union[str, Path], data: ty.Any, **kwargs: ty.Any) -> Self:

fileformats/core/tests/test_fs_mount_identifier.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,23 +324,23 @@ def test_copy_constraints(tmp_path):
324324
mode=copy_modes,
325325
)
326326

327-
assert new_ext4_file.contents == ext4_file.contents
327+
assert new_ext4_file.raw_contents == ext4_file.raw_contents
328328
assert os.path.islink(new_ext4_file)
329329

330330
# Symlinks not supported on CIFS
331331
new_cifs_file = cifs_file.copy(
332332
cifs_mnt / "dest",
333333
mode=copy_modes,
334334
)
335-
assert new_cifs_file.contents == cifs_file.contents
335+
assert new_cifs_file.raw_contents == cifs_file.raw_contents
336336
assert not os.path.islink(new_cifs_file)
337337
assert os.stat(new_cifs_file).st_ino == os.stat(cifs_file).st_ino # Hardlink
338338

339339
# Hardlinks not supported across logical volumes
340340
new_ext4_file2 = ext4_file.copy(
341341
ext4_mnt2 / "dest", mode=File.CopyMode.copy | File.CopyMode.hardlink
342342
)
343-
assert new_ext4_file2.contents == ext4_file.contents
343+
assert new_ext4_file2.raw_contents == ext4_file.raw_contents
344344
assert not os.path.islink(new_ext4_file2)
345345
assert (
346346
os.stat(ext4_file).st_ino != os.stat(new_ext4_file2).st_ino
@@ -351,7 +351,7 @@ def test_copy_constraints(tmp_path):
351351
cifs_mnt / "dest",
352352
mode=copy_modes,
353353
)
354-
assert ext4_file_on_cifs.contents == ext4_file.contents
354+
assert ext4_file_on_cifs.raw_contents == ext4_file.raw_contents
355355
assert not os.path.islink(ext4_file_on_cifs)
356356
assert (
357357
os.stat(ext4_file).st_ino != os.stat(ext4_file_on_cifs).st_ino

fileformats/field/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def __init__(self, value: ty.Any):
126126
if isinstance(value, Decimal):
127127
self.value = value.value
128128
try:
129-
self.value = decimal.Decimal(value)
129+
self.value = (
130+
value.value if isinstance(value, Decimal) else decimal.Decimal(value)
131+
)
130132
except decimal.InvalidOperation as e:
131133
raise FormatMismatchError(str(e)) from None
132134

fileformats/testing/basic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
from fileformats.generic import UnicodeFile
1+
from fileformats.text import Plain
22

33

4-
class Foo(UnicodeFile):
4+
class Foo(Plain):
55

66
ext = ".foo"
77

88

9-
class Bar(UnicodeFile):
9+
class Bar(Plain):
1010

1111
ext = ".bar"
1212

1313

14-
class Baz(UnicodeFile):
14+
class Baz(Plain):
1515

1616
ext = ".baz"
1717

1818

19-
class Qux(UnicodeFile):
19+
class Qux(Plain):
2020

2121
ext = ".qux"

fileformats/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Plain(Text, UnicodeFile):
1212
iana_mime = "text/plain"
1313

1414

15-
class TextFile(Text, UnicodeFile):
15+
class TextFile(Plain, UnicodeFile):
1616
ext = ".txt"
1717

1818

0 commit comments

Comments
 (0)