1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#!/usr/bin/python
# -*-coding:utf-8 -*
import re, os, sys
import base64
import binascii
import datetime
def _tokyf(d, base = '', nbSpace = 0):
out = ""
for key, v in d.items():
if type(v) is int :
out += (key +'#' + str(v)) + '\n'
elif type(v) is str:
out += (key + '=' + v) + '\n'
elif type(v) == datetime.datetime:
out += (key + '@' + str(v)) + '\n'
elif type(v) is dict:
out += ( '[ ' + key + ' ]' ) + '\n'
out += _tokyf(v, base+key+'.',nbSpace+2)
elif type(v) is list:
isMultiple=False
if len(v) == 0:
continue
for e in v[1:]:
if type(e) is not type(v[0]):
isMultiple = True
if isMultiple:
print(key + ' is a multi typed list', file=sys.stderr)
exit()
else:
if type(v[0]) is int:
out += ( '[[#' + base + key + ' ]]' ) + '\n'
for i in v :
out += '#' + i + '\n'
elif type(v[0]) is str:
out += ( '[[=' + base + key + ' ]]' ) + '\n'
for i in v :
out += '=' + i + '\n'
elif str(type(v[0])) == "<class 'datetime.datetime'>":
out += ( '[[@' + key + ' ]]' ) + '\n'
for i in v :
out += '@' + i + '\n'
elif type(v[0]) is list:
out += ( '[[_' + key + ' ]]' ) + '\n'
for e in v:
out += _tokyf(e, base+key+'.',nbSpace+2)
elif type(v[0]) is dict:
for e in v:
out += ( '[[ ' + key + ' ]]' ) + '\n'
out += _tokyf(e, base+key+'.',nbSpace+2)
out += ('') + '\n'
else:
print(key + ' is a list of unknow type !' + str(type(v[0])) + ')',
file=sys.stderr)
exit()
else:
print('Type error :' + key +
' don\'t have knowd type (' + str(type(v)) + ')', file=sys.stderr)
exit()
return out
def getTo( d, key, dico=True ):
tD=d
ls = key.strip().split('.')
for i,k in enumerate(ls):
if type(tD) == dict:
if k in tD:
tD = tD[k]
else:
if dico:
tD[k] = dict()
else:
tD[k] = list()
tD = tD[k]
return tD
elif type(tD) == list:
if type(tD[-1]) == dict:
if k in tD[-1] :
tD = tD[-1][k]
else:
if dico:
tD[-1][k] = dict()
else:
tD[-1][k] = list()
return tD[-1][k]
else:
tD = tD[-1];
return tD
def _fromkyf( d, f ):
tD=d
listType=type(None)
for line in f:
line = line.strip()
if line :
if line[0] == '[':
if line[1] == '[': # List
if line[2] == ' ': # of table
key = line[2:-2].strip()
tD = getTo(d, key, dico=False)
if type(tD) == list:
tD.append(dict());
tD = tD[-1]
continue
else:
print('Error', file=sys.stderr)
exit()
continue
elif line[2] == '=':
key = line[3:-2].strip()
tD = getTo(d, key, dico=False)
listType=type('')
continue
elif line[2] == '#':
key = line[2:-2].strip()
listType=type(0)
tD = getTo(d, key, dico=False)
continue
elif line[2] == '@':
key = line[2:-2].strip()
listType=type( None )
tD = getTo(d, key, dico=False)
continue
else: # Table
key = line[1:-1].strip()
tD = getTo(d, key)
continue
elif line[0] == '=':
if listType == type(''):
tD.append(line[1:])
else:
print('Error', file=sys.stderr)
exit()
continue
elif line[0] == '#':
if listType == type(0):
tD.append( int(line[1:]) )
else:
print('Error', file=sys.stderr)
continue
elif line[0] == '@':
if listType == type(None):
tD.append(line[1:])
else:
print('Error', file=sys.stderr)
continue
else:
listType=type(None)
i = line.find('=')
if i != -1: # String arg
key = line[:i].rstrip()
tD[key] = line[i+1:].lstrip()
continue
i = line.find('#')
if i != -1: # Number arg
key = line[:i].rstrip()
value = int(line[1+i:].strip())
if key:
tD[key] = value
elif type(tD) == list:
tD.append(value)
else:
print('Error', file=sys.stderr)
exit()
continue
o = line.find('@')
if i != -1: # Date arg
print('Date no supported yet')
exit()
else:
print('What ?')
exit()
return d
def dump( d ) :
return _tokyf(d)
def load( f ) :
d = dict()
return _fromkyf( d, f )
|