comparison mercurial/extensions.py @ 10363:c07974215b3d

extensions: refactor disabled()
author Brodie Rao <me+hg@dackz.net>
date Sun, 07 Feb 2010 11:32:08 +0100
parents 25e572394f5c
children de1e7099d100
comparison
equal deleted inserted replaced
10362:2e3ec7ef5349 10363:c07974215b3d
129 129
130 origfn = getattr(container, funcname) 130 origfn = getattr(container, funcname)
131 setattr(container, funcname, wrap) 131 setattr(container, funcname, wrap)
132 return origfn 132 return origfn
133 133
134 def disabled(): 134 def _disabledpaths():
135 '''find disabled extensions from hgext 135 '''find paths of disabled extensions. returns a dict of {name: path}'''
136 returns a dict of {name: desc}, and the max name length'''
137
138 import hgext 136 import hgext
139 extpath = os.path.dirname(os.path.abspath(hgext.__file__)) 137 extpath = os.path.dirname(os.path.abspath(hgext.__file__))
140
141 try: # might not be a filesystem path 138 try: # might not be a filesystem path
142 files = os.listdir(extpath) 139 files = os.listdir(extpath)
143 except OSError: 140 except OSError:
144 return None, 0 141 return {}
145 142
146 exts = {} 143 exts = {}
147 maxlength = 0
148 for e in files: 144 for e in files:
149
150 if e.endswith('.py'): 145 if e.endswith('.py'):
151 name = e.rsplit('.', 1)[0] 146 name = e.rsplit('.', 1)[0]
152 path = os.path.join(extpath, e) 147 path = os.path.join(extpath, e)
153 else: 148 else:
154 name = e 149 name = e
155 path = os.path.join(extpath, e, '__init__.py') 150 path = os.path.join(extpath, e, '__init__.py')
156 if not os.path.exists(path): 151 if not os.path.exists(path):
157 continue 152 continue
158
159 if name in exts or name in _order or name == '__init__': 153 if name in exts or name in _order or name == '__init__':
160 continue 154 continue
161 155 exts[name] = path
162 try: 156 return exts
163 file = open(path) 157
164 except IOError: 158 def _disabledhelp(path):
159 '''retrieve help synopsis of a disabled extension (without importing)'''
160 try:
161 file = open(path)
162 except IOError:
163 return
164 else:
165 doc = help.moduledoc(file)
166 file.close()
167
168 if doc: # extracting localized synopsis
169 return gettext(doc).splitlines()[0]
170 else:
171 return _('(no help text available)')
172
173 def disabled():
174 '''find disabled extensions from hgext
175 returns a dict of {name: desc}, and the max name length'''
176
177 paths = _disabledpaths()
178 if not paths:
179 return None, 0
180
181 exts = {}
182 maxlength = 0
183 for name, path in paths.iteritems():
184 doc = _disabledhelp(path)
185 if not doc:
165 continue 186 continue
166 else: 187
167 doc = help.moduledoc(file) 188 exts[name] = doc
168 file.close()
169
170 if doc: # extracting localized synopsis
171 exts[name] = gettext(doc).splitlines()[0]
172 else:
173 exts[name] = _('(no help text available)')
174
175 if len(name) > maxlength: 189 if len(name) > maxlength:
176 maxlength = len(name) 190 maxlength = len(name)
177 191
178 return exts, maxlength 192 return exts, maxlength
179 193