Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
ad3707a0
Commit
ad3707a0
authored
Apr 01, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved the SpecLoader class into its own spec_loader module.
parent
a9843211
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
83 deletions
+87
-83
pystache/renderer.py
+1
-1
pystache/spec_loader.py
+84
-0
pystache/template_spec.py
+0
-81
tests/test_template_spec.py
+2
-1
No files found.
pystache/renderer.py
View file @
ad3707a0
...
...
@@ -9,7 +9,7 @@ from . import defaults
from
.context
import
Context
from
.loader
import
Loader
from
.renderengine
import
RenderEngine
from
.
template_spec
import
SpecLoader
from
.
spec_loader
import
SpecLoader
from
.template_spec
import
TemplateSpec
...
...
pystache/spec_loader.py
0 → 100644
View file @
ad3707a0
# coding: utf-8
"""
This module supports customized (aka special or specified) template loading.
"""
import
os.path
from
.loader
import
Loader
# TODO: add test cases for this class.
class
SpecLoader
(
object
):
"""
Supports loading custom-specified templates (from TemplateSpec instances).
"""
def
__init__
(
self
,
loader
=
None
):
if
loader
is
None
:
loader
=
Loader
()
self
.
loader
=
loader
def
_find_relative
(
self
,
spec
):
"""
Return the path to the template as a relative (dir, file_name) pair.
The directory returned is relative to the directory containing the
class definition of the given object. The method returns None for
this directory if the directory is unknown without first searching
the search directories.
"""
if
spec
.
template_rel_path
is
not
None
:
return
os
.
path
.
split
(
spec
.
template_rel_path
)
# Otherwise, determine the file name separately.
locator
=
self
.
loader
.
_make_locator
()
template_name
=
(
spec
.
template_name
if
spec
.
template_name
is
not
None
else
locator
.
make_template_name
(
spec
))
file_name
=
locator
.
make_file_name
(
template_name
,
spec
.
template_extension
)
return
(
spec
.
template_rel_directory
,
file_name
)
def
_find
(
self
,
spec
):
"""
Find and return the path to the template associated to the instance.
"""
dir_path
,
file_name
=
self
.
_find_relative
(
spec
)
locator
=
self
.
loader
.
_make_locator
()
if
dir_path
is
None
:
# Then we need to search for the path.
path
=
locator
.
find_object
(
spec
,
self
.
loader
.
search_dirs
,
file_name
=
file_name
)
else
:
obj_dir
=
locator
.
get_object_directory
(
spec
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
return
path
def
load
(
self
,
spec
):
"""
Find and return the template associated to a TemplateSpec instance.
Returns the template as a unicode string.
Arguments:
spec: a TemplateSpec instance.
"""
if
spec
.
template
is
not
None
:
return
self
.
loader
.
unicode
(
spec
.
template
,
spec
.
template_encoding
)
path
=
self
.
_find
(
spec
)
return
self
.
loader
.
read
(
path
,
spec
.
template_encoding
)
pystache/template_spec.py
View file @
ad3707a0
...
...
@@ -5,13 +5,6 @@ This module supports customized (aka special or specified) template loading.
"""
import
os.path
from
.loader
import
Loader
# TODO: consider putting TemplateSpec and SpecLoader in separate modules.
# TODO: finish the class docstring.
class
TemplateSpec
(
object
):
...
...
@@ -48,77 +41,3 @@ class TemplateSpec(object):
template_name
=
None
template_extension
=
None
template_encoding
=
None
# TODO: add test cases for this class.
class
SpecLoader
(
object
):
"""
Supports loading a custom-specified template.
"""
def
__init__
(
self
,
loader
=
None
):
if
loader
is
None
:
loader
=
Loader
()
self
.
loader
=
loader
def
_find_relative
(
self
,
spec
):
"""
Return the path to the template as a relative (dir, file_name) pair.
The directory returned is relative to the directory containing the
class definition of the given object. The method returns None for
this directory if the directory is unknown without first searching
the search directories.
"""
if
spec
.
template_rel_path
is
not
None
:
return
os
.
path
.
split
(
spec
.
template_rel_path
)
# Otherwise, determine the file name separately.
locator
=
self
.
loader
.
_make_locator
()
template_name
=
(
spec
.
template_name
if
spec
.
template_name
is
not
None
else
locator
.
make_template_name
(
spec
))
file_name
=
locator
.
make_file_name
(
template_name
,
spec
.
template_extension
)
return
(
spec
.
template_rel_directory
,
file_name
)
def
_find
(
self
,
spec
):
"""
Find and return the path to the template associated to the instance.
"""
dir_path
,
file_name
=
self
.
_find_relative
(
spec
)
locator
=
self
.
loader
.
_make_locator
()
if
dir_path
is
None
:
# Then we need to search for the path.
path
=
locator
.
find_object
(
spec
,
self
.
loader
.
search_dirs
,
file_name
=
file_name
)
else
:
obj_dir
=
locator
.
get_object_directory
(
spec
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
return
path
def
load
(
self
,
spec
):
"""
Find and return the template associated to a TemplateSpec instance.
Returns the template as a unicode string.
Arguments:
spec: a TemplateSpec instance.
"""
if
spec
.
template
is
not
None
:
return
self
.
loader
.
unicode
(
spec
.
template
,
spec
.
template_encoding
)
path
=
self
.
_find
(
spec
)
return
self
.
loader
.
read
(
path
,
spec
.
template_encoding
)
tests/test_template_spec.py
View file @
ad3707a0
...
...
@@ -16,9 +16,10 @@ from examples.lambdas import Lambdas
from
examples.inverted
import
Inverted
,
InvertedLists
from
pystache
import
TemplateSpec
from
pystache
import
Renderer
from
pystache.
template_spec
import
SpecLoader
from
pystache.
spec_loader
import
SpecLoader
from
pystache.locator
import
Locator
from
pystache.loader
import
Loader
from
pystache.spec_loader
import
SpecLoader
from
.common
import
DATA_DIR
from
.common
import
EXAMPLES_DIR
from
.common
import
AssertIsMixin
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment