[PATCH 1 of 5] templatekw: use decorator to mark a function as template keyword
Gregory Szorc
gregory.szorc at gmail.com
Sat Dec 26 11:47:54 CST 2015
> On Dec 26, 2015, at 04:23, FUJIWARA Katsunori <foozy at lares.dti.ne.jp> wrote:
>
> At Thu, 24 Dec 2015 21:54:17 +0900,
> Yuya Nishihara wrote:
>>
>>> 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().
>
> In fact, once I wrote decorators delaying registration until uisetup()
> or extsetup() in 'cmdutil.command' style.
Relatedly, cmdutil and commands have a cycle-like issue that is making it difficult to convert to absolute import. I've been contemplating the idea of adding registrar.py or similar to hold registrations for commands, templates, revsets, etc.
>
> But I discarded them, because (1) 'cmdutil.command' style isn't
> reasonable to hide internal implementation (like
> _statuscallers/_existingcallers of fileset or safesymbols of revset)
> and (2) convert extension adds template filters in module top level
> code path :-)
>
> I'll post revised ones.
>
>>> +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.
>
> OK, I'll try to define generic decorator to centralize similar logic
> into it.
>
> ----------------------------------------------------------------------
> [FUJIWARA Katsunori] foozy at lares.dti.ne.jp
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
More information about the Mercurial-devel
mailing list