webgen/dialogtest2.py

67 lines
2.2 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2021-11-20 18:19:59 +01:00
import sys, re, os, pg, datetime, locale, glob
from dialog import Dialog
## Initialize a dialog.Dialog instance
d = Dialog(dialog="dialog")
sourcedir = "/home/wn/python/webgen/"
targetdir = "/home/wn/www/i21k.de/posts/"
2021-11-28 15:47:58 +01:00
locale.setlocale(locale.LC_ALL, "")
clear = "\x1b[2J\x1b[H"
xwidth = os.get_terminal_size()[0] - 3
ywidth = os.get_terminal_size()[1] - 3
os.chdir(sourcedir)
# Select a topic for the new post or reedit an existing one
def selectfile():
# Put files in sourcefolder into list
zahl = 0
tabelle = []
for zeile in glob.glob("*.md"):
tabelle.insert(zahl, (str(zahl), zeile))
zahl += 1
# Open list in menu and let user choose one
ausgewaehlt = d.menu(
"Such dir ne Datei:",
width=xwidth,
height=ywidth,
menu_height=ywidth,
title="Ein Thema wählen:",
choices=tabelle,
cancel="Neuen Namen eingeben",
)
if ausgewaehlt[0] == "cancel":
newtopic = d.inputbox("Ok, gib hier ein neues Thema an:", width=xwidth, height=ywidth, title="Ein Thema wählen:", cancel="Exit")
if newtopic[0] == "cancel" or newtopic[1] == "":
sys.exit("Na, dann eben nicht...")
headline = newtopic[1]
else:
headline = ""
# Clean filename from leading .md for later construction of a headline
if headline == "":
headline = re.sub("\.md$", "", tabelle[int(ausgewaehlt[1])][1])
# if the searched filename doesnt match an existing file we construct a new one
if len(glob.glob(sourcedir + headline + ".md")) == 0:
sourcefile = sourcedir + headline + ".md"
else:
sourcefile = str(glob.glob(sourcedir + headline + ".md")[0])
# if the targetfilename doesnt match an existing file we construct a new one
targetfile = glob.glob(targetdir + "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] " + headline + "*")
targetfilelength = len(targetfile)
if targetfilelength >= 1:
targetfile = targetfile[targetfilelength - 1]
else:
targetfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + headline
return sourcefile, targetfile, headline
antwort = selectfile()
print(antwort)