def get_override(var): var_env = "NMIGEN_{}".format(var) if var_env in os.environ: # On Windows, there is no way to define an "empty but set" variable; it is tempting # to use a quoted empty string, but it doesn't do what one would expect. Recognize # this as a useful pattern anyway, and treat `set VAR=""` on Windows the same way # `export VAR=` is treated on Linux. return re.sub(r'^\"\"$', "", os.environ[var_env]) elif var in kwargs: return kwargs[var] else: return jinja2.Undefined(name=var)
def get_override(var): deprecated_var_env = "NMIGEN_{}".format(var) var_env = "AMARANTH_{}".format(var) if deprecated_var_env in os.environ or var_env in os.environ: # On Windows, there is no way to define an "empty but set" variable; it is tempting # to use a quoted empty string, but it doesn't do what one would expect. Recognize # this as a useful pattern anyway, and treat `set VAR=""` on Windows the same way # `export VAR=` is treated on Linux. if var_env in os.environ: var_env_value = os.environ[var_env] elif deprecated_var_env in os.environ: var_env_value = os.environ[deprecated_var_env] return re.sub(r'^\"\"$', "", var_env_value) elif var in kwargs: if isinstance(kwargs[var], str): return textwrap.dedent(kwargs[var]).strip() else: return kwargs[var] else: return jinja2.Undefined(name=var)
def _extract_override(var, *, expected_type): deprecated_var_env = "NMIGEN_{}".format(var) var_env = "AMARANTH_{}".format(var) if deprecated_var_env in os.environ or var_env in os.environ: # On Windows, there is no way to define an "empty but set" variable; it is tempting # to use a quoted empty string, but it doesn't do what one would expect. Recognize # this as a useful pattern anyway, and treat `set VAR=""` on Windows the same way # `export VAR=` is treated on Linux. if var_env in os.environ: var_env_value = os.environ[var_env] elif deprecated_var_env in os.environ: var_env_value = os.environ[deprecated_var_env] return re.sub(r'^\"\"$', "", var_env_value) elif var in kwargs: if not isinstance(kwargs[var], expected_type) and not expected_type is None: raise TypeError( "Override '{}' must be a {}, not {!r}".format( var, expected_type.__name__, kwargs[var])) else: return kwargs[var] else: return jinja2.Undefined(name=var)
def load_sql_query(sql, query_context_params=None): """Read sql file or string and format with a dictionary of params. Parameters ---------- sql : query_context_params : (Default value = None) Returns ------- """ pat = Path(sql).expanduser() if pat.exists(): with open(pat, 'r') as f: sql = f.read() if query_context_params: j = JinjaSql(param_style='pyformat') binded_sql, bind_params = j.prepare_query(sql, query_context_params) missing_placeholders = [ k for k, v in bind_params.items() if jinja2.Undefined() == v ] assert ( len(missing_placeholders) == 0 ), f'Missing placeholders are: {missing_placeholders}' try: sql = binded_sql % bind_params except KeyError as e: print(e) return return sql
def __getitem__(self, key): return jinja2.Undefined(hint=self._undefined_hint, obj=self._undefined_obj, name=self._undefined_name, exc=self._undefined_exception)
def quiet(arg): if "NMIGEN_verbose" in os.environ: return jinja2.Undefined(name="quiet") else: return arg
def quiet(arg): if get_override("verbose"): return jinja2.Undefined(name="quiet") else: return arg
def verbose(arg): if get_override_flag("verbose"): return arg else: return jinja2.Undefined(name="quiet")
class TestIndexPagesPlugin(object): @pytest.fixture def plugin(self, lektor_env, my_plugin_id): return IndexPagesPlugin(lektor_env, my_plugin_id) def test_config_caching(self, plugin): config = plugin.read_config() assert plugin.read_config() is config plugin.on_before_build_all('builder') assert plugin.read_config() is not config @pytest.fixture def generate_index(self, plugin, lektor_env): plugin.on_setup_env() assert len(lektor_env.custom_generators) == 1 return lektor_env.custom_generators[0] @pytest.mark.parametrize("record_path, expected", [ ("/", []), ("/blog", ['/blog@index-pages/year-index']), ]) def test_generate_index(self, generate_index, record, expected): assert [idx.path for idx in generate_index(record)] == expected def test_skip_build(self, plugin, lektor_env): plugin.on_setup_env(extra_flags={'index-pages': 'skip-build'}) assert len(lektor_env.custom_generators) == 0 @pytest.fixture def resolve_virtual_path(self, plugin, lektor_env): plugin.on_setup_env() return lektor_env.virtual_sources[VIRTUAL_PATH_PREFIX] def test_resolve_virtual_path(self, resolve_virtual_path, blog_record): index = resolve_virtual_path(blog_record, ['year-index', '2020']) assert index.path == '/blog@index-pages/year-index/2020' @pytest.fixture def resolve_url(self, plugin, lektor_env): plugin.on_setup_env() assert len(lektor_env.custom_url_resolvers) == 1 return lektor_env.custom_url_resolvers[0] def test_resolve_url(self, resolve_url, blog_record): index = resolve_url(blog_record, ['2020']) assert index.path == '/blog@index-pages/year-index/2020' @pytest.fixture def jinja_env(self, lektor_env): return lektor_env.jinja_env @pytest.fixture def jinja_ctx(self, jinja_env, lektor_pad): return jinja_env.from_string("").new_context({'site': lektor_pad}) @pytest.fixture def index_pages(self, plugin, jinja_env): plugin.on_setup_env() return jinja_env.globals['index_pages'] def test_index_pages(self, index_pages, jinja_ctx): rv = index_pages(jinja_ctx, 'year-index') assert isinstance(rv.indexes, Query) def test_index_pages_returns_undefined(self, index_pages, jinja_ctx): rv = index_pages(jinja_ctx, 'missing-index') assert jinja2.is_undefined(rv) @pytest.mark.parametrize('lektor_pad', [jinja2.Undefined('undefined')]) def test_index_pages_missing_site(self, index_pages, jinja_ctx): rv = index_pages(jinja_ctx, 'year-index') assert jinja2.is_undefined(rv)
def experiment_variant(context, experiment): try: request = context['request'] except KeyError: return jinja2.Undefined('request must be defined in Context') return get_experiment_variant(request, experiment, make_decision=False)