1 ''' @file python_ini/ini_writer.py
2 @brief The IniWriter class for creating and writing an INI file.
4 The writer is configurable to specify optional delimiters, boolean values
5 and tab space for inline comments.
12 '''Ini file writer constructor.
13 Note that configurable values are set to defaults.
17 - ; for comment delimiter
18 - = for key/value delimiter
19 - , for value list delimiter
20 - column 40 for tab to inline comments
38 self.
__NAME_CHARS__NAME_CHARS = [33, 36, 37, 38, 40, 41, 42, 43, 45,
39 46, 60, 62, 63, 64, 94, 95, 123, 124, 125, 126]
43 '''Initialize or reset the writer to its constructor defaults.'''
54 def __validate_name(self, name):
55 if(
not isinstance(name, str)):
59 if(c >= 48
and c <= 57):
61 if(c >= 65
and c <= 90):
63 if(c >= 97
and c <= 122):
72 def __normalize_comment(self, comment):
75 if(
not isinstance(comment, str)):
77 'IniWriter: invalid comment, must be string of ASCII characters 32-126', comment)
78 elif(comment ==
'' or comment.isspace()):
82 if(c >= 32
and c <= 126):
85 'IniWriter: invalid character in comment - char code(' + str(c) +
')')
88 def __normalize_string(self, string):
92 if(c >= 32
and c <= 126):
108 elif(c < 0xd800
or (c >= 0xe000
and c <= 0xffff)):
114 elif(c >= 0xd800
and c < 0xe000):
116 'IniWriter: string has Unicode surrogate value', string)
126 'IniWriter: string has Unicode value out of range (>0x10FFFF)', string)
129 def booleans(self, true=False, false=False, none=False):
130 '''Sets the values to specify for boolean (and null) values.
131 @param true If specified must be one of 'true', 'yes' or 'on', all case insensitive.
132 @param false If specified must be one of 'false', 'no' or 'off', all case insensitive.
133 @param none If specified must be one of 'none', 'null' or 'void', all case insensitive.
134 @return Raises Exception on any parameter error.
138 if(l ==
'true' or l ==
'yes' or l ==
'on'):
142 'IniWriter.booleans(): true argument must be one of "true", "yes" or "on", case insensitive',
146 if(l ==
'false' or l ==
'no' or l ==
'off'):
150 'IniWriter.booleans(): false argument must be one of "false", "no" or "off", case insensitive',
154 if(l ==
'none' or l ==
'null' or l ==
'void'):
158 'IniWriter.booleans(): none argument must be one of "none", "null" or "void", case insensitive',
162 '''Sets the column value for the tab to inline comments.
163 @param tab The column value to begin inline comments.
164 Must be an integer tab >= 0.
165 @return Raises Exception on invalid tab value.
169 elif(isinstance(tab, int)
and (tab >= 0)):
173 'IniWriter.comment_tab(): argument must be positive integer', tab)
176 '''Sets the delimeter values.
177 @param comment The comment delimiter. Must be semicolon(";") or hash("#").
178 @param key The key/value delimiter. Must be equals("="), colon(":") or space.
179 @param value The delimiter between multiple key values. Must be comma(",") or space.
180 @return Raises Exception on invalid argument values.
185 elif(comment ==
'#'):
189 'IniWriter.comment_delimiters(): comment argument must be semicolon(":") or hash("#")', comment)
199 'IniWriter.key_delimiters(): key argument must be equals(=), comma(,) or space', key)
203 elif(value.isspace()):
207 'IniWriter.value_delimiters(): value argument must be comma(,) or space', value)
210 '''Add a section line to the INI file.
211 @param name The section name. Must be one or more characters from the set
212 - a-zA-Z0-9!$%()*+-.<>?@^_{|}~
213 @param comment An inline comment to add to the section name line.
214 @returns Raises Exception on invalid name or comment.
220 def key(self, name, varg, comment=None):
221 '''Add a key/value line to the INI file.
222 @param name The key name. Must be one or more characters from the set
223 - a-zA-Z0-9!$%()*+-.<>?@^_{|}~
224 @param varg The value for the key. May be a single value or a list of values.
229 - integer, positive or negative
230 - floating point number, positive or negative
232 @param comment An inline comment to add to the key/value line.
233 @returns Raises Exception on invalid name, value or comment.
236 if(isinstance(varg, list)):
244 elif(value
is False):
248 elif(isinstance(value, int)
or isinstance(value, float)):
250 elif(isinstance(value, str)):
253 raise Exception(
'IniWriter.key(): invalid value', value)
259 '''Add a comment line to the INI file.
260 @param comment A comment string. Valid comments are:
261 - None (default) adds a blank line
262 - ' ' empty or all white space string adds a blank line
263 - string of printing ASCII characters only, char codes 32-126
264 @returns Raises Exception on invalid ncomment.
270 '''Convert all lines added with the section(), key() and comment() functions
271 to a valid, formatted INI file.
272 @returns The formatted INI file as a string.
282 def add_comment(out, comment):
301 for line
in self.
__lines__lines:
310 elif(line[0] == self.
__KEY__KEY):
318 out += out_line + add_comment(out_line, line[3])
320 out_line =
'[' + line[1] +
']'
321 out += out_line + add_comment(out_line, line[2])
325 '''Write the formatted INI file to a file. Calls to_string().
326 @param fname The file name to write the INI file to.
327 @return Raises exceptions on file open or write errors.
329 with open(fname,
'w')
as fd:
def key(self, name, varg, comment=None)
Add a key/value line to the INI file.
def to_string(self)
Convert all lines added with the section(), key() and comment() functions to a valid,...
def __normalize_comment(self, comment)
def __validate_name(self, name)
def delimiters(self, comment=False, key=False, value=False)
Sets the delimeter values.
def booleans(self, true=False, false=False, none=False)
Sets the values to specify for boolean (and null) values.
def __normalize_string(self, string)
def section(self, name, comment=None)
Add a section line to the INI file.
def comment(self, comment=None)
Add a comment line to the INI file.
def clear(self)
Initialize or reset the writer to its constructor defaults.
def comment_tab(self, tab=None)
Sets the column value for the tab to inline comments.
def write(self, fname)
Write the formatted INI file to a file.
def __init__(self)
Ini file writer constructor.