import os import hashlib import gzip from xml.dom import * class DynastieGenerator: URI = "http://indefero.soutade.fr/p/dynastie" report = '' somethingWrote = False def __init__(self): self.report = '' self.somethingWrote = False def addReport(self, string, color=''): if color != '': self.report = self.report + '' self.report = self.report + '' + self.__class__.__name__ + ' : ' self.report = self.report + string if color != '': self.report = self.report + '' self.report = self.report + '
\n' def addWarning(self, string): self.addReport(string, 'yellow') def addError(self, string): self.addReport(string, 'red') def generate(self, blog, src, output): return def writeIfNotTheSame(self, filename, content): if os.path.exists(filename): src_md5 = hashlib.md5() f = open(filename,'rb') src_md5.update(f.read()) f.close() dst_md5 = hashlib.md5() dst_md5.update(content) if src_md5.digest() == dst_md5.digest(): filename = filename + '.gz' if not os.path.exists(filename): f = gzip.open(filename, 'wb') f.write(content) f.close() return os.unlink(filename) self.addReport('Write ' + filename) f = open(filename,'wb') f.write(content) f.close() filename = filename + '.gz' self.addReport('Compressing it ' + filename) f = gzip.open(filename, 'wb') f.write(content) f.close() self.somethingWrote = True def createElement(self, dom, name='', content=''): div = dom.createElement('div') if name != '': div.setAttribute('class', name) if content != '': div.appendChild(dom.createTextNode(content)) return div def simpleTransform(self, values, dom, elem, root): for node in root.childNodes: if node.prefix == 'dyn': if node.localName in values: new_elem = self.createElement(dom, node.localName, values[node.localName]) elem.appendChild(new_elem) else: new_elem = node.cloneNode(False) self.simpleTransform(values, dom, new_elem, node) elem.appendChild(new_elem) def replaceByText(self, dom, root, node, content): new_node = dom.createTextNode(content) root.replaceChild(new_node, node) def parse(self, hooks, articles, dom, root): for node in root.childNodes: if node.prefix == 'dyn': if node.localName in hooks: hooks[node.localName](articles, dom, root, node) if node.hasChildNodes(): self.parse(hooks, articles, dom, node) return