[Updated] D8786: templater: make templatepaths() return a single path, or None

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Jul 22 12:18:13 UTC 2020


Closed by commit rHG91aa9bba3dc9: templater: make templatepaths() return a single path, or None (authored by martinvonz).
This revision was automatically updated to reflect the committed changes.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D8786?vs=22034&id=22038

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8786/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8786

AFFECTED FILES
  mercurial/debugcommands.py
  mercurial/hgweb/hgwebdir_mod.py
  mercurial/hgweb/webcommands.py
  mercurial/templater.py

CHANGE DETAILS

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -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]
 
diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -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]
diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -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]
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -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")



To: martinvonz, #hg-reviewers, pulkit
Cc: mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20200722/9f73345c/attachment-0002.html>


More information about the Mercurial-patches mailing list