# -*- coding: utf-8 -*-
import logging

import yaml
import sys
import os
import os.path as fp

from domainics.db import set_dsn


def _get_home_path():

    main_file = sys.modules['__main__'].__file__
    
    homepath = fp.abspath(fp.dirname(main_file))

    
    return homepath

def load(homepath=None):

    if homepath is None:
        homepath = _get_home_path()
    
    print('homepath: ' + homepath)

    conf_dir = fp.join(homepath, 'conf')
    
    
    # 加载日志的配置文件
    import logging.config    
    conf_file = fp.join(conf_dir, 'logging.yaml')
    if fp.exists(conf_file):
        with open(conf_file) as f:
            conf = yaml.load(f)
        conf.setdefault('version', 1)
        logging.config.dictConfig(conf)
    # logging.basicConfig()

    _logger = logging.getLogger(__name__)
    _logger.info('configuration loaded, ' + conf_file) 

    abcs =logging.getLogger('abcs')

    abcs.info('test')
     
    conf_file = fp.join(conf_dir, 'database.yaml')
    if fp.exists(conf_file):
        with open(conf_file) as f:
            conf = yaml.load(f)
            for dsn, settings in conf.items():
                if dsn.upper() == 'DEFAULT':
                    dsn = 'DEFAULT'
                
                try:                
                    set_dsn(dsn=dsn, **settings)
                except Exception as ex:
                    _logger.error(str(ex), exc_info=True)

    _logger.info('configuration loaded, ' + conf_file)                       

# load()

@@ -154,20 +154,30 @@
     
-main_file = 
+cur_path = fp.abspath(
 sys.
@@ -208,69 +208,214 @@
 le__
+)
 %0A    
-%0A    homepath = fp.abspath(fp.dirname(main_file))%0A
+while True:%0A        cur_path = fp.dirname(cur_path)%0A        if fp.exists(fp.join(cur_path, '__init__.py')):%0A            continue%0A        break%0A%0A    if fp.exists(fp.join(cur_path, '__main__.py')):
 %0A    
-%0A
     
@@ -421,24 +421,73 @@
  return 
+cur_path%0A%0A    raise ValueError('can not find 
 home
+_
 path
+')%0A
 %0A%0Adef lo
@@ -567,20 +567,16 @@
 _path()%0A
-    
 %0A    pri
@@ -645,25 +645,17 @@
 'conf')%0A
-    %0A    
+%0A
 %0A    # %E5%8A%A0
@@ -688,20 +688,16 @@
 g.config
-    
 %0A    con
@@ -1038,17 +1038,16 @@
 nf_file)
- 
 %0A%0A    ab
@@ -1099,21 +1099,16 @@
 'test')%0A
-     
 %0A    con
@@ -1375,32 +1375,16 @@
 EFAULT'%0A
-                
 %0A       
@@ -1396,32 +1396,16 @@
     try:
-                
 %0A       
@@ -1603,31 +1603,8 @@
 ile)
-                       
 %0A%0A# 

refactor the home_path finder