from stockprophet.cli import entry_point
from stockprophet.crawler import (
    init_stock_type, init_stock_category
)
from stockprophet.db import init_db

from .utils import read_db_settings


def preprocessing() -> bool:
    result = False
    # noinspection PyBroadException
    try:
        db_config = read_db_settings()
        if not db_config:
            print("config.ini 找不到 'database' 區段")
            return result
    except Exception:
        print("無法讀取或解析config.ini")
        return result

    # noinspection PyBroadException
    try:
        init_db(db_config)
        init_stock_category()
        init_stock_type()
        result = True
    except Exception as e:
        print("無法連線資料庫: %s" % (str(e), ))
    return result


def main():
    if preprocessing():
        entry_point()

[(24, 'stockprophet.db.init_db', 'init_db', False, 'from stockprophet.db import init_db\n'), (25, 'stockprophet.crawler.init_stock_category', 'init_stock_category', False, 'from stockprophet.crawler import init_stock_type, init_stock_category\n'), (26, 'stockprophet.crawler.init_stock_type', 'init_stock_type', False, 'from stockprophet.crawler import init_stock_type, init_stock_category\n'), (35, 'stockprophet.cli.entry_point', 'entry_point', False, 'from stockprophet.cli import entry_point\n')]