Format, lint, type
This commit is contained in:
parent
a1ae717c8f
commit
c925079e8b
8 changed files with 159 additions and 91 deletions
|
|
@ -13,13 +13,16 @@
|
|||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from typing import Dict, Any, Tuple, Callable, Iterable, List, Union
|
||||
import os.path
|
||||
from typing import Any, Callable, Dict, List, Tuple, Union
|
||||
|
||||
from jinja2 import Environment as JinjaEnvironment, Template, BaseLoader, TemplateNotFound, FileSystemLoader
|
||||
from jinja2 import BaseLoader
|
||||
from jinja2 import Environment as JinjaEnvironment
|
||||
from jinja2 import Template, TemplateNotFound
|
||||
|
||||
from ops_bot.util import markdown
|
||||
|
||||
|
||||
def sync_read_file(path: str) -> str:
|
||||
with open(path) as file:
|
||||
return file.read()
|
||||
|
|
@ -28,6 +31,7 @@ def sync_read_file(path: str) -> str:
|
|||
def sync_list_files(directory: str) -> list[str]:
|
||||
return os.listdir(directory)
|
||||
|
||||
|
||||
class TemplateUtil:
|
||||
@staticmethod
|
||||
def bold_scope(label: str) -> str:
|
||||
|
|
@ -61,20 +65,29 @@ class TemplateUtil:
|
|||
if minutes > 0:
|
||||
parts.append(cls.pluralize(minutes, "minute"))
|
||||
if seconds > 0 or len(parts) == 0:
|
||||
parts.append(cls.pluralize(seconds + frac_seconds, "second"))
|
||||
parts.append(cls.pluralize(int(seconds + frac_seconds), "second"))
|
||||
|
||||
if len(parts) == 1:
|
||||
return parts[0]
|
||||
return ", ".join(parts[:-1]) + f" and {parts[-1]}"
|
||||
|
||||
@staticmethod
|
||||
def join_human_list(data: List[str], *, joiner: str = ", ", final_joiner: str = " and ",
|
||||
mutate: Callable[[str], str] = lambda val: val) -> str:
|
||||
def join_human_list(
|
||||
data: List[str],
|
||||
*,
|
||||
joiner: str = ", ",
|
||||
final_joiner: str = " and ",
|
||||
mutate: Callable[[str], str] = lambda val: val,
|
||||
) -> str:
|
||||
if not data:
|
||||
return ""
|
||||
elif len(data) == 1:
|
||||
return mutate(data[0])
|
||||
return joiner.join(mutate(val) for val in data[:-1]) + final_joiner + mutate(data[-1])
|
||||
return (
|
||||
joiner.join(mutate(val) for val in data[:-1])
|
||||
+ final_joiner
|
||||
+ mutate(data[-1])
|
||||
)
|
||||
|
||||
|
||||
class TemplateProxy:
|
||||
|
|
@ -97,11 +110,13 @@ class PluginTemplateLoader(BaseLoader):
|
|||
directory: str
|
||||
macros: str
|
||||
|
||||
def __init__(self, base: str, directory: str) -> None:
|
||||
def __init__(self, base: str, directory: str) -> None:
|
||||
self.directory = os.path.join("templates", base, directory)
|
||||
self.macros = sync_read_file(os.path.join("templates", base, "macros.html"))
|
||||
|
||||
def get_source(self, environment: Any, name: str) -> Tuple[str, str, Callable[[], bool]]:
|
||||
def get_source(
|
||||
self, environment: Any, name: str
|
||||
) -> Tuple[str, str, Callable[[], bool]]:
|
||||
path = f"{os.path.join(self.directory, name)}.html"
|
||||
try:
|
||||
tpl = sync_read_file(path)
|
||||
|
|
@ -109,21 +124,31 @@ class PluginTemplateLoader(BaseLoader):
|
|||
raise TemplateNotFound(name)
|
||||
return self.macros + tpl, name, lambda: True
|
||||
|
||||
def list_templates(self) -> Iterable[str]:
|
||||
return [os.path.splitext(os.path.basename(path))[0]
|
||||
for path in sync_list_files(self.directory)
|
||||
if path.endswith(".html")]
|
||||
def list_templates(self) -> List[str]:
|
||||
return [
|
||||
os.path.splitext(os.path.basename(path))[0]
|
||||
for path in sync_list_files(self.directory)
|
||||
if path.endswith(".html")
|
||||
]
|
||||
|
||||
|
||||
class TemplateManager:
|
||||
_env: JinjaEnvironment
|
||||
_loader: PluginTemplateLoader
|
||||
|
||||
def __init__(self, base: str, directory: str) -> None:
|
||||
#self._loader = FileSystemLoader(os.path.join("templates/", base))
|
||||
# self._loader = FileSystemLoader(os.path.join("templates/", base))
|
||||
self._loader = PluginTemplateLoader(base, directory)
|
||||
self._env = JinjaEnvironment(loader=self._loader, lstrip_blocks=True, trim_blocks=True,
|
||||
extensions=["jinja2.ext.do"])
|
||||
self._env.filters["markdown"] = lambda message: markdown.render(message, allow_html=True)
|
||||
self._env = JinjaEnvironment( # nosec B701
|
||||
loader=self._loader,
|
||||
lstrip_blocks=True,
|
||||
trim_blocks=True,
|
||||
autoescape=False,
|
||||
extensions=["jinja2.ext.do"],
|
||||
)
|
||||
self._env.filters["markdown"] = lambda message: markdown.render(
|
||||
message, allow_html=True
|
||||
)
|
||||
|
||||
def __getitem__(self, item: str) -> Template:
|
||||
return self._env.get_template(item)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue