def setup_config(self, createNewConfig, browsersetup, cfg_override): """start the in-browser configuration server, create a config if no configuration is found or provide migration help for old CM versions initialize the configuration if no config setup is needed/requested """ if browsersetup: port = cfg_override.pop('server.port', False) cherrymusicserver.browsersetup.configureAndStartCherryPy(port) if createNewConfig: newconfigpath = pathprovider.configurationFile() + '.new' cfg.write_to_file(cfg.from_defaults(), newconfigpath) log.i(_('New configuration file was written to:{br}{path}').format( path=newconfigpath, br=os.linesep )) sys.exit(0) if not pathprovider.configurationFileExists(): if pathprovider.fallbackPathInUse(): # temp. remove @ v0.30 or so self.printMigrationNoticeAndExit() else: cfg.write_to_file(cfg.from_defaults(), pathprovider.configurationFile()) self.printWelcomeAndExit() self._init_config(cfg_override)
def _check_for_config_updates(self, known_config): new = [] deprecated = [] default = configuration.from_defaults() for property in configuration.to_list(default): #@ReservedAssignment if property.name not in known_config and not property.hidden: new.append(property.name) for property in configuration.to_list(known_config): #@ReservedAssignment if property.name not in default: deprecated.append(property.name) if new: log.i('''New configuration options available: %s Using default values for now.''', '\n\t\t\t'.join(new)) if deprecated: log.i('''The following configuration options are not used anymore: %s''', '\n\t\t\t'.join(deprecated)) if new or deprecated: log.i('''Start with --newconfig to generate a new default config file next to your current one. ''', )
def _init_config(self, override_dict): defaults = cfg.from_defaults() filecfg = cfg.from_configparser(pathprovider.configurationFile()) custom = defaults.replace(filecfg, on_error=log.e) global config config = custom.replace(override_dict, on_error=log.e) self._check_for_config_updates(defaults, filecfg)
def cherryconfig(cfg=None): from cherrymusicserver import configuration cfg = cfg or {} c = configuration.from_defaults() c = c.update({'media.basedir': '/'}) c = c.update(cfg) return c
def cherryconfig(cfg=None): from cherrymusicserver import configuration cfg = cfg or {} c = configuration.from_defaults() c = c.update({'media.basedir': os.path.join(os.path.dirname(__file__), 'data_files')}) c = c.update(cfg) return c
def _init_config(self): global config defaultcfg = configuration.from_defaults() configFilePath = util.configurationFile() log.d('loading configuration from %s', configFilePath) filecfg = configuration.from_configparser(configFilePath) config = defaultcfg + filecfg self._check_for_config_updates(filecfg)
def setUp(self): self.playlistdb = MockPlaylistDB() cherry.config = configuration.from_defaults() self.http = httphandler.HTTPHandler(cherry.config,MockModel()) for apicall, func in self.http.handlers.items(): try: getattr(self,'test_'+func.__name__) except AttributeError: print('Missing test for api handler %s!' % func.__name__)
def setup_config(self, createNewConfig, browsersetup, cfg_override): if browsersetup: port = cfg_override.pop('server.port', False) cherrymusicserver.browsersetup.configureAndStartCherryPy(port) if createNewConfig: newconfigpath = pathprovider.configurationFile() + '.new' cfg.write_to_file(cfg.from_defaults(), newconfigpath) log.i('New configuration file was written to:{br}{path}'.format( path=newconfigpath, br=os.linesep)) sys.exit(0) if not pathprovider.configurationFileExists(): if pathprovider.fallbackPathInUse(): # temp. remove @ v0.30 or so self.printMigrationNoticeAndExit() else: cfg.write_to_file(cfg.from_defaults(), pathprovider.configurationFile()) self.printWelcomeAndExit() self._init_config(cfg_override)
def __init__(self, update=None, createNewConfig=False, dropfiledb=False): if createNewConfig: newconfigpath = util.configurationFile() + '.new' configuration.write_to_file(configuration.from_defaults(), newconfigpath) log.i('''New configuration file was written to: ''' + newconfigpath) exit(0) if not util.configurationFileExists(): configuration.write_to_file(configuration.from_defaults(), util.configurationFile()) self.printWelcomeAndExit() self._init_config() self.db = sqlitecache.SQLiteCache(util.databaseFilePath('cherry.cache.db')) if not update == None or dropfiledb: CherryMusic.UpdateThread(self.db,update,dropfiledb).start() else: self.cherrymodel = cherrymodel.CherryModel(self.db) self.httphandler = httphandler.HTTPHandler(config, self.cherrymodel) self.server()
def cherryconfig(cfg=None): from cherrymusicserver import configuration cfg = cfg or {} c = configuration.from_defaults() c = c.update({ 'media.basedir': os.path.join(os.path.dirname(__file__), 'data_files') }) c = c.update(cfg) return c
def setup_config(self, createNewConfig, browsersetup, cfg_override): if browsersetup: port = cfg_override.pop('server.port', False) cherrymusicserver.browsersetup.configureAndStartCherryPy(port) if createNewConfig: newconfigpath = pathprovider.configurationFile() + '.new' cfg.write_to_file(cfg.from_defaults(), newconfigpath) log.i('New configuration file was written to:{br}{path}'.format( path=newconfigpath, br=os.linesep )) sys.exit(0) if not pathprovider.configurationFileExists(): if pathprovider.fallbackPathInUse(): # temp. remove @ v0.30 or so self.printMigrationNoticeAndExit() else: cfg.write_to_file(cfg.from_defaults(), pathprovider.configurationFile()) self.printWelcomeAndExit() self._init_config(cfg_override)
def cherryconfig(override=None): """ Context manager providing a CherryMusic default configuration that can be overridden. :param dict override: The overridden config values """ override = override or {} config = cherrymusicserver.config or configuration.from_defaults() config = config.update(override) with patch('cherrymusicserver.config', config): yield config
def cherryconfig(override=None): """ Context manager providing a CherryMusic default configuration that can be overridden. :param dict override: The overridden config values """ override = override or {} config = cherrymusicserver.config or configuration.from_defaults() config = config.update(override) with patch('cherrymusicserver.config', config): yield config
def saveconfig(self, values): collect_errors = cfg.error_collector() baseconfig = cfg.from_defaults() newconfig = json.loads(values, encoding='str') customcfg = baseconfig.replace(newconfig, collect_errors) if collect_errors: badkeys = (e.key for e in collect_errors) return json.dumps({"status": "error", 'fields': list(badkeys)}) cfg.write_to_file(customcfg, pathprovider.configurationFile()) # kill server in a second threading.Timer(1, lambda: cherrypy.engine.exit()).start() # so request should still reach client... return json.dumps({"status": "success"})
def saveconfig(self, values): collect_errors = cfg.error_collector() baseconfig = cfg.from_defaults() newconfig = json.loads(values, encoding='str') customcfg = baseconfig.replace(newconfig, collect_errors) if collect_errors: badkeys = (e.key for e in collect_errors) return json.dumps({"status": "error", 'fields': list(badkeys)}) cfg.write_to_file(customcfg, pathprovider.configurationFile()) # kill server in a second threading.Timer(1, lambda: cherrypy.engine.exit()).start() # so request should still reach client... return json.dumps({"status": "success"})
def _init_config(self, override_dict): """update the internal configuration using the following hierarchy: command_line_config > file_config > default_config check if there are new or deprecated configuration keys in the config file """ defaults = cfg.from_defaults() filecfg = cfg.from_configparser(pathprovider.configurationFile()) custom = defaults.replace(filecfg, on_error=log.e) global config config = custom.replace(override_dict, on_error=log.e) self._check_for_config_updates(defaults, filecfg)
def test_file_functions(self): import tempfile tf = tempfile.NamedTemporaryFile() cfg = configuration.from_defaults() configuration.write_to_file(cfg, tf.name) # compare with the limited cfg the configparser can return parsed = configuration.from_configparser(tf.name) assert len(cfg) == len(parsed), '%r <--> %r' % (cfg, parsed) for key in cfg: this, that = cfg[key], parsed[key] assert Key(this.name) == Key(that.name) assert str(this.value) == str(that.value) assert len(cfg) == len(parsed), 'no properties must be added while comparing: %r <--> %r' % (this, that)
def setup_config(override_dict=None): """ Updates the internal configuration using the following hierarchy: override_dict > file_config > default_config Notifies the user if there are new or deprecated configuration keys. See :mod:`~cherrymusicserver.configuration`. """ defaults = cfg.from_defaults() filecfg = cfg.from_configparser(pathprovider.configurationFile()) custom = defaults.replace(filecfg, on_error=log.e) if override_dict: custom = custom.replace(override_dict, on_error=log.e) global config config = custom _notify_about_config_updates(defaults, filecfg)
def setup_config(override_dict=None): """ Updates the internal configuration using the following hierarchy: override_dict > file_config > default_config Notifies the user if there are new or deprecated configuration keys. See :mod:`~cherrymusicserver.configuration`. """ defaults = cfg.from_defaults() filecfg = cfg.from_configparser(pathprovider.configurationFile()) custom = defaults.replace(filecfg, on_error=log.e) if override_dict: custom = custom.replace(override_dict, on_error=log.e) global config config = custom _notify_about_config_updates(defaults, filecfg)
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/> # from mock import * import unittest import json from contextlib import contextmanager import cherrymusicserver as cherry from cherrymusicserver import configuration cherry.config = configuration.from_defaults() from cherrymusicserver import httphandler from cherrymusicserver import service from cherrymusicserver.cherrymodel import CherryModel, MusicEntry from cherrymusicserver import log class MockAction(Exception): pass class MockModel: def __init__(self): pass
def create_default_config_file(path): """ Creates or overwrites a default configuration file at `path` """ cfg.write_to_file(cfg.from_defaults(), path) log.i(_('Default configuration file written to %(path)r'), {'path': path})
# """ Things that are helpful when testing the CherryMusic backend """ import shutil import tempfile from contextlib import contextmanager from mock import * from nose.tools import * from cherrymusicserver import configuration from cherrymusicserver import database from cherrymusicserver import service _default_config = configuration.from_defaults() # load only once @contextmanager def cherryconfig(override=None): """ Context manager providing a CherryMusic default configuration that can be overridden. :param dict override: The overridden config values """ override = override or {} config = _default_config.update(override) with patch('cherrymusicserver.config', config): yield config
# 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 this program. If not, see <http://www.gnu.org/licenses/> # from mock import * import unittest import json import cherrymusicserver as cherry from cherrymusicserver import configuration cherry.config = configuration.from_defaults() from cherrymusicserver import httphandler from cherrymusicserver import service from cherrymusicserver.cherrymodel import MusicEntry from cherrymusicserver import log class MockAction(Exception): pass class MockModel: def __init__(self): pass def search(self,value,isFastSearch=False): if isFastSearch:
def create_default_config_file(path): """ Creates or overwrites a default configuration file at `path` """ cfg.write_to_file(cfg.from_defaults(), path) log.i(_('Default configuration file written to %(path)r'), {'path': path})
def setupConfig(self): cherry.config = configuration.from_defaults() cherry.config = cherry.config.replace({ 'media.basedir': self.testdir, })
import os import shutil import tempfile from contextlib import contextmanager from mock import * from nose.tools import * import cherrymusicserver from cherrymusicserver import configuration from cherrymusicserver import database from cherrymusicserver import service _default_config = configuration.from_defaults() # load only once @contextmanager def cherryconfig(override=None): """ Context manager providing a CherryMusic default configuration that can be overridden. :param dict override: The overridden config values """ override = override or {} config = cherrymusicserver.config or configuration.from_defaults() config = config.update(override) with patch('cherrymusicserver.config', config): yield config @contextmanager
def setupConfig(self): cherry.config = configuration.from_defaults() with configuration.extend(cherry.config) as config: config.media.basedir = self.testdir config.search.autoupdate = 'True'
def setupConfig(self): cherry.config = configuration.from_defaults() cherry.config = cherry.config.replace({ 'media.basedir': self.testdir, })
def setupConfig(self): cherry.config = configuration.from_defaults() cherry.config.media.basedir = self.testdir