def make_entry(self, xml_parent, entry): """ Fabrication d'une entry Atom Utilisation de la proprietes files_props si définies qui contient les liens vers les fichiers distants ainsi que les informations de taille et de disponibilité Si files_props n'est pas disponible, utilisation de la propriété files qui ne contient que la liste des fichiers distants :param xml_parent: Noeud XML auquel rattacher l'entry :param entry: données de l'entry :return: - """ xml_entry = content.xmlelt(xml_parent, "entry") content.xmlelt(xml_entry, "title", entry["title"]) # generation des liens avec les informations detaillees if 'files_props' in entry: self.add_entry_links( xml_entry, list(map(lambda x: x['url'], entry['files_props']))) # sinon avec seulement les liens vers les fichiers elif 'files' in entry: self.add_entry_links(xml_entry, entry['files']) content.xmlelt(xml_entry, "id", entry["id"]) # fabrication du contenu content.make_xhtml(xml_entry, entry) content.xmlelt(xml_entry, "updated", entry["date"]) content.xmlelt(xml_entry, "summary", entry["summary"])
def make_link(self, parent, text, filename): if filename: # Add an <a> element to parent content.xmlelt( parent, "a", f"{text}: {os.path.basename(filename)}", {"href": self.pub_url + filename}, ) else: # nothing to do pass
def test_entry_content(self): expected_out = """<feed xmlns="http://www.w3.org/2005/Atom"> <entry> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <article> <div> <h1>5 Fichiers à télécharger : </h1> <ul> <li> <a href="http://www.codage.ext.cnamts.fr/f_mediam/fo/bdm_it/ucd_total_00407_20180504.dbf">ucd_total_00407_20180504.dbf (2.7 Mo)</a> </li> <li> <a href="http://www.codage.ext.cnamts.fr/f_mediam/fo/bdm_it/ucd_maj_00407_20180504.dbf">ucd_maj_00407_20180504.dbf (33.06 Ko)</a> </li> <li> <a href="http://www.codage.ext.cnamts.fr/f_mediam/fo/bdm_it/ucd_histo_prix_00407_20180504.dbf">ucd_histo_prix_00407_20180504.dbf (1.65 Mo)</a> </li> <li> <a href="http://www.codage.ext.cnamts.fr/f_mediam/fo/bdm_it/retro_histo_taux_00407_20180504.dbf">retro_histo_taux_00407_20180504.dbf (434.33 Ko)</a> </li> <li> <a href="http://www.codage.ext.cnamts.fr/f_mediam/fo/bdm_it/retro_histo_cout_sup_00407_20180504.dbf">retro_histo_cout_sup_00407_20180504.dbf (228.21 Ko)</a> </li> </ul> </div> <div> <h1>Informations sur la version 407</h1> <p>23 lignes mises à jour.</p> <p>Les médicaments concernés par la mise à jour sont les suivants :</p> <p>KOVALTRY, CASPOFUNGINE, NEVIRAPINE, VESANOID, ABACAVIR, EPOPROSTENOL, PANZYGA</p> <p/> </div> </article> </div> </content> </entry> </feed> """ root = content.xmlelt(None, "feed", None, {"xmlns": "http://www.w3.org/2005/Atom"}) xml_entry = content.xmlelt(root, "entry") content.make_xhtml(xml_entry, self.infos_in) print(content.xml2text(root)) #self.assertEqual(content.xml2text(root), expected_out) root = self.assertXmlDocument(expected_out)
def add_entry_links(self, xml_parent, link_list): """ Création des liens atom à partir d'une liste de liens :param xml_parent: noeud XML auquel rattacher ces liens :param link_list: liste de liens :return: - """ for link in link_list: content.xmlelt(xml_parent, "link", None, { "href": link, "rel": "related", "type": self.mimetype(link) })
def to_rss(self, xmlinfos, data_date, data_files): self.logger.debug(f"To RSS, {len(xmlinfos)} blocks") root = content.xmlelt(None, "div") [root.append(inf) for inf in xmlinfos] info = dict( html=content.xml2text(root, atom.Feed.FEED_ENCODING, xml_decl=False), files=list( map(lambda x: self.pub_url + os.path.basename(x), data_files)), title=f"Mise à jour du {data_date}", id=f"rpps{data_date}", date=dt.now(datetime.timezone.utc).isoformat(sep="T"), summary=f"Informations sur la publication du {data_date}", ) return info
def generate(self, entries): """ Génération du fichier XML Atom :param entries: listes des entrées du fichier :return: noeud XML du document Atom XML """ self.logger.debug("Feed to XML : {} entries".format(len(entries))) root = content.xmlelt(None, "feed", None, {"xmlns": "http://www.w3.org/2005/Atom"}) content.xmlelt(root, "id", self.feed_config["header"]["id"]) content.xmlelt(root, "title", self.feed_config["header"]["title"]) content.xmlelt(root, "subtitle", self.feed_config["header"]["subtitle"]) content.xmlelt(root, "link", None, { "href": self.feed_config["header"]["link"], "rel": "related" }) content.xmlelt(root, "link", None, { "href": self.feed_url(), "rel": "self" }) content.xmlelt(root, "updated", self.update_date) author = SubElement(root, "author") content.xmlelt(author, "name", self.feed_config["header"]["author"]["name"]) content.xmlelt(author, "email", self.feed_config["header"]["author"]["email"]) content.xmlelt(root, "category", None, {"term": self.feed_config["header"]["category"]}) content.xmlelt(root, "generator", "python program - atom.py", { "uri": "https://github.com/flrt/atom_gen", "version": "1.0" }) content.xmlelt(root, "rights", "CC BY-SA 3.0 FR") for entry in entries: self.logger.debug("current entry : %s" % entry) self.make_entry(root, entry) return root
def feed_info(self, data_tracks): """ Get Information from data to populate feed :param data_tracks: data about configured tracks to follow """ self.logger.debug(f"feed_info, {len(data_tracks)} data tracks") self.logger.debug(f"feed_info, {data_tracks} ") # # root XML element root = content.xmlelt(None, "div") # content.xmlelt(root, "h1", data_tracks["title"]) # Link to files content.xmlelt(root, "h3", "Fichiers à télécharger") ul = content.xmlelt(root, "ul") if data_tracks["diff_data_filename"]: self.make_link( content.xmlelt(ul, "li"), "fichier de données (delta)", data_tracks["diff_data_filename"], ) if data_tracks["diff_index_filename"]: self.make_link( content.xmlelt(ul, "li"), "fichier d'index des différences", data_tracks["diff_index_filename"], ) content.xmlelt(root, "h3", "Statistiques") for track in data_tracks["tracks"]: self.logger.debug("------ > ------- ") self.logger.debug(track) content.xmlelt(root, "h2", track["title"]) # data tracks ul = content.xmlelt(root, "ul") for data in track["values"]: content.xmlelt(ul, "li", f"{data['key']} : {data['val']}") self.logger.debug(f"Produced feed infos {content.xml2text(root)}") return root