/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/config.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
up=pull
75
75
"""
76
76
 
 
77
from __future__ import absolute_import
77
78
import os
78
79
import sys
79
80
 
80
81
import configobj
81
 
from io import BytesIO
82
82
 
83
83
import breezy
84
84
from .lazy_import import lazy_import
90
90
import stat
91
91
 
92
92
from breezy import (
 
93
    atomicfile,
93
94
    cmdline,
94
95
    controldir,
95
96
    debug,
114
115
    lazy_regex,
115
116
    registry,
116
117
    )
 
118
from .sixish import (
 
119
    binary_type,
 
120
    BytesIO,
 
121
    PY3,
 
122
    string_types,
 
123
    text_type,
 
124
    )
117
125
 
118
126
 
119
127
CHECK_IF_POSSIBLE = 0
250
258
                     % signature_string)
251
259
 
252
260
 
 
261
def _has_decode_bug():
 
262
    """True if configobj will fail to decode to unicode on Python 2."""
 
263
    if PY3:
 
264
        return False
 
265
    conf = configobj.ConfigObj()
 
266
    decode = getattr(conf, "_decode", None)
 
267
    if decode:
 
268
        result = decode(b"\xc2\xa7", "utf-8")
 
269
        if isinstance(result[0], str):
 
270
            return True
 
271
    return False
 
272
 
 
273
 
253
274
def _has_triplequote_bug():
254
275
    """True if triple quote logic is reversed, see lp:710410."""
255
276
    conf = configobj.ConfigObj()
267
288
                                        interpolation=False,
268
289
                                        **kwargs)
269
290
 
 
291
    if _has_decode_bug():
 
292
        def _decode(self, infile, encoding):
 
293
            if isinstance(infile, str) and encoding:
 
294
                return infile.decode(encoding).splitlines(True)
 
295
            return super(ConfigObj, self)._decode(infile, encoding)
 
296
 
270
297
    if _has_triplequote_bug():
271
298
        def _get_triple_quote(self, value):
272
299
            quot = super(ConfigObj, self)._get_triple_quote(value)
493
520
            otherwise.
494
521
        """
495
522
        l = self.get_user_option(option_name, expand=expand)
496
 
        if isinstance(l, str):
 
523
        if isinstance(l, string_types):
497
524
            # A single value, most probably the user forgot (or didn't care to
498
525
            # add) the final ','
499
526
            l = [l]
