sasdata.dataloader.loader module

File handler to support different file extensions. Uses reflectometer registry utility. The default readers are found in the ‘readers’ sub-module and registered by default at initialization time. To add a new default reader, one must register it in the register_readers method found in readers/__init__.py. A utility method (find_plugins) is available to inspect a directory (for instance, a user plug-in directory) and look for new readers/writers.

class sasdata.dataloader.loader.Loader

Bases: object

Utility class to use the Registry as a singleton.

associate_file_reader(ext: str, loader) bool

Append a reader object to readers :param ext: file extension [string] :param module: reader object

associate_file_type(ext: str, module: ModuleType) bool

Look into a module to find whether it contains a Reader class. If so, append it to readers and (potentially) to the list of writers for the given extension :param ext: file extension [string] :param module: module object

find_plugins(directory: str) int

Find plugins in a given directory :param directory: directory to look into to find new readers/writers

get_wildcards()

Return the list of wildcards

load(file_path_list: List[str | Path] | str | Path, format: List[str] | str | None = None) List[Data1D | Data2D]

Load a file or series of files :param file_path_list: String representations of any number of file paths. This can either be a list or a string :param format: specified format to use (optional) :return: a list of DataInfo objects and/or loading exceptions.

save(file: str, data, format: str) bool

Save a DataInfo object to file :param file: file name (path) :param data: DataInfo object :param format: format to write the data in

class sasdata.dataloader.loader.Registry

Bases: ExtensionRegistry

Registry class for file format extensions. Readers and writers are supported.

associate_file_reader(file_extension, reader)

Append a reader object to readers :param file_extension: file extension [string] :param reader: reader object

associate_file_type(ext: str, module: ModuleType) bool

Look into a module to find whether it contains a Reader class. If so, APPEND it to readers and (potentially) to the list of writers for the given extension :param ext: file extension [string] :param module: module object

find_plugins(dir: str)

Find readers in a given directory. This method can be used to inspect user plug-in directories to find new readers/writers. :param dir: directory to search into :return: number of readers found

load(file_path_list: List[str | Path] | str | Path, ext: List[str] | str | None = None, debug: bool | None = False, use_defaults: bool | None = True)

Call the loader for the file type of path.

Parameters:
  • file_path_list – A list of pathlib.Path objects and/or string representations of file paths

  • ext – A list of explicit extensions, to force the use of a particular reader for a particular file. Usage If any ext is passed, the length of the ext list should be the same as the length of the file path list. A single extention, as a string or a list of length 1, will apply ext to all files in the file path list. Any other case will result in an error.

  • debug – when True, print the traceback for each loader that fails

  • use_defaults – Flag to use the default readers as a backup if the main reader fails or no reader exists

Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS readers if no reader was registered for the file’s extension.

lookup_writers(path)
Returns:

the loader associated with the file type of path.

Raises ValueError:

if file type is not known.

save(path: str, data, format: str | None = None)

Call the writer for the file type of path. Raises ValueError if no writer is available. Raises KeyError if format is not available. May raise a writer-defined exception if writer fails.