77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
from configobj import ConfigObj
|
|
import shutil
|
|
import posixpath
|
|
|
|
|
|
class ConfigError(Exception):
|
|
pass
|
|
|
|
|
|
class Config:
|
|
"""
|
|
Gère le fichier de configuration
|
|
"""
|
|
def __init__(self):
|
|
|
|
try:
|
|
self.obj = ConfigObj('config.conf', file_error=True)
|
|
except:
|
|
# pas de fichier: config par défaut
|
|
print('creating config file')
|
|
shutil.copyfile('default.conf', 'config.conf')
|
|
self.obj = ConfigObj('config.conf')
|
|
|
|
self.check_config()
|
|
print('config loaded')
|
|
|
|
@property
|
|
def domains(self):
|
|
return list(self.obj['domains'].keys())
|
|
|
|
@property
|
|
def app(self):
|
|
return self.obj['api']['app']
|
|
|
|
@property
|
|
def token(self):
|
|
return self.obj['api']['token']
|
|
|
|
@property
|
|
def server(self):
|
|
return self.obj['api']['server']
|
|
|
|
def check_config(self):
|
|
if not 'api' in self.obj:
|
|
raise ConfigError("no 'api' section in config file")
|
|
for k in ['app', 'token', 'extensions', 'server']:
|
|
if not k in self.obj['api']:
|
|
raise ConfigError("no '"+k+"' value in config file, subsection 'api'")
|
|
if not 'domains' in self.obj:
|
|
raise ConfigError("no 'domains' section in config file")
|
|
if len(self.obj['domains']) == 0:
|
|
raise ConfigError("no domains in config file, subsection domains")
|
|
for d in self.obj['domains']:
|
|
print('check dom:', d)
|
|
for k in ['movies', 'series']:
|
|
if not k in self.obj['domains'][d]:
|
|
raise ConfigError("no '"+k+"' value in config file, subsection 'domains/"+d+"'")
|
|
|
|
def get_serie_dir(self, domain):
|
|
return self.obj['domains'][domain]['series']
|
|
|
|
def get_excluded_serie_dir(self, domain):
|
|
return self.obj['domains'][domain]['no_series']
|
|
|
|
def get_movie_dir(self, domain):
|
|
return self.obj['domains'][domain]['movies']
|
|
|
|
def get_excluded_movie_dir(self, domain):
|
|
return self.obj['domains'][domain]['no_movies']
|
|
|
|
def get_domain(self, domain):
|
|
return self.obj['domains'][domain]['domain']
|
|
|
|
def is_valid_file(self, name):
|
|
_, ext = posixpath.splitext(name)
|
|
return ext[1:] in self.obj['api']['extensions']
|
|
|