/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-08-10 15:00:17 UTC
  • mfrom: (7490.40.99 work)
  • mto: This revision was merged to the branch mainline in revision 7521.
  • Revision ID: jelmer@jelmer.uk-20200810150017-vs7xnrd1vat4iktg
Merge lp:brz/3.1.

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