(self, changes=True, keys_only=False, comment_spacing=0, min_comment_spacing=8, ignore_comments=None)
return '\n'.join(output[:-1])
Generate lines for a config file.
Generate lines for a config file.
Generate lines for a config file .
def _build_for_file(self, changes=True, keys_only=False, comment_spacing=0, min_comment_spacing=8, ignore_comments=None):
        """Generate lines for a config file."""
        output = []
        
        for heading in _get_priority_order(self._default):

            #Ignore if heading has been deleted
            if self._editable_dict and heading not in self._data:
                continue

            #Add heading
            output.append('[{}]'.format(heading))

            #Add heading help if set
            try:
                header_info = self._default[heading]['__info__']
            except KeyError:
                pass
            else:
                for line in header_info.split('\n'):
                    output.append('// {}'.format(line))

            #Add each variable
            for variable in _get_priority_order(self._default[heading]):
                
                #Ignore if variable has been deleted
                if self._editable_dict and variable not in self._data[heading]:
                    continue

                if changes:
                    info = self._data[heading][variable]
                else:
                    info = self._default[heading][variable]

                #Convert to dict if not one already
                if not isinstance(info, dict):
                    info = {'value': info, 'type': type(info)}
                elif 'type' not in info:
                    try:
                        info['type'] = type(info['value'])
                    except KeyError:
                        if self._default_settings is None:
                            raise
                        if 'type' in self._default_settings:
                            info['type'] = self._default_settings['type']
                        else:
                            raise

                value_type = info['type']
                value = info.get('value', self._DEFAULT_VALUES[value_type])
                
                if keys_only:
                    output.append('{} = '.format(variable))
                else:
                    output.append('{} = {}'.format(variable, value).replace('\n', '\\n'))
                
                #Add the comment
                try:
                    comment = info['__info__']
                except KeyError:
                    pass
                else:
                    current_len = len(output[-1])

                    #Check if comment should be ignored
                    if ignore_comments:
                        if isinstance(ignore_comments, bool):
                            comment = ''
                        if isinstance(ignore_comments, str):
                            for ignore_comment in ignore_comments:
                                if comment.startswith(ignore_comment):
                                    comment = ''
                                    break
                        elif isinstance(ignore_comments, (list, tuple, set)):
                            for ignore_comment in ignore_comments:
                                if any(comment.startswith(i) for i in ignore_comment):
                                    comment = ''
                                    break
                    if comment:
                        extra_spaces = comment_spacing - current_len
                        output[-1] += ' ' * max(min_comment_spacing, extra_spaces) + '// {}'.format(comment)
            output.append('')
        return '\n'.join(output[:-1])
def _build_for_file ( self , changes = True , keys_only = False , comment_spacing = 0 , min_comment_spacing = 8 , ignore_comments = None ) : output = [ ] for heading in _get_priority_order ( self . _default ) : #Ignore if heading has been deleted if self . _editable_dict and heading not in self . _data : continue #Add heading output . append ( '[{}]' . format ( heading ) ) #Add heading help if set try : header_info = self . _default [ heading ] [ '__info__' ] except KeyError : pass else : for line in header_info . split ( '\n' ) : output . append ( '// {}' . format ( line ) ) #Add each variable for variable in _get_priority_order ( self . _default [ heading ] ) : #Ignore if variable has been deleted if self . _editable_dict and variable not in self . _data [ heading ] : continue if changes : info = self . _data [ heading ] [ variable ] else : info = self . _default [ heading ] [ variable ] #Convert to dict if not one already if not isinstance ( info , dict ) : info = { 'value' : info , 'type' : type ( info ) } elif 'type' not in info : try : info [ 'type' ] = type ( info [ 'value' ] ) except KeyError : if self . _default_settings is None : raise if 'type' in self . _default_settings : info [ 'type' ] = self . _default_settings [ 'type' ] else : raise value_type = info [ 'type' ] value = info . get ( 'value' , self . _DEFAULT_VALUES [ value_type ] ) if keys_only : output . append ( '{} = ' . format ( variable ) ) else : output . append ( '{} = {}' . format ( variable , value ) . replace ( '\n' , '\\n' ) ) #Add the comment try : comment = info [ '__info__' ] except KeyError : pass else : current_len = len ( output [ - 1 ] ) #Check if comment should be ignored if ignore_comments : if isinstance ( ignore_comments , bool ) : comment = '' if isinstance ( ignore_comments , str ) : for ignore_comment in ignore_comments : if comment . startswith ( ignore_comment ) : comment = '' break elif isinstance ( ignore_comments , ( list , tuple , set ) ) : for ignore_comment in ignore_comments : if any ( comment . startswith ( i ) for i in ignore_comment ) : comment = '' break if comment : extra_spaces = comment_spacing - current_len output [ - 1 ] += ' ' * max ( min_comment_spacing , extra_spaces ) + '// {}' . format ( comment ) output . append ( '' ) return '\n' . join ( output [ : - 1 ] )