/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-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
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
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
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
    )
117
124
 
118
125
 
119
126
CHECK_IF_POSSIBLE = 0
250
257
                     % signature_string)
251
258
 
252
259
 
 
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
 
253
273
def _has_triplequote_bug():
254
274
    """True if triple quote logic is reversed, see lp:710410."""
255
275
    conf = configobj.ConfigObj()
267
287
                                        interpolation=False,
268
288
                                        **kwargs)
269
289
 
 
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
 
270
296
    if _has_triplequote_bug():
271
297
        def _get_triple_quote(self, value):
272
298
            quot = super(ConfigObj, self)._get_triple_quote(value)
493
519
            otherwise.
494
520
        """
495
521
        l = self.get_user_option(option_name, expand=expand)
496
 
        if isinstance(l, str):
 
522
        if isinstance(l, string_types):
497
523
            # A single value, most probably the user forgot (or didn't care to
498
524
            # add) the final ','
499
525
            l = [l]
536
562
        """
537
563
        v = os.environ.get('BRZ_EMAIL') or os.environ.get('BZR_EMAIL')
538
564
        if v:
 
565
            if not PY3:
 
566
                v = v.decode(osutils.get_user_encoding())
539
567
            return v
540
568
        v = self._get_user_id()
541
569
        if v:
701
729
        return conf
702
730
 
703
731
    def _create_from_string(self, str_or_unicode, save):
704
 
        if isinstance(str_or_unicode, str):
 
732
        if isinstance(str_or_unicode, text_type):
705
733
            str_or_unicode = str_or_unicode.encode('utf-8')
706
734
        self._content = BytesIO(str_or_unicode)
707
735
        # Some tests use in-memory configs, some other always need the config
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)
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
                        ),