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
ed896eb9
Commit
ed896eb9
authored
Apr 01, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed Locator methods to find_name and find_object.
parent
8f8fd6c4
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
35 deletions
+36
-35
pystache/loader.py
+4
-2
pystache/locator.py
+2
-2
pystache/template_spec.py
+7
-8
tests/test_locator.py
+17
-17
tests/test_template_spec.py
+6
-6
No files found.
pystache/loader.py
View file @
ed896eb9
...
@@ -107,6 +107,7 @@ class Loader(object):
...
@@ -107,6 +107,7 @@ class Loader(object):
return
self
.
unicode
(
text
,
encoding
)
return
self
.
unicode
(
text
,
encoding
)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method.
# TODO: unit-test this method.
def
load_name
(
self
,
name
,
search_dirs
):
def
load_name
(
self
,
name
,
search_dirs
):
"""
"""
...
@@ -121,10 +122,11 @@ class Loader(object):
...
@@ -121,10 +122,11 @@ class Loader(object):
"""
"""
locator
=
self
.
_make_locator
()
locator
=
self
.
_make_locator
()
path
=
locator
.
find_
path_by_
name
(
search_dirs
,
name
)
path
=
locator
.
find_name
(
search_dirs
,
name
)
return
self
.
read
(
path
)
return
self
.
read
(
path
)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method.
# TODO: unit-test this method.
def
load_object
(
self
,
obj
,
search_dirs
):
def
load_object
(
self
,
obj
,
search_dirs
):
"""
"""
...
@@ -139,6 +141,6 @@ class Loader(object):
...
@@ -139,6 +141,6 @@ class Loader(object):
"""
"""
locator
=
self
.
_make_locator
()
locator
=
self
.
_make_locator
()
path
=
locator
.
find_
path_by_
object
(
search_dirs
,
obj
)
path
=
locator
.
find_object
(
search_dirs
,
obj
)
return
self
.
read
(
path
)
return
self
.
read
(
path
)
pystache/locator.py
View file @
ed896eb9
...
@@ -123,7 +123,7 @@ class Locator(object):
...
@@ -123,7 +123,7 @@ class Locator(object):
return
path
return
path
def
find_
path_by_
name
(
self
,
search_dirs
,
template_name
):
def
find_name
(
self
,
search_dirs
,
template_name
):
"""
"""
Return the path to a template with the given name.
Return the path to a template with the given name.
...
@@ -132,7 +132,7 @@ class Locator(object):
...
@@ -132,7 +132,7 @@ class Locator(object):
return
self
.
_find_path_required
(
search_dirs
,
file_name
)
return
self
.
_find_path_required
(
search_dirs
,
file_name
)
def
find_
path_by_
object
(
self
,
search_dirs
,
obj
,
file_name
=
None
):
def
find_object
(
self
,
search_dirs
,
obj
,
file_name
=
None
):
"""
"""
Return the path to a template associated with the given object.
Return the path to a template associated with the given object.
...
...
pystache/template_spec.py
View file @
ed896eb9
# coding: utf-8
# coding: utf-8
"""
"""
This module supports customized (
or special/
specified) template loading.
This module supports customized (
aka special or
specified) template loading.
"""
"""
...
@@ -164,22 +164,21 @@ class SpecLoader(object):
...
@@ -164,22 +164,21 @@ class SpecLoader(object):
return
(
template_dir
,
file_name
)
return
(
template_dir
,
file_name
)
# TODO: make this private.
def
_find
(
self
,
spec
):
def
get_template_path
(
self
,
view
):
"""
"""
Return the path to the view's associated templat
e.
Find and return the path to the template associated to the instanc
e.
"""
"""
dir_path
,
file_name
=
self
.
get_relative_template_location
(
view
)
dir_path
,
file_name
=
self
.
get_relative_template_location
(
spec
)
# TODO: share code with the loader attribute here.
# TODO: share code with the loader attribute here.
locator
=
Locator
(
extension
=
self
.
loader
.
extension
)
locator
=
Locator
(
extension
=
self
.
loader
.
extension
)
if
dir_path
is
None
:
if
dir_path
is
None
:
# Then we need to search for the path.
# Then we need to search for the path.
path
=
locator
.
find_
path_by_object
(
self
.
search_dirs
,
view
,
file_name
=
file_name
)
path
=
locator
.
find_
object
(
self
.
search_dirs
,
spec
,
file_name
=
file_name
)
else
:
else
:
obj_dir
=
locator
.
get_object_directory
(
view
)
obj_dir
=
locator
.
get_object_directory
(
spec
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
return
path
return
path
...
@@ -198,6 +197,6 @@ class SpecLoader(object):
...
@@ -198,6 +197,6 @@ class SpecLoader(object):
if
spec
.
template
is
not
None
:
if
spec
.
template
is
not
None
:
return
self
.
loader
.
unicode
(
spec
.
template
,
spec
.
template_encoding
)
return
self
.
loader
.
unicode
(
spec
.
template
,
spec
.
template_encoding
)
path
=
self
.
get_template_path
(
spec
)
path
=
self
.
_find
(
spec
)
return
self
.
loader
.
read
(
path
,
spec
.
template_encoding
)
return
self
.
loader
.
read
(
path
,
spec
.
template_encoding
)
tests/test_locator.py
View file @
ed896eb9
...
@@ -69,21 +69,21 @@ class LocatorTests(unittest.TestCase):
...
@@ -69,21 +69,21 @@ class LocatorTests(unittest.TestCase):
self
.
assertEquals
(
locator
.
make_file_name
(
'foo'
,
template_extension
=
'bar'
),
'foo.bar'
)
self
.
assertEquals
(
locator
.
make_file_name
(
'foo'
,
template_extension
=
'bar'
),
'foo.bar'
)
def
test_find_
path_by_
name
(
self
):
def
test_find_name
(
self
):
locator
=
Locator
()
locator
=
Locator
()
path
=
locator
.
find_
path_by_
name
(
search_dirs
=
[
'examples'
],
template_name
=
'simple'
)
path
=
locator
.
find_name
(
search_dirs
=
[
'examples'
],
template_name
=
'simple'
)
self
.
assertEquals
(
os
.
path
.
basename
(
path
),
'simple.mustache'
)
self
.
assertEquals
(
os
.
path
.
basename
(
path
),
'simple.mustache'
)
def
test_find_
path_by_
name__using_list_of_paths
(
self
):
def
test_find_name__using_list_of_paths
(
self
):
locator
=
Locator
()
locator
=
Locator
()
path
=
locator
.
find_
path_by_
name
(
search_dirs
=
[
'doesnt_exist'
,
'examples'
],
template_name
=
'simple'
)
path
=
locator
.
find_name
(
search_dirs
=
[
'doesnt_exist'
,
'examples'
],
template_name
=
'simple'
)
self
.
assertTrue
(
path
)
self
.
assertTrue
(
path
)
def
test_find_
path_by_
name__precedence
(
self
):
def
test_find_name__precedence
(
self
):
"""
"""
Test the order in which find_
path_by_
name() searches directories.
Test the order in which find_name() searches directories.
"""
"""
locator
=
Locator
()
locator
=
Locator
()
...
@@ -91,47 +91,47 @@ class LocatorTests(unittest.TestCase):
...
@@ -91,47 +91,47 @@ class LocatorTests(unittest.TestCase):
dir1
=
DATA_DIR
dir1
=
DATA_DIR
dir2
=
os
.
path
.
join
(
DATA_DIR
,
'locator'
)
dir2
=
os
.
path
.
join
(
DATA_DIR
,
'locator'
)
self
.
assertTrue
(
locator
.
find_
path_by_
name
(
search_dirs
=
[
dir1
],
template_name
=
'duplicate'
))
self
.
assertTrue
(
locator
.
find_name
(
search_dirs
=
[
dir1
],
template_name
=
'duplicate'
))
self
.
assertTrue
(
locator
.
find_
path_by_
name
(
search_dirs
=
[
dir2
],
template_name
=
'duplicate'
))
self
.
assertTrue
(
locator
.
find_name
(
search_dirs
=
[
dir2
],
template_name
=
'duplicate'
))
path
=
locator
.
find_
path_by_
name
(
search_dirs
=
[
dir2
,
dir1
],
template_name
=
'duplicate'
)
path
=
locator
.
find_name
(
search_dirs
=
[
dir2
,
dir1
],
template_name
=
'duplicate'
)
dirpath
=
os
.
path
.
dirname
(
path
)
dirpath
=
os
.
path
.
dirname
(
path
)
dirname
=
os
.
path
.
split
(
dirpath
)[
-
1
]
dirname
=
os
.
path
.
split
(
dirpath
)[
-
1
]
self
.
assertEquals
(
dirname
,
'locator'
)
self
.
assertEquals
(
dirname
,
'locator'
)
def
test_find_
path_by_
name__non_existent_template_fails
(
self
):
def
test_find_name__non_existent_template_fails
(
self
):
locator
=
Locator
()
locator
=
Locator
()
self
.
assertRaises
(
IOError
,
locator
.
find_
path_by_
name
,
search_dirs
=
[],
template_name
=
'doesnt_exist'
)
self
.
assertRaises
(
IOError
,
locator
.
find_name
,
search_dirs
=
[],
template_name
=
'doesnt_exist'
)
def
test_find_
path_by_
object
(
self
):
def
test_find_object
(
self
):
locator
=
Locator
()
locator
=
Locator
()
obj
=
SayHello
()
obj
=
SayHello
()
actual
=
locator
.
find_
path_by_
object
(
search_dirs
=
[],
obj
=
obj
,
file_name
=
'sample_view.mustache'
)
actual
=
locator
.
find_object
(
search_dirs
=
[],
obj
=
obj
,
file_name
=
'sample_view.mustache'
)
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'sample_view.mustache'
))
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'sample_view.mustache'
))
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
def
test_find_
path_by_
object__none_file_name
(
self
):
def
test_find_object__none_file_name
(
self
):
locator
=
Locator
()
locator
=
Locator
()
obj
=
SayHello
()
obj
=
SayHello
()
actual
=
locator
.
find_
path_by_
object
(
search_dirs
=
[],
obj
=
obj
)
actual
=
locator
.
find_object
(
search_dirs
=
[],
obj
=
obj
)
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'say_hello.mustache'
))
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'say_hello.mustache'
))
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
def
test_find_
path_by_
object__none_object_directory
(
self
):
def
test_find_object__none_object_directory
(
self
):
locator
=
Locator
()
locator
=
Locator
()
obj
=
None
obj
=
None
self
.
assertEquals
(
None
,
locator
.
get_object_directory
(
obj
))
self
.
assertEquals
(
None
,
locator
.
get_object_directory
(
obj
))
actual
=
locator
.
find_
path_by_
object
(
search_dirs
=
[
DATA_DIR
],
obj
=
obj
,
file_name
=
'say_hello.mustache'
)
actual
=
locator
.
find_object
(
search_dirs
=
[
DATA_DIR
],
obj
=
obj
,
file_name
=
'say_hello.mustache'
)
expected
=
os
.
path
.
join
(
DATA_DIR
,
'say_hello.mustache'
)
expected
=
os
.
path
.
join
(
DATA_DIR
,
'say_hello.mustache'
)
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
...
...
tests/test_template_spec.py
View file @
ed896eb9
...
@@ -322,9 +322,9 @@ class TemplateSpecTests(unittest.TestCase):
...
@@ -322,9 +322,9 @@ class TemplateSpecTests(unittest.TestCase):
view
.
template_extension
=
'txt'
view
.
template_extension
=
'txt'
self
.
_assert_template_location
(
view
,
(
None
,
'sample_view.txt'
))
self
.
_assert_template_location
(
view
,
(
None
,
'sample_view.txt'
))
def
test_
get_template_path
__with_directory
(
self
):
def
test_
find
__with_directory
(
self
):
"""
"""
Test
get_template_path
() with a view that has a directory specified.
Test
_find
() with a view that has a directory specified.
"""
"""
locator
=
self
.
_make_locator
()
locator
=
self
.
_make_locator
()
...
@@ -333,14 +333,14 @@ class TemplateSpecTests(unittest.TestCase):
...
@@ -333,14 +333,14 @@ class TemplateSpecTests(unittest.TestCase):
view
.
template_rel_path
=
'foo/bar.txt'
view
.
template_rel_path
=
'foo/bar.txt'
self
.
assertTrue
(
locator
.
get_relative_template_location
(
view
)[
0
]
is
not
None
)
self
.
assertTrue
(
locator
.
get_relative_template_location
(
view
)[
0
]
is
not
None
)
actual
=
locator
.
get_template_path
(
view
)
actual
=
locator
.
_find
(
view
)
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'foo/bar.txt'
))
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'foo/bar.txt'
))
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
def
test_
get_template_path
__without_directory
(
self
):
def
test_
find
__without_directory
(
self
):
"""
"""
Test
get_template_path
() with a view that doesn't have a directory specified.
Test
_find
() with a view that doesn't have a directory specified.
"""
"""
locator
=
self
.
_make_locator
()
locator
=
self
.
_make_locator
()
...
@@ -348,7 +348,7 @@ class TemplateSpecTests(unittest.TestCase):
...
@@ -348,7 +348,7 @@ class TemplateSpecTests(unittest.TestCase):
view
=
SampleView
()
view
=
SampleView
()
self
.
assertTrue
(
locator
.
get_relative_template_location
(
view
)[
0
]
is
None
)
self
.
assertTrue
(
locator
.
get_relative_template_location
(
view
)[
0
]
is
None
)
actual
=
locator
.
get_template_path
(
view
)
actual
=
locator
.
_find
(
view
)
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'sample_view.mustache'
))
expected
=
os
.
path
.
abspath
(
os
.
path
.
join
(
DATA_DIR
,
'sample_view.mustache'
))
self
.
assertEquals
(
actual
,
expected
)
self
.
assertEquals
(
actual
,
expected
)
...
...
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