From dc42bd58437323eb738803f55bfd7e3a079cfbf1 Mon Sep 17 00:00:00 2001 From: Wolfgang Nowak Date: Fri, 23 Sep 2022 18:43:55 +0200 Subject: [PATCH] =?UTF-8?q?modul=20mkindex=20f=C3=BCr=20webgen=20devel=20?= =?UTF-8?q?=20v0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mkindex.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 mkindex.py diff --git a/mkindex.py b/mkindex.py new file mode 100755 index 0000000..cca29ef --- /dev/null +++ b/mkindex.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import sys, os, re, datetime, locale + +webbasedir = os.path.expanduser("~/www/i21k.de/") + +### for module mkdirindex: +targetdir = webbasedir + "test/" +indexfilename = "index.html" +targetdirheadline = "Statements, rants and articles, mostly in german." +showsize = 0 +#### + + +def mkdirindex(targetdir, indexfilename, targetdirheadline, showsize): + """Dig a directory and generate an index.""" + + head1 = ( + '\n \n' + + targetdirheadline + + '\n \n \n ' + ) + + style_fn = '' + head2 = "" + body1 = 'Main

' + body1 += '
' + targetdirheadline + "
" + body1 += '' + foot = '

Some byebye

\n' + linecounter = 0 + + dirindexfile = open(targetdir + indexfilename, "w") + + dirindexfile.write(head1 + style_fn + head2 + body1) + sortedtargetdir = os.listdir(targetdir) + sortedtargetdir.sort() + sortedtargetdir.reverse() + for file in sortedtargetdir: + sizestr = "" + # If showsize is 1 we have some filesdir and want to see the size of a file + if not re.match("^\.", file) and not file == indexfilename: + if showsize == 1: + size = os.lstat(targetdir + file).st_size + if size > 100000000000: + sizestr = str(int(size / 1073741824)) + " GiB" + elif size > 100000000: + sizestr = str(int(size / 1048576)) + " MiB" + elif size > 10000: + sizestr = str(int(size / 1024)) + " KiB" + else: + sizestr = str(size) + " B" + else: + entryname = re.sub(".html$", "", file) + # entryname = re.sub(".html$", "", re.sub("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] ", "", file)) + + line = str('' + entryname + " " + sizestr + "") + dirindexfile.write(line) + linecounter += 1 + + dirindexfile.write(foot) + return linecounter + + +# Dig the files-directory and generate an index. +linecounter = mkdirindex(targetdir, indexfilename, targetdirheadline, showsize) +print("Dirindex in ", targetdir, " has ", linecounter, "lines") + +# Have a nice time.