1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-13 21:52:18 +00:00

WIP: Rewrite config.py to make project more clean

This commit is contained in:
Óscar García Amor 2017-08-07 08:58:32 +02:00
parent f01f63fad0
commit 2a3f13bb82

View File

@ -1,49 +1,72 @@
# coding: utf-8 # -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# This file is part of Supysonic. # This file is part of Supysonic.
#
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# Copyright (C) 2013 Alban 'spl0k' Féron
# #
# This program is free software: you can redistribute it and/or modify # Copyright (C) 2013-2017 Alban 'spl0k' Féron
# it under the terms of the GNU Affero General Public License as published by # 2017 Óscar García Amor
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # Distributed under terms of the GNU AGPLv3 license.
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, sys, tempfile, ConfigParser from ConfigParser import ConfigParser, NoOptionError, NoSectionError
config = ConfigParser.RawConfigParser({ 'cache_dir': os.path.join(tempfile.gettempdir(), 'supysonic') }) import mimetypes
import os
import tempfile
def check(): class Config(object):
"""
Config object to work with config file
"""
def __init__(self):
# Seek for standard locations
config_file = [
'supysonic.conf',
os.path.expanduser('~/.config/supysonic/supysonic.conf'),
os.path.expanduser('~/.supysonic'),
'/etc/supysonic'
]
self.config = ConfigParser({ 'cache_dir': os.path.join(tempfile.gettempdir(), 'supysonic') })
# Try read config file or raise error
try: try:
ret = config.read([ '/etc/supysonic', os.path.expanduser('~/.supysonic') ]) self.config.read(config_file)
except (ConfigParser.MissingSectionHeaderError, ConfigParser.ParsingError), e: except Exception as e:
print >>sys.stderr, "Error while parsing the configuration file(s):\n%s" % str(e) err = 'Config file is corrupted.\n{0}'.format(e)
return False raise SystemExit(err)
if not ret:
print >>sys.stderr, "No configuration file found"
return False
def check(self):
"""
Checks the config for mandatory fields
"""
try: try:
config.get('base', 'database_uri') self.config.get('base', 'database_uri')
except: except (NoSectionError, NoOptionError):
print >>sys.stderr, "No database URI set" raise SystemExit('No database URI set')
return False
return True def get(self, section, option):
"""
Returns a config option value from config file
def get(section, name): :param section: section where the option is stored
:param option: option name
:return: a config option value
:rtype: string
"""
try: try:
return config.get(section, name) return self.config.get(section, option)
except: except (NoSectionError, NoOptionError):
return None return None
def get_mime(self, extension):
"""
Returns mimetype of an extension based on config file
:param extension: extension string
:return: mimetype
:rtype: string
"""
guessed_mime = mimetypes.guess_type('dummy.' + extension, False)[0]
config_mime = self.get('mimetypes', extension)
default_mime = 'application/octet-stream'
return guessed_mime or config_mime or default_mime