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

import sys,importlib,os,shutil
import database2text.tool as dbtt

class main(object):
	def __init__(self):
		self.readconfig()
		if "db" not in dbtt.cfgdata:
			dbtt.quit("配置文件中必须设置db=")
		dbt=importlib.import_module('database2text.%s' %(dbtt.cfgdata["db"]))
#		dbtt.quit(f'未找到名为{dbtt.cfgdata["db"]}的模块，请检查配置文件中的db=')
		dbt.run()
	def readconfig(self):
		fn="dbt.txt"
		if len(sys.argv)>1:
			fn=sys.argv[1]
		if not os.path.isfile(fn):
			print("can't open %s. you must create it or tell us another file name." %(fn))
			print("for help, we copy the sample file(dbt_*.txt) to you, enjoy it!")
			sampledir=os.path.join(os.path.dirname(os.path.abspath(dbtt.__file__)),"datafile","sample")
			for f in os.listdir(sampledir):
				if not os.path.exists(f):
					shutil.copy2(os.path.join(sampledir,f),".")
			sys.exit(-1)
		f=open("dbt.txt")
		dbtt.cfgdata={}
		for s in f.readlines():
			if s.find("=")<0:continue
			name=s[:s.find("=")].strip()
			value=s[s.find("=")+1:].strip()
			dbtt.cfgdata[name]=value

if __name__ == "__main__":
	main()

