SciFile-Core 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scifile_core/__init__.py +111 -0
- scifile_core/_content.py +490 -0
- scifile_core/_diagnostic.py +322 -0
- scifile_core/_exception.py +221 -0
- scifile_core/_io.py +705 -0
- scifile_core/_mapping.py +139 -0
- scifile_core/_model.py +1056 -0
- scifile_core/_number.py +41 -0
- scifile_core/_optional.py +40 -0
- scifile_core/py.typed +0 -0
- scifile_core/testing.py +653 -0
- scifile_core/wording.py +454 -0
- scifile_core-0.0.1.dist-info/METADATA +6 -0
- scifile_core-0.0.1.dist-info/RECORD +16 -0
- scifile_core-0.0.1.dist-info/WHEEL +5 -0
- scifile_core-0.0.1.dist-info/top_level.txt +1 -0
scifile_core/_mapping.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Files mapped into memory, and the arrays of a model that lie in them.
|
|
2
|
+
|
|
3
|
+
A package of large arrays maps a file read-only with `map_file`, so that its
|
|
4
|
+
arrays are views of the pages of the file, read when they are used. A model
|
|
5
|
+
that holds such arrays must not be written over the file that they lie in
|
|
6
|
+
without a copy: `TextFileModel` and `BinaryFileModel` write it from a copy of
|
|
7
|
+
them. Nothing is looked for while no map of `map_file` is in use, and no
|
|
8
|
+
array lies in a map unless NumPy is imported.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import mmap
|
|
14
|
+
import os
|
|
15
|
+
import stat
|
|
16
|
+
import sys
|
|
17
|
+
import weakref
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from scifile_core._io import _GZIP_MAGIC
|
|
21
|
+
from scifile_core._optional import imported_numpy
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class FileMap(mmap.mmap):
|
|
25
|
+
"""A file mapped read-only into memory, which knows the file that it maps.
|
|
26
|
+
|
|
27
|
+
`map_file` makes it, and gives it the device and the inode of the file,
|
|
28
|
+
which tell the file under any of its names, so that a hard link to it is
|
|
29
|
+
the same file. A map that its constructor makes, rather than `map_file`,
|
|
30
|
+
knows no file: writing a model whose arrays lie in it copies nothing.
|
|
31
|
+
|
|
32
|
+
Attributes
|
|
33
|
+
----------
|
|
34
|
+
device
|
|
35
|
+
The device of the file.
|
|
36
|
+
inode
|
|
37
|
+
The inode of the file.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
__slots__ = ("device", "inode")
|
|
41
|
+
|
|
42
|
+
device: int
|
|
43
|
+
inode: int
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
_ALIVE: weakref.WeakSet[FileMap] = weakref.WeakSet()
|
|
47
|
+
"""The maps that `map_file` made and that are still in use."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def map_file(file: object) -> FileMap | None:
|
|
51
|
+
"""Return a file mapped read-only into memory, or `None` to read it.
|
|
52
|
+
|
|
53
|
+
A path is mapped if it names a regular file that is not empty and not
|
|
54
|
+
gzip-compressed, on Python 3.13 and later, whose maps keep no descriptor
|
|
55
|
+
of their file open; a map of Python 3.12 keeps one while it lives, of
|
|
56
|
+
the few that a process has. The file must not change while the map is in
|
|
57
|
+
use, except by writing a model whose arrays lie in it to it, and must not
|
|
58
|
+
be cut short, as opening it with mode ``"w"`` does: a page of the map
|
|
59
|
+
beyond the end of the file ends the process when it is read.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
file
|
|
64
|
+
The input of a package's ``read``.
|
|
65
|
+
|
|
66
|
+
Returns
|
|
67
|
+
-------
|
|
68
|
+
The map, or `None` for anything else, and for a file that cannot be
|
|
69
|
+
mapped, which `read_bytes` then reads or rejects.
|
|
70
|
+
"""
|
|
71
|
+
if sys.version_info < (3, 13):
|
|
72
|
+
return None
|
|
73
|
+
if not isinstance(file, str | os.PathLike):
|
|
74
|
+
return None
|
|
75
|
+
try:
|
|
76
|
+
with Path(file).open("rb") as stream:
|
|
77
|
+
status = os.fstat(stream.fileno())
|
|
78
|
+
if not stat.S_ISREG(status.st_mode) or status.st_size == 0:
|
|
79
|
+
return None
|
|
80
|
+
if stream.read(len(_GZIP_MAGIC)) == _GZIP_MAGIC:
|
|
81
|
+
return None
|
|
82
|
+
mapped = FileMap(
|
|
83
|
+
stream.fileno(), 0, access=mmap.ACCESS_READ, trackfd=False
|
|
84
|
+
)
|
|
85
|
+
except (OSError, ValueError):
|
|
86
|
+
return None
|
|
87
|
+
mapped.device = status.st_dev
|
|
88
|
+
mapped.inode = status.st_ino
|
|
89
|
+
_ALIVE.add(mapped)
|
|
90
|
+
return mapped
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def maps_alive() -> bool:
|
|
94
|
+
"""Return whether a map that `map_file` made is still in use."""
|
|
95
|
+
return bool(_ALIVE)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def map_of(value: object) -> FileMap | None:
|
|
99
|
+
"""Return the map that `map_file` made and that an array lies in, or `None`.
|
|
100
|
+
|
|
101
|
+
An array lies in a map if the chain of its bases, through arrays and
|
|
102
|
+
memory views, ends in one.
|
|
103
|
+
"""
|
|
104
|
+
np = imported_numpy()
|
|
105
|
+
if np is None or not isinstance(value, np.ndarray):
|
|
106
|
+
return None
|
|
107
|
+
array = np.ndarray
|
|
108
|
+
owner: object = value
|
|
109
|
+
while True:
|
|
110
|
+
if isinstance(owner, array):
|
|
111
|
+
owner = owner.base
|
|
112
|
+
elif isinstance(owner, memoryview):
|
|
113
|
+
owner = owner.obj
|
|
114
|
+
else:
|
|
115
|
+
break
|
|
116
|
+
if isinstance(owner, FileMap) and owner in _ALIVE:
|
|
117
|
+
return owner
|
|
118
|
+
return None
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def written_file(target: object) -> os.stat_result | None:
|
|
122
|
+
"""Return the status of the file that a target of ``write`` writes.
|
|
123
|
+
|
|
124
|
+
The file of a path is the one that it names, also through a symbolic
|
|
125
|
+
link. That of a callable is the file of the object that it is a method
|
|
126
|
+
of, such as the method ``write`` of an open file. Other targets, and
|
|
127
|
+
files that cannot be found, give `None`.
|
|
128
|
+
"""
|
|
129
|
+
try:
|
|
130
|
+
if isinstance(target, str | os.PathLike):
|
|
131
|
+
return Path(target).stat()
|
|
132
|
+
fileno = getattr(getattr(target, "__self__", None), "fileno", None)
|
|
133
|
+
if callable(fileno):
|
|
134
|
+
descriptor = fileno()
|
|
135
|
+
if isinstance(descriptor, int):
|
|
136
|
+
return os.fstat(descriptor)
|
|
137
|
+
except (OSError, ValueError):
|
|
138
|
+
pass
|
|
139
|
+
return None
|