comparison mercurial/extensions.py @ 8871:20a25042fadc

extensions: move extensions listing functions from mercurial.help Includes some small fix-ups to comments in enabled() and disabled().
author C?dric Duval <cedricduval@free.fr>
date Sun, 21 Jun 2009 16:32:00 +0200
parents 46293a0c7e9f
children d0c0013f8713
comparison
equal deleted inserted replaced
8870:c3e4d3c1d48b 8871:20a25042fadc
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # 4 #
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference. 6 # GNU General Public License version 2, incorporated herein by reference.
7 7
8 import imp, os 8 import imp, os, sys
9 import util, cmdutil 9 import util, cmdutil, help
10 from i18n import _ 10 from i18n import _, gettext
11 11
12 _extensions = {} 12 _extensions = {}
13 _order = [] 13 _order = []
14 14
15 def extensions(): 15 def extensions():
115 return wrapper(origfn, *args, **kwargs) 115 return wrapper(origfn, *args, **kwargs)
116 116
117 origfn = getattr(container, funcname) 117 origfn = getattr(container, funcname)
118 setattr(container, funcname, wrap) 118 setattr(container, funcname, wrap)
119 return origfn 119 return origfn
120
121 def pathdirs():
122 '''convert sys.path into a list of absolute, existing, unique paths
123 (taken from pydoc)'''
124 dirs = []
125 normdirs = []
126 for dir in sys.path:
127 dir = os.path.abspath(dir or '.')
128 normdir = os.path.normcase(dir)
129 if normdir not in normdirs and os.path.isdir(dir):
130 dirs.append(dir)
131 normdirs.append(normdir)
132 return dirs
133
134 def disabled():
135 '''find disabled extensions from hgext
136 returns a dict of {name: desc}, and the max name length'''
137 exts = {}
138 maxlength = 0
139 for dir in filter(os.path.isdir,
140 (os.path.join(pd, 'hgext') for pd in pathdirs())):
141 for e in os.listdir(dir):
142 if e.endswith('.py'):
143 name = e.rsplit('.', 1)[0]
144 path = os.path.join(dir, e)
145 else:
146 name = e
147 path = os.path.join(dir, e, '__init__.py')
148
149 if name in exts or name == '__init__' or not os.path.exists(path):
150 continue
151
152 try:
153 find(name)
154 except KeyError:
155 pass
156 else:
157 continue # enabled extension
158
159 try:
160 file = open(path)
161 except IOError:
162 continue
163 else:
164 doc = help.moduledoc(file)
165 file.close()
166
167 if doc: # extracting localized synopsis
168 exts[name] = gettext(doc).splitlines()[0]
169 else:
170 exts[name] = _('(no help text available)')
171 if len(name) > maxlength:
172 maxlength = len(name)
173
174 return exts, maxlength
175
176 def enabled():
177 '''return a dict of {name: desc} of extensions, and the max name length'''
178
179 if not enabled:
180 return {}, 0
181
182 exts = {}
183 maxlength = 0
184 exthelps = []
185 for ename, ext in extensions():
186 doc = (gettext(ext.__doc__) or _('(no help text available)'))
187 ename = ename.split('.')[-1]
188 maxlength = max(len(ename), maxlength)
189 exts[ename] = doc.splitlines(0)[0].strip()
190
191 return exts, maxlength