# HG changeset patch # User Martin von Zweigbergk # Date 1596732610 25200 # Node ID 4aa484efc926a7aa9a4fa1e6feaf5ebae46f0f10 # Parent 6e6fe826ba69d2bb138573fa0ec45431a000cb2e templater: add exception-raising version of open_template() I'm about to add another caller of `open_template()` (in the template loader). That caller will want to get exceptions instead of `(None, None)` if the template doesn't exist. This patch therefore changes `open_template()` to raise exceptions and adds a new `try_open_template()` that returns the `(None, None)` value. Differential Revision: https://phab.mercurial-scm.org/D8905 diff -r 6e6fe826ba69 -r 4aa484efc926 mercurial/debugcommands.py --- a/mercurial/debugcommands.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/debugcommands.py Thu Aug 06 09:50:10 2020 -0700 @@ -1672,7 +1672,7 @@ fm.write(b'templatedirs', b'checking templates (%s)...\n', p or b'') fm.condwrite(not p, b'', _(b" no template directories found\n")) if p: - (m, fp) = templater.open_template(b"map-cmdline.default") + (m, fp) = templater.try_open_template(b"map-cmdline.default") if m: # template found, check if it is working err = None diff -r 6e6fe826ba69 -r 4aa484efc926 mercurial/formatter.py --- a/mercurial/formatter.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/formatter.py Thu Aug 06 09:50:10 2020 -0700 @@ -600,9 +600,9 @@ # perhaps a stock style? if not os.path.split(tmpl)[0]: - (mapname, fp) = templater.open_template( + (mapname, fp) = templater.try_open_template( b'map-cmdline.' + tmpl - ) or templater.open_template(tmpl) + ) or templater.try_open_template(tmpl) if mapname: return mapfile_templatespec(topic, mapname, fp) diff -r 6e6fe826ba69 -r 4aa484efc926 mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/hgweb/hgweb_mod.py Thu Aug 06 09:50:10 2020 -0700 @@ -78,7 +78,7 @@ locations = (os.path.join(style, b'map'), b'map-' + style, b'map') for location in locations: - mapfile, fp = templater.open_template(location, path) + mapfile, fp = templater.try_open_template(location, path) if mapfile: return style, mapfile, fp diff -r 6e6fe826ba69 -r 4aa484efc926 mercurial/logcmdutil.py --- a/mercurial/logcmdutil.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/logcmdutil.py Thu Aug 06 09:50:10 2020 -0700 @@ -628,9 +628,9 @@ mapfile = style fp = None if not os.path.split(mapfile)[0]: - (mapname, fp) = templater.open_template( + (mapname, fp) = templater.try_open_template( b'map-cmdline.' + mapfile - ) or templater.open_template(mapfile) + ) or templater.try_open_template(mapfile) if mapname: mapfile = mapname return formatter.mapfile_templatespec(b'changeset', mapfile, fp) diff -r 6e6fe826ba69 -r 4aa484efc926 mercurial/templater.py --- a/mercurial/templater.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/templater.py Thu Aug 06 09:50:10 2020 -0700 @@ -1095,17 +1095,18 @@ templatepath = templatedir() if templatepath is not None or os.path.isabs(name): f = os.path.join(templatepath, name) - try: - return f, open(f, mode='rb') - except EnvironmentError: - return None, None + return f, open(f, mode='rb') else: name_parts = pycompat.sysstr(name).split('/') package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1]) - try: - return ( - name, - resourceutil.open_resource(package_name, name_parts[-1]), - ) - except (ImportError, OSError): - return None, None + return ( + name, + resourceutil.open_resource(package_name, name_parts[-1]), + ) + + +def try_open_template(name, templatepath=None): + try: + return open_template(name, templatepath) + except (EnvironmentError, ImportError): + return None, None