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
cc18da30
Commit
cc18da30
authored
Dec 18, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed issue #40.
parent
9b087b28
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
3 deletions
+46
-3
examples/extensionless
+2
-0
pystache/loader.py
+12
-1
tests/test_loader.py
+32
-2
No files found.
examples/extensionless
0 → 100644
View file @
cc18da30
No file extension: {{foo}}
\ No newline at end of file
pystache/loader.py
View file @
cc18da30
...
@@ -22,6 +22,9 @@ class Loader(object):
...
@@ -22,6 +22,9 @@ class Loader(object):
search_dirs: the directories in which to search for templates.
search_dirs: the directories in which to search for templates.
Defaults to the current working directory.
Defaults to the current working directory.
extension: the template file extension. Defaults to "mustache".
Pass False for no extension.
"""
"""
if
extension
is
None
:
if
extension
is
None
:
extension
=
DEFAULT_EXTENSION
extension
=
DEFAULT_EXTENSION
...
@@ -35,6 +38,13 @@ class Loader(object):
...
@@ -35,6 +38,13 @@ class Loader(object):
self
.
template_encoding
=
encoding
self
.
template_encoding
=
encoding
self
.
template_extension
=
extension
self
.
template_extension
=
extension
def
make_file_name
(
self
,
template_name
):
file_name
=
template_name
if
self
.
template_extension
is
not
False
:
file_name
+=
os
.
path
.
extsep
+
self
.
template_extension
return
file_name
def
load_template
(
self
,
template_name
):
def
load_template
(
self
,
template_name
):
"""
"""
Find and load the given template, and return it as a string.
Find and load the given template, and return it as a string.
...
@@ -43,7 +53,8 @@ class Loader(object):
...
@@ -43,7 +53,8 @@ class Loader(object):
"""
"""
search_dirs
=
self
.
search_dirs
search_dirs
=
self
.
search_dirs
file_name
=
template_name
+
'.'
+
self
.
template_extension
file_name
=
self
.
make_file_name
(
template_name
)
for
dir_path
in
search_dirs
:
for
dir_path
in
search_dirs
:
file_path
=
os
.
path
.
join
(
dir_path
,
file_name
)
file_path
=
os
.
path
.
join
(
dir_path
,
file_name
)
...
...
tests/test_loader.py
View file @
cc18da30
...
@@ -6,6 +6,8 @@ from pystache.loader import Loader
...
@@ -6,6 +6,8 @@ from pystache.loader import Loader
class
LoaderTestCase
(
unittest
.
TestCase
):
class
LoaderTestCase
(
unittest
.
TestCase
):
search_dirs
=
'examples'
def
test_init
(
self
):
def
test_init
(
self
):
"""
"""
Test the __init__() constructor.
Test the __init__() constructor.
...
@@ -14,13 +16,34 @@ class LoaderTestCase(unittest.TestCase):
...
@@ -14,13 +16,34 @@ class LoaderTestCase(unittest.TestCase):
loader
=
Loader
()
loader
=
Loader
()
self
.
assertEquals
(
loader
.
search_dirs
,
[
os
.
curdir
])
self
.
assertEquals
(
loader
.
search_dirs
,
[
os
.
curdir
])
self
.
assertTrue
(
loader
.
template_encoding
is
None
)
self
.
assertTrue
(
loader
.
template_encoding
is
None
)
self
.
assertEquals
(
loader
.
template_extension
,
'mustache'
)
loader
=
Loader
(
search_dirs
=
[
'foo'
],
encoding
=
'utf-8'
,
extension
=
'txt'
)
loader
=
Loader
(
search_dirs
=
[
'foo'
],
encoding
=
'utf-8'
)
self
.
assertEquals
(
loader
.
search_dirs
,
[
'foo'
])
self
.
assertEquals
(
loader
.
search_dirs
,
[
'foo'
])
self
.
assertEquals
(
loader
.
template_encoding
,
'utf-8'
)
self
.
assertEquals
(
loader
.
template_encoding
,
'utf-8'
)
def
test_init__extension
(
self
):
# Test the default value.
loader
=
Loader
()
self
.
assertEquals
(
loader
.
template_extension
,
'mustache'
)
loader
=
Loader
(
extension
=
'txt'
)
self
.
assertEquals
(
loader
.
template_extension
,
'txt'
)
self
.
assertEquals
(
loader
.
template_extension
,
'txt'
)
loader
=
Loader
(
extension
=
False
)
self
.
assertTrue
(
loader
.
template_extension
is
False
)
def
test_make_file_name
(
self
):
loader
=
Loader
()
loader
.
template_extension
=
'bar'
self
.
assertEquals
(
loader
.
make_file_name
(
'foo'
),
'foo.bar'
)
loader
.
template_extension
=
False
self
.
assertEquals
(
loader
.
make_file_name
(
'foo'
),
'foo'
)
loader
.
template_extension
=
''
self
.
assertEquals
(
loader
.
make_file_name
(
'foo'
),
'foo.'
)
def
test_template_is_loaded
(
self
):
def
test_template_is_loaded
(
self
):
loader
=
Loader
(
search_dirs
=
'examples'
)
loader
=
Loader
(
search_dirs
=
'examples'
)
template
=
loader
.
load_template
(
'simple'
)
template
=
loader
.
load_template
(
'simple'
)
...
@@ -37,3 +60,10 @@ class LoaderTestCase(unittest.TestCase):
...
@@ -37,3 +60,10 @@ class LoaderTestCase(unittest.TestCase):
loader
=
Loader
()
loader
=
Loader
()
self
.
assertRaises
(
IOError
,
loader
.
load_template
,
'doesnt_exist'
)
self
.
assertRaises
(
IOError
,
loader
.
load_template
,
'doesnt_exist'
)
def
test_load_template__extensionless_file
(
self
):
loader
=
Loader
(
search_dirs
=
self
.
search_dirs
)
self
.
assertRaises
(
IOError
,
loader
.
load_template
,
'extensionless'
)
loader
.
template_extension
=
False
self
.
assertEquals
(
loader
.
load_template
(
'extensionless'
),
"No file extension: {{foo}}"
)
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