Coder Social home page Coder Social logo

mako's Introduction

SQLAlchemy

PyPI PyPI - Python Version PyPI - Downloads

The Python SQL Toolkit and Object Relational Mapper

Introduction

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

Major SQLAlchemy features include:

  • An industrial strength ORM, built from the core on the identity map, unit of work, and data mapper patterns. These patterns allow transparent persistence of objects using a declarative configuration system. Domain models can be constructed and manipulated naturally, and changes are synchronized with the current transaction automatically.
  • A relationally-oriented query system, exposing the full range of SQL's capabilities explicitly, including joins, subqueries, correlation, and most everything else, in terms of the object model. Writing queries with the ORM uses the same techniques of relational composition you use when writing SQL. While you can drop into literal SQL at any time, it's virtually never needed.
  • A comprehensive and flexible system of eager loading for related collections and objects. Collections are cached within a session, and can be loaded on individual access, all at once using joins, or by query per collection across the full result set.
  • A Core SQL construction system and DBAPI interaction layer. The SQLAlchemy Core is separate from the ORM and is a full database abstraction layer in its own right, and includes an extensible Python-based SQL expression language, schema metadata, connection pooling, type coercion, and custom types.
  • All primary and foreign key constraints are assumed to be composite and natural. Surrogate integer primary keys are of course still the norm, but SQLAlchemy never assumes or hardcodes to this model.
  • Database introspection and generation. Database schemas can be "reflected" in one step into Python structures representing database metadata; those same structures can then generate CREATE statements right back out - all within the Core, independent of the ORM.

SQLAlchemy's philosophy:

  • SQL databases behave less and less like object collections the more size and performance start to matter; object collections behave less and less like tables and rows the more abstraction starts to matter. SQLAlchemy aims to accommodate both of these principles.
  • An ORM doesn't need to hide the "R". A relational database provides rich, set-based functionality that should be fully exposed. SQLAlchemy's ORM provides an open-ended set of patterns that allow a developer to construct a custom mediation layer between a domain model and a relational schema, turning the so-called "object relational impedance" issue into a distant memory.
  • The developer, in all cases, makes all decisions regarding the design, structure, and naming conventions of both the object model as well as the relational schema. SQLAlchemy only provides the means to automate the execution of these decisions.
  • With SQLAlchemy, there's no such thing as "the ORM generated a bad query" - you retain full control over the structure of queries, including how joins are organized, how subqueries and correlation is used, what columns are requested. Everything SQLAlchemy does is ultimately the result of a developer-initiated decision.
  • Don't use an ORM if the problem doesn't need one. SQLAlchemy consists of a Core and separate ORM component. The Core offers a full SQL expression language that allows Pythonic construction of SQL constructs that render directly to SQL strings for a target database, returning result sets that are essentially enhanced DBAPI cursors.
  • Transactions should be the norm. With SQLAlchemy's ORM, nothing goes to permanent storage until commit() is called. SQLAlchemy encourages applications to create a consistent means of delineating the start and end of a series of operations.
  • Never render a literal value in a SQL statement. Bound parameters are used to the greatest degree possible, allowing query optimizers to cache query plans effectively and making SQL injection attacks a non-issue.

Documentation

Latest documentation is at:

https://www.sqlalchemy.org/docs/

Installation / Requirements

Full documentation for installation is at Installation.

Getting Help / Development / Bug reporting

Please refer to the SQLAlchemy Community Guide.

Code of Conduct

Above all, SQLAlchemy places great emphasis on polite, thoughtful, and constructive communication between users and developers. Please see our current Code of Conduct at Code of Conduct.

License

SQLAlchemy is distributed under the MIT license.

mako's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mako's Issues

Document .mak as the standard Mako extension

Migrated issue, originally created by Anonymous

Currently there's no obvious published filename extension for Mako templates, unless you count .html. The Mako docs refer to .html everywhere. Maybe this is inherited from Genshi (like ${}), as Myghty endorsed .myt and .myc.

The problem with encouraging .html for templates is there's no programatic way of determining what kind of template the file is. Then your text editor (or Babel extractor, or whatever) doesn't know what kind of template it's looking at and can't act upon it accordingly

I'm assuming the preferred extension is .mak, as that's the default specified in the TG/Buffet plugin. Can we s/.html/.mak/g in the documentation?

Add .xml support to mako textmate bundle

Migrated issue, originally created by Anonymous

I use mako a lot for RSS and atom feed templating. It just doesn't seem right to call these .html so I editing the textmate bundle to include .xml .

fileTypes = ( 'mao', 'html', 'xml' );

Also, is that a bug where 'mao' is a valid filetype, I figure you might have meant mako. I edited mine to this as well since some of the people i work with us the .mako extension.

fileTypes = ( 'mako', 'html', 'xml' );

Preliminary support for IronPython

Migrated issue, originally created by Anonymous

