##// END OF EJS Templates
rcutil: add a method to convert environment variables to config items...
Jun Wu -
r31684:0be96ac9 default
parent child Browse files
Show More
@@ -1,68 +1,90 b''
1 1 # rcutil.py - utilities about config paths, special config sections etc.
2 2 #
3 3 # Copyright Mercurial Contributors
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import os
11 11
12 12 from . import (
13 13 encoding,
14 14 osutil,
15 15 pycompat,
16 16 util,
17 17 )
18 18
19 19 if pycompat.osname == 'nt':
20 20 from . import scmwindows as scmplatform
21 21 else:
22 22 from . import scmposix as scmplatform
23 23
24 24 systemrcpath = scmplatform.systemrcpath
25 25 userrcpath = scmplatform.userrcpath
26 26
27 27 def _expandrcpath(path):
28 28 '''path could be a file or a directory. return a list of file paths'''
29 29 p = util.expandpath(path)
30 30 if os.path.isdir(p):
31 31 join = os.path.join
32 32 return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
33 33 return [p]
34 34
35 def envrcitems(env=None):
36 '''Return [(section, name, value, source)] config items.
37
38 The config items are extracted from environment variables specified by env,
39 used to override systemrc, but not userrc.
40
41 If env is not provided, encoding.environ will be used.
42 '''
43 if env is None:
44 env = encoding.environ
45 checklist = [
46 ('EDITOR', 'ui', 'editor'),
47 ('VISUAL', 'ui', 'editor'),
48 ('PAGER', 'pager', 'pager'),
49 ]
50 result = []
51 for envname, section, configname in checklist:
52 if envname not in env:
53 continue
54 result.append((section, configname, env[envname], '$%s' % envname))
55 return result
56
35 57 def defaultrcpath():
36 58 '''return rc paths in default.d'''
37 59 path = []
38 60 defaultpath = os.path.join(util.datapath, 'default.d')
39 61 if os.path.isdir(defaultpath):
40 62 path = _expandrcpath(defaultpath)
41 63 return path
42 64
43 65 _rccomponents = None
44 66
45 67 def rccomponents():
46 68 '''return an ordered [(type, obj)] about where to load configs.
47 69
48 70 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
49 71 used. if $HGRCPATH is not set, the platform default will be used.
50 72
51 73 if a directory is provided, *.rc files under it will be used.
52 74
53 75 type could be either 'path' or 'items', if type is 'path', obj is a string,
54 76 and is the config file path. if type is 'items', obj is a list of (section,
55 77 name, value, source) that should fill the config directly.
56 78 '''
57 79 global _rccomponents
58 80 if _rccomponents is None:
59 81 if 'HGRCPATH' in encoding.environ:
60 82 _rccomponents = []
61 83 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
62 84 if not p:
63 85 continue
64 86 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
65 87 else:
66 88 paths = defaultrcpath() + systemrcpath() + userrcpath()
67 89 _rccomponents = [('path', os.path.normpath(p)) for p in paths]
68 90 return _rccomponents
General Comments 0
You need to be logged in to leave comments. Login now