# -*- encoding: utf-8 -*-

from collections import Counter
import os, shutil, tempfile

def str_to_counter(s):
	"""Converts string of form "word;word..." to pythons Counter instance containig
		count of every word.
	"""
	if s != s:
		return Counter({u'empty': 1})
	else:
		splitted = [e.split(':') for e in s.split(';')]
		splitted = [(e[0], int(e[1])) for e in splitted]
		return Counter(dict(splitted))

def remove_accents(text):
    trans = dict((ord(a), b+'_') for a, b in zip(u'ąćęłńóśżźĄĆĘŁŃÓŚŻŹ', u'acelnoszzACELNOSZZ'))
    return text.translate(trans)

def is_unicode_or_str(val):
	return (type(val) is unicode) or (type(val) is str)

def dict_to_str(dictionary, skip_keys=[]):
	"""Pythons dict serialization function for creation of command string"""
	result = ""
	for key in dictionary:
		if not key in skip_keys:
			val = dictionary[key]
			val = ('"' + val + '"') if is_unicode_or_str(val) else str(val)
			result += ", " + key + "=" + val
	return result

def goto_new_tempdir():
	temp_dir = tempfile.mkdtemp()
	if not os.path.exists(temp_dir):
			os.mkdir(temp_dir)
	os.chdir(temp_dir)
	return temp_dir

def move_file_from_dir(filename, directory, newpath):
	filepath = os.path.join(directory, filename)
	shutil.copy2(filepath, newpath)
	shutil.rmtree(directory)
