From da51dfca7b47bc0a5cbb63b7e0af6328b28b0050 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 1 Jan 2017 17:24:04 +0100 Subject: Init commit --- libkyf/dump.py | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 libkyf/dump.py (limited to 'libkyf/dump.py') diff --git a/libkyf/dump.py b/libkyf/dump.py new file mode 100644 index 0000000..b58576c --- /dev/null +++ b/libkyf/dump.py @@ -0,0 +1,198 @@ +#!/usr/bin/python +# -*-coding:utf-8 -* + +""" + +Implémentation de l'écriture du format kyf. + +""" + + +import re, os, sys +import base64 +import binascii +import datetime + + +__all__ = ['dump'] + + + +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])) == "": + 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 ) + -- cgit v1.2.3