def save_yaml():
    saveall = gain_profilePath('/save_all_operation/')
    if os.path.exists(saveall) is False:
        os.mkdir(saveall)
    saveallyaml = saveall+'/all_name.yaml'
    f1 = open(gain_profilePath('/etc/name.yaml'),'r')
    f2 = open(saveallyaml ,'a')
    for i in f1.readlines():
        f2.write(i)
    f2.close()
    f1.close()
def SEND_MAIL(k,sender,password):
    '''
    send mail
    '''
    subject = 'python email test'
    smtpserver = 'smtp.163.com'
    msg = MIMEMultipart('alternative')
    msg['Subject'] = u"共享文件服务器" 
    company_name = k[0].split('_')[0]
    user = k[1]['username']
    Pass = k[1]['passwd']
    if 'vpn' in k[1]:
        html = u"""\
            您好,给你新开的VPN账号和密码为:$user,$vpn_pass。VPN连接方法见附件,不会连接VPN的联系网管设定使用。
            您需要的文件系统账号跟密码为:$user,$Pass。
            文件系统使用方法:使用前必须连接上VPN,打开计算机在地址栏输入\\192.168.1.240,然后进入$company_name目录。
            以后文件系统的权限修改请联系[email protected]!
            xxx-xxx
        """ 
        html = Template(html)
        if 'vpn' in k[1]:
            html = html.substitute(user=k[1]['username'],Pass=k[1]['passwd'],vpn_pass=k[1]['vpn']['vpn_pass'],company_name=k[0].split('_')[0])
        try:

            attach_file = gain_profilePath('/mail attachment')+userconf()['file name']
            att = MIMEText(open(attach_file, 'rb').read(), 'base64', 'utf-8')
            att["Content-Type"] = 'application/octet-stream'
            att["Content-Disposition"] = 'attachment; filename="file"'
            msg.attach(att)
            part2 = MIMEText(html, 'html','utf-8') 
            msg.attach(part2)
        except Exception,e:
            error_info = e
            log_record(error_info)
            exit(0)
def userconf():
    try:
        config = yaml.load(file(gain_profilePath('/etc/userconf.yaml'), 'r'))
        return config
    except Exception,e:
        error_info = e
        log_record(error_info)
        exit(0)
def pass_count():  
    count = 0
    list_pass = []
    di = yaml.load(file(gain_profilePath('/etc/name.yaml'), 'r'))
    for i in di.items():
        if i[1]['op'] == 'add':
            count += 1
    for i in range(count):
        trunk_pass = GenPasswd(12)
        list_pass.append(trunk_pass)
    return list_pass
def YAML_RECO():
    '''
    load yaml doc
    '''
    try:
        config = yaml.load(file(gain_profilePath('/etc/name.yaml'), 'r'))
        return config
        log_record("name.yaml open success!")
    except Exception,e:
        error_info = e
        log_record(error_info)
        exit(0)
def add_pass(x):
    old_file = gain_profilePath('/etc/name.yaml')
    f = open(old_file,'r')
    for_pass = []
    w_str=""
    if x is None:
        exit(0)
    else:
        for line in f:
            if ru.search(line):
                line = ru.sub('  passwd: '+''.join(x[:1]),line)
                x = x[1:]
                w_str+=line
            else:
                w_str+=line
        wf = open(old_file,'w')
        wf.write(w_str)
        f.close()
        wf.close()
def log_record(logCON):
    Path = gain_profilePath('/log/samba_operation_log.txt')
    logger = Logger(logname=Path, loglevel=1, logger="operation record").getlog()
    logger.debug(logCON)