# -*-coding=utf-8-*-
#常用的工具集合

import codecs,re
import json
from collections import OrderedDict

class Toolkit():
    @staticmethod
    def save2file( filename, content):
        # 保存为文件
        filename = filename + ".txt"
        f = open(filename, 'a')
        f.write(content)
        f.close()

    @staticmethod
    def save2filecn( filename, content):
        # 保存为文件
        filename = filename + ".txt"
        f = codecs.open(filename, 'a',encoding='utf-8')
        f.write(content)
        f.close()

    @staticmethod
    def getUserData(cfg_file):
        f=open(cfg_file,'r')
        account={}
        for i in f.readlines():
            ctype,passwd=i.split('=')
            #print ctype
            #print passwd
            account[ctype.strip()]=passwd.strip()
        return account

    @staticmethod
    def getJsonData(cfg_file):
        with open(cfg_file, "r") as f:
            configuration = json.load(f, object_pairs_hook=OrderedDict)
            return configuration

    @staticmethod
    def saveTimestamp(filename, content):
        # 保存为文件
        filename = filename + ".data"
        f = codecs.open(filename, 'w',encoding='utf-8')
        f.write(content)
        f.close()

    @staticmethod
    def filename_filter(filename_old):
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        return filename