#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os,sys
#获取脚本路径
def cur_file_dir():
    pathx = sys.argv[0]
    path,_file = os.path.split(pathx)
    if cmp(path,'') == 0:
        path = sys.path[0]
    #判断为脚本文件还是py2exe编译后的文件，如果是脚本文件，则返回的是脚本的目录，如果是py2exe编译后的文件，则返回的是编译后的文件路径
    if os.path.isdir(path):
        return path
    elif os.path.isfile(path):
        return os.path.dirname(path)
    
#获取父目录
def GetParentPath(strPath):
    if not strPath:
        return None;
    
    lsPath = os.path.split(strPath);
    if lsPath[1]:
        return lsPath[0];
    
    lsPath = os.path.split(lsPath[0]);
    return lsPath[0];

#获取所有文件后缀名的文件路径
def getAllJsonUIFile(path,ftype = '.ExportJson'):
    jsondir = path
    jsonfilelist = []
    for root, _dirs, files in os.walk(jsondir):
        for filex in files:          
            print filex
            name,text = os.path.splitext(filex)
            if cmp(text,ftype) == 0:
                jsonArr = []
                rootdir = path
                dirx = root[len(rootdir):]
                pathName = dirx + os.sep + filex
                jsonArr.append(pathName)
                (newPath,_name) = os.path.split(pathName)
                jsonArr.append(newPath)
                jsonArr.append(name)
                jsonfilelist.append(jsonArr)
    return jsonfilelist
#获取目录下的所有子目录
def getAllDirs(path):
    jsondir = path
    jsonfilelist = []
    for root, dirs, _files in os.walk(jsondir):
        for di in dirs:          
            print di
            rootdir = path
            dirx = root[len(rootdir):]
            pathName = dirx + os.sep + di
            jsonfilelist.append(pathName)
    return jsonfilelist
