-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDictionaryUtils.py
56 lines (48 loc) · 1.73 KB
/
DictionaryUtils.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
import os.path
from typing import Dict
class DictionaryUtils(object):
@staticmethod
def property_to_dict(path_to_file: str) -> Dict:
""" property to map, property to dict, property file to dict
"""
if(path_to_file==None):
return None
if not os.path.isfile(path_to_file):
return None
myprops = {}
with open(path_to_file, 'r') as f:
for line in f:
#removes trailing whitespace and '\n' chars
line = line.rstrip()
#skips blanks and comments w/o =
if "=" not in line :
continue
#skips comments which contain =
if line.startswith("#") :
continue
k, v = line.split("=", 1)
myprops[k] = v
return myprops
@staticmethod
def property_to_string(path_to_file: str, separator: str = " ") -> str:
""" property to map, property to dict, property file to dict
"""
if(path_to_file==None):
return None
if not os.path.isfile(path_to_file):
return None
return_value = []
with open(path_to_file, 'r') as f:
for line in f:
#removes trailing whitespace and '\n' chars
line = line.rstrip()
#skips blanks and comments w/o =
if "=" not in line :
continue
#skips comments which contain =
if line.startswith("#") :
continue
k, v = line.split("=", 1)
return_value.append(f"{k}={v}")
myprops[k] = v
return separator.join(return_value)