1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-09-11 23:23:27 +02:00

Make text formatting faster via StringIO

Avoid concatenating strings over and over. Also, the wiki formatting
wasn't necessary at all since it was just joining lines.
This commit is contained in:
Daniel Martí 2015-12-03 12:02:47 +01:00
parent 088929711c
commit 1b43d8e33c
2 changed files with 47 additions and 36 deletions

View File

@ -24,6 +24,11 @@ import glob
import cgi import cgi
import textwrap import textwrap
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
import yaml import yaml
# use libyaml if it is available # use libyaml if it is available
try: try:
@ -467,22 +472,24 @@ def check_metadata(app):
# Formatter for descriptions. Create an instance, and call parseline() with # Formatter for descriptions. Create an instance, and call parseline() with
# each line of the description source from the metadata. At the end, call # each line of the description source from the metadata. At the end, call
# end() and then text_wiki and text_html will contain the result. # end() and then text_txt and text_html will contain the result.
class DescriptionFormatter: class DescriptionFormatter:
stNONE = 0 stNONE = 0
stPARA = 1 stPARA = 1
stUL = 2 stUL = 2
stOL = 3 stOL = 3
bold = False
ital = False
state = stNONE
text_wiki = ''
text_html = ''
text_txt = ''
para_lines = []
linkResolver = None
def __init__(self, linkres): def __init__(self, linkres):
self.bold = False
self.ital = False
self.state = self.stNONE
self.text_html = ''
self.text_txt = ''
self.html = StringIO()
self.text = StringIO()
self.para_lines = []
self.linkResolver = None
self.linkResolver = linkres self.linkResolver = linkres
def endcur(self, notstates=None): def endcur(self, notstates=None):
@ -499,20 +506,21 @@ class DescriptionFormatter:
self.state = self.stNONE self.state = self.stNONE
whole_para = ' '.join(self.para_lines) whole_para = ' '.join(self.para_lines)
self.addtext(whole_para) self.addtext(whole_para)
self.text_txt += textwrap.fill(whole_para, 80, self.text.write(textwrap.fill(whole_para, 80,
break_long_words=False, break_long_words=False,
break_on_hyphens=False) + '\n\n' break_on_hyphens=False))
self.text_html += '</p>' self.text.write('\n\n')
self.html.write('</p>')
del self.para_lines[:] del self.para_lines[:]
def endul(self): def endul(self):
self.text_html += '</ul>' self.html.write('</ul>')
self.text_txt += '\n' self.text.write('\n')
self.state = self.stNONE self.state = self.stNONE
def endol(self): def endol(self):
self.text_html += '</ol>' self.html.write('</ol>')
self.text_txt += '\n' self.text.write('\n')
self.state = self.stNONE self.state = self.stNONE
def formatted(self, txt, html): def formatted(self, txt, html):
@ -585,40 +593,44 @@ class DescriptionFormatter:
def addtext(self, txt): def addtext(self, txt):
p, h = self.linkify(txt) p, h = self.linkify(txt)
self.text_html += h self.html.write(h)
def parseline(self, line): def parseline(self, line):
self.text_wiki += "%s\n" % line
if not line: if not line:
self.endcur() self.endcur()
elif line.startswith('* '): elif line.startswith('* '):
self.endcur([self.stUL]) self.endcur([self.stUL])
self.text_txt += "%s\n" % line self.text.write(line)
self.text.write('\n')
if self.state != self.stUL: if self.state != self.stUL:
self.text_html += '<ul>' self.html.write('<ul>')
self.state = self.stUL self.state = self.stUL
self.text_html += '<li>' self.html.write('<li>')
self.addtext(line[1:]) self.addtext(line[1:])
self.text_html += '</li>' self.html.write('</li>')
elif line.startswith('# '): elif line.startswith('# '):
self.endcur([self.stOL]) self.endcur([self.stOL])
self.text_txt += "%s\n" % line self.text.write(line)
self.text.write('\n')
if self.state != self.stOL: if self.state != self.stOL:
self.text_html += '<ol>' self.html.write('<ol>')
self.state = self.stOL self.state = self.stOL
self.text_html += '<li>' self.html.write('<li>')
self.addtext(line[1:]) self.addtext(line[1:])
self.text_html += '</li>' self.html.write('</li>')
else: else:
self.para_lines.append(line) self.para_lines.append(line)
self.endcur([self.stPARA]) self.endcur([self.stPARA])
if self.state == self.stNONE: if self.state == self.stNONE:
self.text_html += '<p>' self.html.write('<p>')
self.state = self.stPARA self.state = self.stPARA
def end(self): def end(self):
self.endcur() self.endcur()
self.text_txt = self.text_txt.strip() self.text_txt = self.text.getvalue().rstrip()
self.text_html = self.html.getvalue()
self.text.close()
self.html.close()
# Parse multiple lines of description as written in a metadata file, returning # Parse multiple lines of description as written in a metadata file, returning
@ -635,11 +647,7 @@ def description_txt(s):
# a single string in wiki format. Used for the Maintainer Notes field as well, # a single string in wiki format. Used for the Maintainer Notes field as well,
# because it's the same format. # because it's the same format.
def description_wiki(s): def description_wiki(s):
ps = DescriptionFormatter(None) return s
for line in s.splitlines():
ps.parseline(line)
ps.end()
return ps.text_wiki
# Parse multiple lines of description as written in a metadata file, returning # Parse multiple lines of description as written in a metadata file, returning

View File

@ -21,7 +21,10 @@
from argparse import ArgumentParser from argparse import ArgumentParser
import os import os
import logging import logging
import StringIO try:
from cStringIO import StringIO
except:
from StringIO import StringIO
import common import common
import metadata import metadata
@ -70,7 +73,7 @@ def main():
to_ext = options.to to_ext = options.to
if options.list: if options.list:
s = StringIO.StringIO() s = StringIO()
# TODO: currently reading entire file again, should reuse first # TODO: currently reading entire file again, should reuse first
# read in metadata.py # read in metadata.py
with open(metadatapath, 'r') as f: with open(metadatapath, 'r') as f: