~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
151
151
            mail_client_class = {
152
152
                None: mail_client.DefaultMail,
153
153
                # Specific clients
 
154
                'emacsclient': mail_client.EmacsMail,
154
155
                'evolution': mail_client.Evolution,
155
156
                'kmail': mail_client.KMail,
156
157
                'mutt': mail_client.Mutt,
891
892
    """Branch configuration data associated with its contents, not location"""
892
893
 
893
894
    def __init__(self, branch):
 
895
        transport = branch.control_files._transport
 
896
        self._config = TransportConfig(transport, 'branch.conf')
894
897
        self.branch = branch
895
898
 
896
899
    def _get_parser(self, file=None):
897
900
        if file is not None:
898
901
            return IniBasedConfig._get_parser(file)
899
 
        return self._get_config()
900
 
 
901
 
    def _get_config(self):
902
 
        try:
903
 
            obj = ConfigObj(self.branch.control_files.get('branch.conf'),
904
 
                            encoding='utf-8')
905
 
        except errors.NoSuchFile:
906
 
            obj = ConfigObj(encoding='utf=8')
907
 
        return obj
 
902
        return self._config._get_configobj()
908
903
 
909
904
    def get_option(self, name, section=None, default=None):
910
905
        self.branch.lock_read()
911
906
        try:
912
 
            obj = self._get_config()
913
 
            try:
914
 
                if section is not None:
915
 
                    obj = obj[section]
916
 
                result = obj[name]
917
 
            except KeyError:
918
 
                result = default
 
907
            return self._config.get_option(name, section, default)
919
908
        finally:
920
909
            self.branch.unlock()
921
910
        return result
924
913
        """Set a per-branch configuration option"""
925
914
        self.branch.lock_write()
926
915
        try:
927
 
            cfg_obj = self._get_config()
928
 
            if section is None:
929
 
                obj = cfg_obj
930
 
            else:
931
 
                try:
932
 
                    obj = cfg_obj[section]
933
 
                except KeyError:
934
 
                    cfg_obj[section] = {}
935
 
                    obj = cfg_obj[section]
936
 
            obj[name] = value
937
 
            out_file = StringIO()
938
 
            cfg_obj.write(out_file)
939
 
            out_file.seek(0)
940
 
            self.branch.control_files.put('branch.conf', out_file)
 
916
            self._config.set_option(value, name, section)
941
917
        finally:
942
918
            self.branch.unlock()
943
919
 
1125
1101
 
1126
1102
    def decode_password(self, credentials, encoding):
1127
1103
        return credentials
 
1104
 
 
1105
 
 
1106
class TransportConfig(object):
 
1107
    """A Config that reads/writes a config file on a Transport.
 
1108
 
 
1109
    It is a low-level object that considers config data to be name/value pairs
 
1110
    that may be associated with a section.  Assigning meaning to the these
 
1111
    values is done at higher levels like TreeConfig.
 
1112
    """
 
1113
 
 
1114
    def __init__(self, transport, filename):
 
1115
        self._transport = transport
 
1116
        self._filename = filename
 
1117
 
 
1118
    def get_option(self, name, section=None, default=None):
 
1119
        """Return the value associated with a named option.
 
1120
 
 
1121
        :param name: The name of the value
 
1122
        :param section: The section the option is in (if any)
 
1123
        :param default: The value to return if the value is not set
 
1124
        :return: The value or default value
 
1125
        """
 
1126
        configobj = self._get_configobj()
 
1127
        if section is None:
 
1128
            section_obj = configobj
 
1129
        else:
 
1130
            try:
 
1131
                section_obj = configobj[section]
 
1132
            except KeyError:
 
1133
                return default
 
1134
        return section_obj.get(name, default)
 
1135
 
 
1136
    def set_option(self, value, name, section=None):
 
1137
        """Set the value associated with a named option.
 
1138
 
 
1139
        :param value: The value to set
 
1140
        :param name: The name of the value to set
 
1141
        :param section: The section the option is in (if any)
 
1142
        """
 
1143
        configobj = self._get_configobj()
 
1144
        if section is None:
 
1145
            configobj[name] = value
 
1146
        else:
 
1147
            configobj.setdefault(section, {})[name] = value
 
1148
        self._set_configobj(configobj)
 
1149
 
 
1150
    def _get_configobj(self):
 
1151
        try:
 
1152
            return ConfigObj(self._transport.get(self._filename),
 
1153
                             encoding='utf-8')
 
1154
        except errors.NoSuchFile:
 
1155
            return ConfigObj(encoding='utf-8')
 
1156
 
 
1157
    def _set_configobj(self, configobj):
 
1158
        out_file = StringIO()
 
1159
        configobj.write(out_file)
 
1160
        out_file.seek(0)
 
1161
        self._transport.put_file(self._filename, out_file)