1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-12 21:22:17 +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):
try: """
ret = config.read([ '/etc/supysonic', os.path.expanduser('~/.supysonic') ]) Config object to work with config file
except (ConfigParser.MissingSectionHeaderError, ConfigParser.ParsingError), e: """
print >>sys.stderr, "Error while parsing the configuration file(s):\n%s" % str(e) def __init__(self):
return False # 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:
self.config.read(config_file)
except Exception as e:
err = 'Config file is corrupted.\n{0}'.format(e)
raise SystemExit(err)
if not ret: def check(self):
print >>sys.stderr, "No configuration file found" """
return False Checks the config for mandatory fields
"""
try:
self.config.get('base', 'database_uri')
except (NoSectionError, NoOptionError):
raise SystemExit('No database URI set')
try: def get(self, section, option):
config.get('base', 'database_uri') """
except: Returns a config option value from config file
print >>sys.stderr, "No database URI set"
return False
return True :param section: section where the option is stored
:param option: option name
:return: a config option value
:rtype: string
"""
try:
return self.config.get(section, option)
except (NoSectionError, NoOptionError):
return None
def get(section, name): def get_mime(self, extension):
try: """
return config.get(section, name) Returns mimetype of an extension based on config file
except:
return None
: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