# -*- coding: utf-8 -*-

from json import loads

def move(dit_s, src, dit_d, dst):
	value = dit_s[src]
	del dit_s[src]
	dit_d[dst] = value


def move_dict(dit, dit_d, key=None):
	dit_t = {}
	for dkey, dvar in dit.items():
		dit_t[dkey] = dvar
		move(dit, dkey, dit_d, key+u'.'+dkey)
		base_bulid(dit_t, dit_d, key)
		dit_t = {}
		
	del dit_d[key]

	
def move_list(dit, dit_d, key=None):
	dit_t = {}
	for i in range(len(dit)):
		dit_s = dit[i]
		for lkey, lvar in dit_s.items():
			dit_t[lkey] = lvar
			move(dit_s, lkey, dit_d, key+u'.'+str(i+1)+u'.'+lkey)
			base_bulid(dit_t, dit_d, key+u'.'+str(i+1))
			dit_t = {}
				
	del dit_d[key]

	
def base_bulid(dit, dit_d, prefix_key=None):
	for key,var in dit.items():
		if type(var) == dict:
			if prefix_key is None:
				move_dict(var, dit_d, key)
			else:
				move_dict(var, dit_d, prefix_key+u'.'+key)
				
		elif type(var) == list:
			if prefix_key is None:
				move_list(var, dit_d, key)
			else:
				move_list(var, dit_d, prefix_key+u'.'+key)
				
		else: # the end
			if prefix_key is None:
				move(dit, key, dit_d, key)
			else:
				move(dit, key, dit_d, prefix_key+u'.'+key)

			
