comparison mercurial/templater.py @ 45330:65a812ed9e9f

templater: replace templatepath() with function that also opens the file For frozen binaries, such as those created by PyOxidizer, I plan to make it so the templatespec can keep an opened file/resource to read from instead of needing a file path. Having `templatepath()` return an opened file should help with that. At this point, it's just a wasteful extra opening of mapfiles that we'll open again later. I'll update the read-side next so it reads from the file-like object without opening the file again. Differential Revision: https://phab.mercurial-scm.org/D8892
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 04 Aug 2020 13:21:29 -0700
parents dc10bcd5c08d
children f3481e4fcc3a
comparison
equal deleted inserted replaced
45329:dc10bcd5c08d 45330:65a812ed9e9f
1069 '''return the directory used for template files, or None.''' 1069 '''return the directory used for template files, or None.'''
1070 path = os.path.normpath(os.path.join(resourceutil.datapath, b'templates')) 1070 path = os.path.normpath(os.path.join(resourceutil.datapath, b'templates'))
1071 return path if os.path.isdir(path) else None 1071 return path if os.path.isdir(path) else None
1072 1072
1073 1073
1074 def templatepath(name): 1074 def open_template(name):
1075 '''return location of template file. returns None if not found.''' 1075 '''returns a file-like object for the given template, and its full path'''
1076 dir = templatedir() 1076 templatepath = templatedir()
1077 if dir is None: 1077 if templatepath is not None or os.path.isabs(name):
1078 return None 1078 f = os.path.join(templatepath, name)
1079 f = os.path.join(templatedir(), name) 1079 try:
1080 if f and os.path.isfile(f): 1080 return f, open(f, mode='rb')
1081 return f 1081 except EnvironmentError:
1082 return None 1082 return None, None
1083 else:
1084 # TODO: read from resources here
1085 return None, None