A reader/writer of any supported workspace file format.
Module sppas.src.wkps
Class sppasWkpRW
Description
Constructor
Create a workspace reader/writer.
Parameters
- filename: (str)
View Source
def __init__(self, filename):
"""Create a workspace reader/writer.
:param filename: (str)
"""
self.__filename = u(filename)
Public functions
default_extension
Return the default extension to read/write workspaces.
View Source
@staticmethod
def default_extension():
"""Return the default extension to read/write workspaces."""
return sppasWJSON().default_extension
extensions
Return the list of supported extensions in lower case.
View Source
@staticmethod
def extensions():
"""Return the list of supported extensions in lower case."""
return list(sppasWkpRW.WORKSPACE_TYPES.keys())
read
Read a workspace from a file.
Returns
- (sppasWkpRW)
View Source
def read(self):
"""Read a workspace from a file.
:returns: (sppasWkpRW)
"""
try:
wkp = sppasWkpRW.create_wkp_from_extension(self.__filename)
wkp.read(self.__filename)
except Exception:
raise
return wkp
create_wkp_from_extension
Return a workspace according to a filename.
Parameters
- filename: (str)
Returns
- sppasBaseWkpIO()
View Source
@staticmethod
def create_wkp_from_extension(filename):
"""Return a workspace according to a filename.
:param filename: (str)
:returns: sppasBaseWkpIO()
"""
extension = os.path.splitext(filename)[1][1:]
extension = extension.lower()
if extension in sppasWkpRW.extensions():
return sppasWkpRW.WORKSPACE_TYPES[extension]()
raise IOExtensionError(filename)
write
Write a workspace into a file.
Parameters
- wkp: (sppasWorkspace) Data to be saved
View Source
def write(self, wkp):
"""Write a workspace into a file.
:param wkp: (sppasWorkspace) Data to be saved
"""
wkp_rw = sppasWkpRW.create_wkp_from_extension(self.__filename)
wkp_rw.set(wkp)
try:
wkp_rw.write(self.__filename)
except UnicodeDecodeError as e:
raise AioEncodingError(self.__filename, str(e))
except Exception:
raise
return wkp_rw