Re: [PATCH] Adds template overriding to dotsmug

0 views
Skip to first unread message

James Carroll

unread,
Mar 5, 2009, 5:03:11 PM3/5/09
to smug...@googlegroups.com
bump?

Andrew said he didn't get this last one....

James

On Thu, Mar 5, 2009 at 1:50 PM, James Carroll <jlc@sage> wrote:
> From: James L. Carroll <jlca...@gmail.com>
>
> With this patch, you can now add lines to the .smug file
> such as:
>
> template templatename.html
>
> and it will over-ride the base template. Currently this
> only works for the present filter but will be trivial to
> add for others later. Eventually perhaps they should be
> nested, so that this code only shows up in one place.
> ---
>  smug/dotsmug.py |   17 ++++++++++++++++-
>  smug/filters.py |    4 +++-
>  smug/load.py    |   11 ++++++++---
>  3 files changed, 27 insertions(+), 5 deletions(-)
>
> diff --git a/smug/dotsmug.py b/smug/dotsmug.py
> index e2edbfd..20e35a3 100644
> --- a/smug/dotsmug.py
> +++ b/smug/dotsmug.py
> @@ -34,6 +34,14 @@ class Config(object):
>                 return index
>         return None
>
> +    def template(self):
> +        """Find the over-ride template in the smug file"""
> +        for dotsmug in self.dotsmugs:
> +            template = dotsmug.template
> +            if template is not None:
> +                return template
> +        return None
> +
>     def default_mimetype(self):
>         """Find the default mimetype."""
>         for dotsmug in self.dotsmugs:
> @@ -103,6 +111,7 @@ class DotSmug(object):
>
>         self.directory_index = None
>         self.default_mimetype = None
> +        self.template = None
>         self.convert = []
>
>         self.parse()
> @@ -110,7 +119,7 @@ class DotSmug(object):
>     def parse(self):
>         """Parse the .smug file."""
>
> -        valid_directives = ('convert', 'directory_index', 'default_mimetype')
> +        valid_directives = ('convert', 'directory_index', 'default_mimetype','template')
>
>         for line in self.tokenizer:
>             if not line:
> @@ -167,6 +176,12 @@ class DotSmug(object):
>
>         self.directory_index = line[1]
>
> +    def parse_template(self, line):
> +        """Parses the template directive."""
> +        if len(line) != 2:
> +            raise DotSmugError('wrong number of arguments to template.')
> +        self.template = line[1]
> +
>     def parse_default_mimetype(self, line):
>         """Parse the default_mimetype directive."""
>
> diff --git a/smug/filters.py b/smug/filters.py
> index 061cc84..4a3fb8b 100644
> --- a/smug/filters.py
> +++ b/smug/filters.py
> @@ -76,7 +76,9 @@ def present(file, **kwds):
>     """Render a Django template with the content as a context variable."""
>
>     from django.conf import settings
> -    template = kwds.get('template', config.basetemplate)
> +    template = kwds.get('template')
> +    if not template:
> +        template = config.basetemplate
>     context_dict = kwds.get('context_dict', {})
>     basepath = kwds['basepath']
>     request = kwds['request']
> diff --git a/smug/load.py b/smug/load.py
> index e4fadb8..64ea3ec 100644
> --- a/smug/load.py
> +++ b/smug/load.py
> @@ -82,7 +82,9 @@ def get_source(path, branch, repo):
>         source_path = path
>         filter = None
>
> -    return source_file, filter
> +    filter_args = {'template': dotsmug.template()}
> +
> +    return source_file, filter, filter_args
>
>
>  def get_content(request_path, branch, repo, raw=False, **filter_args):
> @@ -283,7 +285,8 @@ class FilteredFile(CachedFile):
>         return SourceFile(self.sourcepath, self.branch, self.repo)
>
>     def retrieve(self):
> -        source_file, filter = get_source(self.path, self.branch, self.repo)
> +        source_file, filter, filter_args = get_source(self.path, self.branch,
> +                self.repo)
>         if not source_file.exists():
>             return
>
> @@ -309,7 +312,9 @@ class FilteredFile(CachedFile):
>
>         if filter:
>             # TODO: allow filters to be chained (and only cache the last one)
> -            filter(self, **self.filter_args)
> +            kwds = self.filter_args.copy()
> +            kwds.update(filter_args)
> +            filter(self, **kwds)
>
>
>  if __name__ == '__main__':
> --
> 1.6.0.4
>
>

--
"And very early in the morning
the first day of the week,
they came unto the sepulchre
at the rising of the sun..." (Mark 16:2)

Web: http://james.jlcarroll.net

James Carroll

unread,
Mar 5, 2009, 5:27:17 PM3/5/09
to smug...@googlegroups.com, James L. Carroll

Andrew McNabb

unread,
Mar 5, 2009, 5:44:38 PM3/5/09
to smug...@googlegroups.com
On Thu, Mar 05, 2009 at 03:27:17PM -0700, James Carroll wrote:
>
> With this patch, you can now add lines to the .smug file
> such as:
>
> template templatename.html
>
> and it will over-ride the base template. Currently this
> only works for the present filter but will be trivial to
> add for others later. Eventually perhaps they should be
> nested, so that this code only shows up in one place.

Thanks for the patch. I've applied it with a few minor changes. Let me
know if I broke anything.

Thanks.

--
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55 8012 AB4D 6098 8826 6868

Reply all
Reply to author
Forward
0 new messages