]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/pages/torrent/client/inifile.py
1 # Written by John Hoffman
2 # see LICENSE.txt for license information
5 reads/writes a Windows-style INI file
15 d = { '': {'aa':'bb','cc':'11'}, 'eee': {'ff':'gg'} }
17 the encoder can also take this as input:
19 d = { 'aa': 'bb, 'cc': 11, 'eee': {'ff':'gg'} }
21 though it will only decode in the above format. Keywords must be strings.
22 Values that are strings are written surrounded by quotes, and the decoding
23 routine automatically strips any.
24 Booleans are written as integers. Anything else aside from string/int/float
25 may have unpredictable results.
28 from cStringIO
import StringIO
29 from traceback
import print_exc
30 from types
import DictType
, StringType
32 from types
import BooleanType
44 def ini_write(f
, d
, comment
=''):
48 assert type(k
) == StringType
50 if type(v
) == DictType
:
52 print 'new section:' +k
54 assert not a
.has_key(k
)
58 assert type(kk
) == StringType
60 assert not aa
.has_key(kk
)
61 if type(vv
) == BooleanType
:
63 if type(vv
) == StringType
:
67 print 'a['+k
+']['+kk
+'] = '+str(vv
)
70 assert not aa
.has_key(k
)
71 if type(v
) == BooleanType
:
73 if type(v
) == StringType
:
77 print 'a[\'\']['+k
+'] = '+str(v
)
80 for c
in comment
.split('\n'):
87 r
.write('\n['+k
+']\n')
92 r
.write(kk
+' = '+aa
[kk
]+'\n')
106 def errfunc(lineno
, line
, err
):
107 print '('+str(lineno
)+') '+err
+': '+line
109 errfunc
= lambda lineno
, line
, err
: None
111 def ini_read(f
, errfunc
= errfunc
):
117 for i
in xrange(len(ll
)):
126 errfunc(i
,l
,'syntax error')
128 l1
= l
[1:-1].strip().lower()
130 errfunc(i
,l
,'syntax error')
133 errfunc(i
,l
,'duplicate section')
145 errfunc(i
,l
,'syntax error')
147 k
= k
.strip().lower()
149 if len(v
) > 1 and ( (v
[0] == '"' and v
[-1] == '"') or
150 (v
[0] == "'" and v
[-1] == "'") ):
153 errfunc(i
,l
,'syntax error')
156 errfunc(i
,l
,'duplicate entry')