[docs]@singledispatchdefsave_object(x:Any,path:str,**kwargs):""" Save an object to its on-disk representation. **dolomite** extensions should define methods for this generic to stage different object classes. Saver methods may accept additional arguments in the ``kwargs``; these should be prefixed by the object type to avoid conflicts (see :py:func:`~dolomite_base.save_data_frame.save_data_frame` for examples). Saver methods should also use the :py:func:`~validate_saves` decorator to ensure that the generated output in ``path`` is valid. Args: x: Object to be saved. path: Path to the output directory. kwargs: Further arguments to be passed to individual methods. Returns: `x` is saved to `path`. """ifhasattr(type(x),"mro"):importsyshierarchy=type(x).mro()foryinhierarchy:nm=y.__name__ifnmnotin_save_object_implementations:continuepkg=_save_object_implementations[nm]try:import_module(pkg)# this should hopefully register the methods to the dispatcher.except:raiseModuleNotFoundError("cannot find '"+pkg+"', which contains a 'save_object' method for type '"+type(x).__name__+"'")returnsave_object(x,path,**kwargs)raiseNotImplementedError("'save_object' is not implemented for type '"+type(x).__name__+"'")
[docs]defvalidate_saves(fn):""" Decorator to validate the output of :py:func:`~save_object`. Args: fn: Function that implements a method for ``save_object``. Returns: A wrapped version of the function that validates the directory containing the on-disk representation of the saved object. """@wraps(fn)defwrapper(x,path,**kwargs):out=fn(x,path,**kwargs)validate_object(path)returnoutreturnwrapper