A preliminary patch to support Mako Templates on IronPython. Rather, IronPython Community Edition, as Mako needs compiler.parse().

Mako is from SVN trunk, IPCE is r6.

Download IPCE r6 from http://sourceforge.net/projects/fepy.

There are two patches. I think ironpython.patch should be applied. workaround.patch should not, as it won't be needed once http://bugzilla.ximian.com/show_bug.cgi?id=81714 is fixed. However, if you want to test on IronPython/Mono, you need to apply it to test at all.

The command line I used for testing is:
$ IRONPYTHONPATH=lib FEPY_OPTIONS=ast,codecs_errors ipy test/alltests.py

Test result (log attached):
Ran 183 tests FAILED (failures=30, errors=11)

I disabled test.cache for now. Test passes on CPython after the patch.


Attachments: ironpython.patch | workaround.patch | test.log

mako.exceptions.text_error_template

Migrated issue, originally created by Anonymous

156: for (filename, lineno, function, line) in tback.traceback:

text_error_template will fail if line is None.

And I guess it can be None, At least when running on WindowsXP/Python-2.5


Attachments: text_exception.patch

using multiple caller methods in the same output command yields strange behaviour

Migrated issue, originally created by Anonymous

If you use a caller twice in the same output string all of the resulting output appears out of order.

For example, ${somefunc(caller.head(), caller.body())} will exhibit the strange behaviour.

The test case is a bit convoluted, so bear with me. Here is the test template:

${self.f1()}

<%def name="f1()">
  <%call expr="f2()">
    <%def name="inner()">
      one
    </%def>
    two
  </%call>
</%def>

<%def name="f2()">
  ${self.output(caller.inner(), caller.body())}
</%def>

# we need a 2 argument function to get both in the same output line
<%def name="output(a1, a2)">
<pre>
  A1: ${a1}
  A2: ${a2}
</pre>
</%def>

Now, according to the docs I should expect this:

  A1: one
  A2: two

But I get this instead:

one two
  A1:
  A2:

I realize that the output() function is contrived. I actually discovered this while trying to use the Pylons function webhelpers.rails.javascript.link_to_function(). Same issue. The arguments to link_to_function() were being messed up before they were passed in. Very odd.

Allow long expressions to span lines

Migrated issue, originally created by Anonymous

Allow long Mako expressions such as:

 % if (somevar == 1 or somevar == 2) and othervar == 3:

to be split between lines as in regular Python, e.g.,

 % if (somevar == 1 or somevar == 2) \
       and othervar == 3:

Cannot check out files from SVN

Migrated issue, originally created by Anonymous

Hi, I am having problems with checking out mako files from SVN. I am running Windows XPSP2 and svn 1.4.2. Svn complains about some textmate files with funny names. See below:

