Skip to content

Commit

Permalink
Site: Make many functions accept PathLike args
Browse files Browse the repository at this point in the history
Many of the functions in Site would make sense to accept
PathLike objects as arguments. In particular, is_partial()
(and similar) are used for testing what to do with files, and
these functions are called from both get_dependencies()
(which is assuming file paths) and from template_names()
(which is calling the filter functions with jinja2 posix '/'
separators). Either way, the argument represents a path.

This does directly revert many of the changes in
#104, but I
am thinking now that that was going in the wrong
direction. This should free up client code to call these
filter and helpt functions with whatever they want
(and I don't think it's allowing them to use arguments
that don't make sense?)
  • Loading branch information
NickCrews committed Feb 15, 2021
1 parent 2aafa70 commit a662a37
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions staticjinja/staticjinja.py
Expand Up @@ -7,6 +7,7 @@
import inspect
import logging
import os
from pathlib import Path
import re
import shutil
import warnings
Expand All @@ -27,7 +28,7 @@ def _has_argument(func):

def _ensure_dir(path):
"""Ensure the directory for a file exists."""
os.makedirs(os.path.dirname(path), exist_ok=True)
os.makedirs(os.path.dirname(Path(path)), exist_ok=True)


class Site(object):
Expand Down Expand Up @@ -237,6 +238,7 @@ def get_template(self, template_name):
:param template_name: A string representing the name of the template.
"""
template_name = Path(template_name).as_posix()
try:
return self.env.get_template(template_name)
except UnicodeDecodeError as e:
Expand Down Expand Up @@ -283,49 +285,50 @@ def get_rule(self, template_name):
return render_func
raise ValueError("no matching rule")

def is_static(self, template_name):
"""Check if a template is a static template. Static template are copied,
rather than compiled using Jinja2.
def is_static(self, filename):
"""Check if a file is static. Static files are copied, rather than
compiled using Jinja2.
.. deprecated:: 0.3.4
A template is considered static if it lives in any of the directories
specified in ``staticpaths``.
:param template_name: the name of the template to check
:param filename: A PathLike name of the file to check.
"""
return any(template_name.startswith(path) for path in self.staticpaths)
path = Path(filename).as_posix()
return any(path.startswith(Path(sp).as_posix()) for sp in self.staticpaths)

def is_partial(self, template_name):
"""Check if a template is a partial template. Partial templates are not
def is_partial(self, filename):
"""Check if a file is partial. Partial files are not
rendered, but they are used in rendering templates.
A template is considered a partial if it or any of its parent
A file is considered a partial if it or any of its parent
directories are prefixed with an ``'_'``.
:param template_name: the name of the template to check
:param filename: A PathLike name of the file to check
"""
return any((x.startswith("_") for x in template_name.split("/")))
return any(part.startswith("_") for part in Path(filename).parts)

def is_ignored(self, template_name):
"""Check if a template is an ignored template. Ignored templates are
def is_ignored(self, filename):
"""Check if a file is an ignored. Ignored files are
neither rendered nor used in rendering templates.
A template is considered ignored if it or any of its parent directories
A file is considered ignored if it or any of its parent directories
are prefixed with an ``'.'``.
:param template_name: the name of the template to check
:param filename: A PathLike name of the file to check
"""
return any((x.startswith(".") for x in template_name.split("/")))
return any(part.startswith(".") for part in Path(filename).parts)

def is_template(self, filename):
"""Check if a file is a template.
A file is a considered a template if it is not partial, ignored, or
static.
:param filename: the name of the file to check
:param filename: A PathLike name of the file to check
"""
if self.is_partial(filename):
return False
Expand Down Expand Up @@ -381,9 +384,10 @@ def render_templates(self, templates):

def copy_static(self, files):
for f in files:
input_location = os.path.join(self.searchpath, f)
output_location = os.path.join(self.outpath, f)
self.logger.info("Copying %s to %s." % (f, output_location))
f = Path(f)
input_location = Path(self.searchpath) / f
output_location = Path(self.outpath) / f
self.logger.info("Copying %s to %s.", f, output_location)
_ensure_dir(output_location)
shutil.copy2(input_location, output_location)

Expand Down

0 comments on commit a662a37

Please sign in to comment.