-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepicconfig.py
176 lines (141 loc) · 4.83 KB
/
epicconfig.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
from bs4 import BeautifulSoup
from os.path import join,exists
from time import time as ttime,sleep
import json
from datetime import datetime
import logging
import logging.config
from workshopcrawler import WorkshopCrawler
logger = logging.getLogger(__name__)
'''
Json structure
ver:
downloadPath:
paths:
appId: path
apps:
appId:
name:
modpath:
mods:
modId:
name:
date:
requirements:
id
'''
class EpicConfig:
def __init__(self,filename_data):
self._data = {}
self._fileData = filename_data
self._wscrawler = WorkshopCrawler()
self._version = '0.1'
def load(self):
if exists(self._fileData):
with open(self._fileData, 'r') as f:
self._data = json.load(f)
else:
print("file {} doesn't exists.".format(self._fileData))
self._data = {}
self._data['version'] = self._version
self._data['downloadPath'] = ''
def save(self):
with open(self._fileData, 'w') as outfile:
json.dump(self._data, outfile,indent=4)
def checkDownloadPath(self):
'''check that the download path exists (only)'''
if not self._data['downloadPath']:
return False
if self._data['downloadPath'] == '':
return False
if exists(self._data['downloadPath']):
return True
else:
return False
def setDownloadPath(self,dp):
if exists(dp):
self._data['downloadPath'] = dp
def getDownloadPath(self):
return self._data['downloadPath']
def getAppPathForMods(self,appId):
''' get the installation path for mods of a game'''
if appId in self._data['paths'].keys():
return self._data['paths'][appId]
return ''
def getAppId(self,modId):
'''get app id from mod id'''
for appId,app in self._data['apps'].items():
if modId in app['mods'].keys():
return appId
return 0
def getAllModIds(self):
'''get all modId of all apps'''
result = []
for app in self._data['apps'].values():
for modId in app['mods'].keys():
result.append(modId)
return result
def _setModMainData(self,appId,appName,modId,data):
if 'apps' not in self._data :
self._data['apps'] = {}
if appId not in self._data['apps'].keys():
# new app
self._data['apps'][appId] = {}
#else:
# old app
self._data['apps'][appId]['name'] = appName
if 'mods' not in self._data['apps'][appId]:
self._data['apps'][appId]['mods'] = {}
if modId not in self._data['apps'][appId]['mods'].keys():
self._data['apps'][appId]['mods'][modId] = {}
self._data['apps'][appId]['mods'][modId] = data
def getIdsOfEmptyPaths(self):
result = []
if 'paths' not in self._data:
self._data['paths'] = {}
for k in self._data['apps'].keys():
if k not in self._data['paths'].keys():
result.append(k)
return result
def setAppPath(self,appId,newPath):
if 'paths' not in self._data.keys():
self._data['paths'] = {}
self._data['paths'][appId] = newPath
def appHasPath(self,appId):
if 'paths' not in self._data.keys():
return False
if appId in self._data['paths'].keys():
return True
return False
def getAppName(self,appId):
if appId in self._data['apps'].keys():
return self._data['apps'][appId]['name']
return 'unknown?'
def getAppsIds(self):
if 'apps' not in self._data.keys():
return None
return self._data['apps'].keys()
def getAppMods(self,appId):
if appId not in self._data['apps'].keys():
return None
return self._data['apps'][appId]['mods']
def _getDate(self,date1):
fmt1 = '%d %b @ %I:%M%p'
fmt2 = '%d %b, %Y @ %I:%M%p'
try:
res1 = datetime.strptime(date1,fmt1)
except ValueError:
res1 = datetime.strptime(date1,fmt2)
if res1.year < 1940:
today = datetime.now()
res1 = res1.replace(year=today.year)
return res1
def is_new(self,newMod,oldMod):
''' return True if the first date is more recent than the second'''
res1 = self._getDate(newMod['date'])
res2 = self._getDate(oldMod['date'])
if res1 > res2:
return True
else:
return False