/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-02-14 03:16:54 UTC
  • mfrom: (7479.2.3 no-more-python2)
  • Revision ID: breezy.the.bot@gmail.com-20200214031654-bp1xtv2jr9nmhto3
Drop python2 support.

Merged from https://code.launchpad.net/~jelmer/brz/no-more-python2/+merge/378694

Show diffs side-by-side

added added

removed removed

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