536
563
        """
537
564
        v = os.environ.get('BRZ_EMAIL') or os.environ.get('BZR_EMAIL')
538
565
        if v:
 
566
            if not PY3:
 
567
                v = v.decode(osutils.get_user_encoding())
539
568
            return v
540
569
        v = self._get_user_id()
541
570
        if v:
701
730
        return conf
702
731
 
703
732
    def _create_from_string(self, str_or_unicode, save):
704
 
        if isinstance(str_or_unicode, str):
 
733
        if isinstance(str_or_unicode, text_type):
705
734
            str_or_unicode = str_or_unicode.encode('utf-8')
706
735
        self._content = BytesIO(str_or_unicode)
707
736
        # Some tests use in-memory configs, some other always need the config
902
931
    def _write_config_file(self):
903
932
        if self.file_name is None:
904
933
            raise AssertionError('We cannot save, self.file_name is None')
905
 
        from . import atomicfile
906
934
        conf_dir = os.path.dirname(self.file_name)
907
935
        bedding.ensure_config_dir_exists(conf_dir)
908
936
        with atomicfile.AtomicFile(self.file_name) as atomic_file:
2101
2129
                raise AssertionError(
2102
2130
                    'Only empty lists are supported as default values')
2103
2131
            self.default = u','
2104
 
        elif isinstance(default, (bytes, str, bool, int, float)):
 
2132
        elif isinstance(default, (binary_type, text_type, bool, int, float)):
2105
2133
            # Rely on python to convert strings, booleans and integers
2106
2134
            self.default = u'%s' % (default,)
2107
2135
        elif callable(default):
2148
2176
            try:
2149
2177
                # If the env variable is defined, its value takes precedence
2150
2178
                value = os.environ[var]
 
2179
                if not PY3:
 
2180
                    value = value.decode(osutils.get_user_encoding())
2151
2181
                break
2152
2182
            except KeyError:
2153
2183
                continue
2159
2189
            try:
2160
2190
                # If the env variable is defined, its value is the default one
2161
2191
                value = os.environ[var]
 
2192
                if not PY3:
 
2193
                    value = value.decode(osutils.get_user_encoding())
2162
2194
                break
2163
2195
            except KeyError:
2164
2196
                continue
2166
2198
            # Otherwise, fallback to the value defined at registration
2167
2199
            if callable(self.default):
2168
2200
                value = self.default()
2169
 
                if not isinstance(value, str):
 
2201
                if not isinstance(value, text_type):
2170
2202
                    raise AssertionError(
2171
2203
                        "Callable default value for '%s' should be unicode"
2172
2204
                        % (self.name))
2251
2283
            invalid=invalid, unquote=False)
2252
2284
 
2253
2285
    def from_unicode(self, unicode_str):
2254
 
        if not isinstance(unicode_str, str):
 
2286
        if not isinstance(unicode_str, string_types):
2255
2287
            raise TypeError
2256
2288
        # Now inject our string directly as unicode. All callers got their
2257
2289
        # value from configobj, so values that need to be quoted are already
2259
2291
        _list_converter_config.reset()
2260
2292
        _list_converter_config._parse([u"list=%s" % (unicode_str,)])
2261
2293
        maybe_list = _list_converter_config['list']
2262
 
        if isinstance(maybe_list, str):
 
2294
        if isinstance(maybe_list, string_types):
2263
2295
            if maybe_list:
2264
2296
                # A single value, most probably the user forgot (or didn't care
2265
2297
                # to add) the final ','
2291
2323
        self.registry = registry
2292
2324
 
2293
2325
    def from_unicode(self, unicode_str):
2294
 
        if not isinstance(unicode_str, str):
 
2326
        if not isinstance(unicode_str, string_types):
2295
2327
            raise TypeError
2296
2328
        try:
2297
2329
            return self.registry.get(unicode_str)
2902
2934
            try:
2903
2935
                name, value = over.split('=', 1)
2904
2936
            except ValueError:
2905
 
                raise errors.CommandError(
 
2937
                raise errors.BzrCommandError(
2906
2938
                    gettext("Invalid '%s', should be of the form 'name=value'")
2907
2939
                    % (over,))
2908
2940
            self.options[name] = value
3050
3082
            self._config_obj.list_values = False
3051
3083
 
3052
3084
    def unquote(self, value):
3053
 
        if value and isinstance(value, str):
 
3085
        if value and isinstance(value, string_types):
3054
3086
            # _unquote doesn't handle None nor empty strings nor anything that
3055
3087
            # is not a string, really.
3056
3088
            value = self._config_obj._unquote(value)
3467
3499
            # None or ends up being None during expansion or conversion.
3468
3500
            if val is not None:
3469
3501
                if expand:
3470
 
                    if isinstance(val, str):
 
3502
                    if isinstance(val, string_types):
3471
3503
                        val = self._expand_options_in_string(val)
3472
3504
                    else:
3473
3505
                        trace.warning('Cannot expand "%s":'
3874
3906
        # http://pad.lv/788991 -- vila 20101115
3875
3907
        commands.Option('scope', help='Reduce the scope to the specified'
3876
3908
                        ' configuration file.',
3877
 
                        type=str),
 
3909
                        type=text_type),
3878
3910
        commands.Option('all',
3879
3911
                        help='Display all the defined values for the matching options.',
3880
3912
                        ),
4003
4035
 
4004
4036
    def _remove_config_option(self, name, directory, scope):
4005
4037
        if name is None:
4006
 
            raise errors.CommandError(
 
4038
            raise errors.BzrCommandError(
4007
4039
                '--remove expects an option to remove.')
4008
4040
        conf = self._get_stack(directory, scope, write_access=True)
4009
4041
        try: