def test_from_file_should_error_if_unknown_plugin_type(self): config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['plugins'] = { '%s.Foo' % self.__module__: {}, } with self._write(config_dict) as f: with self.assertRaises(AttributeError): from_file(f)
def test_from_file_should_error_if_unknown_plugin_type_module(self): config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['plugins'] = { 'foo.bar.Baz': {}, } with self._write(config_dict) as f: with self.assertRaises(ImportError): from_file(f)
def test_from_file_should_clean_urls(self): clean_urls = True config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['clean_urls'] = clean_urls with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(clean_urls, configuration.clean_urls)
def test_from_file_should_parse_author(self): author = 'Bart' config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['author'] = author with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(author, configuration.author)
def test_from_file_should_parse_title(self): title = 'My first Betty site' config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['title'] = title with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(title, configuration.title)
def test_from_file_should_root_path(self): configured_root_path = '/betty' expected_root_path = '/betty/' config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['root_path'] = configured_root_path with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(expected_root_path, configuration.root_path)
def __init__(self, configuration_template_file_path: str): with open( path.join(path.dirname(__file__), 'test_integration_assets', configuration_template_file_path)) as f: self._configuration = from_file(f) self._output_directory = None self._server = None
def test_from_file_should_parse_assets_directory_path(self): with TemporaryDirectory() as assets_directory_path: config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['assets_directory_path'] = assets_directory_path with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(assets_directory_path, configuration.assets_directory_path)
def test_from_file_should_content_negotiation(self): content_negotiation = True config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['content_negotiation'] = content_negotiation with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(content_negotiation, configuration.content_negotiation)
def get_configuration( config_file_path: Optional[str]) -> Optional[Configuration]: if config_file_path is None: config_file_path = join(getcwd(), 'betty.json') try: with open(config_file_path) as f: return from_file(f) except FileNotFoundError: pass
def __init__(self, configuration_file_path: str, *args, **kwargs): with open(configuration_file_path) as f: self._configuration = from_file(f) self._configuration.react.react_weakref(self._save_configuration) self._app = App(self._configuration) self._configuration_file_path = configuration_file_path super().__init__(*args, **kwargs) self._set_window_title() self.init() central_widget = QWidget() central_layout = QGridLayout() central_widget.setLayout(central_layout) self.setCentralWidget(central_widget) pane_selectors_layout = QVBoxLayout() central_layout.addLayout(pane_selectors_layout, 0, 0, Qt.AlignTop | Qt.AlignLeft) panes_layout = QStackedLayout() central_layout.addLayout(panes_layout, 0, 1, Qt.AlignTop | Qt.AlignRight) self._general_configuration_pane = _ProjectGeneralConfigurationPane( self._app) panes_layout.addWidget(self._general_configuration_pane) pane_selectors_layout.addWidget( _PaneButton(pane_selectors_layout, panes_layout, self._general_configuration_pane, 'General', self)) self._theme_configuration_pane = _ProjectThemeConfigurationPane( self._app) panes_layout.addWidget(self._theme_configuration_pane) pane_selectors_layout.addWidget( _PaneButton(pane_selectors_layout, panes_layout, self._theme_configuration_pane, 'Theme', self)) self._localization_configuration_pane = _ProjectLocalizationConfigurationPane( self._app) panes_layout.addWidget(self._localization_configuration_pane) pane_selectors_layout.addWidget( _PaneButton(pane_selectors_layout, panes_layout, self._localization_configuration_pane, 'Localization', self)) for extension_type in discover_extension_types(): if issubclass(extension_type, GuiBuilder): extension_pane = _ProjectExtensionConfigurationPane( self._app, extension_type) panes_layout.addWidget(extension_pane) pane_selectors_layout.addWidget( _PaneButton(pane_selectors_layout, panes_layout, extension_pane, extension_type.gui_name(), self))
def test_from_file_should_parse_minimal(self, extension, dumper): with self._writes(dumper(self._MINIMAL_CONFIG_DICT), extension) as f: configuration = from_file(f) self.assertEquals(self._MINIMAL_CONFIG_DICT['output'], configuration.output_directory_path) self.assertEquals(self._MINIMAL_CONFIG_DICT['base_url'], configuration.base_url) self.assertEquals('Betty', configuration.title) self.assertEquals('production', configuration.mode) self.assertEquals('/', configuration.root_path) self.assertFalse(configuration.clean_urls)
def test_from_file_should_parse_one_plugin_without_configuration(self): config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['plugins'] = { NonConfigurablePlugin.name(): None, } with self._write(config_dict) as f: configuration = from_file(f) expected = { NonConfigurablePlugin: None, } self.assertEquals(expected, dict(configuration.plugins))
def test_from_file_should_parse_locale_locale(self): locale = 'nl-NL' locale_config = { 'locale': locale, } config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['locales'] = [locale_config] with self._write(config_dict) as f: configuration = from_file(f) self.assertDictEqual(OrderedDict({ locale: LocaleConfiguration(locale), }), configuration.locales)
def test_from_file_should_parse_one_plugin_with_configuration(self): config_dict = dict(**self._MINIMAL_CONFIG_DICT) plugin_configuration = { 'check': 1337, } config_dict['plugins'] = { Plugin.name(): plugin_configuration, } with self._write(config_dict) as f: configuration = from_file(f) expected = { Plugin: plugin_configuration, } self.assertEquals(expected, configuration.plugins)
async def _init_ctx(ctx, configuration_file_path: Optional[str] = None) -> None: ctx.ensure_object(dict) if 'initialized' in ctx.obj: return ctx.obj['initialized'] = True logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(CliHandler()) ctx.obj['commands'] = { 'clear-caches': _clear_caches, 'demo': _demo, } if configuration_file_path is None: try_configuration_file_paths = [ path.join(getcwd(), 'betty.%s' % extension) for extension in {'json', 'yaml', 'yml'} ] else: try_configuration_file_paths = [configuration_file_path] for try_configuration_file_path in try_configuration_file_paths: with suppress(FileNotFoundError): with open(try_configuration_file_path) as f: logger.info('Loading the configuration from %s.' % try_configuration_file_path) configuration = from_file(f) app = App(configuration) async with app: ctx.obj['commands']['generate'] = _generate ctx.obj['commands']['serve'] = _serve for extension in app.extensions.values(): if isinstance(extension, CommandProvider): for command_name, command in extension.commands.items( ): ctx.obj['commands'][command_name] = command ctx.obj['app'] = app return if configuration_file_path is not None: raise CommandValueError('Configuration file "%s" does not exist.' % configuration_file_path)
async def _init_ctx(ctx, configuration_file_path: Optional[str] = None) -> None: ctx.ensure_object(dict) if 'initialized' in ctx.obj: return ctx.obj['initialized'] = True logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(CliHandler()) ctx.obj['commands'] = { 'clear-caches': _clear_caches, } if configuration_file_path is None: try_configuration_file_paths = [join(getcwd(), 'betty.%s' % extension) for extension in {'json', 'yaml', 'yml'}] else: try_configuration_file_paths = [configuration_file_path] for try_configuration_file_path in try_configuration_file_paths: with suppress(FileNotFoundError): with open(try_configuration_file_path) as f: logger.info('Loading the site from %s...' % try_configuration_file_path) try: configuration = from_file(f) except ConfigurationError as e: logger.error(str(e)) exit(2) site = Site(configuration) async with site: ctx.obj['commands']['generate'] = _generate for plugin in site.plugins.values(): if isinstance(plugin, CommandProvider): for command_name, command in plugin.commands.items(): if command_name in ctx.obj['commands']: raise BadParameter('Plugin %s defines command "%s" which has already been defined.' % (plugin.name, command_name)) ctx.obj['commands'][command_name] = command ctx.obj['site'] = site return if configuration_file_path is not None: raise BadParameter('Configuration file "%s" does not exist.' % configuration_file_path)
def test_should_error_unknown_format(self) -> None: with self._writes('', 'abc') as f: with self.assertRaises(ConfigurationError): from_file(f)
def test_from_file_should_error_if_invalid_config(self): config_dict = {} with self._write(config_dict) as f: with self.assertRaises(ConfigurationError): from_file(f)
def test_from_file_should_error_if_invalid_yaml(self): with self._writes('"foo', 'yaml') as f: with self.assertRaises(ConfigurationError): from_file(f)
def test_from_file_should_error_if_invalid_json(self): with self._writes('', 'json') as f: with self.assertRaises(ConfigurationError): from_file(f)
def test_from_file_should_parse_mode(self, mode: str): config_dict = dict(**self._MINIMAL_CONFIG_DICT) config_dict['mode'] = mode with self._write(config_dict) as f: configuration = from_file(f) self.assertEquals(mode, configuration.mode)