[PATCH 1 of 5] templatekw: use decorator to mark a function as template keyword

Yuya Nishihara yuya at tcha.org
Thu Dec 24 06:54:17 CST 2015


On Thu, 24 Dec 2015 01:46:37 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
> # Date 1450888882 -32400
> #      Thu Dec 24 01:41:22 2015 +0900
> # Node ID e49528b9e652dc0e5d2543ea34de3068f78ae9ed
> # Parent  fe376159a58d9b3d748b669ac011b0eed0346fea
> templatekw: use decorator to mark a function as template keyword

> + at templatekw.templatekeyword('svnrev')
>  def kwsvnrev(repo, ctx, **args):
> -    """:svnrev: String. Converted subversion revision number."""
> +    """String. Converted subversion revision number."""
>      return kwconverted(ctx, 'svnrev')
>  
> + at templatekw.templatekeyword('svnpath')
>  def kwsvnpath(repo, ctx, **args):
> -    """:svnpath: String. Converted subversion revision project path."""
> +    """String. Converted subversion revision project path."""
>      return kwconverted(ctx, 'svnpath')
>  
> + at templatekw.templatekeyword('svnuuid')
>  def kwsvnuuid(repo, ctx, **args):
> -    """:svnuuid: String. Converted subversion revision repository identifier."""
> +    """String. Converted subversion revision repository identifier."""
>      return kwconverted(ctx, 'svnuuid')
>  
> -def extsetup(ui):
> -    templatekw.keywords['svnrev'] = kwsvnrev
> -    templatekw.keywords['svnpath'] = kwsvnpath
> -    templatekw.keywords['svnuuid'] = kwsvnuuid

Extensions shouldn't change the keywords table just by importing. It should be
delayed until uisetup() or extsetup().

> +keywords = {}
>  
> +def templatekeyword(name):
> +    """Return a decorator for template keyword function
> +
> +    'name' argument is the keyword name.
> +    """
> +    def decorator(func):
> +        keywords[name] = func
> +        if func.__doc__:
> +            func.__doc__ = ":%s: %s" % (name, func.__doc__.strip())
> +        return func
> +    return decorator

Perhaps, we'll need a decorator that can handle common cases? And the hgweb
will eventually use it.


More information about the Mercurial-devel mailing list