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

Adrian Buehlmann adrian at cadifra.com
Wed May 11 11:35:10 CDT 2011


On 2011-05-11 18:13, Adrian Buehlmann wrote:
> # HG changeset patch
> # User Adrian Buehlmann <adrian at cadifra.com>
> # Date 1305097807 -7200
> # Node ID 8b331aca1943f7840591dd3289e55f99ba164375
> # Parent  c97d8485b5fa46c94b8afcfab0cde97629ba4086
> commands: use a class as decorator to build table incrementally
> 
> this allows to define the table entries near the command function

FWIW, I haven't found any noticeable slowdown of the command line.

> diff --git a/mercurial/commands.py b/mercurial/commands.py
> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -17,8 +17,134 @@
>  import dagparser, context, simplemerge
>  import random, setdiscovery, treediscovery, dagutil
>  
> +table = {}
> +
> +class command(object):
> +    '''decorator for populating the 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

In case you're wondering: this exactly reproduces the existing behavoir,
which the current code depends on elsewhere.



More information about the Mercurial-devel mailing list