示例#1
0
    def read(self, heuristic=True):
        """ 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 = sppasRW.create_trs_from_extension(self.__filename)
        except AioFileExtensionError:
            if heuristic is True:
                trs = sppasRW.create_trs_from_heuristic(self.__filename)
            else:
                raise

        try:
            # Add metadata about the file
            trs.set_meta('file_reader', trs.__class__.__name__)
            trs.set_meta('file_name', os.path.basename(self.__filename))
            trs.set_meta('file_path', os.path.dirname(self.__filename))
            trs.set_meta('file_ext', os.path.splitext(self.__filename)[1])
            trs.set_meta('file_read_date', sppasTime().now)

            # Read the file content dans store into a Transcription()
            trs.read(self.__filename)

        except UnicodeError as e:
            raise AioEncodingError(filename=self.__filename, error=str(e))
        except Exception:
            raise

        return trs
示例#2
0
    def DoLogRecord(self, level, msg, info=None):
        """Override. Called to log a new record.

        :param level: (wx.LogLevel)
        :param msg: (string)
        :param info: (wx.LogRecordInfo)

        Display the message with colors.

        """
        # Display time with the default color
        self.textctrl.SetDefaultStyle(self.textctrl.default)
        self.textctrl.write("{:s} ".format(sppasTime().now[:-6]))

        # Display the log level name and message with colors
        if level == wx.LOG_Error or level == wx.LOG_FatalError:
            self.textctrl.SetDefaultStyle(self.textctrl.error)

        elif level == wx.LOG_Warning:
            self.textctrl.SetDefaultStyle(self.textctrl.warning)

        elif level in (wx.LOG_Info, wx.LOG_Message, wx.LOG_Status):
            self.textctrl.SetDefaultStyle(self.textctrl.default)

        else:
            self.textctrl.SetDefaultStyle(self.textctrl.debug)

        level_name = "[{:s}]".format(match_levels[level])
        self.textctrl.write("{0: <10}".format(level_name))
        self.textctrl.write("{:s}\n".format(msg))
示例#3
0
    def test_document_root(self):
        """document <-> root."""

        # create an element tree for EAF format
        root = sppasEAF._format_document()

        # then, create a Transcription from the element tree
        eaf = sppasEAF()
        eaf._parse_document(root)

        # so, test the result!
        author = sg.__name__ + " " + sg.__version__ + " (C) " + sg.__author__
        self.assertEqual(sppasTime().now, eaf.get_meta("file_created_date"))
        self.assertEqual("3.0", eaf.get_meta("file_created_format_version"))
        self.assertEqual(author, eaf.get_meta("file_created_author"))
示例#4
0
 def get_header():
     """Return a string with an header for logs."""
     header = "-" * 78
     header += "\n\n"
     header += " {:s} {:s}".format(sg.__name__, sg.__version__)
     header += "\n"
     header += " {:s}".format(sppasTime().now)
     header += "\n"
     header += " {:s}".format(platform.platform())
     header += "\n"
     header += " python {:s}".format(platform.python_version())
     if IMPORT_WX:
         header += "\n"
         header += " wxpython {:s}".format(wx.version())
     header += "\n\n"
     header += "-" * 78
     header += "\n\n"
     return header
示例#5
0
    def print_annotations_header(self):
        """Print the parameters information in the output stream.

        Do not print anything if no parameters were given.

        """
        if self.parameters is None:
            return

        self.print_message(' '*24 + info(1054, "annotations"))
        self.print_newline()
        self.print_message(' '*24 + info(1035, "annotations"))
        self.print_separator()
        self.print_newline()

        self.print_message(info(1036, "annotations") + ': ' + sppasTime().now)
        self.print_message(info(1037, "annotations") + ': ')
        for i in range(self.parameters.get_step_numbers()):
            if self.parameters.get_lang(i) is not None:
                self.print_item(self.parameters.get_step_name(i),
                                self.parameters.get_lang(i))
            else:
                self.print_item(self.parameters.get_step_name(i), "---")
        self.print_newline()

        self.print_message(info(1038, "annotations") + ': ')
        for sinput in self.parameters.get_sppasinput():
            self.print_item(sinput)
        self.print_newline()

        self.print_message(info(1039, "annotations") + ': ')
        for i in range(self.parameters.get_step_numbers()):
            self.print_stat_item(i)
        self.print_newline()

        self.print_message(info(1040, "annotations") +
                           ': ' +
                           self.parameters.get_output_format())
        self.print_newline()
示例#6
0
    def write(self, filename):
        """Write an XRA file.

        :param filename: (str)

        """
        root = ET.Element('Document')
        author = sg.__name__ + " " + sg.__version__ + " (C) " + sg.__author__
        root.set('author', author)
        root.set('date', sppasTime().now)
        root.set('format', self.__format)
        root.set('name', self.get_name())

        metadata_root = ET.SubElement(root, 'Metadata')
        sppasXRA.format_metadata(metadata_root, self)
        if len(metadata_root.findall('Entry')) == 0:
            root.remove(metadata_root)

        for tier in self:
            tier_root = ET.SubElement(root, 'Tier')
            sppasXRA.format_tier(tier_root, tier)

        for media in self.get_media_list():
            media_root = ET.SubElement(root, 'Media')
            self._format_media(media_root, media)

        hierarchy_root = ET.SubElement(root, 'Hierarchy')
        self._format_hierarchy(hierarchy_root)

        for vocabulary in self.get_ctrl_vocab_list():
            vocabulary_root = ET.SubElement(root, 'Vocabulary')
            self._format_vocabulary(vocabulary_root, vocabulary)

        sppasXRA.indent(root)
        tree = ET.ElementTree(root)
        tree.write(filename,
                   encoding=sg.__encoding__,
                   method="xml",
                   xml_declaration=True)
示例#7
0
    def __init__(self, meta_object=None):
        """Create a new sppasMetaInfoTier instance.

        Add and activate all known information about SPPAS.

        :param meta_object: (sppasMetadata) where to get meta infos.

        """
        super(sppasMetaInfoTier, self).__init__()

        if meta_object is None:
            m = sppasMetaData()
            m.add_software_metadata()

            for key in m.get_meta_keys():
                self.add_metainfo(key, m.get_meta(key))
            self.add_metainfo('date', sppasTime().now)

        else:

            for key in meta_object.get_meta_keys():
                self.add_metainfo(key, meta_object.get_meta(key))
示例#8
0
    def write(self, transcription):
        """Write a transcription into a file.

        :param transcription: (sppasTranscription)

        """
        trs_rw = sppasRW.create_trs_from_extension(self.__filename)
        trs_rw.set(transcription)

        # Add metadata about the file
        trs_rw.set_meta('file_writer', trs_rw.__class__.__name__)
        trs_rw.set_meta('file_name', os.path.basename(self.__filename))
        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