templater: make templatepaths() return a single path, or None
authorMartin von Zweigbergk <martinvonz@google.com>
Tue, 21 Jul 2020 13:11:49 -0700
changeset 45199 91aa9bba3dc9
parent 45198 e2ec36167151
child 45200 4e9b39033d3f
templater: make templatepaths() return a single path, or None The function returns either a singleton list or an empty list, so it makes more sense to return a value or None. The plural in the name also doesn't make sense, but `templatepath()` is already taken, so I renamed it to `templatedir()` instead. Differential Revision: https://phab.mercurial-scm.org/D8786
mercurial/debugcommands.py
mercurial/hgweb/hgwebdir_mod.py
mercurial/hgweb/webcommands.py
mercurial/templater.py
--- a/mercurial/debugcommands.py	Tue Jul 21 13:05:37 2020 -0700
+++ b/mercurial/debugcommands.py	Tue Jul 21 13:11:49 2020 -0700
@@ -1668,8 +1668,8 @@
     fm.data(re2=bool(util._re2))
 
     # templates
-    p = templater.templatepaths()
-    fm.write(b'templatedirs', b'checking templates (%s)...\n', b' '.join(p))
+    p = templater.templatedir()
+    fm.write(b'templatedirs', b'checking templates (%s)...\n', p)
     fm.condwrite(not p, b'', _(b" no template directories found\n"))
     if p:
         m = templater.templatepath(b"map-cmdline.default")
--- a/mercurial/hgweb/hgwebdir_mod.py	Tue Jul 21 13:05:37 2020 -0700
+++ b/mercurial/hgweb/hgwebdir_mod.py	Tue Jul 21 13:11:49 2020 -0700
@@ -414,7 +414,7 @@
                     fname = req.qsparams[b'static']
                 static = self.ui.config(b"web", b"static", untrusted=False)
                 if not static:
-                    tp = self.templatepath or templater.templatepaths()
+                    tp = self.templatepath or templater.templatedir()
                     if isinstance(tp, bytes):
                         tp = [tp]
                     static = [os.path.join(p, b'static') for p in tp]
--- a/mercurial/hgweb/webcommands.py	Tue Jul 21 13:05:37 2020 -0700
+++ b/mercurial/hgweb/webcommands.py	Tue Jul 21 13:11:49 2020 -0700
@@ -1319,7 +1319,7 @@
     # readable by the user running the CGI script
     static = web.config(b"web", b"static", untrusted=False)
     if not static:
-        tp = web.templatepath or templater.templatepaths()
+        tp = web.templatepath or templater.templatedir()
         if isinstance(tp, bytes):
             tp = [tp]
         static = [os.path.join(p, b'static') for p in tp]
--- a/mercurial/templater.py	Tue Jul 21 13:05:37 2020 -0700
+++ b/mercurial/templater.py	Tue Jul 21 13:11:49 2020 -0700
@@ -800,10 +800,10 @@
 
 
 def stylelist():
-    paths = templatepaths()
-    if not paths:
+    path = templatedir()
+    if not path:
         return _(b'no templates found, try `hg debuginstall` for more info')
-    dirlist = os.listdir(paths[0])
+    dirlist = os.listdir(path)
     stylelist = []
     for file in dirlist:
         split = file.split(b".")
@@ -823,7 +823,7 @@
         )
 
     base = os.path.dirname(mapfile)
-    conf = config.config(includepaths=templatepaths())
+    conf = config.config(includepaths=[templatedir()])
     conf.read(mapfile, remap={b'': b'templates'})
 
     cache = {}
@@ -837,15 +837,13 @@
 
         # fallback check in template paths
         if not os.path.exists(path):
-            for p in templatepaths():
-                p2 = util.normpath(os.path.join(p, val))
-                if os.path.isfile(p2):
-                    path = p2
-                    break
+            p2 = util.normpath(os.path.join(templatedir(), val))
+            if os.path.isfile(p2):
+                path = p2
+            else:
                 p3 = util.normpath(os.path.join(p2, b"map"))
                 if os.path.isfile(p3):
                     path = p3
-                    break
 
         cache, tmap, aliases = _readmapfile(path)
 
@@ -1045,18 +1043,17 @@
         return stream
 
 
-def templatepaths():
-    '''return locations used for template files.'''
+def templatedir():
+    '''return the directory used for template files, or None.'''
     path = os.path.normpath(os.path.join(resourceutil.datapath, b'templates'))
-    return [path] if os.path.isdir(path) else []
+    return path if os.path.isdir(path) else None
 
 
 def templatepath(name):
     '''return location of template file. returns None if not found.'''
-    for p in templatepaths():
-        f = os.path.join(p, name)
-        if os.path.exists(f):
-            return f
+    f = os.path.join(templatedir(), name)
+    if f and os.path.exists(f):
+        return f
     return None
 
 
@@ -1070,7 +1067,7 @@
     """
 
     if paths is None:
-        paths = templatepaths()
+        paths = [templatedir()]
     elif isinstance(paths, bytes):
         paths = [paths]