/
config.py
389 lines (295 loc) · 13.9 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Author: Nic Wolfe <nic@wolfeden.ca>
# URL: http://code.google.com/p/sickbeard/
#
# This file is part of Sick Beard.
#
# Sick Beard is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sick Beard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
import cherrypy
import os.path
import datetime
import re
from sickbeard import helpers
from sickbeard import logger
from sickbeard import naming
from sickbeard import db
import sickbeard
naming_ep_type = ("%(seasonnumber)dx%(episodenumber)02d",
"s%(seasonnumber)02de%(episodenumber)02d",
"S%(seasonnumber)02dE%(episodenumber)02d",
"%(seasonnumber)02dx%(episodenumber)02d")
naming_ep_type_text = ("1x02", "s01e02", "S01E02", "01x02")
naming_multi_ep_type = {0: ["-%(episodenumber)02d"]*len(naming_ep_type),
1: [" - " + x for x in naming_ep_type],
2: [x + "%(episodenumber)02d" for x in ("x", "e", "E", "x")]}
naming_multi_ep_type_text = ("extend", "duplicate", "repeat")
naming_sep_type = (" - ", " ")
naming_sep_type_text = (" - ", "space")
def change_HTTPS_CERT(https_cert):
if https_cert == '':
sickbeard.HTTPS_CERT = ''
return True
if os.path.normpath(sickbeard.HTTPS_CERT) != os.path.normpath(https_cert):
if helpers.makeDir(os.path.dirname(os.path.abspath(https_cert))):
sickbeard.HTTPS_CERT = os.path.normpath(https_cert)
logger.log(u"Changed https cert path to " + https_cert)
else:
return False
return True
def change_HTTPS_KEY(https_key):
if https_key == '':
sickbeard.HTTPS_KEY = ''
return True
if os.path.normpath(sickbeard.HTTPS_KEY) != os.path.normpath(https_key):
if helpers.makeDir(os.path.dirname(os.path.abspath(https_key))):
sickbeard.HTTPS_KEY = os.path.normpath(https_key)
logger.log(u"Changed https key path to " + https_key)
else:
return False
return True
def change_LOG_DIR(log_dir):
if os.path.normpath(sickbeard.LOG_DIR) != os.path.normpath(log_dir):
if helpers.makeDir(log_dir):
sickbeard.LOG_DIR = os.path.normpath(log_dir)
logger.sb_log_instance.initLogging()
logger.log(u"Initialized new log file in " + log_dir)
cherry_log = os.path.join(sickbeard.LOG_DIR, "cherrypy.log")
cherrypy.config.update({'log.access_file': cherry_log})
logger.log(u"Changed cherry log file to " + cherry_log)
else:
return False
return True
def change_NZB_DIR(nzb_dir):
if nzb_dir == '':
sickbeard.NZB_DIR = ''
return True, u"Removed NZB directory"
if os.path.normpath(sickbeard.NZB_DIR) != os.path.normpath(nzb_dir):
if helpers.makeDir(nzb_dir):
sickbeard.NZB_DIR = os.path.normpath(nzb_dir)
logger.log(u"Changed NZB folder to " + nzb_dir)
else:
return False, u"Unable to create NZB directory '" + nzb_dir + "', directory unchanged"
return True, u"Changed NZB directory to " + nzb_dir
def change_TORRENT_DIR(torrent_dir):
if torrent_dir == '':
sickbeard.TORRENT_DIR = ''
return True, u"Removed TORRENT directory"
if os.path.normpath(sickbeard.TORRENT_DIR) != os.path.normpath(torrent_dir):
if helpers.makeDir(torrent_dir):
sickbeard.TORRENT_DIR = os.path.normpath(torrent_dir)
logger.log(u"Changed torrent directory to " + torrent_dir)
else:
return False, u"Unable to create TORRENT directory '" + torrent_dir + "', directory unchanged"
return True, u"Changed TORRENT directory to " + torrent_dir
def change_TV_DOWNLOAD_DIR(tv_download_dir):
if tv_download_dir == '':
sickbeard.TV_DOWNLOAD_DIR = ''
return True
if os.path.normpath(sickbeard.TV_DOWNLOAD_DIR) != os.path.normpath(tv_download_dir):
if helpers.makeDir(tv_download_dir):
sickbeard.TV_DOWNLOAD_DIR = os.path.normpath(tv_download_dir)
logger.log(u"Changed TV download folder to " + tv_download_dir)
else:
return False
return True
def change_SEARCH_FREQUENCY(freq):
if freq == None:
freq = sickbeard.DEFAULT_SEARCH_FREQUENCY
else:
freq = int(freq)
if freq < sickbeard.MIN_SEARCH_FREQUENCY:
freq = sickbeard.MIN_SEARCH_FREQUENCY
sickbeard.SEARCH_FREQUENCY = freq
sickbeard.currentSearchScheduler.cycleTime = datetime.timedelta(minutes=sickbeard.SEARCH_FREQUENCY)
sickbeard.backlogSearchScheduler.cycleTime = datetime.timedelta(minutes=sickbeard.get_backlog_cycle_time())
return True, u"SEARCH_FREQUENCY changed to " + str(freq)
def change_VERSION_NOTIFY(version_notify):
oldSetting = sickbeard.VERSION_NOTIFY
sickbeard.VERSION_NOTIFY = version_notify
if version_notify == False:
sickbeard.NEWEST_VERSION_STRING = None;
if oldSetting == False and version_notify == True:
sickbeard.versionCheckScheduler.action.run() #@UndefinedVariable
def CheckSection(CFG, sec):
""" Check if INI section exists, if not create it """
try:
CFG[sec]
return True
except:
CFG[sec] = {}
return False
################################################################################
# Check_setting_int #
################################################################################
def minimax(val, low, high):
""" Return value forced within range """
try:
val = int(val)
except:
val = 0
if val < low:
return low
if val > high:
return high
return val
################################################################################
# Check_setting_int #
################################################################################
def check_setting_int(config, cfg_name, item_name, def_val):
try:
my_val = int(config[cfg_name][item_name])
except:
my_val = def_val
try:
config[cfg_name][item_name] = my_val
except:
config[cfg_name] = {}
config[cfg_name][item_name] = my_val
logger.log(item_name + " -> " + str(my_val), logger.DEBUG)
return my_val
################################################################################
# Check_setting_float #
################################################################################
def check_setting_float(config, cfg_name, item_name, def_val):
try:
my_val = float(config[cfg_name][item_name])
except:
my_val = def_val
try:
config[cfg_name][item_name] = my_val
except:
config[cfg_name] = {}
config[cfg_name][item_name] = my_val
logger.log(item_name + " -> " + str(my_val), logger.DEBUG)
return my_val
################################################################################
# Check_setting_str #
################################################################################
def check_setting_str(config, cfg_name, item_name, def_val, log=True):
try:
my_val = config[cfg_name][item_name]
except:
my_val = def_val
try:
config[cfg_name][item_name] = my_val
except:
config[cfg_name] = {}
config[cfg_name][item_name] = my_val
if log:
logger.log(item_name + " -> " + my_val, logger.DEBUG)
else:
logger.log(item_name + " -> ******", logger.DEBUG)
return my_val
class ConfigMigrator():
def __init__(self, config_obj):
"""
Initializes a config migrator that can take the config from the version indicated in the config
file up to the version required by SB
"""
self.config_obj = config_obj
# check the version of the config
self.config_version = check_setting_int(config_obj, 'General', 'config_version', 0)
self.migration_names = {1: 'Custom naming'}
def migrate_config(self):
"""
Calls each successive migration until the config is the same version as SB expects
"""
while self.config_version < sickbeard.CONFIG_VERSION:
next_version = self.config_version + 1
if next_version in self.migration_names:
migration_name = ': ' + self.migration_names[next_version]
else:
migration_name = ''
helpers.backupVersionedFile(sickbeard.CONFIG_FILE, next_version)
# do the migration, expect a method named _migrate_v<num>
logger.log(u"Migrating config up to version "+str(next_version)+migration_name)
getattr(self, '_migrate_v'+str(next_version))()
self.config_version = next_version
def _migrate_v1(self):
"""
Reads in the old naming settings from your config and generates a new config template from them.
"""
sickbeard.NAMING_PATTERN = self._name_to_pattern()
logger.log("Based on your old settings I'm setting your new naming pattern to: " + sickbeard.NAMING_PATTERN)
sickbeard.NAMING_CUSTOM_ABD = bool(check_setting_int(self.config_obj, 'General', 'naming_dates', 0))
if sickbeard.NAMING_CUSTOM_ABD:
sickbeard.NAMING_ABD_PATTERN = self._name_to_pattern(True)
logger.log("Adding a custom air-by-date naming pattern to your config: " + sickbeard.NAMING_ABD_PATTERN)
else:
sickbeard.NAMING_ABD_PATTERN = naming.name_abd_presets[0]
sickbeard.NAMING_MULTI_EP = int(check_setting_int(self.config_obj, 'General', 'naming_multi_ep_type', 1))
# see if any of their shows used season folders
myDB = db.DBConnection()
season_folder_shows = myDB.select("SELECT * FROM tv_shows WHERE flatten_folders = 0")
# if any shows had season folders on then prepend season folder to the pattern
if season_folder_shows:
old_season_format = check_setting_str(self.config_obj, 'General', 'season_folders_format', 'Season %02d')
if old_season_format:
try:
new_season_format = old_season_format % 9
new_season_format = new_season_format.replace('09', '%0S')
new_season_format = new_season_format.replace('9', '%S')
logger.log(u"Changed season folder format from " + old_season_format + " to " + new_season_format + ", prepending it to your naming config")
sickbeard.NAMING_PATTERN = new_season_format + os.sep + sickbeard.NAMING_PATTERN
except (TypeError, ValueError):
logger.log(u"Can't change " + old_season_format + " to new season format", logger.ERROR)
# if no shows had it on then don't flatten any shows and don't put season folders in the config
else:
logger.log(u"No shows were using season folders before so I'm disabling flattening on all shows")
# don't flatten any shows at all
myDB.action("UPDATE tv_shows SET flatten_folders = 0")
sickbeard.NAMING_FORCE_FOLDERS = naming.check_force_season_folders()
def _name_to_pattern(self, abd=False):
# get the old settings from the file
use_periods = bool(check_setting_int(self.config_obj, 'General', 'naming_use_periods', 0))
ep_type = check_setting_int(self.config_obj, 'General', 'naming_ep_type', 0)
sep_type = check_setting_int(self.config_obj, 'General', 'naming_sep_type', 0)
use_quality = bool(check_setting_int(self.config_obj, 'General', 'naming_quality', 0))
use_show_name = bool(check_setting_int(self.config_obj, 'General', 'naming_show_name', 1))
use_ep_name = bool(check_setting_int(self.config_obj, 'General', 'naming_ep_name', 1))
# make the presets into templates
naming_ep_type = ("%Sx%0E",
"s%0Se%0E",
"S%0SE%0E",
"%0Sx%0E")
naming_sep_type = (" - ", " ")
# set up our data to use
if use_periods:
show_name = '%S.N'
ep_name = '%E.N'
ep_quality = '%Q.N'
abd_string = '%A.D'
else:
show_name = '%SN'
ep_name = '%EN'
ep_quality = '%QN'
abd_string = '%A-D'
if abd:
ep_string = abd_string
else:
ep_string = naming_ep_type[ep_type]
finalName = ""
# start with the show name
if use_show_name:
finalName += show_name + naming_sep_type[sep_type]
# add the season/ep stuff
finalName += ep_string
# add the episode name
if use_ep_name:
finalName += naming_sep_type[sep_type] + ep_name
# add the quality
if use_quality:
finalName += naming_sep_type[sep_type] + ep_quality
if use_periods:
finalName = re.sub("\s+", ".", finalName)
return finalName