54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
#coding:utf-8
|
|
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 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']
|
|
|
|
@property
|
|
def extensions(self):
|
|
return self.obj['api']['extensions']
|
|
|
|
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'")
|
|
|
|
def is_valid_file(self, name):
|
|
_, ext = posixpath.splitext(name)
|
|
return ext[1:] in self.obj['api']['extensions']
|
|
|