Skip to content
This repository
Browse code

Guessing modname for Link Resources. Fixes #14.

  • Loading branch information...
commit 6ec1a2dcc2f67566f2aa18b8fbab80893eadfe2c 1 parent f21f4f9
Ralph Bean authored March 04, 2012
13  tests/test_resources.py
... ...
@@ -1,4 +1,5 @@
1 1
 import webob as wo, webtest as wt, tw2.core as twc, os, testapi, tw2.core.resources as twr, tw2.core.testbase as tb, tw2.core.params as pm
  2
+import tw2.core.core
2 3
 from nose.tools import eq_, raises
3 4
 from strainer.operators import eq_xhtml
4 5
 from unittest import TestCase
@@ -295,6 +296,17 @@ def testLinkHash(self):
295 296
         l = twr.Link(link="http://google.com")
296 297
         self.assert_(hash(l.req()))  # meh
297 298
 
  299
+    def testAutoModnameReqPrep(self):
  300
+        l = twr.Link(filename="somefile")
  301
+        l = l.req()
  302
+        l.prepare()
  303
+        eq_(l.modname, "test_resources")
  304
+
  305
+    def testAutoModnameInject(self):
  306
+        l = twr.Link(filename="somefile")
  307
+        l.inject()
  308
+        local = tw2.core.core.request_local()
  309
+        eq_(local['resources'][0].modname, "test_resources")
298 310
 
299 311
     def testDirLink(self):
300 312
         dl = twr.DirLink(modname="tw2.core", filename="somefile")
@@ -302,7 +314,6 @@ def testDirLink(self):
302 314
         i.prepare()
303 315
         self.assert_(i.link)
304 316
 
305  
-
306 317
     def testJSSource(self):
307 318
         import uuid
308 319
         token = str(uuid.uuid4())
21  tw2/core/resources.py
... ...
@@ -1,6 +1,7 @@
1 1
 import widgets as wd, util, core, params as pm
2 2
 import re, logging, itertools
3 3
 import os, webob as wo, pkg_resources as pr, mimetypes, simplejson
  4
+import inspect
4 5
 
5 6
 
6 7
 log = logging.getLogger(__name__)
@@ -93,7 +94,27 @@ class Link(Resource):
93 94
     filename = pm.Param('Path to file, relative to module base.', default=None)
94 95
     no_inject = pm.Param("Don't inject this link. (Default: False)", default=False)
95 96
 
  97
+    def guess_modname(self):
  98
+        """ Try to guess my modname.
  99
+
  100
+        If I wasn't supplied any modname, take a guess by stepping back up the
  101
+        frame stack until I find something not in tw2.core
  102
+        """
  103
+
  104
+        try:
  105
+            frame, i = inspect.stack()[0][0], 0
  106
+            while frame.f_globals['__name__'].startswith('tw2.core'):
  107
+                frame, i = inspect.stack()[i][0], i + 1
  108
+
  109
+            return frame.f_globals['__name__']
  110
+        except Exception as e:
  111
+            return None
  112
+
96 113
     def prepare(self):
  114
+
  115
+        if not self.modname:
  116
+            self.modname = self.guess_modname()
  117
+
97 118
         rl = core.request_local()
98 119
         if not self.no_inject:
99 120
             if not hasattr(self, 'link'):

0 notes on commit 6ec1a2d

Please sign in to comment.
Something went wrong with that request. Please try again.