Skip to content

Commit 867e4bb

Browse files
committed
In engine.py, move Generate inside Engine
1 parent 5965b9b commit 867e4bb

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

alteza/engine.py

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -312,75 +312,6 @@ def getTemplateHtml(self, env: dict[str, Any]) -> str:
312312
)
313313

314314

315-
class Generate:
316-
# pylint: disable=too-few-public-methods
317-
# This class is just here to organize a bunch of related functions together.
318-
# This class should never be instantiated. Generate.generate(...) should
319-
# be called to write the output of a processed Content object.
320-
@staticmethod
321-
def writeMdContents(md: Md) -> None:
322-
if os.path.exists("index.html"):
323-
raise AltezaException(
324-
f"An index.html already exists, and conflicts with {md}, at {os.getcwd()}."
325-
)
326-
with open("index.html", "w", encoding="utf-8") as pageHtml:
327-
pageHtml.write(md.getPyPageOutput())
328-
329-
@staticmethod
330-
def writeMd(md: Md) -> None:
331-
if not md.isIndex():
332-
os.mkdir(md.realName)
333-
with enterDir(md.realName):
334-
Generate.writeMdContents(md)
335-
else:
336-
Generate.writeMdContents(md)
337-
338-
@staticmethod
339-
def writeNonMd(nonMd: NonMd) -> None:
340-
fileName = nonMd.rectifiedFileName
341-
if os.path.exists(fileName):
342-
raise AltezaException(
343-
f"File {fileName} already exists, and conflicts with {nonMd}."
344-
)
345-
with open(fileName, "w", encoding="utf-8") as nonMdPageFile:
346-
nonMdPageFile.write(nonMd.getPyPageOutput())
347-
348-
@staticmethod
349-
def writePyPageNode(pyPageNode: PyPageNode) -> None:
350-
if isinstance(pyPageNode, Md):
351-
Generate.writeMd(pyPageNode)
352-
353-
elif isinstance(pyPageNode, NonMd):
354-
Generate.writeNonMd(pyPageNode)
355-
356-
else:
357-
raise AltezaException(f"{pyPageNode} pyPage attribute is invalid.")
358-
359-
@staticmethod
360-
def linkStaticAsset(fileNode: FileNode, shouldCopy: bool) -> None:
361-
if shouldCopy:
362-
shutil.copyfile(fileNode.absoluteFilePath, fileNode.fileName)
363-
else:
364-
os.symlink(fileNode.absoluteFilePath, fileNode.fileName)
365-
366-
@staticmethod
367-
def generate(args: Args, content: Content) -> None:
368-
def walk(curDir: DirNode) -> None:
369-
for subDir in filter(lambda node: node.shouldPublish, curDir.subDirs):
370-
os.mkdir(subDir.dirName)
371-
with enterDir(subDir.dirName):
372-
walk(subDir)
373-
374-
for fileNode in filter(lambda node: node.shouldPublish, curDir.files):
375-
if isinstance(fileNode, PyPageNode):
376-
Generate.writePyPageNode(fileNode)
377-
else:
378-
Generate.linkStaticAsset(fileNode, args.copy_assets)
379-
380-
with enterDir(args.output):
381-
walk(content.rootDir)
382-
383-
384315
@contextlib.contextmanager
385316
def enterDir(newDir: str) -> Generator[None, None, None]:
386317
# https://stackoverflow.com/a/13847807/908430
@@ -393,6 +324,74 @@ def enterDir(newDir: str) -> Generator[None, None, None]:
393324

394325

395326
class Engine:
327+
class Generate:
328+
# These classes are just here to organize a bunch of related functions together.
329+
# This class should never be instantiated. Generate.generate(...) is the entry
330+
# point to be called to write the output of a processed Content object.
331+
# Similarly, Engine.run(args) is used to invoke Alteza overall.
332+
@staticmethod
333+
def writeMdContents(md: Md) -> None:
334+
if os.path.exists("index.html"):
335+
raise AltezaException(
336+
f"An index.html already exists, and conflicts with {md}, at {os.getcwd()}."
337+
)
338+
with open("index.html", "w", encoding="utf-8") as pageHtml:
339+
pageHtml.write(md.getPyPageOutput())
340+
341+
@staticmethod
342+
def writeMd(md: Md) -> None:
343+
if not md.isIndex():
344+
os.mkdir(md.realName)
345+
with enterDir(md.realName):
346+
Engine.Generate.writeMdContents(md)
347+
else:
348+
Engine.Generate.writeMdContents(md)
349+
350+
@staticmethod
351+
def writeNonMd(nonMd: NonMd) -> None:
352+
fileName = nonMd.rectifiedFileName
353+
if os.path.exists(fileName):
354+
raise AltezaException(
355+
f"File {fileName} already exists, and conflicts with {nonMd}."
356+
)
357+
with open(fileName, "w", encoding="utf-8") as nonMdPageFile:
358+
nonMdPageFile.write(nonMd.getPyPageOutput())
359+
360+
@staticmethod
361+
def writePyPageNode(pyPageNode: PyPageNode) -> None:
362+
if isinstance(pyPageNode, Md):
363+
Engine.Generate.writeMd(pyPageNode)
364+
365+
elif isinstance(pyPageNode, NonMd):
366+
Engine.Generate.writeNonMd(pyPageNode)
367+
368+
else:
369+
raise AltezaException(f"{pyPageNode} pyPage attribute is invalid.")
370+
371+
@staticmethod
372+
def linkStaticAsset(fileNode: FileNode, shouldCopy: bool) -> None:
373+
if shouldCopy:
374+
shutil.copyfile(fileNode.absoluteFilePath, fileNode.fileName)
375+
else:
376+
os.symlink(fileNode.absoluteFilePath, fileNode.fileName)
377+
378+
@staticmethod
379+
def generate(args: Args, content: Content) -> None:
380+
def walk(curDir: DirNode) -> None:
381+
for subDir in filter(lambda node: node.shouldPublish, curDir.subDirs):
382+
os.mkdir(subDir.dirName)
383+
with enterDir(subDir.dirName):
384+
walk(subDir)
385+
386+
for fileNode in filter(lambda node: node.shouldPublish, curDir.files):
387+
if isinstance(fileNode, PyPageNode):
388+
Engine.Generate.writePyPageNode(fileNode)
389+
else:
390+
Engine.Generate.linkStaticAsset(fileNode, args.copy_assets)
391+
392+
with enterDir(args.output):
393+
walk(content.rootDir)
394+
396395
@staticmethod
397396
def checkContentDir(contentDir: str) -> None:
398397
if not os.path.isdir(contentDir):
@@ -445,7 +444,7 @@ def run(args: Args) -> None:
445444

446445
content = Engine.process(args)
447446
print("Generating...")
448-
Generate.generate(args, content)
447+
Engine.Generate.generate(args, content)
449448

450449
elapsedMilliseconds = (time.time_ns() - startTimeNs) / 10**6
451450
# pylint: disable=consider-using-f-string

0 commit comments

Comments
 (0)