~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/config/__init__.py

ivle.config.Config: Added set_by_path method (based on code from
    setup.configure).
setup.configure: Use ivle.config.Config.set_by_path rather than doing it
    manually.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
            # XXX This doesn't raise errors if it doesn't validate
78
78
            self.validate(Validator())
79
79
 
 
80
    def set_by_path(self, path, value, comment=None):
 
81
        """Writes a value to an option, given a '/'-separated path.
 
82
        @param path: '/'-separated path to configuration option.
 
83
        @param value: value to write to the option.
 
84
        @param comment: optional comment string (lines separated by '\n's).
 
85
        """
 
86
        path = path.split('/')
 
87
        # Iterate over each segment of the path, and find the section in conf
 
88
        # file to insert the value into (use all but the last path segment)
 
89
        conf_section = self
 
90
        for seg in path[:-1]:
 
91
            # Create the section if it isn't there
 
92
            if seg not in conf_section:
 
93
                conf_section[seg] = {}
 
94
            conf_section = conf_section[seg]
 
95
        # The final path segment names the key to insert into
 
96
        keyname = path[-1]
 
97
        conf_section[keyname] = value
 
98
        if comment is not None:
 
99
            conf_section.comments[keyname] = comment.split('\n')
 
100
 
80
101
    def get_by_path(self, path):
81
102
        """Gets an option's value, given a '/'-separated path.
82
103
        @param path: '/'-separated path to configuration option.