def extractone(self, params): #self.em.rclog("extractone %s" % params["filename:"]) ok = False if "filename:" not in params: self.em.rclog("extractone: no file name") return (ok, docdata, "", rclexecm.RclExecM.eofnow) filename = params["filename:"] try: metadata = pyexiv2.ImageMetadata(filename) metadata.read() keys = metadata.exif_keys + metadata.iptc_keys + metadata.xmp_keys mdic = {} for k in keys: # we skip numeric keys and undecoded makernote data if k != 'Exif.Photo.MakerNote' and not khexre.match(k): mdic[k] = str(metadata[k].raw_value) except Exception as err: self.em.rclog("extractone: extract failed: [%s]" % err) return (ok, "", "", rclexecm.RclExecM.eofnow) docdata = b'<html><head>\n' ttdata = set() for k in pyexiv2_titles: if k in mdic: ttdata.add(self.em.htmlescape(mdic[k])) if ttdata: title = "" for v in ttdata: v = v.replace('[', '').replace(']', '').replace("'", "") title += v + " " docdata += rclexecm.makebytes("<title>" + title + "</title>\n") for k in exiv2_dates: if k in mdic: # Recoll wants: %Y-%m-%d %H:%M:%S. # We get 2014:06:27 14:58:47 dt = mdic[k].replace(":", "-", 2) docdata += b'<meta name="date" content="' + \ rclexecm.makebytes(dt) + b'">\n' break for k, v in mdic.items(): if k == 'Xmp.digiKam.TagsList': docdata += b'<meta name="keywords" content="' + \ rclexecm.makebytes(self.em.htmlescape(mdic[k])) + \ b'">\n' docdata += b'</head><body>\n' for k, v in mdic.items(): docdata += rclexecm.makebytes(k + " : " + \ self.em.htmlescape(mdic[k]) + "<br />\n") docdata += b'</body></html>' self.em.setmimetype("text/html") return (True, docdata, "", rclexecm.RclExecM.eofnext)
def extractone(self, params): #self.em.rclog("extractone %s" % params["filename:"]) ok = False if "filename:" not in params: self.em.rclog("extractone: no file name") return (ok, docdata, "", rclexecm.RclExecM.eofnow) filename = params["filename:"] try: metadata = pyexiv2.ImageMetadata(filename) metadata.read() keys = metadata.exif_keys + metadata.iptc_keys + metadata.xmp_keys mdic = {} for k in keys: # we skip numeric keys and undecoded makernote data if k != 'Exif.Photo.MakerNote' and not khexre.match(k): mdic[k] = str(metadata[k].raw_value) except Exception as err: self.em.rclog("extractone: extract failed: [%s]" % err) return (ok, "", "", rclexecm.RclExecM.eofnow) docdata = b'<html><head>\n' ttdata = set() for k in pyexiv2_titles: if k in mdic: ttdata.add(self.em.htmlescape(mdic[k])) if ttdata: title = "" for v in ttdata: v = v.replace('[', '').replace(']', '').replace("'", "") title += v + " " docdata += rclexecm.makebytes("<title>" + title + "</title>\n") for k in exiv2_dates: if k in mdic: # Recoll wants: %Y-%m-%d %H:%M:%S. # We get 2014:06:27 14:58:47 dt = mdic[k].replace(":", "-", 2) docdata += b'<meta name="date" content="' + \ rclexecm.makebytes(dt) + b'">\n' break for k,v in mdic.items(): if k == 'Xmp.digiKam.TagsList': docdata += b'<meta name="keywords" content="' + \ rclexecm.makebytes(self.em.htmlescape(mdic[k])) + \ b'">\n' docdata += b'</head><body>\n' for k,v in mdic.items(): docdata += rclexecm.makebytes(k + " : " + \ self.em.htmlescape(mdic[k]) + "<br />\n") docdata += b'</body></html>' self.em.setmimetype("text/html") return (True, docdata, "", rclexecm.RclExecM.eofnext)
def format(self): """ Parse and send the colored source. """ # store line offsets in self.lines self.lines = [0, 0] pos = 0 while 1: pos = self.raw.find(b'\n', pos) + 1 if not pos: break self.lines.append(pos) self.lines.append(len(self.raw)) # parse the source and write it self.pos = 0 text = io.BytesIO(self.raw) self.out.write(rclexecm.makebytes(self.stylesheet)) self.out.write(b'<pre class="code">\n') try: for a,b,c,d,e in tokenize.tokenize(text.readline): self(a,b,c,d,e) except Exception as ex: # There are other possible exceptions, for example for a # bad encoding line (->SyntaxError). e.g. # -*- coding: lala -*- self.out.write(("<h3>ERROR: %s</h3>\n" % ex).encode('utf-8')) self.out.write(b'\n</pre>')
def html_text(self, filename): ok = False metadata = pyexiv2.ImageMetadata(filename) metadata.read() keys = metadata.exif_keys + metadata.iptc_keys + metadata.xmp_keys mdic = {} for k in keys: # we skip numeric keys and undecoded makernote data if k != 'Exif.Photo.MakerNote' and not khexre.match(k): mdic[k] = str(metadata[k].raw_value) docdata = b'<html><head>\n' ttdata = set() for k in pyexiv2_titles: if k in mdic: ttdata.add(rclexecm.htmlescape(mdic[k])) if ttdata: title = "" for v in ttdata: v = v.replace('[', '').replace(']', '').replace("'", "") title += v + " " docdata += rclexecm.makebytes("<title>" + title + "</title>\n") for k in exiv2_dates: if k in mdic: # Recoll wants: %Y-%m-%d %H:%M:%S. # We get 2014:06:27 14:58:47 dt = mdic[k].replace(":", "-", 2) docdata += b'<meta name="date" content="' + \ rclexecm.makebytes(dt) + b'">\n' break for k,v in mdic.items(): if k == 'Xmp.digiKam.TagsList': docdata += b'<meta name="keywords" content="' + \ rclexecm.makebytes(rclexecm.htmlescape(mdic[k])) + \ b'">\n' docdata += b'</head><body>\n' for k,v in mdic.items(): docdata += rclexecm.makebytes(k + " : " + \ rclexecm.htmlescape(mdic[k]) + "<br />\n") docdata += b'</body></html>' return docdata
def __call__(self, toktype, toktext, startpos, endpos, line): """ Token handler. """ if 0: print("type %s %s text %s start %s %s end %s %s<br>\n" % \ (toktype, token.tok_name[toktype], toktext, \ srow, scol,erow,ecol)) srow, scol = startpos erow, ecol = endpos # calculate new positions oldpos = self.pos newpos = self.lines[srow] + scol self.pos = newpos + len(toktext) # handle newlines if toktype in [token.NEWLINE, tokenize.NL]: self.out.write(b'\n') return # send the original whitespace, if needed if newpos > oldpos: self.out.write(self.raw[oldpos:newpos]) # skip indenting tokens if toktype in [token.INDENT, token.DEDENT]: self.pos = newpos return # map token type to a color group if token.LPAR <= toktype and toktype <= token.OP: toktype = token.OP elif toktype == token.NAME and keyword.iskeyword(toktext): toktype = _KEYWORD css_class = _css_classes.get(toktype, 'text') # send text self.out.write(rclexecm.makebytes('<span class="%s">' % (css_class,))) self.out.write(rclexecm.makebytes(html.escape(toktext))) self.out.write(b'</span>')
def colorize_file(file=None, outstream=sys.stdout, standalone=True): """Convert a python source file into colorized HTML. Reads file and writes to outstream (default sys.stdout). file can be a filename or a file-like object (only the read method is used). If file is None, act as a filter and read from sys.stdin. If standalone is True (default), send a complete HTML document with header and footer. Otherwise only a stylesheet and a <pre> section are written. """ from os.path import basename if hasattr(file, 'read'): sourcefile = file file = None try: filename = basename(file.name) except: filename = 'STREAM' elif file is not None: try: sourcefile = open(file, 'rb') filename = basename(file) except IOError: raise SystemExit("File %s unknown." % file) else: sourcefile = sys.stdin filename = 'STDIN' source = sourcefile.read() if standalone: outstream.write(rclexecm.makebytes(_HTML_HEADER % {'title': filename})) Parser(source, out=outstream).format() if standalone: outstream.write(rclexecm.makebytes(_HTML_FOOTER)) if file: sourcefile.close()
def _metatag(self, nm, val): return b"<meta name=\"" + rclexecm.makebytes(nm) + b"\" content=\"" + \ rclexecm.htmlescape(rclexecm.makebytes(val)) + b"\">"