Skip to content

Commit

Permalink
Rework link(...) to use reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
arjun-menon committed Aug 6, 2024
1 parent cf08515 commit 984e47b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions alteza/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import time
import types
from typing import Optional, Generator, List, Dict, Set, Any
from typing import Optional, Generator, List, Union, Dict, Set, Any

import sh # type: ignore
from pypage import pypage # type: ignore
Expand Down Expand Up @@ -52,9 +52,7 @@ def __init__(self, args: Args, fs: FsCrawlResult) -> None:
self.seed: Dict[str, Any] = json.loads(args.seed)
self.fixSysPath()

def linkObj(
self, srcFile: FileNode, dstFile: FileNode, pathOnly: bool = False
) -> str:
def link(self, srcFile: FileNode, dstFile: FileNode, pathOnly: bool = False) -> str:
if not pathOnly:
srcFile.linksTo.append(dstFile) # This is used to determine reachability.
if dstFile not in self.seenTemplateLinks:
Expand Down Expand Up @@ -111,18 +109,20 @@ def invokePyPage(self, pyPageNode: PyPageNode, env: dict[str, Any]) -> None:
else:
raise AltezaException(f"{pyPageNode} Unsupported type of PyPageNode.")

def linkObj(dstFile: FileNode, pathOnly: bool = False) -> str:
return self.linkObj(pyPageNode, dstFile, pathOnly)

def link(name: str, pathOnly: bool = False) -> str:
dstFile: FileNode = self.nameRegistry.lookup(name)
return linkObj(dstFile, pathOnly)
def link(destination: Union[str, FileNode], pathOnly: bool = False) -> str:
if isinstance(destination, str):
dstFile: FileNode = self.nameRegistry.lookup(destination)
return self.link(pyPageNode, dstFile, pathOnly)
if isinstance(destination, FileNode):
return self.link(pyPageNode, destination, pathOnly)
raise AltezaException(
f"Unknown link destination type: `{type(destination)}`."
)

def path(name: str) -> str:
return link(name, True)

env |= {"link": link}
env |= {"linkObj": linkObj}
env |= {"path": path}

env |= {"getLastModifiedObj": lambda: pyPageNode.lastModifiedObj}
Expand Down Expand Up @@ -165,7 +165,7 @@ def runConfigIfAny(self, dirNode: DirNode, env: dict[str, Any]) -> Dict[str, Any
configFile: FileNode = configFileL[0]

def path(name: str) -> str:
return self.linkObj(configFile, self.nameRegistry.lookup(name), True)
return self.link(configFile, self.nameRegistry.lookup(name), True)

configEnv |= {"path": path}

Expand Down
2 changes: 1 addition & 1 deletion test_content/sectionK/sectionL/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Welcome to Section L
{{
for file in dir.files:
write('<li><a href="%s">%s</a> <code>%s</code> </li>' %
(linkObj(file), file.title, ' -> ' + file.fullPath))
(link(file), file.title, ' -> ' + file.fullPath))
}}
</ol>

Expand Down
2 changes: 1 addition & 1 deletion test_content/sectionK/sectionM/index.py.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h4>
<ul>
{% for file in dir.pages %}
<li>
<a href="{{ linkObj(file) }}">
<a href="{{ link(file) }}">
{{file.title}}
</a>
</li>
Expand Down

0 comments on commit 984e47b

Please sign in to comment.