Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Guessing modname for Link Resources. Fixes #14.

  • Loading branch information...
commit 6ec1a2dcc2f67566f2aa18b8fbab80893eadfe2c 1 parent f21f4f9
Ralph Bean ralphbean authored
Showing with 33 additions and 1 deletion.
  1. +12 −1 tests/test_resources.py
  2. +21 −0 tw2/core/resources.py
13 tests/test_resources.py
View
@@ -1,4 +1,5 @@
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
+import tw2.core.core
from nose.tools import eq_, raises
from strainer.operators import eq_xhtml
from unittest import TestCase
@@ -295,6 +296,17 @@ def testLinkHash(self):
l = twr.Link(link="http://google.com")
self.assert_(hash(l.req())) # meh
+ def testAutoModnameReqPrep(self):
+ l = twr.Link(filename="somefile")
+ l = l.req()
+ l.prepare()
+ eq_(l.modname, "test_resources")
+
+ def testAutoModnameInject(self):
+ l = twr.Link(filename="somefile")
+ l.inject()
+ local = tw2.core.core.request_local()
+ eq_(local['resources'][0].modname, "test_resources")
def testDirLink(self):
dl = twr.DirLink(modname="tw2.core", filename="somefile")
@@ -302,7 +314,6 @@ def testDirLink(self):
i.prepare()
self.assert_(i.link)
-
def testJSSource(self):
import uuid
token = str(uuid.uuid4())
21 tw2/core/resources.py
View
@@ -1,6 +1,7 @@
import widgets as wd, util, core, params as pm
import re, logging, itertools
import os, webob as wo, pkg_resources as pr, mimetypes, simplejson
+import inspect
log = logging.getLogger(__name__)
@@ -93,7 +94,27 @@ class Link(Resource):
filename = pm.Param('Path to file, relative to module base.', default=None)
no_inject = pm.Param("Don't inject this link. (Default: False)", default=False)
+ def guess_modname(self):
+ """ Try to guess my modname.
+
+ If I wasn't supplied any modname, take a guess by stepping back up the
+ frame stack until I find something not in tw2.core
+ """
+
+ try:
+ frame, i = inspect.stack()[0][0], 0
+ while frame.f_globals['__name__'].startswith('tw2.core'):
+ frame, i = inspect.stack()[i][0], i + 1
+
+ return frame.f_globals['__name__']
+ except Exception as e:
+ return None
+
def prepare(self):
+
+ if not self.modname:
+ self.modname = self.guess_modname()
+
rl = core.request_local()
if not self.no_inject:
if not hasattr(self, 'link'):
Please sign in to comment.
Something went wrong with that request. Please try again.