-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.py
51 lines (37 loc) · 1.63 KB
/
constants.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
import json
import string
import pathlib
class Constants():
"""wraps all constant data"""
CARD_LIMIT = 7
def __init__(self, constantJSON=pathlib.Path(__file__).parent.resolve() / 'data/constants.json'):
with open(constantJSON, 'r', encoding='utf8') as file:
constants = json.load(file)
# alternative names
self.__translations = {}
for actual_name, alt_names in constants['alternative_names'].items():
if isinstance(alt_names, list):
for alt_name in alt_names:
self.__translations[alt_name] = actual_name
else:
self.__translations[alt_names] = actual_name
self.alternativeNames = self.__translations.keys()
##
self.__wikis = {}
for wiki_name, subreddit_names in constants['sub_to_wiki'].items():
if isinstance(subreddit_names, list):
for subreddit_name in subreddit_names:
self.__wikis[subreddit_name] = wiki_name
else:
self.__wikis[subreddit_names] = wiki_name
def translateAlt(self, name):
"""translate alternative name or return name"""
return self.__translations.get(self._cleanName(name), name)
def sub_to_wiki(self, sub: str):
"""gets the appropriate wiki link for a sub"""
return self.__wikis.get(sub, sub)
def get_subs_to_check(self):
return '+'.join(list(self.__wikis.keys()))
def _cleanName(self, name):
"""ignore all special characters, numbers, whitespace, case"""
return ''.join(c for c in name.lower() if c in string.ascii_lowercase)