FileInfo¶
- class baseband.base.base.FileInfo(opener)[source] [edit on github]¶
Bases:
object
File information collector.
The instance can be used as a function on a file name to get information from that file, by opening it and retrieving
info
.- Parameters:
- openercallable
The function to use to open files
Notes
The class is perhaps most easily used via the class method
create
.Methods Summary
__call__
(name, **kwargs)Collect baseband file information.
check_consistency
(info, **kwargs)Check consistency between info and the given arguments.
check_key
(key, value, info)Check consistency for a given key and value.
create
(ns)Create an info getter for the given namespace.
get_file_info
(name, **kwargs)Open a file in binary mode and retrieve info.
get_stream_info
(name, file_info, **kwargs)Open a file in stream mode and retrieve info.
is_ok
(info)Wether the item returned by _get_info has valid information.
wrapped
([module, doc])Wrap as a function named info, replacing docstring and module.
Methods Documentation
- __call__(name, **kwargs)[source] [edit on github]¶
Collect baseband file information.
First try opening as a binary file and check whether the file is of the correct format. If so, and no required information is missing, re-open as a stream, and get information like the start time, sample rate, etc.
- Parameters:
- namestr or filehandle, or sequence of str
File name, filehandle, or sequence of file names.
- **kwargs
Any other arguments the opener needs to open as a stream.
- Returns:
- info
FileReaderInfo
orStreamReaderInfo
. In addition to the normalinfo
attributes, also stored are attributes about what happened to the keyword arguments:used_kwargs
,consistent_kwargs
,inconsistent_kwargs
andirrelevant_kwargs
.
- check_consistency(info, **kwargs)[source] [edit on github]¶
Check consistency between info and the given arguments.
The keyword arguments will be sorted into those that were used by the file opener and those that were unused, with the latter split in those that had consistent, inconsistent, or irrelevant information. They are stored on the
info
instance inused_kwargs
,consistent_kwargs
,inconsistent_kwargs
andirrelevant_kwargs
attributes, respectively.- Parameters:
- info
StreamReaderInfo
Information gleaned from a file opened in stream reading mode.
- **kwargs
Keyword arguments passed to the opener.
- info
- check_key(key, value, info)[source] [edit on github]¶
Check consistency for a given key and value.
- Parameters:
- keystr
Name of the key.
- valueobject
Corresponding value.
- info
StreamReaderInfo
Information collected by opening a file in stream reader mode.
- Returns:
- consistentTrue, False, or None
Whether the information on
info
forkey
is consistent withvalue
.None
if it could not be determined.
- classmethod create(ns)[source] [edit on github]¶
Create an info getter for the given namespace.
This assumes that the namespace contains an
open
function, which is used to create an instance of the info class that is wrapped in a function with__module__
set to the calling module (inferred from the namespace).- Parameters:
- nsdict
Namespace to look in. Generally, pass in
globals()
at the call site.
- get_file_info(name, **kwargs)[source] [edit on github]¶
Open a file in binary mode and retrieve info.
Any keyword arguments that were required to open the file will be stored as a
used_kwargs
attribute on the returnedinfo
.- Parameters:
- namestr or filehandle
Item to be opened for reading in binary mode.
- **kwargs
Any keyword arguments that might be required to open the file successfully (e.g.,
decade
for Mark 4).
- Returns:
- info
FileReaderInfo
Information on the file. Will evaluate as
False
if the file was not in the right format.
- info
- Raises:
- FileNotFoundError
If the file does not exist.
Notes
Getting information for an existing file should never fail. If an
Exception
is raised or returned, it is a bug in the file reader.
- get_stream_info(name, file_info, **kwargs)[source] [edit on github]¶
Open a file in stream mode and retrieve info.
Any keyword arguments that were required to open the file will be stored as a
used_kwargs
attribute on the returnedinfo
.- Parameters:
- namestr or filehandle
Item to be opened for reading in stream mode.
- file_info
FileReaderInfo
Information gleaned from opening in binary mode.
- **kwargs
Any keyword arguments that might be required to open the file successfully (e.g.,
decade
for Mark 4).
- Returns:
- info
StreamReaderInfo
Information on the file. Will evaluate as
False
if the file was not in the right format. Will returnNone
if no sample rate information was present, or anException
if the opening as a stream failed.
- info
- is_ok(info)[source] [edit on github]¶
Wether the item returned by _get_info has valid information.
- wrapped(module=None, doc=None)[source] [edit on github]¶
Wrap as a function named info, replacing docstring and module.