-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigUtils.py
65 lines (56 loc) · 1.7 KB
/
configUtils.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
from configparser import ConfigParser
import json
import boto3
import os
from collections import defaultdict
def findConfig() -> str:
"""
Finds config file locally
:return: string of config file location
"""
# searches for main file, falls back to example file if not found
fileList = [
'./reddit.cfg',
'../reddit.cfg',
'../../reddit.cfg',
'./example_reddit.cfg',
'../example_reddit.cfg',
'../../example_reddit.cfg'
]
for f in fileList:
if os.path.exists(f):
return f
raise RuntimeError("Reddit config file not found. Place it in either ./ or ../")
DEFAULT_KEYS = {
'reddit_api': ['CLIENTID', 'CLIENTSECRET', 'PASSWORD', 'USERNAME'],
'S3_access': ['ACCESSKEY', 'SECRETKEY', ],
'Discord': ['BOTTOKEN', 'MYSNOWFLAKEID', 'CHANNELSNOWFLAKEID'],
'Postgres': ['USERNAME', 'PASSWORD', 'HOST', 'PORT', 'DATABASE']
}
def parseConfig(
cfgFile: str,
keysToRead: dict = None
) -> dict:
"""
Read in the config data from a location to a dictionary and return that dictionary.
:param cfgFile: location of config file. Can be an S3 location
:param keysToRead:
:return: config dictionary
"""
if keysToRead is None:
keysToRead = DEFAULT_KEYS
parser = ConfigParser()
cfg = defaultdict(dict)
if cfgFile[:2].lower() == 's3':
s3 = boto3.client('s3')
pathSplit = cfgFile.replace('s3://', '').split('/')
bucket = pathSplit[0]
objLoc = '/'.join(pathSplit[1:])
obj = s3.get_object(Bucket=bucket, Key=objLoc)
_ = parser.read_string(obj['Body'].read().decode())
else:
_ = parser.read(cfgFile)
for k, vList in keysToRead.items():
for v in vList:
cfg[k][v] = json.loads(parser.get(k, v)) # json helps with list conversion
return cfg