|
|
@@ -0,0 +1,147 @@
|
|
|
+#!/usr/bin/python3
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import time
|
|
|
+import adi
|
|
|
+
|
|
|
+baseDir = os.path.expanduser("~/qslKarten")
|
|
|
+inDir = f"{baseDir}/Eingang"
|
|
|
+outDir = f"{baseDir}/Karten"
|
|
|
+templateDir = f"{baseDir}/Vorlage"
|
|
|
+template = None
|
|
|
+dpi = 300
|
|
|
+
|
|
|
+verbose = False
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def exec(cmd, logFile=None):
|
|
|
+ if verbose:
|
|
|
+ print(f"# {cmd}")
|
|
|
+
|
|
|
+ if logFile is not None:
|
|
|
+ appendFile(logFile, f"# {cmd}")
|
|
|
+ return os.system(f"{cmd} >> {logFile} 2>&1")
|
|
|
+ return os.system(cmd)
|
|
|
+
|
|
|
+
|
|
|
+def progressBar(nMax, pos, name):
|
|
|
+ p = int(20 * (pos / nMax))
|
|
|
+ msg = "[" + ("=" * p) + (" " * (20 - p)) + "]"
|
|
|
+ msg += f" {pos}/{nMax}: {name} "
|
|
|
+ print(msg, end="\r")
|
|
|
+
|
|
|
+
|
|
|
+def appendFile(name, message):
|
|
|
+ with open(name, "at") as f:
|
|
|
+ f.write(f"{message}\n")
|
|
|
+
|
|
|
+
|
|
|
+def createQslCards(fileName, name):
|
|
|
+ global outDir
|
|
|
+ global dpi
|
|
|
+
|
|
|
+ nCards = 0
|
|
|
+
|
|
|
+ logFile = f"{outDir}/logs/{name}.log"
|
|
|
+
|
|
|
+ exec(f"mkdir -p {outDir}/logs")
|
|
|
+ appendFile(logFile, "--------------------")
|
|
|
+ exec(f"date", logFile)
|
|
|
+
|
|
|
+ data = adi.loadAdi(fileName)
|
|
|
+ numOfRows = len(data)
|
|
|
+ for pos, row in enumerate(data):
|
|
|
+ tCall = row.fileName()
|
|
|
+ if not verbose:
|
|
|
+ progressBar(numOfRows, pos + 1, tCall)
|
|
|
+
|
|
|
+ svg = row.fillTemplate(f"{template}")
|
|
|
+
|
|
|
+ d = f"{outDir}/{row.subPath()}"
|
|
|
+ name = f"{d}/{tCall}"
|
|
|
+
|
|
|
+ os.makedirs(d, exist_ok=True)
|
|
|
+
|
|
|
+ n = 0
|
|
|
+ fOut = f'{name}'
|
|
|
+ while os.path.exists(fOut + ".jpg"):
|
|
|
+ n += 1
|
|
|
+ fOut = f'{name}-{n}'
|
|
|
+
|
|
|
+ fTmp = f"{fOut}.svg"
|
|
|
+ with open(fTmp, "wt") as f:
|
|
|
+ f.write(svg)
|
|
|
+
|
|
|
+ if 0 != exec(f"inkscape -C -d {dpi} --export-type=png -o '{fOut}.png' '{fTmp}'", logFile):
|
|
|
+ raise "Problem with inkscape!"
|
|
|
+
|
|
|
+ if 0 != exec(f"convert '{fOut}.png' '{fOut}.jpg'", logFile):
|
|
|
+ raise "Problem with Imagemagick"
|
|
|
+
|
|
|
+ os.remove(f"{fOut}.png")
|
|
|
+ os.remove(fTmp)
|
|
|
+
|
|
|
+ nCards += 1
|
|
|
+
|
|
|
+ print("")
|
|
|
+
|
|
|
+ return nCards
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def selectTemplate():
|
|
|
+ templates = [None]
|
|
|
+ for name in os.listdir(templateDir):
|
|
|
+ if name.endswith(".svg"):
|
|
|
+ templates.append(name)
|
|
|
+
|
|
|
+ while True:
|
|
|
+ for k, v in enumerate(templates):
|
|
|
+ if k > 0:
|
|
|
+ print(f"[{k}] '{v}'")
|
|
|
+
|
|
|
+ print()
|
|
|
+ print("[0] Abbrechen")
|
|
|
+ print()
|
|
|
+ x = input("Bitte Nummer waehlen: ")
|
|
|
+ try:
|
|
|
+ x = int(x)
|
|
|
+ except:
|
|
|
+ continue
|
|
|
+
|
|
|
+ if x < 0 or x >= len(templates):
|
|
|
+ continue
|
|
|
+
|
|
|
+ if x == 0 or templates[x] is None:
|
|
|
+ print("* Verarbeitung abgebrochen.")
|
|
|
+ exit(0)
|
|
|
+
|
|
|
+ return templates[x]
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ if "-v" in sys.argv or "--verbose" in sys.argv:
|
|
|
+ verbose = True
|
|
|
+
|
|
|
+ print("|------------------------------------------------------------")
|
|
|
+ print("| qslGen.py V1.0.1")
|
|
|
+ print("| Programm zum erstellen von QSL-Karten aus ADI Dateien.")
|
|
|
+ print("|------------------------------------------------------------")
|
|
|
+ print("")
|
|
|
+ print("====[ Auswahl der Vorlage ]==================================")
|
|
|
+
|
|
|
+ templateName = selectTemplate()
|
|
|
+ print(f"* Lade Vorlage '{templateName}'")
|
|
|
+ with open(f"{templateDir}/{templateName}", "rt") as f:
|
|
|
+ template = f.read()
|
|
|
+
|
|
|
+ print("====[ Verarbeite ADI Dateien ]===============================")
|
|
|
+ for name in os.listdir(inDir):
|
|
|
+ if name.endswith(".adi"):
|
|
|
+ print(f"* Verarbeite '{name}'")
|
|
|
+ fname = f"{inDir}/{name}"
|
|
|
+ createQslCards(fname, name)
|
|
|
+ os.remove(fname)
|