Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

The loader now has an interface for retrieving both molds and mold in…

…stances
  • Loading branch information...
commit b0a07669833633a4aa7e794e748f57a84cb75582 1 parent ee51bbb
@johnsoft authored
Showing with 23 additions and 9 deletions.
  1. +23 −9 loader.py
View
32 loader.py
@@ -22,16 +22,20 @@ def __init__(self, root_path, *, mold_class=MoldCompiler):
self.escaper = None
def from_file(self, filename):
- """Loads a mold from a file."""
+ """Creates a mold instance from a file."""
+ ret = self.mold_from_file(filename).instance()
+ ret.update(self.args)
+ return ret
+
+ def mold_from_file(self, filename):
+ """Creates a mold from a file."""
if not filename.endswith('.mold'):
filename += '.mold'
filename = path(filename).realpath()
- ret = self.from_file_template(filename).instance()
- ret.update(self.args)
- return ret
+ return self._from_file(filename)
@functools.lru_cache()
- def from_file_template(self, filename):
+ def _from_file(self, filename):
try:
with open(filename) as file:
data = file.read()
@@ -42,14 +46,24 @@ def from_file_template(self, filename):
def add_name(self, name, preset):
"""
- Adds a preset name to a list, which can be used by :func:`.from_name`.
+ Adds a preset mold to a list, which can be used by :func:`.from_name`.
+ If the preset is a string, it's considered a filename; otherwise, it's
+ treated as a mold.
"""
self._mold_names[name] = preset
def from_name(self, name, root=''):
"""
+ Creates an instance of a mold, using the same rules as :func:`.mold_from_name`.
+ """
+ ret = self.mold_from_name(name, root).instance()
+ ret.update(self.args)
+ return ret
+
+ def mold_from_name(self, name, root=''):
+ """
Loads a mold using a name. If ``name`` was added via :func:`.add_name`,
- that mold is returned. Otherwise it's treated as a filename,
+ the preset mold is returned. Otherwise it's treated as a filename,
relative to ``Loader.root_path`` if it begins with ``'/'``, or
``Loader.root_path/root`` otherwise.
"""
@@ -61,9 +75,9 @@ def from_name(self, name, root=''):
else:
source = name
if source[0] == '/':
- return self.from_file(self.root_path/source[1:])
+ return self.mold_from_file(self.root_path/source[1:])
else:
- return self.from_file(self.root_path/root/source)
+ return self.mold_from_file(self.root_path/root/source)
def escape(self, text):
if self.escaper and not getattr(text, 'is_escaped', False):
Please sign in to comment.
Something went wrong with that request. Please try again.