mercurial/templater.py
changeset 7107 125c8fedcbe0
parent 6783 6d824dc86907
child 7396 526c40a74bd0
equal deleted inserted replaced
7106:4674706b5b95 7107:125c8fedcbe0
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
     8 from i18n import _
     8 from i18n import _
     9 import re, sys, os
     9 import re, sys, os
    10 from mercurial import util
    10 from mercurial import util
       
    11 
       
    12 path = ['templates', '../templates']
    11 
    13 
    12 def parsestring(s, quoted=True):
    14 def parsestring(s, quoted=True):
    13     '''parse a string using simple c-like syntax.
    15     '''parse a string using simple c-like syntax.
    14     string must be in quotes if quoted is True.'''
    16     string must be in quotes if quoted is True.'''
    15     if quoted:
    17     if quoted:
   148                 yield str(item)
   150                 yield str(item)
   149 
   151 
   150 def templatepath(name=None):
   152 def templatepath(name=None):
   151     '''return location of template file or directory (if no name).
   153     '''return location of template file or directory (if no name).
   152     returns None if not found.'''
   154     returns None if not found.'''
       
   155     normpaths = []
   153 
   156 
   154     # executable version (py2exe) doesn't support __file__
   157     # executable version (py2exe) doesn't support __file__
   155     if hasattr(sys, 'frozen'):
   158     if hasattr(sys, 'frozen'):
   156         module = sys.executable
   159         module = sys.executable
   157     else:
   160     else:
   158         module = __file__
   161         module = __file__
   159     for f in 'templates', '../templates':
   162     for f in path:
   160         fl = f.split('/')
   163         if f.startswith('/'):
   161         if name: fl.append(name)
   164             p = f
   162         p = os.path.join(os.path.dirname(module), *fl)
   165         else:
   163         if (name and os.path.exists(p)) or os.path.isdir(p):
   166             fl = f.split('/')
       
   167             p = os.path.join(os.path.dirname(module), *fl)
       
   168         if name:
       
   169             p = os.path.join(p, name)
       
   170         if name and os.path.exists(p):
   164             return os.path.normpath(p)
   171             return os.path.normpath(p)
       
   172         elif os.path.isdir(p):
       
   173             normpaths.append(os.path.normpath(p))
       
   174 
       
   175     return normpaths
   165 
   176 
   166 def stringify(thing):
   177 def stringify(thing):
   167     '''turn nested template iterator into string.'''
   178     '''turn nested template iterator into string.'''
   168     if hasattr(thing, '__iter__'):
   179     if hasattr(thing, '__iter__'):
   169         return "".join([stringify(t) for t in thing if t is not None])
   180         return "".join([stringify(t) for t in thing if t is not None])