adapters
Interface to perform DirSchema validation on various directory-like structures.
IDirectory ¶
Bases: ABC
Abstract interface for things that behave like directories and files.
An adapter is intended to be instantiated mainly using for_path
,
based on a regular path in the file system.
Use the constructor to initialize an adapter for a more general data source (such as an object to work with an open archive file, etc.)
Source code in src/dirschema/adapters.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
__init__
abstractmethod
¶
__init__(obj: object) -> None
Initialize an instance for a suitable directory-like object.
Source code in src/dirschema/adapters.py
46 47 48 |
|
for_path
abstractmethod
classmethod
¶
for_path(path: Path)
Return an instance for a path to a archive file or a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
Path to a file or directory compatible with the adapter. |
required |
Source code in src/dirschema/adapters.py
50 51 52 53 54 55 56 57 |
|
get_paths
abstractmethod
¶
get_paths() -> Iterable[str]
Return paths relative to root directory that are to be checked.
Source code in src/dirschema/adapters.py
59 60 61 |
|
is_dir
abstractmethod
¶
is_dir(path: str) -> bool
Return whether the path is (like) a directory.
Source code in src/dirschema/adapters.py
63 64 65 |
|
is_file
abstractmethod
¶
is_file(path: str) -> bool
Return whether the path is (like) a file.
Source code in src/dirschema/adapters.py
67 68 69 |
|
open_file
abstractmethod
¶
open_file(path: str) -> Optional[IO[bytes]]
Try loading data from file at given path (to perform validation on it).
Source code in src/dirschema/adapters.py
71 72 73 |
|
decode_json ¶
decode_json(data: IO[bytes], path: str) -> Optional[Any]
Try parsing binary data stream as JSON (to perform validation on it).
Second argument is the path of the opened stream.
Default implementation will first try parsing as JSON, then as YAML.
Source code in src/dirschema/adapters.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
load_meta ¶
load_meta(path: str) -> Optional[Any]
Use open_file and decode_json to load JSON metadata.
Source code in src/dirschema/adapters.py
90 91 92 93 |
|
RealDir ¶
Bases: IDirectory
Pass-through implementation for working with actual file system.
Source code in src/dirschema/adapters.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
__init__ ¶
__init__(path: Path) -> None
Initialize adapter for a plain directory path.
Source code in src/dirschema/adapters.py
99 100 101 |
|
for_path
classmethod
¶
for_path(path: Path)
See dirschema.adapters.IDirectory.for_path.
Source code in src/dirschema/adapters.py
103 104 105 106 |
|
get_paths ¶
get_paths() -> Iterable[str]
See dirschema.adapters.IDirectory.get_paths.
Source code in src/dirschema/adapters.py
108 109 110 111 112 113 |
|
open_file ¶
open_file(path: str) -> Optional[IO[bytes]]
See dirschema.adapters.IDirectory.open_file.
Source code in src/dirschema/adapters.py
115 116 117 118 119 120 |
|
is_dir ¶
is_dir(dir: str) -> bool
See dirschema.adapters.IDirectory.is_dir.
Source code in src/dirschema/adapters.py
122 123 124 |
|
is_file ¶
is_file(dir: str) -> bool
See dirschema.adapters.IDirectory.is_file.
Source code in src/dirschema/adapters.py
126 127 128 |
|
ZipDir ¶
Bases: IDirectory
Adapter for working with zip files (otherwise equivalent to RealDir
).
Source code in src/dirschema/adapters.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
__init__ ¶
__init__(zip_file: zip.ZipFile)
Initialize adapter for a zip file.
Source code in src/dirschema/adapters.py
134 135 136 137 138 |
|
for_path
classmethod
¶
for_path(path: Path)
See dirschema.adapters.IDirectory.for_path.
Source code in src/dirschema/adapters.py
140 141 142 143 144 |
|
get_paths ¶
get_paths() -> Iterable[str]
See dirschema.adapters.IDirectory.get_paths.
Source code in src/dirschema/adapters.py
146 147 148 |
|
open_file ¶
open_file(path: str) -> Optional[IO[bytes]]
See dirschema.adapters.IDirectory.open_file.
Source code in src/dirschema/adapters.py
150 151 152 153 154 155 |
|
is_dir ¶
is_dir(dir: str) -> bool
See dirschema.adapters.IDirectory.is_dir.
Source code in src/dirschema/adapters.py
160 161 162 163 |
|
is_file ¶
is_file(dir: str) -> bool
See dirschema.adapters.IDirectory.is_file.
Source code in src/dirschema/adapters.py
165 166 167 168 |
|
H5Dir ¶
Bases: IDirectory
Adapter for working with HDF5 files.
Attributes do not fit nicely into the concept of just directories and files. The following conventions are used to checking attributes:
An attribute 'attr' of some dataset or group '/a/b' is mapped to the path '/a/b@attr' and is interpreted as a file.
Therefore, '@' MUST NOT be used in names of groups, datasets or attributes.
Only JSON is supported for the metadata, assuming that HDF5 files are usually not constructed by hand (which is the main reason for YAML support in the other cases).
All stored metadata entities must have a name ending with ".json" in order to distinguish them from plain strings. This is done because datasets or attributes are often used for storing simple values that could also be validated using a JSON Schema.
Source code in src/dirschema/adapters.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
__init__ ¶
__init__(hdf5_file: h5py.File) -> None
Initialize adapter for a HDF5 file.
Source code in src/dirschema/adapters.py
197 198 199 |
|
for_path
classmethod
¶
for_path(dir: Path)
See dirschema.adapters.IDirectory.for_path.
Source code in src/dirschema/adapters.py
201 202 203 204 205 206 |
|
get_paths ¶
get_paths() -> Iterable[str]
See dirschema.adapters.IDirectory.get_paths.
Source code in src/dirschema/adapters.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
is_dir ¶
is_dir(path: str) -> bool
See dirschema.adapters.IDirectory.is_dir.
Source code in src/dirschema/adapters.py
224 225 226 227 228 229 230 231 232 |
|
is_file ¶
is_file(path: str) -> bool
See dirschema.adapters.IDirectory.is_file.
Source code in src/dirschema/adapters.py
234 235 236 237 238 239 240 241 242 243 244 |
|
decode_json ¶
decode_json(data: IO[bytes], path: str) -> Optional[Any]
See dirschema.adapters.IDirectory.decode_json.
Source code in src/dirschema/adapters.py
246 247 248 249 250 251 252 253 254 255 256 257 |
|
open_file ¶
open_file(path: str) -> Optional[IO[bytes]]
See dirschema.adapters.IDirectory.open_file.
Source code in src/dirschema/adapters.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
get_adapter_for ¶
get_adapter_for(path: Path) -> IDirectory
Return suitable interface adapter based on path and file extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
Path to directory or archive file |
required |
Returns:
Type | Description |
---|---|
IDirectory
|
An adapter instance that can be used for dirschema validation. |
Raises:
Type | Description |
---|---|
ValueError
|
If no suitable adapter was found for the path. |
Source code in src/dirschema/adapters.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|