Skip to content

Commit b6c538c

Browse files
committed
Slight re-organization in engine.py
1 parent acf1e36 commit b6c538c

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

alteza/__main__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#!/usr/bin/env python3
2-
from .engine import run, Args
3-
4-
5-
def main() -> None:
6-
run(Args().parse_args())
7-
8-
9-
# See: https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
10-
11-
if __name__ == "__main__":
12-
main()
1+
#!/usr/bin/env python3
2+
from .engine import Engine, Args
3+
4+
5+
def main() -> None:
6+
Engine.run(Args().parse_args())
7+
8+
9+
# See: https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
10+
11+
if __name__ == "__main__":
12+
main()

alteza/engine.py

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -363,26 +363,6 @@ def linkStaticAsset(fileNode: FileNode, shouldCopy: bool) -> None:
363363
else:
364364
os.symlink(fileNode.absoluteFilePath, fileNode.fileName)
365365

366-
@staticmethod
367-
def resetOutputDir(outputDir: str, shouldDelete: bool) -> None:
368-
if os.path.isfile(outputDir):
369-
raise AltezaException(
370-
f"A file named {outputDir} already exists. Please move it or delete it. "
371-
"Note that if this had been a directory, we would have erased it."
372-
)
373-
if os.path.isdir(outputDir):
374-
if not shouldDelete:
375-
raise AltezaException(
376-
f"Specified output directory {outputDir} already exists.\n"
377-
"Please use --clear_output_dir to delete it prior to site generation."
378-
)
379-
print(
380-
f"Deleting directory {Fore.dark_red_2}%s{Style.reset} and all of its content...\n"
381-
% outputDir
382-
)
383-
shutil.rmtree(outputDir)
384-
os.mkdir(outputDir)
385-
386366
@staticmethod
387367
def generate(args: Args, content: Content) -> None:
388368
def walk(curDir: DirNode) -> None:
@@ -412,30 +392,61 @@ def enterDir(newDir: str) -> Generator[None, None, None]:
412392
os.chdir(oldDir)
413393

414394

415-
def run(args: Args) -> None:
416-
startTimeNs = time.time_ns()
417-
contentDir = args.content
418-
if not os.path.isdir(contentDir):
419-
raise AltezaException(
420-
f"The provided path '{contentDir}' does not exist or is not a directory."
421-
)
395+
class Engine:
396+
@staticmethod
397+
def checkContentDir(contentDir: str) -> None:
398+
if not os.path.isdir(contentDir):
399+
raise AltezaException(
400+
f"The provided path '{contentDir}' does not exist or is not a directory."
401+
)
422402

423-
Generate.resetOutputDir(args.output, args.clear_output_dir)
403+
@staticmethod
404+
def resetOutputDir(outputDir: str, shouldDelete: bool) -> None:
405+
if os.path.isfile(outputDir):
406+
raise AltezaException(
407+
f"A file named {outputDir} already exists. Please move it or delete it. "
408+
"Note that if this had been a directory, we would have erased it."
409+
)
410+
if os.path.isdir(outputDir):
411+
if not shouldDelete:
412+
raise AltezaException(
413+
f"Specified output directory {outputDir} already exists.\n"
414+
"Please use --clear_output_dir to delete it prior to site generation."
415+
)
416+
print(
417+
f"Deleting directory {Fore.dark_red_2}%s{Style.reset} and all of its content...\n"
418+
% outputDir
419+
)
420+
shutil.rmtree(outputDir)
421+
os.mkdir(outputDir)
422+
423+
@staticmethod
424+
def process(args: Args) -> Content:
425+
with enterDir(args.content):
426+
print("Analyzing content directory...")
427+
fs = Fs()
428+
print(fs.nameRegistry)
429+
content = Content(args, fs)
430+
print("Processing...\n")
431+
content.process()
432+
print("\nSuccessfully completed processing.\n")
424433

425-
with enterDir(args.content):
426-
fs = Fs()
427-
print(fs.nameRegistry)
428-
content = Content(args, fs)
429-
print("Processing...\n")
430-
content.process()
431-
print("\nSuccessfully completed processing.\n")
434+
print("File Tree:")
435+
print(fs.rootDir.displayDir())
436+
437+
return content
438+
439+
@staticmethod
440+
def run(args: Args) -> None:
441+
startTimeNs = time.time_ns()
432442

433-
print("File Tree:")
434-
print(fs.rootDir.displayDir())
443+
Engine.checkContentDir(args.content)
444+
Engine.resetOutputDir(args.output, args.clear_output_dir)
435445

436-
print("Generating...")
437-
Generate.generate(args, content)
446+
content = Engine.process(args)
447+
print("Generating...")
448+
Generate.generate(args, content)
438449

439-
elapsedMilliseconds = (time.time_ns() - startTimeNs) / 10**6
440-
# pylint: disable=consider-using-f-string
441-
print("\nTime elapsed: %.2f ms" % elapsedMilliseconds)
450+
elapsedMilliseconds = (time.time_ns() - startTimeNs) / 10**6
451+
# pylint: disable=consider-using-f-string
452+
print("\nTime elapsed: %.2f ms" % elapsedMilliseconds)

0 commit comments

Comments
 (0)