Permalink
Browse files

merge

  • Loading branch information...
2 parents c137e82 + 725fd96 commit 5411995edc4fb67e840c823cff3510f95020b0fb @rockyburt rockyburt committed Feb 26, 2011
Showing with 47 additions and 18 deletions.
  1. +20 −15 pyramid_jinja2/__init__.py
  2. +27 −3 pyramid_jinja2/tests/test_it.py
View
@@ -91,25 +91,30 @@ def uptodate(self):
return False
-def _caller_package(allowed=()):
- f = None
- for t in inspect.stack():
- f = t[0]
- if f.f_globals.get('__name__') not in allowed:
- break
+class _PackageFinder(object):
+ inspect = staticmethod(inspect)
- if f is None:
- return None
+ def caller_package(self, allowed=()):
+ f = None
+ for t in self.inspect.stack():
+ f = t[0]
+ if f.f_globals.get('__name__') not in allowed:
+ break
- pname = f.f_globals.get('__name__') or '__main__'
- m = sys.modules[pname]
- f = getattr(m, '__file__', '')
- if (('__init__.py' in f) or ('__init__$py' in f)): # empty at >>>
- return m
+ if f is None:
+ return None
+
+ pname = f.f_globals.get('__name__') or '__main__'
+ m = sys.modules[pname]
+ f = getattr(m, '__file__', '')
+ if (('__init__.py' in f) or ('__init__$py' in f)): # empty at >>>
+ return m
+
+ pname = m.__name__.rsplit('.', 1)[0]
- pname = m.__name__.rsplit('.', 1)[0]
+ return sys.modules[pname]
- return sys.modules[pname]
+_caller_package = _PackageFinder().caller_package
class SmartAssetSpecLoader(FileSystemLoader):
@@ -293,10 +293,9 @@ def test_get_source(self):
# make sure new non-prefixed asset spec based loading works
# without the leading package name
- class MockEnv(object):
- _default_package = 'pyramid_jinja2.tests'
asset = 'templates/helloworld.jinja2'
- self.assertNotEqual(loader.get_source(MockEnv, asset), None)
+ env = Mock(_default_package='pyramid_jinja2.tests')
+ self.assertNotEqual(loader.get_source(env, asset), None)
class TestFileInfo(unittest.TestCase):
@@ -353,6 +352,26 @@ def myview(request):
self.assertEqual(testapp.get('/').body, 'bar')
+class TestPackageFinder(unittest.TestCase):
+
+ def test_caller_package(self):
+ from pyramid_jinja2 import _PackageFinder
+ pf = _PackageFinder()
+
+ class MockInspect(object):
+ def __init__(self, items=()):
+ self.items = items
+
+ def stack(self):
+ return self.items
+ pf.inspect = MockInspect()
+ self.assertEqual(pf.caller_package(), None)
+
+ import xml
+ pf.inspect.items = [(Mock(f_globals={'__name__': 'xml'}),)]
+ self.assertEqual(pf.caller_package(), xml)
+
+
class DummyEnvironment(object):
def get_template(self, path):
self.path = path
@@ -368,3 +387,8 @@ def __init__(self, kw):
self.__dict__.update(kw)
if 'registry' in self.__dict__:
self.settings = self.registry.settings
+
+
+class Mock(object):
+ def __init__(self, **kwargs):
+ self.__dict__.update(kwargs)

0 comments on commit 5411995

Please sign in to comment.