Completely migrate to pathlib

This commit is contained in:
Govindas 2022-01-27 12:29:24 +02:00
parent 78817f4467
commit 5d27050293
1 changed files with 16 additions and 22 deletions

View File

@ -1,4 +1,3 @@
import os
from html5tagger import Document, E, HTML from html5tagger import Document, E, HTML
from shutil import rmtree from shutil import rmtree
import markdown import markdown
@ -84,11 +83,6 @@ def generatePage(title, doc):
def writePages(homepage): def writePages(homepage):
global pages global pages
global titles global titles
try:
os.mkdir("./website-output")
except FileExistsError:
pass
#TODO only delete files that aren't present in newest site generation #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 #deleting contents of folder without deleting the folder, to increase compatibility with various systems
#for root, dirs, files in os.walk('./website-output'): #for root, dirs, files in os.walk('./website-output'):
@ -104,21 +98,17 @@ def writePages(homepage):
if titles[id] != homepage: if titles[id] != homepage:
foldername = "/" + titles[id].replace(" ", "-").lower() foldername = "/" + titles[id].replace(" ", "-").lower()
#doing this here, because homepage directory is already created above Path("./website-output" + foldername).mkdir(parents=True, exist_ok=True)
try:
os.mkdir("./website-output" + foldername)
except FileExistsError:
pass
filepath = "./website-output" + foldername + "/index.html" filepath = Path("./website-output" + foldername + "/index.html")
if Path(filepath).exists(): if filepath.exists():
with open(filepath, 'r') as newpage: with filepath.open('r') as newpage:
if newpage.read() == page: if newpage.read() == page:
print("Page not changed: " + titles[id]) print("Page not changed: " + titles[id])
continue continue
with open(filepath, 'w') as newpage: with filepath.open('w') as newpage:
newpage.write(page) newpage.write(page)
print("Written changed page: " + titles[id]) print("Written changed page: " + titles[id])
@ -127,18 +117,22 @@ def main():
#if homepage is at Home.page, set homepage to "Home" #if homepage is at Home.page, set homepage to "Home"
homepage = "Home" homepage = "Home"
if Path("./navbar").exists(): navbarfile = Path("./navbar")
with open("./navbar", 'r') as navbarfile:
if navbarfile.exists():
with navbarfile.open('r') as navbarfile:
generateNavigationBar(navbarfile.readlines()) generateNavigationBar(navbarfile.readlines())
else: else:
print("No 'navbar' file found, there will be no navigation bar.") print("No 'navbar' file found, there will be no navigation bar.")
pagescount = 0 pagescount = 0
for file in os.listdir("./"):
if file.endswith(".page"): for file in Path(__file__).parent.iterdir():
pagescount += 1 if file.is_file():
with open(file, 'r') as page: if str(file).endswith(".page"):
generateLines(os.path.basename(file), page.readlines()) pagescount += 1
with file.open('r') as page:
generateLines(str(file), page.readlines())
print("Found " + str(pagescount) + " pages") print("Found " + str(pagescount) + " pages")
#write all pages to files #write all pages to files