aboutsummaryrefslogtreecommitdiff
path: root/libkyf/libkyf.py
diff options
context:
space:
mode:
Diffstat (limited to 'libkyf/libkyf.py')
-rw-r--r--libkyf/libkyf.py187
1 files changed, 187 insertions, 0 deletions
diff --git a/libkyf/libkyf.py b/libkyf/libkyf.py
new file mode 100644
index 0000000..b978430
--- /dev/null
+++ b/libkyf/libkyf.py
@@ -0,0 +1,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 )
+