comparison mercurial/templater.py @ 45321:735756ecda8c

templater: restructure open_template() a little to prepare for relative paths I found that it was easier to add support for relative paths after this restructuring. It also made it easier to explain each case with a code comment (which I did). Differential Revision: https://phab.mercurial-scm.org/D8906
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 06 Aug 2020 10:52:52 -0700
parents 4aa484efc926
children c3376a724e32
comparison
equal deleted inserted replaced
45320:4aa484efc926 45321:735756ecda8c
1089 1089
1090 If the name is a relative path and we're in a frozen binary, the template 1090 If the name is a relative path and we're in a frozen binary, the template
1091 will be read from the mercurial.templates package instead. The returned path 1091 will be read from the mercurial.templates package instead. The returned path
1092 will then be the relative path. 1092 will then be the relative path.
1093 ''' 1093 '''
1094 # Does the name point directly to a map file?
1095 if os.path.isabs(name):
1096 return name, open(name, mode='rb')
1097
1098 # Does the name point to a template in the provided templatepath, or
1099 # in mercurial/templates/ if no path was provided?
1094 if templatepath is None: 1100 if templatepath is None:
1095 templatepath = templatedir() 1101 templatepath = templatedir()
1096 if templatepath is not None or os.path.isabs(name): 1102 if templatepath is not None:
1097 f = os.path.join(templatepath, name) 1103 f = os.path.join(templatepath, name)
1098 return f, open(f, mode='rb') 1104 return f, open(f, mode='rb')
1099 else: 1105
1100 name_parts = pycompat.sysstr(name).split('/') 1106 # Otherwise try to read it using the resources API
1101 package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1]) 1107 name_parts = pycompat.sysstr(name).split('/')
1102 return ( 1108 package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1])
1103 name, 1109 return (
1104 resourceutil.open_resource(package_name, name_parts[-1]), 1110 name,
1105 ) 1111 resourceutil.open_resource(package_name, name_parts[-1]),
1112 )
1106 1113
1107 1114
1108 def try_open_template(name, templatepath=None): 1115 def try_open_template(name, templatepath=None):
1109 try: 1116 try:
1110 return open_template(name, templatepath) 1117 return open_template(name, templatepath)