# Path: library/utils/path.py
# def make_dir(directory, chmods=755 , force=True , backup=True) :
#     '''
#     用于创建目录
#     :parm
#         directory:创建目录
#         chmods：权限，必须是数字，例如755
#         force：当目录存在，强制执行
#         backup：当目录存在，备份文件，后缀名为%Y%m%d%H%M%S-bk
#     :return
#         True：成功
#         Flase：失败
#     '''
#     if os.path.exists(directory) :
#         if not force :
#             return False
#         
#         dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
#         if backup :
#             try : 
#                 pre_result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 pre_result = str(e)
#         else : 
#             pre_result = False
#             '''
#             #result = os.removedirs(dir)
#             try : 
#                 result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 result = str(e)
#             '''
#     else :
#         pre_result = False
#     
#     if not pre_result :
#         try :
#             chmods = int(chmods)
#         except :
#             chmods = 755
#         
#         try :
#             result = os.makedirs(directory, chmods, exist_ok=True)
#         except Exception as e :
#             result = str(e)
#     else :
#         result = False
#         
#     if not result :
#         result = True
#     else :
#         result = False
#         
#     return result
# 
# Path: library/utils/type_conv.py
# def bytes2string(data, encoding='utf-8'):
#     
#     '''
#     简单粗暴地把bytes字符串转换为str字符串
#     data必须是简单bytes格式
#     如果复杂，转化可能成功，但后续处理出差几率很大，请使用obj2string方法
#     :parm
#         data：字符串
#         encoding：编码
#     '''
#         
#     if isinstance(data, str):
#         return data
#     
#     try :
#         data = data.decode(encoding)
#         # result.decode('ascii') bytes解码会得到str
#     except :
#         new_data = obj2string(data, encoding=encoding)
#         if new_data[0] :
#             data = new_data[1]
#         
#     return data
# 
# Path: library/utils/type_conv.py
# def random_str(ranlen=8):
#     strings = ''
#     chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
#     length = len(chars) - 1
#     random = Random()
#     for i in range(ranlen):
#         i = i
#         strings = strings + chars[random.randint(0, length)]
#     return strings
# 
# Path: lykchat/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


import os, time, stat, subprocess, re, filetype
from library.utils.path import make_dir
from library.utils.type_conv import bytes2string
from library.utils.type_conv import random_str
from lykchat.settings import BASE_DIR
    from library.utils.syscmd import os_popen
    if os.path.exists(file) :
        if os.path.isdir(file) :
            return (False, file + '是一个存在的目录')
        else :
            if not 'a' in mode :
                if not force :
                    return (False, file + '文件存在')
                
                if backup == True :
                    try : 
                        dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
                        pre_result = os.renames(file , file + '-' + str(dt) + '-' + 'bk')
                    except Exception as e :
                        pre_result = str(e)
                else :
                    pre_result = False
            else :
                pre_result = False
    else :
        dirname = os.path.dirname(file)
        if os.path.exists(dirname) : 
            if not os.path.isdir(dirname) :
                return (False, '目录为文件')
        else :
            mkresult = make_dir(dirname)
            if not mkresult :
                return (False, '无法创建目录')
        
        pre_result = False
        

# Path: library/utils/path.py
# def make_dir(directory, chmods=755 , force=True , backup=True) :
#     '''
#     用于创建目录
#     :parm
#         directory:创建目录
#         chmods：权限，必须是数字，例如755
#         force：当目录存在，强制执行
#         backup：当目录存在，备份文件，后缀名为%Y%m%d%H%M%S-bk
#     :return
#         True：成功
#         Flase：失败
#     '''
#     if os.path.exists(directory) :
#         if not force :
#             return False
#         
#         dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
#         if backup :
#             try : 
#                 pre_result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 pre_result = str(e)
#         else : 
#             pre_result = False
#             '''
#             #result = os.removedirs(dir)
#             try : 
#                 result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 result = str(e)
#             '''
#     else :
#         pre_result = False
#     
#     if not pre_result :
#         try :
#             chmods = int(chmods)
#         except :
#             chmods = 755
#         
#         try :
#             result = os.makedirs(directory, chmods, exist_ok=True)
#         except Exception as e :
#             result = str(e)
#     else :
#         result = False
#         
#     if not result :
#         result = True
#     else :
#         result = False
#         
#     return result
# 
# Path: library/utils/type_conv.py
# def bytes2string(data, encoding='utf-8'):
#     
#     '''
#     简单粗暴地把bytes字符串转换为str字符串
#     data必须是简单bytes格式
#     如果复杂，转化可能成功，但后续处理出差几率很大，请使用obj2string方法
#     :parm
#         data：字符串
#         encoding：编码
#     '''
#         
#     if isinstance(data, str):
#         return data
#     
#     try :
#         data = data.decode(encoding)
#         # result.decode('ascii') bytes解码会得到str
#     except :
#         new_data = obj2string(data, encoding=encoding)
#         if new_data[0] :
#             data = new_data[1]
#         
#     return data
# 
# Path: library/utils/type_conv.py
# def random_str(ranlen=8):
#     strings = ''
#     chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
#     length = len(chars) - 1
#     random = Random()
#     for i in range(ranlen):
#         i = i
#         strings = strings + chars[random.randint(0, length)]
#     return strings
# 
# Path: lykchat/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Path: library/utils/file.py
import os, time, stat, subprocess, re, filetype
from library.utils.path import make_dir
from library.utils.type_conv import bytes2string
from library.utils.type_conv import random_str
from lykchat.settings import BASE_DIR
    from library.utils.syscmd import os_popen

    if os.path.exists(file) :
        if os.path.isdir(file) :
            return (False, file + '是一个存在的目录')
        else :
            if not 'a' in mode :
                if not force :
                    return (False, file + '文件存在')
                
                if backup == True :
                    try : 
                        dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
                        pre_result = os.renames(file , file + '-' + str(dt) + '-' + 'bk')
                    except Exception as e :
                        pre_result = str(e)
                else :
                    pre_result = False
            else :
                pre_result = False
    else :
        dirname = os.path.dirname(file)
        if os.path.exists(dirname) : 
            if not os.path.isdir(dirname) :
                return (False, '目录为文件')
        else :
            mkresult = make_dir(dirname)
            if not mkresult :
                return (False, '无法创建目录')
        
        pre_result = False
        

    content = bytes2string(content)