From 7a15ea0964fa7ab9b09844949bb0858f3d46e771 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> Date: Wed, 22 Sep 2021 22:33:09 +0200 Subject: [PATCH] Nuke tabs in Python MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit TAB → 4 × SP Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> --- www/html.py | 326 ++++++++++++++++++++++++++-------------------------- 1 file changed, 163 insertions(+), 163 deletions(-) diff --git a/www/html.py b/www/html.py index fe341d10..eb19271c 100755 --- a/www/html.py +++ b/www/html.py @@ -15,31 +15,31 @@ import xml.sax import codecs if sys.version_info >= (3, 0): - sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) + sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) else: - reload(sys) - sys.setdefaultencoding('utf-8') - sys.stdout = codecs.getwriter("utf-8")(sys.stdout) + reload(sys) + sys.setdefaultencoding('utf-8') + sys.stdout = codecs.getwriter("utf-8")(sys.stdout) lookupdir = '' # Print the usage information def usage(): - print("USAGE:") - print("html.py <-f -h file.xml>") - print(" -d DIR use DIR as base directory for opening files") - print(" -f write output to file.html (default is stdout)") - print(" -h help") - return + print("USAGE:") + print("html.py <-f -h file.xml>") + print(" -d DIR use DIR as base directory for opening files") + print(" -f write output to file.html (default is stdout)") + print(" -h help") + return # Headerfields header = [ - "Mime-Version: 1.0\r\n", - "Content-Type: text/plain; charset=utf-8\r\n", - "Content-Transfer-Encoding: 8bit\r\n", - "Content-Disposition: inline\r\n", + "Mime-Version: 1.0\r\n", + "Content-Type: text/plain; charset=utf-8\r\n", + "Content-Transfer-Encoding: 8bit\r\n", + "Content-Disposition: inline\r\n", ] html = [] @@ -48,166 +48,166 @@ fdout = sys.stdout def replaceVars(line): - cnt = 0 - while cnt < len(replace): - if line.find(replace[cnt]) >= 0: - line = line.replace(replace[cnt], replace[cnt+1]) - cnt = cnt + 2 - return line + cnt = 0 + while cnt < len(replace): + if line.find(replace[cnt]) >= 0: + line = line.replace(replace[cnt], replace[cnt+1]) + cnt = cnt + 2 + return line def writeHtml(line): - fdout.write(replaceVars(line)) + fdout.write(replaceVars(line)) def startMenu(level): - writeHtml("
\n" % (level)) + writeHtml("
\n" % (level)) def placeMenu(topic, link, mode): - topic = replaceVars(topic) - mode = replaceVars(mode) + topic = replaceVars(topic) + mode = replaceVars(mode) - if mode == 'text': - writeHtml("

%s

\n" % (topic)) - return - if mode == 'selected': - writeHtml("\n") - else: - writeHtml("\n") + if mode == 'text': + writeHtml("

%s

