##// END OF EJS Templates
config: type `rcutil`...
marmoute -
r53317:91f22b89 default
parent child Browse files
Show More
@@ -1,119 +1,139
1 # rcutil.py - utilities about config paths, special config sections etc.
1 # rcutil.py - utilities about config paths, special config sections etc.
2 #
2 #
3 # Copyright Mercurial Contributors
3 # Copyright Mercurial Contributors
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 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import annotations
8 from __future__ import annotations
9
9
10 import os
10 import os
11
11
12 from typing import (
13 Dict,
14 List,
15 Optional,
16 Tuple,
17 Union,
18 )
19
12 from .. import (
20 from .. import (
13 encoding,
21 encoding,
14 pycompat,
22 pycompat,
15 util,
23 util,
16 )
24 )
17
25
18 from ..utils import resourceutil
26 from ..utils import resourceutil
19
27
20 if pycompat.iswindows:
28 if pycompat.iswindows:
21 from .. import scmwindows as scmplatform
29 from .. import scmwindows as scmplatform
22 else:
30 else:
23 from .. import scmposix as scmplatform
31 from .. import scmposix as scmplatform
24
32
25 fallbackpager = scmplatform.fallbackpager
33 fallbackpager = scmplatform.fallbackpager
26 systemrcpath = scmplatform.systemrcpath
34 systemrcpath = scmplatform.systemrcpath
27 userrcpath = scmplatform.userrcpath
35 userrcpath = scmplatform.userrcpath
28
36
37 ConfigItemT = Tuple[bytes, bytes, bytes, bytes]
38 ResourceIDT = Tuple[bytes, bytes]
39 FileRCT = bytes
40 ComponentT = Tuple[
41 bytes,
42 Union[
43 List[ConfigItemT],
44 FileRCT,
45 ResourceIDT,
46 ],
47 ]
29
48
30 def _expandrcpath(path):
49
50 def _expandrcpath(path: bytes) -> List[FileRCT]:
31 '''path could be a file or a directory. return a list of file paths'''
51 '''path could be a file or a directory. return a list of file paths'''
32 p = util.expandpath(path)
52 p = util.expandpath(path)
33 if os.path.isdir(p):
53 if os.path.isdir(p):
34 join = os.path.join
54 join = os.path.join
35 return sorted(
55 return sorted(
36 join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc')
56 join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc')
37 )
57 )
38 return [p]
58 return [p]
39
59
40
60
41 def envrcitems(env=None):
61 def envrcitems(env: Optional[Dict[bytes, bytes]] = None) -> List[ConfigItemT]:
42 """Return [(section, name, value, source)] config items.
62 """Return [(section, name, value, source)] config items.
43
63
44 The config items are extracted from environment variables specified by env,
64 The config items are extracted from environment variables specified by env,
45 used to override systemrc, but not userrc.
65 used to override systemrc, but not userrc.
46
66
47 If env is not provided, encoding.environ will be used.
67 If env is not provided, encoding.environ will be used.
48 """
68 """
49 if env is None:
69 if env is None:
50 env = encoding.environ
70 env = encoding.environ
51 checklist = [
71 checklist = [
52 (b'EDITOR', b'ui', b'editor'),
72 (b'EDITOR', b'ui', b'editor'),
53 (b'VISUAL', b'ui', b'editor'),
73 (b'VISUAL', b'ui', b'editor'),
54 (b'PAGER', b'pager', b'pager'),
74 (b'PAGER', b'pager', b'pager'),
55 ]
75 ]
56 result = []
76 result = []
57 for envname, section, configname in checklist:
77 for envname, section, configname in checklist:
58 if envname not in env:
78 if envname not in env:
59 continue
79 continue
60 result.append((section, configname, env[envname], b'$%s' % envname))
80 result.append((section, configname, env[envname], b'$%s' % envname))
61 return result
81 return result
62
82
63
83
64 def default_rc_resources():
84 def default_rc_resources() -> List[ResourceIDT]:
65 """return rc resource IDs in defaultrc"""
85 """return rc resource IDs in defaultrc"""
66 rsrcs = resourceutil.contents(b'mercurial.defaultrc')
86 rsrcs = resourceutil.contents(b'mercurial.defaultrc')
67 return [
87 return [
68 (b'mercurial.defaultrc', r)
88 (b'mercurial.defaultrc', r)
69 for r in sorted(rsrcs)
89 for r in sorted(rsrcs)
70 if resourceutil.is_resource(b'mercurial.defaultrc', r)
90 if resourceutil.is_resource(b'mercurial.defaultrc', r)
71 and r.endswith(b'.rc')
91 and r.endswith(b'.rc')
72 ]
92 ]
73
93
74
94
75 def rccomponents():
95 def rccomponents() -> List[ComponentT]:
76 """return an ordered [(type, obj)] about where to load configs.
96 """return an ordered [(type, obj)] about where to load configs.
77
97
78 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
98 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
79 used. if $HGRCPATH is not set, the platform default will be used.
99 used. if $HGRCPATH is not set, the platform default will be used.
80
100
81 if a directory is provided, *.rc files under it will be used.
101 if a directory is provided, *.rc files under it will be used.
82
102
83 type could be either 'path', 'items' or 'resource'. If type is 'path',
103 type could be either 'path', 'items' or 'resource'. If type is 'path',
84 obj is a string, and is the config file path. if type is 'items', obj is a
104 obj is a string, and is the config file path. if type is 'items', obj is a
85 list of (section, name, value, source) that should fill the config directly.
105 list of (section, name, value, source) that should fill the config directly.
86 If type is 'resource', obj is a tuple of (package name, resource name).
106 If type is 'resource', obj is a tuple of (package name, resource name).
87 """
107 """
88 envrc = (b'items', envrcitems())
108 envrc = (b'items', envrcitems())
89
109
90 if b'HGRCPATH' in encoding.environ:
110 if b'HGRCPATH' in encoding.environ:
91 # assume HGRCPATH is all about user configs so environments can be
111 # assume HGRCPATH is all about user configs so environments can be
92 # overridden.
112 # overridden.
93 _rccomponents = [envrc]
113 _rccomponents = [envrc]
94 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep):
114 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep):
95 if not p:
115 if not p:
96 continue
116 continue
97 _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
117 _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
98 else:
118 else:
99 _rccomponents = [(b'resource', r) for r in default_rc_resources()]
119 _rccomponents = [(b'resource', r) for r in default_rc_resources()]
100
120
101 normpaths = lambda paths: [
121 normpaths = lambda paths: [
102 (b'path', os.path.normpath(p)) for p in paths
122 (b'path', os.path.normpath(p)) for p in paths
103 ]
123 ]
104 _rccomponents.extend(normpaths(systemrcpath()))
124 _rccomponents.extend(normpaths(systemrcpath()))
105 _rccomponents.append(envrc)
125 _rccomponents.append(envrc)
106 _rccomponents.extend(normpaths(userrcpath()))
126 _rccomponents.extend(normpaths(userrcpath()))
107 return _rccomponents
127 return _rccomponents
108
128
109
129
110 def defaultpagerenv():
130 def defaultpagerenv() -> Dict[bytes, bytes]:
111 """return a dict of default environment variables and their values,
131 """return a dict of default environment variables and their values,
112 intended to be set before starting a pager.
132 intended to be set before starting a pager.
113 """
133 """
114 return {b'LESS': b'FRX', b'LV': b'-c'}
134 return {b'LESS': b'FRX', b'LV': b'-c'}
115
135
116
136
117 def use_repo_hgrc():
137 def use_repo_hgrc() -> bool:
118 """True if repositories `.hg/hgrc` config should be read"""
138 """True if repositories `.hg/hgrc` config should be read"""
119 return b'HGRCSKIPREPO' not in encoding.environ
139 return b'HGRCSKIPREPO' not in encoding.environ
General Comments 0
You need to be logged in to leave comments. Login now