Example #1
0
    def getTextDBDictRepr(self):
        """
                Body.getTextDBDictRepr

                Return a string representing the data in the TextDBDict format.
        """
        res = []

        for entrydata in self.bodydata.getEntries():

            entry = Entry( errors = self.errors,
                           logotherasdata = self.logotherasdata )
            entry.setData( entrydata )
            res.append( entry.getTextDBDictRepr() )

        return NEWLINE.join(res)
Example #2
0
    def initFromStr(self,
                    informationsdata,
                    src,
                    reading_position,
                    srclanguage):
        """
                Body.initFromStr

                informationsdata        : InformationsData object
                src                     : either None if no body available either
                                          a list of strings without any EOF marker.
                reading_position        : ReadingPosition object
                srclanguage             : Logotheras.language.language.Language object
        """
        self.reset()
        self.reading_position = reading_position

        # no body at all ?
        if src is None:
            return self

        # we have a body to be analysed in <src> :
        self.reading_position.update( body = src )

        current_position = None         # None or "entry.content"

        current_entry_title__hlevel = None
        current_entry_title__pend_txt = None # pend[ing] text in the title
        current_entry_content = []      # list of strings

        for line in src:

            # maybe an "entry title" ?
            _hlevel = HierarchicalLevel(errors = self.errors,
                formatstr = logotheras.options.OPTIONS["textdbdict::HLEVELformatrst by reading"])
            (hlevel_found,
             hlevel,
             hlevel_pending_txt) = _hlevel.initFromTextDBDictEntryTitle(line)

            if current_position is None:

                if hlevel_found:
                    # entry title : with <line> begins the first entry of <src>
                    current_position = "entry.content"

                    current_entry_title__hlevel = HierarchicalLevel(self.errors,
                        formatstr = \
                            logotheras.options.OPTIONS["textdbdict::HLEVELformatrst by reading"])
                    current_entry_title__pend_txt = hlevel_pending_txt
                    current_entry_content = []

                else:
                    # content (text or extracts) to be linked with the article (not with an entry) :
                    # -> hierarchical level = []:
                    current_entry_title__hlevel = HierarchicalLevel(self.errors,
                        formatstr = \
                            logotheras.options.OPTIONS["textdbdict::HLEVELformatrst by reading"])
                    current_position = "entry.content"
                    current_entry_content.append( line )

            elif current_position == "entry.content":

                if hlevel_found:
                    # entry title : end of the current entry; with <line> begins a new entry.

                    # current entry, to be stored :
                    new_entry = Entry(errors = self.errors,
                                      logotherasdata = self.logotherasdata,
                                     )

                    title_hlevel        = current_entry_title__hlevel
                    title_pending_text  = current_entry_title__pend_txt

                    # we store every entry but the entries with no text and no hlevel.
                    # This kind of entries are detected since there may be empty lines
                    # after an article's header. But entries other than article with
                    # no text are accepted and stored.
                    if not (title_pending_text is None and title_hlevel.getDepth()==0):
                        new_entry.initFromStr(informationsdata = informationsdata,
                                              title_hlevel = title_hlevel,
                                              title_pending_text = title_pending_text,
                                              str_content = current_entry_content,
                                              reading_position = self.reading_position,
                                              srclanguage = srclanguage)

                        entry_name_in_bodydata = new_entry.entrydata.getEntryNameInBodyData()
                        self.bodydata[entry_name_in_bodydata] = new_entry.entrydata

                    # new entry, to be read :
                    current_position = "entry.content"

                    current_entry_title__hlevel = hlevel
                    current_entry_title__pend_txt = hlevel_pending_txt
                    current_entry_content = []      # list of strings

                else:
                    # entry content :
                    current_entry_content.append( line )

        if current_entry_title__hlevel is not None:
            # last entry to be added :
            new_entry = Entry(errors = self.errors,
                              logotherasdata = self.logotherasdata,
                             )
            new_entry.initFromStr(informationsdata = informationsdata,
                                  title_hlevel = current_entry_title__hlevel,
                                  title_pending_text = current_entry_title__pend_txt,
                                  str_content = current_entry_content,
                                  reading_position = self.reading_position,
                                  srclanguage = srclanguage)

            self.bodydata[new_entry.entrydata.title] = new_entry.entrydata

        return self