-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathread_data.py
More file actions
59 lines (45 loc) · 1.94 KB
/
Copy pathread_data.py
File metadata and controls
59 lines (45 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import yaml
import json
import os
from configparser import ConfigParser
from test.common.logger import logger
class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults=defaults)
def optionxform(self, option_str):
return option_str
def get_sections(self):
return self._sections
class ReadFileData:
def __init__(self):
self.base_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
def load_yaml(self, yaml_file_name):
file_path = os.path.join(self.base_path, "data", yaml_file_name)
logger.info("load {} file......".format(file_path))
with open(file_path, encoding='utf-8') as f:
_data = yaml.safe_load(f)
logger.info("read data ==>> {} ".format(_data))
return _data
def load_json(self):
file_path = os.path.join(self.base_path, "config", "setting.json")
logger.info("load {} file......".format(file_path))
with open(file_path, encoding='utf-8') as f:
_data = json.load(f)
logger.info("read data ==>> {} ".format(_data))
return _data
def load_ini(self):
file_path = os.path.join(self.base_path, "config", "setting.ini")
logger.info("load {} file......".format(file_path))
config = MyConfigParser()
config.read(file_path, encoding="UTF-8")
_data = config.get_sections()
# print("read data ==>> {} ".format(_data))
return _data
def write_yml(self, yaml_file_name, _data):
file_path = os.path.join(self.base_path, "data", yaml_file_name)
logger.info("load {} file......".format(file_path))
with open(file_path, 'wb') as f:
logger.info("write data ==>> {} ".format(_data))
yaml.safe_dump(_data, f, default_flow_style=False,
explicit_start=True, allow_unicode=True, encoding='utf-8')
data = ReadFileData()