1
- import os
1
+ """Class to store the raw data of Abinit files."""
2
+
2
3
import base64
3
- from monty .json import MSONable
4
- from typing import TYPE_CHECKING
4
+ import os
5
5
from pathlib import Path
6
+
7
+ from monty .json import MSONable
6
8
from typing_extensions import Self
7
9
10
+
8
11
class AbinitStoredFile (MSONable ):
9
12
"""Wrap a file to store its raw data."""
10
13
@@ -13,57 +16,69 @@ def __init__(self, data: str | bytes, source_filepath: str | Path) -> None:
13
16
self .source_filepath = source_filepath
14
17
15
18
def as_dict (self ) -> dict :
16
- data = base64 .b64encode (self .data ).decode ('ascii' ) if self .data_type == "bytes" else self .data
17
-
19
+ """Return a dict of the stored file."""
20
+ data = (
21
+ base64 .b64encode (self .data ).decode ("ascii" )
22
+ if isinstance (self .data , bytes )
23
+ else self .data
24
+ )
25
+
18
26
return {
19
27
"@module" : self .__class__ .__module__ ,
20
28
"@class" : self .__class__ .__name__ ,
21
29
"data" : data ,
22
30
"data_type" : self .data_type ,
23
31
"source_filepath" : self .source_filepath ,
24
- }
32
+ }
25
33
26
34
@classmethod
27
35
def from_dict (cls , d : dict ) -> Self :
36
+ """Create an AbinitStoredFile from a dict."""
28
37
data_type = d ["data_type" ]
29
38
data = base64 .b64decode (d ["data" ]) if data_type == "bytes" else d ["data" ]
30
39
return cls (data = data , source_filepath = d ["source_filepath" ])
31
40
32
41
@classmethod
33
42
def from_file (cls , filepath : str | Path , data_type : str | type ) -> Self :
43
+ """Create an AbinitStoredFile from the original file."""
34
44
source_filepath = os .path .abspath (filepath )
35
- if data_type == "bytes" or data_type == bytes :
36
- read_type = 'rb'
37
- elif data_type == "str" or data_type == str :
38
- read_type = 'r'
45
+ if data_type in { "bytes" , bytes } :
46
+ read_type = "rb"
47
+ elif data_type in { "str" , str } :
48
+ read_type = "r"
39
49
else :
40
50
raise TypeError ("data_type should be either bytes or str." )
41
51
with open (source_filepath , read_type ) as f :
42
52
data = f .read ()
43
-
53
+
44
54
return cls (data = data , source_filepath = source_filepath )
45
55
46
56
@property
47
57
def data_type (self ) -> str :
58
+ """Return the type of the data."""
48
59
return type (self .data ).__name__
49
60
50
61
@property
51
62
def filename (self ) -> str :
63
+ """Return the name of the source file."""
52
64
return os .path .basename (self .source_filepath )
53
65
54
66
@property
55
67
def extension (self ) -> str :
56
- return self .source_filepath .split ("_" )[- 1 ]
68
+ """Return the extension of the source file."""
69
+ return str (self .source_filepath ).split ("_" )[- 1 ]
57
70
58
- def write (self , filepath : str | Path = None ) -> None :
71
+ def write (self , filepath : str | Path = None ) -> None :
72
+ """Write the data into a file."""
59
73
filepath = filepath or self .filename
60
- out_type = self .data_type [0 ]
61
- if self .data_type == 'bytes' :
62
- write_type = 'wb'
74
+ if self .data_type == "bytes" :
75
+ write_type = "wb"
63
76
elif self .data_type == "str" :
64
- write_type = 'w'
77
+ write_type = "w"
65
78
else :
66
- raise TypeError ("The data type is neither bytes nor string,\
67
- does it make sense to write it into a file?" )
79
+ raise TypeError (
80
+ "The data type is neither bytes nor string,\
81
+ does it make sense to write it into a file?"
82
+ )
68
83
with open (filepath , write_type ) as f :
69
84
f .write (self .data )
0 commit comments