def test_normalize_no_effect(self): decl = Declaration() decl.declare_int(Key().a, "10") cfg = CKANConfig() assert not decl.normalize(cfg) assert cfg == CKANConfig()
def test_declarations_are_gettable(self): decl = Declaration() key = Key().test decl.declare(key, 1) option = decl[key] assert option.default == 1
def test_make_safe_in_safe_mode(self): decl = Declaration() decl.declare(Key().a, 10) cfg = CKANConfig({"config.mode": "strict"}) assert decl.make_safe(cfg) assert cfg == CKANConfig({"config.mode": "strict", "a": 10})
def test_make_safe_no_overrides(self): decl = Declaration() decl.declare(Key().a, 10) cfg = CKANConfig({"config.mode": "strict", "a": 20}) assert decl.make_safe(cfg) assert cfg == CKANConfig({"config.mode": "strict", "a": 20})
def test_make_safe_no_effect(self): decl = Declaration() decl.declare(Key().a, 10) cfg = CKANConfig() assert not decl.make_safe(cfg) assert cfg == CKANConfig()
def declare_config_options(self, declaration: Declaration, key: Key): datapusher = key.ckan.datapusher declaration.annotate("Datapusher settings") declaration.declare_list(datapusher.formats, _default_formats) declaration.declare(datapusher.url) declaration.declare(datapusher.callback_url_base) declaration.declare_int(datapusher.assume_task_stale_after, 3600)
def test_load_core(self): k = Key().ckan.site_url decl = Declaration() assert k not in decl decl.load_core_declaration() assert k in decl
def declare_config_options(self, declaration: Declaration, key: Key): section = key.ckan.preview declaration.annotate("image_view settings") declaration.declare( section.image_formats, "png jpeg jpg gif").set_description( "Customize which image formats the image_view plugin will show" )
def declare_config_options(self, declaration: Declaration, key: Key): section = key.ckan.preview declaration.annotate("text_view settings") declaration.declare(section.text_formats, "text/plain txt plain") declaration.declare(section.xml_formats, "xml rdf rdf+xml owl+xml atom rss") declaration.declare(section.json_formats, "json") declaration.declare(section.jsonp_formats, "jsonp")
def test_load_dict(self): k = Key().hello.world decl = Declaration() assert k not in decl option: OptionV1 = {"key": str(k)} group: GroupV1 = { "annotation": "hello", "options": [option], } decl.load_dict({"version": 1, "groups": [group]}) assert k in decl
def test_basic_iteration(self): key = Key() decl = Declaration() decl.annotate("Start") decl.declare(key.a) decl.declare(key.b) assert list(decl.iter_options()) == [key.a, key.b]
def test_safe_setup(self, ckan_config): delimiter = Key().ckan.template_title_delimiter decl = Declaration() assert delimiter not in ckan_config decl.setup() decl.make_safe(ckan_config) assert delimiter in ckan_config
def declare_config_options(self, declaration: Declaration, option: Key): proxy = option.ckan.resource_proxy declaration.annotate("Resource Proxy settings") declaration.declare_int(proxy.max_file_size, 1048576).set_description( "Preview size limit, default: 1MB") declaration.declare_int(proxy.chunk_size, 4096).set_description( "Size of chunks to read/write.")
def test_load_plugin(self): k = Key().ckan.datastore.write_url decl = Declaration() assert k not in decl decl.load_plugin("datapusher") assert k not in decl decl.load_plugin("datastore") assert k in decl
def test_normalize_in_normalized_mode(self): decl = Declaration() decl.declare_int(Key().a, "10") cfg = CKANConfig({"config.mode": "strict"}) assert decl.normalize(cfg) # in non-safe mode default value has no effect assert cfg == CKANConfig({"config.mode": "strict"}) cfg = CKANConfig({"config.mode": "strict", "a": "10"}) assert decl.normalize(cfg) assert cfg == CKANConfig({"config.mode": "strict", "a": 10})
def test_setup(self, ckan_config): decl = Declaration() decl.setup() # setup seals declaration with pytest.raises(TypeError): decl.annotate("hello") # core declarations loaded assert Key().ckan.site_url in decl # no safe-mode by default missing = set(decl.iter_options()) - set(ckan_config) assert Key().api_token.jwt.algorithm in missing # no normalization by default assert isinstance(ckan_config["debug"], str)
def _declaration(plugins: Iterable[str], include_core: bool, include_enabled: bool) -> Declaration: decl = Declaration() if include_core: decl.load_core_declaration() additional = () if include_enabled: additional = (p for p in cfg.get_value("ckan.plugins") if p not in plugins) for name in itertools.chain(additional, plugins): decl.load_plugin(name) return decl
def declare_config_options(self, declaration: Declaration, key: Key): declaration.annotate("video_view settings") declaration.declare(key.ckan.preview.video_formats, "mp4 ogg webm")
def test_annotations_make_declaration_non_empty(self): decl = Declaration() decl.annotate("Hello") assert decl
def declare_config_options(self, declaration: Declaration, key: Key): declaration.annotate("audio_view settings") declaration.declare(key.ckan.preview.audio_formats, "wav ogg mp3")
lst = [v.strip() for v in lst] return lst elif isinstance(obj, (list, tuple)): return cast(Any, obj) elif obj is None: return [] else: return [obj] local = Local() # This a proxy to the bounded config object local(u'config') # Thread-local safe objects config = local.config = CKANConfig() local("config_declaration") config_declaration = local.config_declaration = Declaration() # Proxies to already thread-local safe objects request = cast(flask.Request, CKANRequest(_get_request)) # Provide a `c` alias for `g` for backwards compatibility g: Any = LocalProxy(_get_c) c = g session: Any = LocalProxy(_get_session) truthy = frozenset([u'true', u'yes', u'on', u'y', u't', u'1']) falsy = frozenset([u'false', u'no', u'off', u'n', u'f', u'0'])
def declare_config_options(self, declaration: Declaration, key: Key): declaration.annotate("expire_api_token plugin") key = key.expire_api_token.default_lifetime declaration.declare_int(key, 3600)
def test_option_make_declaration_non_empty(self): decl = Declaration() decl.declare(Key().test) assert decl
def declare_config_options(self, declaration: Declaration, key: Key): declaration.declare_bool( key.ckan.example_theme.show_most_popular_groups)
def config_defaults_from_declaration(): from ckan.config.declaration import Declaration decl = Declaration() decl.load_core_declaration() decl.load_plugin("resource_proxy") decl.load_plugin("text_view") decl.load_plugin("image_view") decl.load_plugin("recline_view") decl.load_plugin("datatables_view") decl.load_plugin("datastore") decl.load_plugin("datapusher") _write_config_options_file(decl) return { f"config:{k}": "``{}``".format( repr(decl[k].default) if decl[k].has_default() else None) for k in decl.iter_options() }
def declare_config_options(self, declaration: Declaration, key: Key): declaration.annotate("recline_view settings") declaration.declare(key.ckan.recline.dataproxy_url, "//jsonpdataproxy.appspot.com")
def declare_config_options(self, declaration: Declaration, key: Key): section = key.ckan.datatables declaration.annotate("datatables_view settings") declaration.declare_list(section.page_length_choices, [ 20, 50, 100, 500, 1000 ]).set_description( "https://datatables.net/examples/advanced_init/length_menu.html") declaration.declare_bool(section.state_saving, True) declaration.declare_int(section.state_duration, 7200) declaration.declare_bool(section.data_dictionary_labels, True) declaration.declare_int(section.ellipsis_length, 100) declaration.declare(section.date_format, "llll").set_description( "see Moment.js cheatsheet https://devhints.io/moment") declaration.declare(section.default_view, "table")
def test_pattern_and_flag_iteration(self): key = Key() decl = Declaration() decl.annotate("Start") decl.declare(key.aloha) decl.declare(key.hello) decl.declare(key.hey).ignore() pattern = key.dynamic("anything") assert list(decl.iter_options(pattern=pattern)) == [ key.aloha, key.hello, ] pattern = Pattern(key) + "he*" assert list(decl.iter_options(pattern=pattern)) == [key.hello] assert list(decl.iter_options(exclude=Flag(0))) == [ key.aloha, key.hello, key.hey, ]
def declare_config_options(self, declaration: Declaration, key: Key): section = key.ckan.datastore declaration.annotate("Datastore settings") declaration.declare( section.write_url, "postgresql://*****:*****@localhost/datastore_default" ).required() declaration.declare( section.read_url, "postgresql://*****:*****@localhost/datastore_default" ).required() declaration.declare( section.sqlsearch.allowed_functions_file, _SQL_FUNCTIONS_ALLOWLIST_FILE) declaration.declare_bool(section.sqlsearch.enabled, False) declaration.declare_int(section.search.rows_default, 100) declaration.declare_int(section.search.rows_max, 32000) declaration.declare_dynamic(section.sqlalchemy.dynamic("OPTION")) declaration.annotate("PostgreSQL' full-text search parameters") declaration.declare(section.default_fts_lang, "english") declaration.declare(section.default_fts_index_method, "gist")
def test_strict_setup(self, ckan_config): decl = Declaration() decl.setup() _, errors = decl.validate(ckan_config) assert "ckan.jobs.timeout" in errors