forked from euloggeradmin/LootNanny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
98 lines (76 loc) · 2.85 KB
/
config.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
93
94
95
96
97
98
import os
import json
from typing import List
from helpers import format_filename
import utils.config_utils as CU
from modules.combat import Loadout
CONFIG_FILENAME = format_filename("config.json")
STREAMER_LAYOUT_DEFAULT = {'layout': [
[
['{}%', 'PERCENTAGE_RETURN', 'font-size: 20pt;']
],
[
['Total Loots: {}', 'TOTAL_LOOTS'],
['Total Spend: {} PED', 'TOTAL_SPEND'],
['Total Return: {} PED', 'TOTAL_RETURN']
]], 'style': 'font-size: 12pt;'}
class Config(object):
# Version
version = CU.ConfigValue(2)
# Core Configuration
location = CU.ConfigSecret("")
name = CU.ConfigValue("")
theme = CU.ConfigValue("dark")
# Screenshot Configuration
screenshot_directory = CU.ConfigValue("~/Documents/Globals/")
screenshot_delay = CU.ConfigValue(500)
screenshot_threshold = CU.ConfigValue(0)
screenshot_enabled = CU.ConfigValue(True)
# Combat Configuration
loadouts: List[Loadout] = CU.ConfigValue(None)
selected_loadout: Loadout = CU.ConfigValue(None)
# Streaming and Twitch
streamer_layout = CU.JsonConfigValue(STREAMER_LAYOUT_DEFAULT)
twitch_prefix = CU.ConfigValue("!")
twitch_token = CU.ConfigValue("oauth:")
twitch_username = CU.ConfigValue("NannyBot")
twitch_channel = CU.ConfigValue("")
twitch_commands_enabled = CU.ConfigValue(None)
def __init__(self):
# Initialize mutable options
self.initialized = False
self.loadouts = []
self.twitch_commands_enabled = ["commands", "allreturns", "toploots", "info"]
self.load_config()
self.print()
self.initialized = True
def load_config(self):
if not os.path.exists(CONFIG_FILENAME):
return
with open(CONFIG_FILENAME, 'r') as f:
CONFIG = json.loads(f.read())
if CONFIG.get("version", 1) < self.version.value:
fn_name = "version_{}_to_{}".format(CONFIG.get("version", 1), self.version.value)
CONFIG = getattr(CU, fn_name)(CONFIG)
for item, value in CONFIG.items():
setattr(self, item, value)
def dump(self) -> dict:
p = {}
for attr_name in dir(self):
attr = getattr(self, attr_name)
if isinstance(attr, CU.ConfigValue):
p[attr_name] = attr.value
return p
def print(self):
print(json.dumps(self.dump(), sort_keys=True, indent=4))
def save(self):
if not self.initialized:
return
with open(CONFIG_FILENAME, 'w') as f:
f.write(json.dumps(self.dump(), indent=2, sort_keys=True))
def __setattr__(self, item, value):
if not isinstance(getattr(self, item, None), CU.ConfigValue):
return super().__setattr__(item, value)
config_item: CU.ConfigValue = getattr(self, item)
config_item._value = value
self.save()