def writeAddendum(self, destpath, _license): """ TextDBDict.writeAddendum Write into the file named ADDENDUM_FILENAME the articles not marked by the NOTALEXICAL_ARTINAME symbol. destpath : (str) target path _license : (str) text to be written at the beginning of the addendum file """ self.errors.info("writing data into the addendum file... : " + ADDENDUM_FILENAME) source_dir = os.path.join(destpath, SOURCEDIRECTORY_DEFAULTNAME) with open( os.path.join(source_dir, ADDENDUM_FILENAME), 'w') as addendumfile: for line in _license: addendumfile.write(line) for articledata in self.logotherasdata.getArticles(): if NOTALEXICAL_ARTINAME in articledata.headerdata.artiname: article = Article( errors = self.errors, logotherasdata = self.logotherasdata ) article.setData( self.logotherasdata[articledata.headerdata.artiname] ) addendumfile.write( article.getTextDBDictRepr() )
def writeFromDataTheVersionNumber(self, destfile): """ TextDBDictInfoFile.writeFromDataTheVersionNumber destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = ( str(self.logotherasdata.informations.version_number), ) article = article.setAsSimpleArticle( artiname = VERSION_NUMBER_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheDictName(self, destfile): """ TextDBDictInfoFile.writeFromDataTheDictName destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = ( str(self.logotherasdata.informations.dictname), ) article = article.setAsSimpleArticle( artiname = ARTINAME_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheArtCategories(self, destfile): """ TextDBDictInfoFile.writeFromDataTheArtCategories destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for art_category in self.logotherasdata.informations.articlecategories: lines.append( art_category ) article = article.setAsSimpleArticle( artiname = ARTCATEGORIES_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheCreationDate(self, destfile): """ TextDBDictInfoFile.writeFromDataTheCreationDate destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) # format @003 lines = ( datetime.datetime.strftime(self.logotherasdata.informations.creation_date, CREATION_DATE_FORMAT), ) article = article.setAsSimpleArticle( artiname = CREATION_DATE_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheDictLanguage(self, destfile): """ TextDBDictInfoFile.writeFromDataTheDictLanguage destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) textdbdict_dictlanguages = DictLanguages(self.errors) textdbdict_dictlanguages.initFromInfosDictLanguages( \ self.logotherasdata.informations.dict_languages) lines = ( textdbdict_dictlanguages.getTextDBDictRepr(), ) article = article.setAsSimpleArticle( artiname = FILE_LANGUAGES_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheEditions(self, destfile): """ TextDBDictInfoFile.writeFromDataTheEditions destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for edition in self.logotherasdata.informations.editions: value = self.logotherasdata.informations.editions[edition] lines.append( "{0:30} {1} {2}".format(edition, INFOFILE_MAINSEPARATOR, value )) article = article.setAsSimpleArticle( artiname = EDITIONS_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheAuthorsGroups(self, destfile): """ TextDBDictInfoFile.writeFromDataTheAuthorsGroups destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for authors_group in self.logotherasdata.informations.authors_groups: value = self.logotherasdata.informations.authors_groups[authors_group] lines.append( "{0:30} {1} {2:06X}".format(authors_group, INFOFILE_MAINSEPARATOR, value )) article = article.setAsSimpleArticle( artiname = AUTHORS_GROUPS_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheTranslSources(self, destfile): """ TextDBDictInfoFile.writeFromDataTheTranslSources destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for tsource in self.logotherasdata.informations.translations_sources: value = self.logotherasdata.informations.translations_sources[tsource] lines.append( "{0:30} {1} {2}".format(tsource, INFOFILE_MAINSEPARATOR, value )) article = article.setAsSimpleArticle( artiname = TRANSCRIPTIONS_SOURCES_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheLicenses(self, destfile): """ TextDBDictInfoFile.writeFromDataTheLicenses destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) # we use "_license" instead of "license" to avoid a message from Pylint : lines = [] for _license in self.logotherasdata.informations.licenses: value = self.logotherasdata.informations.licenses[_license] lines.append( "{0:30} {1} {2}".format(_license, INFOFILE_MAINSEPARATOR, value)) article = article.setAsSimpleArticle( artiname = LICENSES_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def addNewArticle(self, informationsdata, artiname_prefix, current_header, current_body, srclanguage, do_not_normalize, ): """ TextDBDictFile.addNewArticle() ________________________________________________________________ informationsdata : InformationsData object ________________________________________________________________ no returned value """ new_art = Article(errors = self.errors, logotherasdata = self.logotherasdata) new_art = new_art.initFromStr( informationsdata = informationsdata, str_header = current_header, str_body = current_body, reading_position = self.reading_position, artiname_prefix = artiname_prefix, srclanguage = srclanguage, do_not_normalize = do_not_normalize) article_name = artiname_prefix + new_art.articledata.headerdata.artiname if article_name in self.logotherasdata: msg = "(ERR043) redundant header artiname : '{0}'; -> {1}" self.errors.error(msg.format(new_art.articledata.headerdata.artiname, self.reading_position)) self.logotherasdata[article_name] = new_art.articledata self.errors.debug("(TextDBDictFile.addNewArticle)"\ " the article '{0}' has been read".format(article_name))
def writeFromDataTheAuthors(self, destfile): """ TextDBDictInfoFile.writeFromDataTheAuthorsGroups destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for author in self.logotherasdata.informations.authors: value = self.logotherasdata.informations.authors[author] lines.append( "{0:30} {1} {2} {3} {4}".format(author, INFOFILE_MAINSEPARATOR, value.localized_name, INFOFILE_SECONDARYSEPARATOR, value.group, )) article = article.setAsSimpleArticle( artiname = AUTHORS_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeFromDataTheWorksTitles(self, destfile): """ TextDBDictInfoFile.writeFromDataTheWorksTitlesGroups destfile : (file descriptor) """ article = Article( errors = self.errors, logotherasdata = self.logotherasdata) lines = [] for works_title in self.logotherasdata.informations.works_titles: value = self.logotherasdata.informations.works_titles[works_title] lines.append( "{0:30} {1} {2} {3} {4}".format(works_title, INFOFILE_MAINSEPARATOR, value.author, INFOFILE_SECONDARYSEPARATOR, value.localized_title, )) article = article.setAsSimpleArticle( artiname = WORKS_TITLES_ARTICLE, lines = lines) destfile.write(article.getTextDBDictRepr())
def writeTextDBDictInto(self, destpath, srclanguage): """ TextDBDict.writeTextDBDictInto destpath : (str) srclanguage : Language object """ self.errors.debug("TextDBDict.writeTextDBDictInto; destpath=" + str(destpath) ) # maximal length for the name of the files : max_length = \ logotheras.options.OPTIONS["textdbdict::maximal number of characters in a filename"] #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Is <destpath> valid ? if not os.path.exists(destpath): msg = "(ERR021) (TextDBDict.writeTextDBDictInto) Missing directory : '{0}'" self.errors.error(msg.format(destpath)) return if not os.path.isdir(destpath): msg = "(ERR020) (TextDBDict.writeTextDBDictInto) '{0}' is not a directory" self.errors.error(msg.format(destpath)) return #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # creation of the source directory : source_dir = os.path.join(destpath, SOURCEDIRECTORY_DEFAULTNAME) if os.path.exists(source_dir): shutil.rmtree(source_dir) os.makedirs( source_dir ) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # license : # adding "#COMMENT#" before each line : sep_license = NEWLINE + "#COMMENT# " _license = sep_license.join( logotheras.options.LICENSE_TEXT.split("\n") ) # we add "#COMMENT#" before the first line since .join() doesn't. _license = "#COMMENT# " + _license # information file : infofile = TextDBDictInfoFile(logotherasdata = self.logotherasdata, errors = self.errors) infofile.writeFromData(destpath = destpath) # data file : if logotheras.options.OPTIONS["textdbdict::share out TextDBDict files"] is None: #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ # everything is packed into one file : datafile_name = "unique_file.textdbdict" datafile_name = datafile_name[:max_length] with open( os.path.join(source_dir, datafile_name), "w" ) as datafile: for line in _license: datafile.write(line) for articledata in self.logotherasdata.getArticles(): article = Article( errors = self.errors, logotherasdata = self.logotherasdata ) article.setData( articledata ) self.errors.info("writing data into the unique file : " + datafile.name) datafile.write( article.getTextDBDictRepr() ) elif logotheras.options.OPTIONS["textdbdict::share out TextDBDict files"].startswith('a'): #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ # several outputfiles : all the words begin with the same letters length = len(logotheras.options.OPTIONS["textdbdict::share out TextDBDict files"]) for (datafile_number, (articles, initial)) in enumerate(self.logotherasdata.getArticlesGroupedBy( method="initial", length=length, srclanguage=srclanguage)): # name of this new file ? if not logotheras.options.OPTIONS["textdbdict::" \ "write TextDBDict files with detailed names"]: # short name : datafile_name = "{0:0>6}.textdbdict".format(datafile_number) else: # long name : first_article = articles[0] first_article = first_article.strip() last_article = articles[-1] last_article = last_article.strip() datafile_name = \ logotheras.options.OPTIONS["textdbdict::detailed filename"].format( datafile_number, first_article, last_article) datafile_name = os.path.join( source_dir, datafile_name ) datafile_name = datafile_name[:max_length] with open(datafile_name, 'w') as datafile: self.errors.info("writing data into a new file : " + datafile.name) for line in _license: datafile.write(line) for article_name in articles: article = Article( errors = self.errors, logotherasdata = self.logotherasdata ) article.setData( self.logotherasdata[article_name] ) datafile.write( article.getTextDBDictRepr() ) # let's add the articles marked with NOTALEXICAL_ARTINAME in a # special file. self.writeAddendum(destpath, _license) else : #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ # several outputfiles : each outputfile contain the same number of articles length = int(logotheras.options.OPTIONS["textdbdict::share out TextDBDict files"]) for (datafile_number, articles) in enumerate(self.logotherasdata.getArticlesGroupedBy( method="articles' number", length=length, srclanguage=srclanguage)): # name of this new file ? if not logotheras.options.OPTIONS["textdbdict::" \ "write TextDBDict files with detailed names"]: # short name : datafile_name = "{0:0>6}.textdbdict".format(datafile_number) else: # long name : first_article = articles[0] first_article = first_article.strip() last_article = articles[-1] last_article = last_article.strip() datafile_name = \ logotheras.options.OPTIONS["textdbdict::" \ "detailed filename"].format(datafile_number, first_article, last_article) datafile_name = os.path.join( source_dir, datafile_name ) datafile_name = datafile_name[:max_length] with open(datafile_name, 'w') as datafile: self.errors.info("writing data into a new file : " + datafile.name) for line in _license: datafile.write(line) for article_name in articles: article = Article( errors = self.errors, logotherasdata = self.logotherasdata ) article.setData( self.logotherasdata[article_name] ) datafile.write( article.getTextDBDictRepr() ) # let's add the articles marked with NOTALEXICAL_ARTINAME in a # special file. self.writeAddendum(destpath, _license)