SPPAS 4.20

Module sppas.src.anndata

Class sppasTrsRW

Description

Main parser of annotated data: Reader and writer of annotated data.

All the 3 types of annotated files are supported: ANNOT, MEASURE, TABLE.

Constructor

Create a Transcription reader-writer.

Parameters
  • filename: (str)
View Source
def __init__(self, filename):
    """Create a Transcription reader-writer.

    :param filename: (str)

    """
    self.__filename = u(filename)

Public functions

extensions

Return the whole list of supported extensions (case sensitive).

View Source
@staticmethod
def extensions():
    """Return the whole list of supported extensions (case sensitive)."""
    return list(sppasTrsRW.TRANSCRIPTION_TYPES.keys())
extensions_in

Return the list of supported extensions if the reader exists.

View Source
@staticmethod
def extensions_in():
    """Return the list of supported extensions if the reader exists."""
    e = list()
    for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
        fp = FileFormatProperty(extension=ext)
        if fp.get_reader() is True:
            e.append(ext)
    return e
extensions_out

Return the list of supported extensions if the writer exists.

View Source
@staticmethod
def extensions_out():
    """Return the list of supported extensions if the writer exists."""
    e = list()
    for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
        fp = FileFormatProperty(extension=ext)
        if fp.get_writer() is True:
            e.append(ext)
    return e
annot_extensions

Return the list of ANNOT extensions (case sensitive).

View Source
@staticmethod
def annot_extensions():
    """Return the list of ANNOT extensions (case sensitive)."""
    e = list()
    for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
        fp = FileFormatProperty(extension=ext)
        if fp.get_trs_type() == 'ANNOT':
            e.append(ext)
    return e
measure_extensions

Return the list of MEASURE extensions (case sensitive).

View Source
@staticmethod
def measure_extensions():
    """Return the list of MEASURE extensions (case sensitive)."""
    e = list()
    for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
        fp = FileFormatProperty(extension=ext)
        if fp.get_trs_type() == 'MEASURE':
            e.append(ext)
    return e
table_extensions

Return the list of TABLE extensions (case sensitive).

View Source
@staticmethod
def table_extensions():
    """Return the list of TABLE extensions (case sensitive)."""
    e = list()
    for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
        fp = FileFormatProperty(extension=ext)
        if fp.get_trs_type() == 'TABLE':
            e.append(ext)
    return e
get_filename

Return the filename.

View Source
def get_filename(self):
    """Return the filename."""
    return self.__filename
set_filename

Set a new filename.

Parameters
  • filename: (str)
View Source
def set_filename(self, filename):
    """Set a new filename. 

        :param filename: (str)

        """
    self.__filename = u(filename)
read

Read a transcription from a file.

Parameters
  • heuristic: (bool) if the extension of the file is unknown, use an heuristic to detect the format, then to choose the reader-writer.
Returns
  • sppasTranscription reader-writer
View Source
def read(self, heuristic=False):
    """Read a transcription from a file.

        :param heuristic: (bool) if the extension of the file is unknown, use
        an heuristic to detect the format, then to choose the reader-writer.
        :returns: sppasTranscription reader-writer

        """
    try:
        trs = sppasTrsRW.create_trs_from_extension(self.__filename)
    except IOExtensionError:
        if heuristic is True:
            trs = sppasTrsRW.create_trs_from_heuristic(self.__filename)
        else:
            raise
    if os.path.exists(self.__filename) is False:
        raise AioError(self.__filename)
    try:
        fn = u(self.__filename)
        trs.set_meta('file_reader', trs.__class__.__name__)
        trs.set_meta('file_name', os.path.basename(fn))
        trs.set_meta('file_path', os.path.dirname(fn))
        trs.set_meta('file_ext', os.path.splitext(fn)[1])
        trs.set_meta('file_read_date', sppasTime().now)
        trs.read(self.__filename)
    except UnicodeError as e:
        raise AioEncodingError(filename=self.__filename, error_msg=str(e))
    except IOError:
        raise
    except Exception:
        raise
    return trs
create_trs_from_extension

Return a transcription according to a given filename.

Only the extension of the filename is used.

Parameters
  • filename: (str)
Returns
  • Transcription()
View Source
@staticmethod
def create_trs_from_extension(filename):
    """Return a transcription according to a given filename.

        Only the extension of the filename is used.

        :param filename: (str)
        :returns: Transcription()

        """
    extension = os.path.splitext(filename)[1][1:]
    logging.debug('Parse an annotated file. Extension: {:s}'.format(extension))
    for ext in sppasTrsRW.TRANSCRIPTION_TYPES.keys():
        if ext.lower() == extension.lower():
            return sppasTrsRW.TRANSCRIPTION_TYPES[ext]()
    raise IOExtensionError(filename)
create_trs_from_heuristic

Return a transcription according to a given filename.

The given file is opened and an heuristic allows to fix the format.

Parameters
  • filename: (str)
Returns
  • Transcription()
View Source
@staticmethod
def create_trs_from_heuristic(filename):
    """Return a transcription according to a given filename.

        The given file is opened and an heuristic allows to fix the format.

        :param filename: (str)
        :returns: Transcription()

        """
    for file_reader in sppasTrsRW.TRANSCRIPTION_TYPES.values():
        try:
            if file_reader.detect(filename) is True:
                return file_reader()
        except:
            continue
    return sppasRawText()
write

Write a transcription into a file.

Parameters
  • transcription: (sppasTranscription)
View Source
def write(self, transcription):
    """Write a transcription into a file.

        :param transcription: (sppasTranscription)

        """
    trs_rw = sppasTrsRW.create_trs_from_extension(self.__filename)
    trs_rw.set(transcription)
    trs_rw.set_meta('file_writer', trs_rw.__class__.__name__)
    trs_rw.set_meta('file_name', os.path.basename(self.__filename))
    if trs_rw.is_meta_key('file_path') is False:
        trs_rw.set_meta('file_path', os.path.dirname(self.__filename))
    trs_rw.set_meta('file_ext', os.path.splitext(self.__filename)[1])
    trs_rw.set_meta('file_write_date', '{:s}'.format(sppasTime().now))
    file_version = int(trs_rw.get_meta('file_version', '0')) + 1
    trs_rw.set_meta('file_version', str(file_version))
    try:
        trs_rw.write(self.__filename)
    except UnicodeError as e:
        raise AioEncodingError(self.__filename, str(e))
    except Exception:
        raise