[PATCH V2] commands: use a class as decorator to build table incrementally

Adrian Buehlmann adrian at cadifra.com
Wed May 11 16:48:15 CDT 2011


On 2011-05-11 20:34, Adrian Buehlmann wrote:
> # HG changeset patch
> # User Adrian Buehlmann <adrian at cadifra.com>
> # Date 1305097807 -7200
> # Node ID 7c9c95bbfb3bab2663f0512867dd9115753caede
> # Parent  c97d8485b5fa46c94b8afcfab0cde97629ba4086
> commands: use a class as decorator to build table incrementally
> 
> this allows to define the table entries near the command function
> 
> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py
> +++ b/mercurial/cmdutil.py
> @@ -1251,3 +1251,23 @@
>          raise util.Abort(_("empty commit message"))
>  
>      return text
> +
> +def command(table):
> +    '''returns a class bound to table that can be used as a decorator
> +    for populating that command table'''
> +
> +    class cmd(object):
> +        '''decorator for populating a command table'''
> +        def __init__(self, name, options, synopsis=None):
> +            self.name = name
> +            self.options = options
> +            self.synopsis = synopsis
> +
> +        def __call__(self, func):
> +            if self.synopsis:
> +                table[self.name] = func, self.options, self.synopsis
> +            else:
> +                table[self.name] = func, self.options
> +            return func
> +
> +    return cmd
> diff --git a/mercurial/commands.py b/mercurial/commands.py
> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -10,15 +10,129 @@
>  from i18n import _, gettext
>  import os, re, sys, difflib, time, tempfile
>  import hg, scmutil, util, revlog, extensions, copies, error, bookmarks
> -import patch, help, url, encoding, templatekw, discovery
> +import patch, help, url, encoding, templatekw, discovery, cmdutil
>  import archival, changegroup, cmdutil, sshserver, hbisect, hgweb, hgweb.server
                                 ^^^^^^^
Bah. Stupid me. cmdutil is already there.

Will resend.


More information about the Mercurial-devel mailing list