import os
import sys


class pathutil(object):
    """路径处理工具类"""

    def __init__(self):
        # 判断调试模式
        debug_vars = dict((a, b) for a, b in os.environ.items()
                          if a.find('IPYTHONENABLE') >= 0)
        # 根据不同场景获取根目录
        if len(debug_vars) > 0:
            """当前为debug运行时"""
            self.rootPath = sys.path[0]
        elif getattr(sys, 'frozen', False):
            """当前为exe运行时"""
            self.rootPath = os.getcwd()
        else:
            """正常执行"""
            self.rootPath = sys.path[0]
        # 替换斜杠
        self.rootPath = self.rootPath.replace("\\", "/")

    def get_path_from_resources(self, fileName):
        """按照文件名拼接资源文件路径"""
        filePath = "%s/resources/%s" % (self.rootPath, fileName)
        return filePath


PathUtil = pathutil()

if __name__ == '__main__':
    """测试"""
    # path = PathUtil.get_path_from_resources("context.ini")
    print(PathUtil.rootPath)
    print(PathUtil.rootPath + "/demo/data/excel_data.xlsx")