BTW. Maybe you could set up a separate "component" for such kind of tickets ? (like this one and also like #5)

C:\>svn co http://svn.makotemplates.org/mako/trunk mako
A    mako\test
A    mako\test\filters.py
A    mako\test\inheritance.py
A    mako\test\lookup.py
A    mako\test\alltests.py
A    mako\test\call.py
A    mako\test\template.py
A    mako\test\namespace.py
A    mako\test\util.py
A    mako\test\def.py
A    mako\test\__init__.py
A    mako\test\lexer.py
A    mako\test\lru.py
A    mako\test\pygen.py
A    mako\test\cache.py
A    mako\test\ast.py
A    mako\contrib
A    mako\contrib\textmate
A    mako\contrib\textmate\Mako.tmbundle
A    mako\contrib\textmate\Mako.tmbundle\Snippets
svn: Can't check path 'mako\contrib\textmate\Mako.tmbundle\Snippets\<%names__ name> __ (nsn).tmSnippet'

C:\>



C:\>svn --version
svn, version 1.4.2 (r22196)
   compiled Nov  3 2006, 16:53:07

Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository access (RA) modules are available:

* ra_dav : Module for accessing a repository via WebDAV (DeltaV) protocol.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme

C:\>

quote & apostrophe chars inside comments in python blocks

Migrated issue, originally created by Anonymous

A simple template file:

<%
# it's a comment
%>

will raise a Syntax exception, because of the apostrophe character in the comment text. The same happens if there is a quotation mark.

-style comments outside python blocks are not affected.

Traceback:

Traceback (most recent call last):
  File "wsgiref/handlers.py", line 92, in run
  File "/usr/local/myapp/lib/myapp/wsgiapp.py", line 85, in application
    template = templatelookup.get_template(templateuri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 60, in get_template
    return self.__check(uri, self.__collection[uri])
  File "/usr/local/myapp/lib/mako/lookup.py", line 126, in __check
    return self.__load(template.filename, uri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 110, in __load
    self.__collection[uri] = Template(uri=uri, filename=posixpath.normpath(filename), lookup=self, module_filename=(self.modulename_callable is not None and self.modulename_callable(filename, uri) or None), **self.template_args)
  File "/usr/local/myapp/lib/mako/template.py", line 68, in __init__
    _compile_module_file(self, file(filename).read(), filename, path)
  File "/usr/local/myapp/lib/mako/template.py", line 186, in _compile_module_file
    node = Lexer(text, filename, input_encoding=template.input_encoding, preprocessor=template.preprocessor).parse()
  File "/usr/local/myapp/lib/mako/lexer.py", line 139, in parse
    if self.match_python_block():
  File "/usr/local/myapp/lib/mako/lexer.py", line 251, in match_python_block
    (text, end) = self.parse_until_text(r'%>')
  File "/usr/local/myapp/lib/mako/lexer.py", line 66, in parse_until_text
    raise exceptions.SyntaxException("Unmatched '%s'" % match.group(1), self.matched_lineno, self.matched_charpos, self.filename)
SyntaxException: Unmatched ''' in file '/usr/local/myapp/templates/bug.html' at line: 3 char: 5

lexer trips up on python code-intensive sections

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

im assuming the condition will usually, if not always, involve quotes. such as:

${foo and "|" or "bar"}

${"some text}"}

<% print "hi %>"%>

what we have to do is take the match sections that look for ${} and <% %>, and break them down to first match the ${ or <% symbol. then a new function will be called, using a signature like this:

   def match_python_to_str(self, *strings):

used like this:

   match = self.match(r'<%')
   if match:
       (text, matched) = self.match_python_to_str('%>')

or multiple:

   (text, matched) = self.match_python_to_str('|', '}')

the function performs submatches at the current match point, matching on quoted sections, i.e. ''', """, ', ", and consuming quotes in a balanced fashion. only at the "balance" points do we also try to match one of }, | or %>. the function then returns the full range of text corresponding to where it was first called vs. where it found an appropriate end character.

Make module_directory argument accept string template

Migrated issue, originally created by Anonymous

Make module_directory argument accept string template, so you can construct a template lookup like this:

from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['dir1', 'dir2'], module_directory='${directory}')

to generate module files in the same directory as the corresponding template file.

RichTraceback assumes Template was from a file

Migrated issue, originally created by Anonymous

I realized I had some old tests for exceptions.py that I never submitted -- turns out they don't work because RichTraceback assumes the error occured on a template that was read from a file.

i.e. it gets the filename to read the source code (incase html_error_template wants it). But in a case like:

Template("test")

there is no filename

======================================================================
ERROR: test the html_error_template
----------------------------------------------------------------------
Traceback (most recent call last):
  File "exceptions.py", line 22, in test_html_error_template
    html_error = exceptions.html_error_template().render()
  File "/beans/home/pjenvey/src/python/mako/lib/mako/template.py", line 109, in render
    return runtime._render(self, self.callable_, args, data)
  File "/beans/home/pjenvey/src/python/mako/lib/mako/runtime.py", line 287, in _render
    _render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data))
  File "/beans/home/pjenvey/src/python/mako/lib/mako/runtime.py", line 304, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/beans/home/pjenvey/src/python/mako/lib/mako/runtime.py", line 337, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0x83efeec", line 26, in render_body
  File "/beans/home/pjenvey/src/python/mako/lib/mako/exceptions.py", line 71, in __init__
    self.source = file(self.error.filename).read()
TypeError: coercing to Unicode: need string or buffer, NoneType found

<%def> and <%call> with unicode string as argument

Migrated issue, originally created by Anonymous

Can't use u"unicode strings" as arguments in <%def> and <%call> tags.
Tested with SVN revision 224.

## -*- coding: utf-8 -*-
<%def name="bello(foo, bar)">
<b>Foo:</b> ${ foo }<br>
<b>Bar:</b> ${ bar }<br>
</%def>
<%call expr="bello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP'">
</%call>

will raise

Traceback (most recent call last):
  File "wsgiref/handlers.py", line 92, in run
  File "/usr/local/myapp/lib/myapp/wsgiapp.py", line 87, in application
    template = templatelookup.get_template(templateuri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 68, in get_template
    return self.__load(srcfile, uri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 110, in __load
    self.__collection[uri] = Template(uri=uri, filename=posixpath.normpath(filename), lookup=self, module_filename=(self.modulename_callable is not None and self.modulename_callable(filename, uri) or None), **self.template_args)
  File "/usr/local/myapp/lib/mako/template.py", line 68, in __init__
    _compile_module_file(self, file(filename).read(), filename, path)
  File "/usr/local/myapp/lib/mako/template.py", line 186, in _compile_module_file
    node = Lexer(text, filename, input_encoding=template.input_encoding, preprocessor=template.preprocessor).parse()
  File "/usr/local/myapp/lib/mako/lexer.py", line 138, in parse
    if self.match_tag_start():
  File "/usr/local/myapp/lib/mako/lexer.py", line 188, in match_tag_start
    self.append_node(parsetree.Tag, keyword, attributes)
  File "/usr/local/myapp/lib/mako/lexer.py", line 83, in append_node
    node = nodecls(*args, **kwargs)
  File "/usr/local/myapp/lib/mako/parsetree.py", line 149, in __call__
    return type.__call__(cls, keyword, attributes, **kwargs)
  File "/usr/local/myapp/lib/mako/parsetree.py", line 262, in __init__
    self.code = ast.PythonCode(attributes['expr'], self.lineno, self.pos, self.filename)
  File "/usr/local/myapp/lib/mako/ast.py", line 39, in __init__
    expr = parse(code.lstrip(), "exec", lineno, pos, filename)
  File "/usr/local/myapp/lib/mako/ast.py", line 17, in parse
    return compiler_parse(code, mode)
  File "compiler/transformer.py", line 52, in parse
  File "compiler/transformer.py", line 129, in parsesuite
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 12: ordinal not in range(128)

and

## -*- coding: utf-8 -*-
<%def name="hello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')">
<b>Foo:</b> ${ foo }<br>
<b>Bar:</b> ${ bar }<br>
</%def>
${ hello() }

will raise

Traceback (most recent call last):
  File "wsgiref/handlers.py", line 92, in run
  File "/usr/local/myapp/lib/myapp/wsgiapp.py", line 87, in application
    template = templatelookup.get_template(templateuri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 68, in get_template
    return self.__load(srcfile, uri)
  File "/usr/local/myapp/lib/mako/lookup.py", line 110, in __load
    self.__collection[uri] = Template(uri=uri, filename=posixpath.normpath(filename), lookup=self, module_filename=(self.modulename_callable is not None and self.modulename_callable(filename, uri) or None), **self.template_args)
  File "/usr/local/myapp/lib/mako/template.py", line 68, in __init__
    _compile_module_file(self, file(filename).read(), filename, path)
  File "/usr/local/myapp/lib/mako/template.py", line 186, in _compile_module_file
    node = Lexer(text, filename, input_encoding=template.input_encoding, preprocessor=template.preprocessor).parse()
  File "/usr/local/myapp/lib/mako/lexer.py", line 138, in parse
    if self.match_tag_start():
  File "/usr/local/myapp/lib/mako/lexer.py", line 188, in match_tag_start
    self.append_node(parsetree.Tag, keyword, attributes)
  File "/usr/local/myapp/lib/mako/lexer.py", line 83, in append_node
    node = nodecls(*args, **kwargs)
  File "/usr/local/myapp/lib/mako/parsetree.py", line 149, in __call__
    return type.__call__(cls, keyword, attributes, **kwargs)
  File "/usr/local/myapp/lib/mako/parsetree.py", line 247, in __init__
    self.function_decl = ast.FunctionDecl("def " + name + ":pass", self.lineno, self.pos, self.filename)
  File "/usr/local/myapp/lib/mako/ast.py", line 166, in __init__
    expr = parse(code, "exec", lineno, pos, filename)
  File "/usr/local/myapp/lib/mako/ast.py", line 17, in parse
    return compiler_parse(code, mode)
  File "compiler/transformer.py", line 52, in parse
  File "compiler/transformer.py", line 129, in parsesuite
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 16: ordinal not in range(128)

Make module_directory argument accept string template

Migrated issue, originally created by Anonymous

Make module_directory argument accept string template, so you can construct a template lookup like this:

from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['dir1', 'dir2'], module_directory='${directory}')

to generate module files in the same directory as the corresponding template file.


Attachments: module_directory.patch

TurboGears dot notation support broken

Migrated issue, originally created by Anonymous

When using the !TurboGears plugin with dot notation template names, the plugin is unable to locate the template files.

ex:

    @expose(template="site.templates.foo")
    def foo(self):
        return dict(result='bar')

This should lookup site/templates/foo.mak, but fails.


Attachments: turbogears.patch

attribute parsing cant handle certain literals

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

such as:

  <%page args="x=7, y={}"/>
  <%page args="y=dict()"/>

the bug lies in mako.ast.FunctionGenerator.get_argument_expressions() and possibly ExpressionGenerator as well.

Def kw arg default value incorrect when negative

Migrated issue, originally created by Anonymous

Looks like the lexer is passing over the sign for keyword args with a negative default value.

Template:
<%def name="test(x=-1)">
${x}
</%def>

Compiled:
def test(x=1):
return render_test(context.locals_(__locals),x)

html_error_template doesn't handle non-ascii characters in source code

Migrated issue, originally created by Anonymous

Max Ischenko reported this here:

http://groups.google.com/group/pylons-discuss/browse_frm/thread/22e2af6ba7bbf778

I've attached a patch to convert the source code to unicode via the source code's magic encoding comment (like Myghty does -- in fact I pretty much ripped all of dairiki's code for this). In the rare case that the magic encoding comment doesn't describe the non-ascii characters correctly, the 'replace' error handler is used to add garbage in its place

But before we commit it, I think I would like to see an addition to the Template class.

Pylons currently does this in its exception handler to render the exception:

            return mako.exceptions.html_error_template().render()[610:-16]

With this patch, and Max's example, Pylons now causes a UnicodeEncodeError here:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-9: ordinal not in range(128)

What's needed for Pylons to render the html_error_template correctly (including the non-ascii characters showing up in the browser), it now has to do this:

            return mako.exceptions.html_error_template().render_unicode().encode(sys.getdefaultencoding(), 'htmlentityreplace')[610:-16]

Mike I may have asked you this before, but is there a reason why mako Templates don't have an 'encoding_errors' parameter to go along with 'output_encoding'?

If it did, we could have html_error_template set encoding_errors='htmlentitiyreplace'. Then anyone needing an html_error_template wouldn't have to be concerned with unicode at all -- they could simply render() (like Pylons is doing now) and always get back a sys.getdefaultencoding() encoded string with html entitys for non-defaultencoding characters. That is what Myghty did:

http://www.myghty.org/trac/changeset/2120

http://www.myghty.org/trac/changeset/2121

I remember arguing this implementation a little with dairiki when he did this, but it seemed to work out pretty well


Attachments: html_error_source_encoding.diff

Bizarre compile error

Migrated issue, originally created by Anonymous

This bug is exceedingly bizarre, so if you can't reproduce it at first, please don't give up! It comes and goes, and manifests itself in at least two different ways. I swear that I have gone though the following sequence of events:

  1. Run testcase, get error.
  2. Make a small change to the test.
  3. Rerun, and now it works as it should.
  4. Undo the change, returning the file exactly to the state it was in for step 1, and it still works fine!

The two error I have seen are:

  1. mako.exceptions.SyntaxException: Unterminated control keyword: 'for' in file ...
  2. Control lines (eg: "%for foo in bar:") are written directly to the output.

OK, I know that at this point you think I'm nuts, and I'm starting to think the same thing. But I'm at the end of my rope trying to create a simple, consistently reproducable testcase, so I'm just submitting the attached test, which is not consitent. In its current form, it demonstrates the error for me, but making almost any, seeminly unrelated change to the files will sometimes make the error go away.

OK, now you really think I'm nuts ;) But anyway, here it is.


Attachments: buggy.mako | buggy.py

"caller" should not be defined for a regular def call even if youre nested in a <%call>'ed def

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

easier to illustrate:

<%def name="a()">
%	if caller:
${ caller.body() }\
%	endif
AAA
${ b() }
</%def>

<%def name="b()">
%	if caller:
${ caller.body() }\
%	endif
BBB
${ c() }
</%def>

<%def name="c()">
%	if caller:
${ caller.body() }\
%	endif
CCC
</%def>

<%call expr="a()">
CALL
</%call>

result:

CALL AAA CALL BBB CALL CCC

should be:

CALL AAA BBB CCC

add CompositeLookup; templates need a "loaded_by" attribute

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

CompositeLookup will combine together one or more TemplateCollection items.

as Templates need to get a hold of the concrete TemplateLookup that loaded them, will need to add an additional attribute "loaded_by", which points to the actual lookup that loaded the template (and knows about its file location), in addition to the "lookup" attribute, which would point to the outermost lookup that is used to look up other templates (such as a CompositeLookup).

lexer/exception stuff

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

ex 1.

<%call expr="func('hi', 'there')%>
some content
</%call>

<%call expr="somethingelse()">
other content
</%call>

additionally, if the call to 'somethingelse' contains the wrong number of arguments, the exception formatting seems to place the error line on the previous <%call> tag. not sure if i can reproduce.

switch caching to use beaker instead of MyghtyUtils

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

ben has merged the latest myghtyutils code directly into Beaker and ive cleaned it up some more, so we are going to deprecate MyghtyUtils in favor of beaker. next mako release should require beaker instead of myghtyutils.

Handle windows path separator correctly

Migrated issue, originally created by Anonymous

Say there is a template file 'temp.html' in current directory, then this code will fail on windows:

t = Template(filename='temp.html', module_directory='.')

First of all, this is because the path to be used to store the compiled module file is calculated to be 'temp.html.py' (in template.py line 56), then posixpath.dirname(path)(template.py line 60) returns empty string, then util.verify_directory fails. I think in this case, util.verify_directory should return directly.

In addition, I've found that in template.py line 56, it uses os.path to assemble the path, but in line 60, it uses posixpath.dirname(path) to try to get the directory name, but on windows this would always return empty string, because the path separator on windows is '\'.
Use os.path to replace posixpath will be ok, but i find posixpath is used everywhere, why not use os.path? What's your opinion on this?


Attachments: path_seq.patch

unicode literals in template parts that get sent to the compiler

Migrated issue, originally created by DmitryL (@dds)

Given t = Template(u""" u"\u30c6\u30b9\u30c8" """) , render() fails when mako
tries to send that to compiler.parse because it tries to decode it as a string.

This issue comes up in practice when you have a template such as

"""
# -*- coding: utf-8 -*-
${ make_link(u"テスト", "http://www.google.co.jp/") }
"""

I have templates rendering correctly using the patch at

http://bosabosa.org/~dds/darcs/index.cgi?r=mako;a=commitdiff;h=20070131155105-1caef-e78b493d105e3baa6a865b07423e5f93382128f6.gz

but it is not a final patch and the test case above still fails with that patch.

Please confirm, thanks.

Make module_directory argument accept string template

Migrated issue, originally created by Anonymous

Make module_directory argument accept string template, so you can use a lookup like this:

from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['dir1', 'dir2'], module_directory='${directory}')

to generate module files in the same directory as the corresponding template file.

the module_directory's process bug

Migrated issue, originally created by Anonymous

the patch fixed this.

mylookup = TemplateLookup( directories=['./'], module_directory='./', 
                           output_encoding='gbk', default_filters=['decode.gbk'])
mytemplate = mylookup.get_template( 'table.txt' )
print mytemplate.render( title=['csdm', 'csmc', 'csz'] )

raise

OSError: [Errno 2] No such file or directory: '' 

Attachments: template.patch

Control structure parsing fails on DOS/Windows carriage returns.

Migrated issue, originally created by Anonymous

Taken from the docs (with Windows/DOS end-of-line formatting):

% if x==5:\r\n
    this is some output\r\n
% endif\r\n

This causes an exception in the templating engine. However, UNIX style end-of-lines (newline without a carriage return) work fine:

% if x==5:\n
    this is some output\n
% endif\n

I discovered this problem when creating the template on my Windows workstation and then SFTPing it to the server. My editor allows me to strip out the CRs when saving, but simplier Windows-based editors (like Notepad) won't be able to do this.

<%include file="${i}"/> broken as of r228

Migrated issue, originally created by Anonymous

We have the following template on PylonsHQ:

<%include file="/docs/${g.current_version}/install.html"/>
<%def name="title()">${parent.title()} - Install</%def>
<%def name="nav()">Install</%def>
<%inherit file="/layout.html"/>

As of r228, the include is broken with the error:

I keep getting this wack error
Module _layout_html:59 in render_body
>>  context.write(unicode(self.yui_grid()))
Module mako.runtime:200 in <lambda>
>>  return lambda *args, **kwargs:callable_(self.context, *args, **kwargs)
Module _layout_html:100 in render_yui_grid
>>  context.write(unicode(caller.body()))
Module mako.runtime:91 in call_wno_caller
>>  return callable_(*args, **kwargs)
Module _layout_html:51 in body
>>  context.write(unicode(next.body()))
Module mako.runtime:200 in <lambda>
>>  return lambda *args, **kwargs:callable_(self.context, *args, **kwargs)
Module _install_index_html:26 in render_body
>>  runtime._include_file(context, u'/docs/' + g.current_version + u'/install.html', _template_uri)
NameError: global name 'g' is not defined

The .py generated for this file as of r228 is:

def render_body(context,**pageargs):
    __locals = dict(pageargs=pageargs)
    foo = context.get('foo', UNDEFINED)
    # SOURCE LINE 1
    runtime._include_file(context, u'/docs/' + g.current_version + u'/install.html', _template_uri)

Whereas before it would do something along the lines of:

def render_body(context,**pageargs):
    __locals = dict(pageargs=pageargs)
    g = context.get('g', UNDEFINED)
    # SOURCE LINE 1
    runtime._include_file(context, u'/docs/' + g.current_version + u'/install.html', _template_uri)

I've attached a test for this and a patch that fixes it. You probably have a better version of the patch -- what it generates now is:

def render_body(context,**pageargs):
    __locals = dict(pageargs=pageargs)
    foo = context.get('foo', UNDEFINED)
    g = context.get('g', UNDEFINED)
    # SOURCE LINE 1
    runtime._include_file(context, u'/docs/' + g.current_version + u'/install.html', _template_uri)

I suspect the 'foo' PythonCode business is some test code you left around in the r228 commit


Attachments: include_fix_r240.diff

allow usage of non-ascii bytestring literals in templates

Migrated issue, originally created by Anonymous

The mako template parser has a problem, or a weirdness, depending on your view. Basically it is not possible to compile any template that contains non-ascii characters inside the ${} code. The problem traces back to python's built-in compiler inability to compile out-of-ascii unicode source. To fix it some kind of encoding-juggling inside ast.py (the 'parse' function?) would be needed as well as adding a #-*- prefix to the code being compiled there. Alas, I haven't been able to fix this myself (mysterious body snatcher exceptions pop out) neither have I enough time to work on it but I'm sure you get the idea.

To replicate the problem, just compile "${f('\u0142')}" as a mako template.

I should add that the problem is serious, at least for us and a showstopper for mako adoption in our project.


Attachments: alternate_unicode.patch

Cannot install mako 0.1 with setuptools

Migrated issue, originally created by Anonymous

Hi, there is a problem with installing Mako with setuptools.
See below for the stacktrace.
Looks like the problem is caused by the line prune contrib/ in source:mako/trunk/MANIFEST.in

C:\>easy_install Mako
Searching for Mako
Reading http://www.python.org/pypi/Mako/
Reading http://www.makotemplates.org/
Reading http://www.python.org/pypi/Mako/0.1.0
Best match: Mako 0.1.0
Downloading http://cheeseshop.python.org/packages/source/M/Mako/Mako-0.1.0.tar.gz#md5=50adfdf4f407b9a9d3dfa86aeafe503c
Processing Mako-0.1.0.tar.gz
Running Mako-0.1.0\setup.py -q bdist_egg --dist-dir c:\docume~1\mast\ustawi~1\temp\easy_install-hitznt\Mako-0.1.0\egg-dist-tmp-7mjvk9
Traceback (most recent call last):
  File "c:\Python25\Scripts\easy_install-script.py", line 8, in <module>
    load_entry_point('setuptools==0.6c3', 'console_scripts', 'easy_install')()
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 1588, in main
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 1577, in with_ei_usage
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 1592, in <lambda>
  File "c:\Python25\lib\distutils\core.py", line 151, in setup
    dist.run_commands()
  File "c:\Python25\lib\distutils\dist.py", line 974, in run_commands
    self.run_command(cmd)
  File "c:\Python25\lib\distutils\dist.py", line 994, in run_command
    cmd_obj.run()
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 211, in run
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 446, in easy_install
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 471, in install_item
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 655, in install_eggs
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 930, in build_and_install
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\easy_install.py", line 919, in run_setup
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\sandbox.py", line 27, in run_setup
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\sandbox.py", line 63, in run
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\sandbox.py", line 29, in <lambda>
  File "setup.py", line 48, in <module>
  File "c:\Python25\lib\distutils\core.py", line 151, in setup
    dist.run_commands()
  File "c:\Python25\lib\distutils\dist.py", line 974, in run_commands
    self.run_command(cmd)
  File "c:\Python25\lib\distutils\dist.py", line 994, in run_command
    cmd_obj.run()
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\bdist_egg.py", line 167, in run
  File "c:\Python25\lib\distutils\cmd.py", line 333, in run_command
    self.distribution.run_command(command)
  File "c:\Python25\lib\distutils\dist.py", line 994, in run_command
    cmd_obj.run()
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\egg_info.py", line 171, in run
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\egg_info.py", line 252, in find_sources
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\egg_info.py", line 308, in run
  File "c:\Python25\lib\site-packages\setuptools-0.6c3-py2.5.egg\setuptools\command\sdist.py", line 157, in read_template
  File "c:\Python25\lib\distutils\command\sdist.py", line 335, in read_template
    self.filelist.process_template_line(line)
  File "c:\Python25\lib\distutils\filelist.py", line 130, in process_template_line
    (action, patterns, dir, dir_pattern) = self._parse_template_line(line)
  File "c:\Python25\lib\distutils\filelist.py", line 113, in _parse_template_line
    dir_pattern = convert_path(words[1])
  File "c:\Python25\lib\distutils\util.py", line 140, in convert_path
    raise ValueError, "path '%s' cannot end with '/'" % pathname
ValueError: path 'contrib/' cannot end with '/'

C:\>

start_form should result in lower case post, not upper case POST

Migrated issue, originally created by Anonymous

The start_form should result in lower case post, not upper case POST. HTML Tidy is complaining that the POST in the form attribute method should be in lower case:

Warning: <form> attribute value "POST" must be lower case for XHTML

wrap directories in a list if it is a string

Migrated issue, originally created by Anonymous

Thu Jan 11 19:38:52 JST 2007 [email protected]

  • If template directories is a string, wrap it as a list.
    diff -rN -b -u old-mako/lib/mako/lookup.py new-mako/lib/mako/lookup.py
    --- old-mako/lib/mako/lookup.py 2007-01-11 19:39:40.000000000 +0900
    +++ new-mako/lib/mako/lookup.py 2007-01-11 19:39:40.000000000 +0900
    @@ -38,6 +38,8 @@

class TemplateLookup(TemplateCollection):
def init(self, directories=None, module_directory=None, filesystem_checks=True, collection_size=-1, format_exceptions=False, error_handler=None, output_encoding=None, cache_type=None, cache_dir=None):

  •    if isinstance(directories, basestring):
    
  •        directories = [directories]
       self.directories = [posixpath.normpath(d) for d in directories or []]
       self.module_directory = module_directory
       self.filesystem_checks = filesystem_checks
    

Attachments: mako.templates.patch

More descriptive error message when missing required arguments for a template

Migrated issue, originally created by Anonymous

When a template takes an argument like <%page args="x" /> and x is not provided, it yields the following error:

exceptions.TypeError: render_body() takes exactly 2 arguments (1 given)

I think we should add some more explicit reason of what's missing. Like "page.html takes exactly 2 arguments (1 given). Missing required argument x."

"self" is not defined in a NameSpace unless a method is called

Migrated issue, originally created by Anonymous

Hello,

I'm not sure if this is a bug, or if it's me not coding properly, but I believe it is strange enough to let you decide. If this is normal behaviour, please ignore this ticket :)

Inside a namespace, a exec "self.something()" fails unless a "real" call to self is made self.anything() before the exec statement.

Steps to reproduce:

drax@billybox[~/svn/server/libs]$ cat ../wui/xul/test
<%
    functions = ["test1", "test2"]
    self.do_nothing()
    for f in functions:
        exec "self.%s()" % f
%>
<%def name="do_nothing()"></%def>
<%def name="test1()">Hello</%def>
<%def name="test2()">World</%def>
drax@billybox[~/svn/server/libs]$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mako.template import Template
>>> t = Template(filename='../wui/xul/test')
>>> t.render()
'HelloWorld\n\n\n\n'
>>> #### I edit the file, and comment out the call to self.do_nothing ####
>>> t = Template(filename='../wui/xul/test')
>>> t.render()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "./libs/mako/template.py", line 98, in render
  File "./libs/mako/runtime.py", line 259, in _render
  File "./libs/mako/runtime.py", line 277, in _render_context
  File "./libs/mako/runtime.py", line 310, in _exec_template
  File "___wui_xul_test", line 18, in render_body
  File "<string>", line 1, in ?
NameError: name 'self' is not defined
>>>

Hope this helped.

(if you need more precisions, don't hesitate to contact me on drax [at] sweon.net)

Enhance filters.Decode unicode handling

Migrated issue, originally created by Anonymous

Right now, even when using filters.decode.utf8, it's possible to get str object after filter - when using some object which is not instance of basestring.

For example when using Mako with Django oldforms, Mako will crash on something like

${ form['country'] }

Because form['country'] is a !FormFieldWrapper instance, and it doesn't get decoded by filter.decode.utf8.

So here's the small patch that will decode using given encoding even not basestring objects after converting object to string using str().


Attachments: decode.diff

add "export_module=True" flag to <%page>

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

this flag would allow regular python functions in the <%! %> section of a template to be part of the returned namespace. particularly handy for a template that defines some filtering functions.

e.g.

 # somefile.html
 <%page export_module="True"/>

  <%!
     def myfilter(text):
          return highlighter.highlight(text)
  %>




  <%namespace file="somefile.html" import="*"/>
  ${"this is some text" | myfilter}

<%! import foo.bar %> <% foo.bar.bazz() %> raises AttributeError

Migrated issue, originally created by Anonymous

<%! import myapp.utils %>
<% bar = myapp.utils.Sequence() %>

raises:

  File "/tmp/myapp/templates/bar.html.py", line 19, in render_body
    bar = myapp.utils.Sequence()
AttributeError: 'Undefined' object has no attribute 'utils'

looking into the compiled template:

import myapp.utils 

def render_body(context,**pageargs):
    __locals = dict(pageargs=pageargs)
    myapp = context.get('myapp', UNDEFINED)
    bar = myapp.utils.Sequence() 
    __locals.update(dict([(k, locals()[k]) for k in ['bar'] if k in locals()]))
    return ''

i think

myapp = context.get('myapp', UNDEFINED)

should not be there...

SVN: r235

(can be worked over with the "from something import object, anotherobject" form, and sometimes "import myapp" then "import myapp.utils" solved this yesterday, but now i can't reproduce that=:)

use lookup's parameters for Template instance

Migrated issue, originally created by Anonymous

when you create a Template object, the "output_encoding" and "default_filters" parameters can automatically assigned from the lookup parameter.

we patch it because every Template object must set those parameters.

PS: another parameters can be added also.

eg:

lookup = TemplateLookup(directories='/txt'],output_encoding='gbk',
                        default_filters=['decode.gbk'],
                        module_directory='/txt' )
mytemplate = Template("""<%include file="t.txt"/>hello world!""",lookup=lookup )# output_encoding = 'gbk',default_filters=['decode.gbk'] )

take care above example, without the patch ,zhe comment line must be uncommeted.


Attachments: lookupdefault.patch

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.