sqlalchemy / sqlalchemy/mako

add source namespace to get_def() ?

Open
#160 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature low priority runtime
Dominant language
Python
Stars
459
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Migrated issue, originally created by Michael Bayer (@zzzeek)

When render() is called, runtime will pull out the requested namespace after _populate_self_namespace is called and effectively run whatever named def relative to that namespace, such as self.

The feature would look like:

    class Template(object):
       # ...
    
       def get_def(self, name, via_namespace=None):
           # ...

Below is not the patch but this is the kind of tinkering we'd need

diff -r 6779081b32cfb237062da8bc623a27d47301ce4c mako/runtime.py
--- a/mako/runtime.py	Wed Jan 19 10:33:00 2011 -0500
+++ b/mako/runtime.py	Thu Feb 10 17:12:30 2011 -0500
@@ -575,6 +575,14 @@
                             **_kwargs_for_callable(callable_, data))
     return context._pop_buffer().getvalue()
 
+def _get_self(tmpl, callable_, args, data, as_unicode=False):
+    buf = util.StringIO()
+    context = Context(buf, **data)
+    context._outputting_as_unicode = as_unicode
+    context._with_template = tmpl
+    (inherit, lclcontext) = _populate_self_namespace(context, tmpl)
+    return buf, context._data['self']
+
 def _kwargs_for_callable(callable_, data):
     argspec = inspect.getargspec(callable_)
     # for normal pages, **pageargs is usually present
@@ -609,7 +617,7 @@
         # otherwise, call the actual rendering method specified
         (inherit, lclcontext) = _populate_self_namespace(context, tmpl.parent)
         _exec_template(callable_, context, args=args, kwargs=kwargs)
- 
+
 def _exec_template(callable_, context, args=None, kwargs=None):
     """execute a rendering callable given the callable, a
     Context, and optional explicit arguments
diff -r 6779081b32cfb237062da8bc623a27d47301ce4c mako/template.py
--- a/mako/template.py	Wed Jan 19 10:33:00 2011 -0500
+++ b/mako/template.py	Thu Feb 10 17:12:30 2011 -0500
@@ -304,7 +304,15 @@
                                 context, 
                                 *args, 
                                 **kwargs)
- 
+
+    def get_self(self, *args, **data):
+        return runtime._get_self(
+            self, 
+            self.callable_,
+            args, 
+            data
+        )
+
     def has_def(self, name):
         return hasattr(self.module, "render_%s" % name)
  

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading mako/template.py around Template.get_def() and mako/runtime.py around render() and _populate_self_namespace(). Compare the requested source namespace for get_def() with the proposed get_self() sketch, then define completion around resolving a named def relative to that namespace without changing existing rendering behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.