From 5d27050293d34324cd4ba573e7abe0e132924812 Mon Sep 17 00:00:00 2001 From: Govindas Date: Thu, 27 Jan 2022 12:29:24 +0200 Subject: [PATCH] Completely migrate to pathlib --- gsitegen/generate.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/gsitegen/generate.py b/gsitegen/generate.py index d46c64e..bd88115 100644 --- a/gsitegen/generate.py +++ b/gsitegen/generate.py @@ -1,4 +1,3 @@ -import os from html5tagger import Document, E, HTML from shutil import rmtree import markdown @@ -84,11 +83,6 @@ def generatePage(title, doc): def writePages(homepage): global pages global titles - - try: - os.mkdir("./website-output") - except FileExistsError: - pass #TODO only delete files that aren't present in newest site generation #deleting contents of folder without deleting the folder, to increase compatibility with various systems #for root, dirs, files in os.walk('./website-output'): @@ -104,21 +98,17 @@ def writePages(homepage): if titles[id] != homepage: foldername = "/" + titles[id].replace(" ", "-").lower() - #doing this here, because homepage directory is already created above - try: - os.mkdir("./website-output" + foldername) - except FileExistsError: - pass + Path("./website-output" + foldername).mkdir(parents=True, exist_ok=True) - filepath = "./website-output" + foldername + "/index.html" + filepath = Path("./website-output" + foldername + "/index.html") - if Path(filepath).exists(): - with open(filepath, 'r') as newpage: + if filepath.exists(): + with filepath.open('r') as newpage: if newpage.read() == page: print("Page not changed: " + titles[id]) continue - with open(filepath, 'w') as newpage: + with filepath.open('w') as newpage: newpage.write(page) print("Written changed page: " + titles[id]) @@ -127,18 +117,22 @@ def main(): #if homepage is at Home.page, set homepage to "Home" homepage = "Home" - if Path("./navbar").exists(): - with open("./navbar", 'r') as navbarfile: + navbarfile = Path("./navbar") + + if navbarfile.exists(): + with navbarfile.open('r') as navbarfile: generateNavigationBar(navbarfile.readlines()) else: print("No 'navbar' file found, there will be no navigation bar.") pagescount = 0 - for file in os.listdir("./"): - if file.endswith(".page"): - pagescount += 1 - with open(file, 'r') as page: - generateLines(os.path.basename(file), page.readlines()) + + for file in Path(__file__).parent.iterdir(): + if file.is_file(): + if str(file).endswith(".page"): + pagescount += 1 + with file.open('r') as page: + generateLines(str(file), page.readlines()) print("Found " + str(pagescount) + " pages") #write all pages to files