data_source.py 875 B

123456789101112131415161718192021222324252627
  1. # -*- coding:utf-8 -*-
  2. from configparser import ConfigParser
  3. from typing import Dict
  4. class DataSource(object):
  5. def __init__(self, ds_file: str):
  6. self.ds_file = ds_file
  7. self.config_parser = ConfigParser()
  8. self.config_parser.read(self.ds_file)
  9. self.ds_dict = {}
  10. self.source_type = None
  11. self.keys = []
  12. def get_datasource_dict(self) -> Dict[str, str]:
  13. for key in self.keys:
  14. try:
  15. self.ds_dict[key] = self.config_parser.get('base', key)
  16. except KeyError:
  17. raise KeyError('%s must specified at %s data source config.' % (key, self.source_type))
  18. return self.ds_dict
  19. def parse(self):
  20. if self.source_type is None:
  21. raise NotImplementedError('please use a specified class of data source.')
  22. return self.get_datasource_dict()