\n" % (topic)) + return + if mode == 'selected': + writeHtml("\n") + else: + writeHtml("\n") - writeHtml("%s\n" % (link, topic)) - writeHtml("\n") + writeHtml("%s\n" % (link, topic)) + writeHtml("\n") # configuration parser class docHandler(xml.sax.ContentHandler): - def __init__(self): - super().__init__() - self.content = "" - return - - def startElement(self, name, attrs): - self.element = name - - if len(self.content) > 0: - writeHtml(self.content) - self.content = "" - - if name == "PAGE": - return - elif name == "INCLUDE": - try: - fd = codecs.open(attrs.get('file'), 'r', 'utf-8') - except OSError: - fd = codecs.open(lookupdir + attrs.get('file'), 'r', 'utf-8') - lines = fd.readlines() - fd.close() - for line in lines: - writeHtml(line) - elif name == "PARSE": - parseConfig(attrs.get('file')) - - elif name == 'STARTMENU': - startMenu(attrs.get('level')) - - elif name == 'MENU': - placeMenu(attrs.get('topic'), attrs.get('link'), attrs.get('mode')) - - elif name == 'ENDMENU': - writeHtml("
\n") - - elif name == 'VAR': - match = attrs.get('match') - repl = attrs.get('replace') - idx = len(replace) - replace[idx:] = [match] - idx = len(replace) - replace[idx:] = [repl] - - elif name == "br": - writeHtml(" 0: - names = attrs.getNames() - for name in names: - writeHtml(" " + name + "=\"" + attrs.get(name) + "\"") - writeHtml(" />") - - else: - writeHtml("<" + name) - if attrs.getLength() > 0: - names = attrs.getNames() - for name in names: - writeHtml(" " + name + "=\"" + attrs.get(name) + "\"") - writeHtml(">") - - def characters(self, ch): - self.content = self.content + ch - - def endElement(self, name): - - if name == "PAGE": - return - elif name == 'INCLUDE': - return - elif name == 'PARSE': - return - elif name == 'PAGE': - return - elif name == 'STARTMENU': - return - elif name == 'ENDMENU': - return - elif name == 'MENU': - return - elif name == 'VAR': - return - elif name == 'br': - return - - if len(self.content) > 0: - writeHtml(self.content) - self.content = "" - writeHtml("") + def __init__(self): + super().__init__() + self.content = "" + return + + def startElement(self, name, attrs): + self.element = name + + if len(self.content) > 0: + writeHtml(self.content) + self.content = "" + + if name == "PAGE": + return + elif name == "INCLUDE": + try: + fd = codecs.open(attrs.get('file'), 'r', 'utf-8') + except OSError: + fd = codecs.open(lookupdir + attrs.get('file'), 'r', 'utf-8') + lines = fd.readlines() + fd.close() + for line in lines: + writeHtml(line) + elif name == "PARSE": + parseConfig(attrs.get('file')) + + elif name == 'STARTMENU': + startMenu(attrs.get('level')) + + elif name == 'MENU': + placeMenu(attrs.get('topic'), attrs.get('link'), attrs.get('mode')) + + elif name == 'ENDMENU': + writeHtml("
\n") + + elif name == 'VAR': + match = attrs.get('match') + repl = attrs.get('replace') + idx = len(replace) + replace[idx:] = [match] + idx = len(replace) + replace[idx:] = [repl] + + elif name == "br": + writeHtml(" 0: + names = attrs.getNames() + for name in names: + writeHtml(" " + name + "=\"" + attrs.get(name) + "\"") + writeHtml(" />") + + else: + writeHtml("<" + name) + if attrs.getLength() > 0: + names = attrs.getNames() + for name in names: + writeHtml(" " + name + "=\"" + attrs.get(name) + "\"") + writeHtml(">") + + def characters(self, ch): + self.content = self.content + ch + + def endElement(self, name): + + if name == "PAGE": + return + elif name == 'INCLUDE': + return + elif name == 'PARSE': + return + elif name == 'PAGE': + return + elif name == 'STARTMENU': + return + elif name == 'ENDMENU': + return + elif name == 'MENU': + return + elif name == 'VAR': + return + elif name == 'br': + return + + if len(self.content) > 0: + writeHtml(self.content) + self.content = "" + writeHtml("") # error handler class errHandler(xml.sax.ErrorHandler): - def __init__(self): - return + def __init__(self): + return - def error(self, exception): - sys.stderr.write("%s\n" % exception) + def error(self, exception): + sys.stderr.write("%s\n" % exception) - def fatalError(self, exception): - sys.stderr.write("Fatal error while parsing configuration\n") - sys.stderr.write("%s\n" % exception) - sys.exit(1) + def fatalError(self, exception): + sys.stderr.write("Fatal error while parsing configuration\n") + sys.stderr.write("%s\n" % exception) + sys.exit(1) # parse the configuration file def parseConfig(file): - # handlers - dh = docHandler() - eh = errHandler() + # handlers + dh = docHandler() + eh = errHandler() - # Create an XML parser - parser = xml.sax.make_parser() + # Create an XML parser + parser = xml.sax.make_parser() - # Set the handlers - parser.setContentHandler(dh) - parser.setErrorHandler(eh) + # Set the handlers + parser.setContentHandler(dh) + parser.setErrorHandler(eh) - try: - fd = codecs.open(file, 'r', 'utf-8') - except OSError: - fd = codecs.open(lookupdir + file, 'r', 'utf-8') + try: + fd = codecs.open(file, 'r', 'utf-8') + except OSError: + fd = codecs.open(lookupdir + file, 'r', 'utf-8') - # Parse the file - parser.parse(fd) - fd.close() + # Parse the file + parser.parse(fd) + fd.close() # Here we go @@ -216,22 +216,22 @@ def parseConfig(file): writefile = 0 try: - (options, arguments) = getopt.getopt(sys.argv[1:], 'fhd:') + (options, arguments) = getopt.getopt(sys.argv[1:], 'fhd:') except getopt.GetoptError as ex: - print - print("ERROR:") - print(ex.msg) - usage() - sys.exit(1) + print + print("ERROR:") + print(ex.msg) + usage() + sys.exit(1) for option, value in options: - if option == '-d': - lookupdir = value + '/' - if option == '-f': - writefile = 1 - elif option == '-h': - usage() - sys.exit(0) + if option == '-d': + lookupdir = value + '/' + if option == '-f': + writefile = 1 + elif option == '-h': + usage() + sys.exit(0) # Handle special case VAR_ORIGIN idx = len(replace) @@ -240,16 +240,16 @@ idx = len(replace) replace[idx:] = [lookupdir] if not arguments: - print("No source file specified") - usage() - sys.exit(1) + print("No source file specified") + usage() + sys.exit(1) if writefile > 0: - fname = arguments[0].split('.')[0] - fname = fname + ".html" - fdout = codecs.open(fname, 'w', 'utf-8') + fname = arguments[0].split('.')[0] + fname = fname + ".html" + fdout = codecs.open(fname, 'w', 'utf-8') parseConfig(arguments[0]) if writefile > 0: - fdout.close() + fdout.close() -- 2.50.1