def entry_point_class_list(self, fixture): """EntryPointClassList is a special ConfigSetting which reads its value from a pkg_resources entry point which contains a list of classes published by any (possibly other) egg.""" # Because we cannot remove EasterEggs from pkg_resources, the next test must happen after # this one. The next check just ensures that we know when that does not happen: with expected(pkg_resources.DistributionNotFound): pkg_resources.require('test-inject') fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithEntryPointClassList') # Publish some classes on the entry point being tested line = 'ListedClass1 = reahl.component_dev.test_config:ListedClass1' easter_egg.add_entry_point_from_line('some.test.entrypoint', line) line = 'ListedClass2 = reahl.component_dev.test_config:ListedClass2' easter_egg.add_entry_point_from_line('some.test.entrypoint', line) # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) config.configure() # The classes are found from the entry point vassert( set(config.some_key.some_setting) == {ListedClass1, ListedClass2})
def execute(self, options, args): super(ListConfig, self).execute(options, args) with self.context: print('Listing config for %s' % self.directory) config = StoredConfiguration(self.directory) config.configure(validate=False) for config_file, key, value, setting in config.list_all(): to_print = '%-35s' % key if options.print_files: to_print += '\t%s' % config_file if options.print_values: to_print += '\t%s' % value if options.print_defaults: if setting.defaulted: message = six.text_type(setting.default) if setting.dangerous: message += ' (DANGEROUS DEFAULT)' elif setting.automatic: message = 'AUTOMATIC' else: message = 'NO DEFAULT' to_print += '\t%s' % message if options.print_description: to_print += '\t%s' % setting.description if options.print_missing_only and not isinstance( value, MissingValue): pass else: print(to_print)
def for_config_directory(cls, config_directory): from reahl.component.config import StoredConfiguration # Here, to avoid circular dependency config = StoredConfiguration(config_directory) config.configure() new_context = cls() new_context.config = config return new_context
def new_config(self): config = StoredConfiguration('etc/') try: config.configure() except pkg_resources.DistributionNotFound as ex: six.reraise(CouldNotConfigureServer, CouldNotConfigureServer(ex), sys.exc_info()[2]) return config
def fromConfigDirectory(cls, directory, port): """Creates a new ReahlWebServer given a port and standard configuration directory for an application. :param directory: The directory from which configuration will be read. :param port: The HTTP port on which the server will be started. """ config = StoredConfiguration(directory) config.configure() return cls(config, port)
def missing_settings(self, fixture): """An exception is raised if setting do not have values set.""" fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) with expected(ConfigurationException): config.configure()
def test_config_strict_checking(config_with_files): """When a Configuration is created with strict_checking=True, dangerous defaulted config is not allowed.""" fixture = config_with_files fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDangerousDefaultedSetting') config = StoredConfiguration(fixture.config_dir.name, strict_checking=True) with expected(ConfigurationException): config.configure()
def new_config(self): """The main :class:`~reahl.component.config.Configuration` of the system. This is read from disk from the 'etc' directory present in the current working directory where tests are run. """ config = StoredConfiguration('etc/') try: config.configure() except pkg_resources.DistributionNotFound as ex: raise CouldNotConfigureServer(ex).with_traceback(sys.exc_info()[2]) return config
def config_in_production(self, fixture): """When a Configuration is created as in_production, dangerous defaulted config is not allowed.""" fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDangerousDefaultedSetting' ) config = StoredConfiguration(fixture.config_dir.name, in_production=True) with expected(ConfigurationException): config.configure()
def config_defaults(self, fixture): """If a default is specified for the ConfigSetting, it need not be set in the file.""" fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDefaultedSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) config.configure() # The setting was read vassert(config.some_key.some_setting == 'default value')
def incorrect_settings(self, fixture): """An exception is raised when an attempt is made to set a setting that does not exist.""" config_file = fixture.new_config_file( filename=ConfigWithSetting.filename, contents='some_key.some_wrong_name = 3') fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) with expected(ConfigurationException): config.configure()
def test_config_dependent_defaults(config_with_files): """The default of one setting can be dependent on another setting if passed a callable""" fixture = config_with_files fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDependentSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) config.configure() # The setting was read assert config.some_key.some_setting == 'default value' assert config.some_key.some_other_setting == 'tra default value lala'
def init(): ExecutionContext().install() config_dir = sys.argv[1] config = StoredConfiguration(config_dir) config.configure() db_uri = config.reahlsystem.connection_uri print("DB URI = {}".format(db_uri)) metadata.bind = db_uri setup() Session.commit()
def config_basics(self, fixture): """Config is specified per component, via its ReahlEgg interface, and read from its own config file.""" config_file = fixture.new_config_file( filename=ConfigWithSetting.filename, contents='some_key.some_setting = 3') fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) config.configure() # The setting was read vassert(config.some_key.some_setting == 3)
def from_config_directory(cls, directory): """Creates a new ReahlWebServer given a port and standard configuration directory for an application. :param directory: The directory from which configuration will be read. ..versionchanged:: 5.1 Removed port keyword argument, port and encrypted port will be set from the config found in the directory argument. ..versionchanged:: 5.1 Renamed from camel case name. """ config = StoredConfiguration(directory) config.configure() return cls(config)
def incorrect_replacement_of_configuration(self, fixture): """An exception is raised when an attempt is made to replace a Configuration with another of the wrong type.""" config_file = fixture.new_config_file( filename=ConfigWithSetting.filename, contents= 'from reahl.component.config import Configuration; some_key = Configuration()' ) fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) with expected(ConfigurationException): config.configure()
def config_defaults(self, fixture): """Defaults that are dangerous to leave at their at their settings can be marked as such. This will result in a logged warning.""" fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDangerousDefaultedSetting' ) # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) with CallMonitor(logging.getLogger( 'reahl.component.config').warning) as monitor: config.configure() # A warning was issued with warn severity to the log logged_message = monitor.calls[0].args[0] message_regex = 'some_key.some_setting in /.*/config_file_for_this_egg.py is using a dangerous default setting' vassert(re.match(message_regex, logged_message)) # The default value is still used vassert(config.some_key.some_setting == 'default value')
def test_config_defaults_dangerous(config_with_files): """Defaults that are dangerous to leave at their at their settings can be marked as such. This will result in a logged warning.""" fixture = config_with_files fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWithDangerousDefaultedSetting') # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) with CallMonitor( logging.getLogger('reahl.component.config').warning) as monitor: config.configure() # A warning was issued with warn severity to the log logged_message = monitor.calls[0].args[0] message_regex = '^some_key.some_setting has been defaulted to a value not suitable for production use: "default value". You can set it in /.*/config_file_for_this_egg.py' assert re.match(message_regex, logged_message) # The default value is still used assert config.some_key.some_setting == 'default value'
def config_defaults(self, fixture): """To facilitate dependency injection, the Configuration of one Egg can set the 'automatic' ConfigSettings of another egg on which it depends.""" egg_needing_injection = EasterEgg('test-inject') fixture.set_config_spec( egg_needing_injection, 'reahl.component_dev.test_config:ConfigWithInjectedSetting') pkg_resources.working_set.add(egg_needing_injection) fixture.set_config_spec( easter_egg, 'reahl.component_dev.test_config:ConfigWhichInjectsSetting') easter_egg.add_dependency( egg_needing_injection.as_requirement_string()) # Usually this happens inside other infrastructure, such as the implementation of reahl serve (see reahl-dev) config = StoredConfiguration(fixture.config_dir.name) config.configure() # The default value is still used vassert(config.some_key.injected_setting == 123)
def read_existing_config(self, config_directory): config = StoredConfiguration(config_directory) config.configure(validate=False) return config
def execute(self, options, args): super(CheckConfig, self).execute(options, args) print('Checking config in %s' % self.directory) config = StoredConfiguration(self.directory) config.configure(validate=True) print('Config parses OK')
import sys from reahl.component.config import StoredConfiguration from reahl.component.dbutils import SystemControl from reahl.sqlalchemysupport import Session, metadata from reahl.component.context import ExecutionContext from reahl.doc.examples.tutorial.migrationexamplebootstrap.migrationexamplebootstrap import Address config = StoredConfiguration(sys.argv[1]) config.configure() context = ExecutionContext().install() context.config = config context.system_control = SystemControl(config) try: context.system_control.orm_control.connect() metadata.create_all() Session.add(Address(name='John Doe', email_address='*****@*****.**')) Session.add( Address(name='Jane Johnson', email_address='*****@*****.**')) Session.add(Address(name='Jack Black', email_address='*****@*****.**')) context.system_control.orm_control.commit() finally: context.system_control.orm_control.disconnect()