-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_file.py
92 lines (83 loc) · 2.85 KB
/
m_file.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
file module - handles file operations
mainly ini/json file storage of dictionaries
"""
from os import getcwd #, path #import os
try:
from os import path
except:
print('warning, path module not imported')
import json
import io
class ini:
"""
class for ini file operations
"""
def __init__(self):
self.version = '0.1'
self.host = ''
self.port = 0
def read(self, file1):
"""
reads ini file, returns dictionary
"""
dir_path = path.dirname(path.realpath(__file__))
print('working directory: ', dir_path)
print('config file location: ', file1)
try:
with io.open(file1) as data_file:#with io.open(path.join(dir_path, file1)) as data_file:
data_loaded = json.load(data_file)
except:
data_loaded = {'host': '10.10.10.1', 'port': 8003} #returns default
print('error reading file, using default: ', data_loaded)
print(data_loaded)
return data_loaded
def write(self, file1, data):
"""
writes dictionary into the file
"""
str1 = json.dumps(data, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
print(str1)
dir_path = path.dirname(path.realpath(__file__))
file1 = io.open(path.join(dir_path, file1), 'w', encoding='utf8')
file1.write(str1)
file1.close()
class ini2:
def read(self, file1):
"""
reads ini file, returns dictionary
"""
dir_path = path.dirname(path.realpath(__file__))
print('working directory: ', dir_path)
print('config file location: ', file1)
try:
with io.open(file1) as data_file:#with io.open(path.join(dir_path, file1)) as data_file:
data_loaded = json.load(data_file)
except:
data_loaded = {} #returns default
print('error reading file, returning default: ', data_loaded)
print('data loaded: ', data_loaded)
return data_loaded
def write(self, file1, data):
"""
writes dictionary into the file
"""
str1 = json.dumps(data, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
print('data to write: ' + str1)
print('writing file: ' + file1)
file_1 = io.open(file1, 'w', encoding='utf8')
file_1.write(str1)
file_1.close()
class uini:
# microptyhon 1.9.4 ini file operations implementation
def read(self, file1):
# reads json file, returns dictionary
self.data_dir = getcwd()
print('data dir: ', self.data_dir)
try:
with io.open(self.data_dir + file1) as data_file:
json_data = json.load(data_file)
except:
json_data = {}
print('json data: ', json_data)
return json_data