[PATCH v3] releasenotes: add custom admonitions support for release notes

Yuya Nishihara yuya at tcha.org
Mon Jul 10 09:22:29 EDT 2017


On Sun, 09 Jul 2017 19:04:33 +0200, Rishabh Madan wrote:
> # HG changeset patch
> # User Rishabh Madan <rishabhmadan96 at gmail.com>
> # Date 1499619850 -7200
> #      Sun Jul 09 19:04:10 2017 +0200
> # Node ID 5f22d3d43d36d46cea98c86b2a49eee2d323fd9e
> # Parent  4672db164c986da4442bd864cd044512d975c3f2
> releasenotes: add custom admonitions support for release notes

Generally looks good. A couple of nits follow.

> --- a/hgext/releasenotes.py	Sat Jun 24 15:29:42 2017 -0700
> +++ b/hgext/releasenotes.py	Sun Jul 09 19:04:10 2017 +0200
> @@ -20,6 +20,7 @@
>  
>  from mercurial.i18n import _
>  from mercurial import (
> +    config,
>      error,
>      minirst,
>      registrar,
> @@ -111,9 +112,25 @@
>                  self.addnontitleditem(section, paragraphs)
>  
>  class releasenotessections(object):
> -    def __init__(self, ui):
> -        # TODO support defining custom sections from config.
> -        self._sections = list(DEFAULT_SECTIONS)
> +    def __init__(self, ui, repo=None):
> +        if repo:
> +            custom_sections = getcustomadmonitions(repo)
> +            if custom_sections:
> +                self._sections = custom_sections
> +                for default_name, default_value in DEFAULT_SECTIONS:
> +                    duplicate_key = False
> +                    for name, value in custom_sections:
> +                        if name == default_name:
> +                            duplicate_key = True
> +                            break
> +                    if duplicate_key:
> +                        continue
> +                    else:
> +                        self._sections.append((default_name, default_value))
> +            else:
> +                self._sections = list(DEFAULT_SECTIONS)
> +        else:
> +            self._sections = list(DEFAULT_SECTIONS)

You can use a dict (or util.sortdict() if the order matters) to deduplicate
items.

  sections = dict(DEFAULT_SECTIONS)
  sections.update(getcustomadmonitions(repo))
  self._sections = list(sections.iteritems())

> +def getcustomadmonitions(repo):
> +    custom_sections = list()
> +    ctx = repo['.']
> +    p = config.config()
> +    repo = ctx.repo()
> +
> +    def read(f, sections=None, remap=None):
> +        if f in ctx:
> +            data = ctx[f].data()
> +            p.parse(f, data, sections, remap, read)
> +            sectiondict = p['sections']
> +            sectionlist = list()
> +            for key, value in sectiondict.iteritems():
> +                temp = (key, value)
> +                sectionlist.append(temp)
> +            return sectionlist

Since read() is the callback to handle %include syntax, it should do just
p.parse() and nothing else.

> +        else:
> +            return

And probably better to raise an error if an %include file not found.
subrepo.state() has a good example.

> +    if '.hgreleasenotes' in ctx:
> +        custom_sections = read('.hgreleasenotes')
> +    return custom_sections


More information about the Mercurial-devel mailing list