class LicenseType(IniSectionObject): description = IniSectionAttribute() description_de = IniSectionAttribute() def get_description(self, locale): if locale == 'de': return self.description_de or self.description return self.description
def get_default_values(app): from univention.appcenter.ini_parser import IniSectionAttribute from univention.appcenter.settings import BoolSetting, FileSetting, IntSetting, ListSetting, PasswordFileSetting, PasswordSetting, StatusSetting, StringSetting, UDMListSetting for kls in [ StringSetting, IntSetting, BoolSetting, ListSetting, UDMListSetting, FileSetting, PasswordFileSetting, PasswordSetting, StatusSetting ]: if 'default_for_testing' not in kls._attrs: attr = IniSectionAttribute() attr.name = 'default_for_testing' kls._attrs['default_for_testing'] = attr app = FindApps().find(app) app = FindApps().find_candidate(app) or app ret = {} for setting in app.get_settings(): value = setting.default_for_testing if value is not None: ret[setting.name] = setting.sanitize_value(app, value) return ret
class Rating(IniSectionObject): label = IniSectionAttribute() label_de = IniSectionAttribute() description = IniSectionAttribute() description_de = IniSectionAttribute() def get_label(self, locale): if locale == 'de': return self.label_de or self.label return self.label def get_description(self, locale): if locale == 'de': return self.label_de or self.label return self.label def to_dict(self, locale): return { 'name': self.name, 'description': self.get_description(locale), 'label': self.get_label(locale) }
class FileSetting(Setting): filename = IniSectionAttribute(required=True) def _log_set_value(self, app, value): # do not log complete file content pass def _read_file_content(self, filename): try: with open(filename) as fd: return fd.read() except EnvironmentError: return None def _touch_file(self, filename): if not os.path.exists(filename): mkdir(os.path.dirname(filename)) open(filename, 'wb') def _write_file_content(self, filename, content): try: if content: settings_logger.debug('Writing to %s' % filename) self._touch_file(filename) with open(filename, 'w') as fd: fd.write(content) else: settings_logger.debug('Deleting %s' % filename) if os.path.exists(filename): os.unlink(filename) except EnvironmentError as exc: settings_logger.error('Could not set content: %s' % exc) def get_value(self, app, phase='Settings'): if self.is_outside(app): value = self._read_file_content(self.filename) else: if app_is_running(app): from univention.appcenter.docker import Docker docker = Docker(app) value = self._read_file_content(docker.path(self.filename)) else: settings_logger.info('Cannot read %s while %s is not running' % (self.name, app)) value = None if value is None and phase == 'Install': settings_logger.info('Falling back to initial value for %s' % self.name) value = self.get_initial_value(app) return value def set_value(self, app, value, together_config_settings, part): if part == 'outside': return self._write_file_content(self.filename, value) else: if not app_is_running(app): settings_logger.error( 'Cannot write %s while %s is not running' % (self.name, app)) return from univention.appcenter.docker import Docker docker = Docker(app) return self._write_file_content(docker.path(self.filename), value) def should_go_into_image_configuration(self, app): return False
class UDMListSetting(ListSetting): udm_filter = IniSectionAttribute()
class Rating(IniSectionObject): label = IniSectionAttribute(localisable=True) description = IniSectionAttribute(localisable=True)
class LicenseType(IniSectionObject): description = IniSectionAttribute(localisable=True)
class AdvancedIniSectionObjectTest(IniSectionObjectTest): a_key = IniSectionAttribute(required=True, localisable=True) a_list = IniSectionListAttribute()
class DefaultTypedObject(TypedObjectWithDefault): my_key = IniSectionAttribute(default='Typed9')
class TypedObjectWithDefault1(TypedObjectWithDefault): my_key = IniSectionAttribute(default='Typed1')
class TypedObjectWithDefault(TypedIniSectionObject): _type_attr = 'klass' klass = IniSectionAttribute(default='DefaultTypedObject') my_key = IniSectionAttribute(default='Typed0')
class TypedObject2(TypedObject1): my_key = IniSectionAttribute(default='Typed2')
class TypedObject(TypedIniSectionObject): my_key = IniSectionAttribute(default='Typed0')
class ChoicesObject(IniSectionObject): a_key = IniSectionAttribute(choices=['A Value', 'Another Value'])
class PasswordSetting(Setting): description = IniSectionAttribute(default=_('Password'), localisable=True) def _log_set_value(self, app, value): # do not log password pass
class Setting(TypedIniSectionObject): '''Based on the .settings file, models additional settings for Apps that can be configured before installation, during run-time, etc.''' type = IniSectionAttribute(default='String', choices=[ 'String', 'Int', 'Bool', 'List', 'Password', 'File', 'PasswordFile', 'Status' ]) description = IniSectionAttribute(localisable=True, required=True) group = IniSectionAttribute(localisable=True) show = IniSectionListAttribute( default=['Settings'], choices=['Install', 'Upgrade', 'Remove', 'Settings']) show_read_only = IniSectionListAttribute( choices=['Install', 'Upgrade', 'Remove', 'Settings']) initial_value = IniSectionAttribute() required = IniSectionBooleanAttribute() scope = IniSectionListAttribute(choices=['inside', 'outside']) @classmethod def get_class(cls, name): if name and not name.endswith('Setting'): name = '%sSetting' % name return super(Setting, cls).get_class(name) def is_outside(self, app): # for Non-Docker Apps, Docker Apps when called from inside, Settings specified for 'outside' return not app.docker or container_mode() or 'outside' in self.scope def is_inside(self, app): # only for Docker Apps (and called from the Docker Host). And not only 'outside' is specified return app.docker and not container_mode() and ('inside' in self.scope or self.scope == []) def get_initial_value(self, app): if self.is_outside(app): value = ucr_get(self.name) if value is not None: return self.sanitize_value(app, value) if isinstance(self.initial_value, string_types): return ucr_run_filter(self.initial_value) return self.initial_value def get_value(self, app, phase='Settings'): '''Get the current value for this Setting. Easy implementation''' if self.is_outside(app): value = ucr_get(self.name) else: if app_is_running(app): from univention.appcenter.actions import get_action configure = get_action('configure') ucr = configure._get_app_ucr(app) value = ucr.get(self.name) else: settings_logger.info('Cannot read %s while %s is not running' % (self.name, app)) value = None try: value = self.sanitize_value(app, value) except SettingValueError: settings_logger.info('Cannot use %r for %s' % (value, self.name)) value = None if value is None and phase == 'Install': settings_logger.info('Falling back to initial value for %s' % self.name) value = self.get_initial_value(app) return value def _log_set_value(self, app, value): if value is None: settings_logger.info('Unsetting %s' % self.name) else: settings_logger.info('Setting %s to %r' % (self.name, value)) def set_value(self, app, value, together_config_settings, part): together_config_settings[part][self.name] = value def set_value_together(self, app, value, together_config_settings): value = self.sanitize_value(app, value) value = self.value_for_setting(app, value) self._log_set_value(app, value) if self.is_outside(app): together_config_settings.setdefault('outside', {}) self.set_value(app, value, together_config_settings, 'outside') if self.is_inside(app): together_config_settings.setdefault('inside', {}) self.set_value(app, value, together_config_settings, 'inside') def sanitize_value(self, app, value): if self.required and value in [None, '']: raise SettingValueError('%s is required' % self.name) return value def value_for_setting(self, app, value): if value is None: return None value = str(value) if value == '': return None return value def should_go_into_image_configuration(self, app): return self.is_inside(app) and ('Install' in self.show or 'Upgrade' in self.show)
class IniSectionObjectTest(IniSectionObject): a_key = IniSectionAttribute() a_bool = IniSectionBooleanAttribute() a_default = IniSectionAttribute(default='The default')