-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_parser.py
More file actions
137 lines (107 loc) · 4.5 KB
/
Copy pathconfig_parser.py
File metadata and controls
137 lines (107 loc) · 4.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import configparser
import os
import codes
from CloudStorages.cloud_storage_interface import CloudStorageInterface
from poe import PoeDictKeys
class ProjectConfig(codes.ReturnCodes):
def __init__(self):
super().__init__()
self.config = configparser.ConfigParser()
self.config_name = ".project_config.ini"
if os.path.exists(self.config_name):
self.config.read(self.config_name)
self.delimiter = "."
self.storage_index = "storage"
self.poe_index = "poe"
self.poe_b_cookie = "b_cookie"
self.poe_lat_cookie = "lat_cookie"
self.poe_chat_id = "chatId"
self.section_application = "app"
self.application_language = "language"
def saveConfig(self) -> None:
with open(self.config_name, "w") as config_file:
self.config.write(config_file)
def deletePoeCredentials(self) -> None:
self.config.remove_section(self.poe_index)
self.saveConfig()
def deleteStorageEntry(self, provider_name: str, storage_name: str) -> None:
section_name = self.storage_index + self.delimiter + provider_name + self.delimiter + storage_name
self.config.remove_section(section_name)
self.saveConfig()
def removeFieldFromSection(self, section: str, field: str):
if section in self.config and field in self.config[section]:
self.config.remove_option(section, field)
self.saveConfig()
def isEntryExist(self, section_name: str) -> bool:
isExist = True
try:
self.config[section_name]
except KeyError:
isExist = False
return isExist
def addPoeAccount(self, token_dict: dict) -> None:
isExist = self.isEntryExist(self.poe_index)
if isExist:
return self.section_exist_code
token_dict[PoeDictKeys().b_cookie_dict_key] = token_dict[PoeDictKeys().b_cookie_dict_key].replace("%", "%%")
token_dict[PoeDictKeys().lat_cookie_dict_key] = token_dict[PoeDictKeys().lat_cookie_dict_key].replace("%", "%%")
self.config[self.poe_index] = token_dict
self.saveConfig()
def setPoeChatId(self, chatId: str or int) -> None:
self.config[self.poe_index][self.poe_chat_id] = str(chatId)
self.saveConfig()
def getPoeChatId(self) -> int:
isExist = self.isEntryExist(self.poe_index)
if not isExist:
return self.section_not_exist_code
try:
return int(self.config[self.poe_index][self.poe_chat_id])
except KeyError:
return None
def addStorageEntry(self, provider_name: str, storage_name: str, key_value_pairs: dict) -> str:
section_name = self.storage_index + self.delimiter + provider_name + self.delimiter + storage_name
isExist = self.isEntryExist(section_name)
if isExist:
return self.section_exist_code
# iterating through data
self.config[section_name] = {}
for item in key_value_pairs.items():
self.config[section_name][item[0]] = item[1]
self.saveConfig()
return self.success_code
def getStorageEntrees(self) -> list[list]:
result_list = list()
cloud_interface = CloudStorageInterface()
for each_section in self.config.sections():
# Checking if entry is a storage type
splitted_section_name = each_section.split(self.delimiter)
if not splitted_section_name[0] == self.storage_index:
continue
cloud_instance = cloud_interface.getStorageInstance(splitted_section_name[1])
if cloud_instance == cloud_interface.unknown_code:
continue
if cloud_instance.is_auth_via_credentials:
result = cloud_instance.loginViaCredentials(
self.config[each_section]["email"], self.config[each_section]["password"]
)
elif cloud_instance.is_auth_via_token:
result = cloud_instance.loginViaToken(each_section["token"])
if result == cloud_instance.success_code:
result_list.append([cloud_instance, splitted_section_name[2]])
return result_list
def getPoeTokens(self) -> dict:
if not self.isEntryExist(self.poe_index):
return None
return {
PoeDictKeys().b_cookie_dict_key: self.config[self.poe_index][PoeDictKeys().b_cookie_dict_key].replace("%%", "%"),
PoeDictKeys().lat_cookie_dict_key: self.config[self.poe_index][PoeDictKeys().lat_cookie_dict_key].replace(
"%%", "%"
),
}
def getAppLanguage(self) -> str:
if not self.isEntryExist(self.section_application):
return self.section_not_exist_code
return self.config[self.section_application][self.application_language]
def setAppLanguage(self, language: str) -> None:
self.config[self.section_application] = {self.application_language: language}
self.saveConfig()