def _get_cache(self): try: plugin = get_plugin('index-pages', self.pad.env) except LookupError: return DummyCache() # testing else: return plugin.cache
def test_path_installed_plugin_is_none(self, scratch_project): # XXX: this test is slow and fragile. (It won't run # without an internet connection.) add_package_to_project(scratch_project, "webpack-support") env = scratch_project.make_env() plugin = get_plugin("webpack-support", env) assert plugin.path is None
def __html__(self): ctx = get_ctx() # Parse config config = get_plugin(u"graphviz").get_config() img_format = config.get("format", "png") substitutions = { k.replace("substitutions.", ""): v for (k, v) in config.items() if k.startswith("substitutions.") } # Add default styles additional_options = [] if u"style.graph" in config: additional_options.append("-G{}".format(config.get("style.graph"))) if u"style.node" in config: additional_options.append("-N{}".format(config.get("style.node"))) if u"style.edge" in config: additional_options.append("-E{}".format(config.get("style.edge"))) # Get artifact image filename image_filename = self.get_image_filename(ctx.source.url_path, img_format) @ctx.sub_artifact(artifact_name=image_filename, sources=[ctx.source.source_filename]) def generate_image(artifact): with artifact.open("w") as f: substituted_source = Template(self.source).safe_substitute(substitutions) f.write(render_diagram(substituted_source, img_format, additional_options)) return image_filename
def image(self, src, title, text): image = resolve_image(self.record, src) if image is not None and image.format in ('png', 'gif', 'jpeg'): plugin = get_plugin('responsive-image', env=self.record.pad.env) attrs = {'alt': text, 'title': title or None} attrs.update(plugin.responsive_image(image).attrs) return "<img {}>".format(fmt_attrs(attrs)) else: return super(ResponsiveImageMixin, self).image(src, title, text)
def test_webclient_add_serve_button(env, webclient): from lektor.pluginsystem import get_plugin plugin = get_plugin('admin-extra', env) plugin.add_serve_button('/nirvana', 'albatros', 'A') rv = webclient.get('/') print(rv.data) assert b'albatros' in rv.data rv = webclient.get('/admin/edit') print(rv.data) assert b'albatros' not in rv.data
def test_webclient_add_dash_button(env, webclient): from lektor.pluginsystem import get_plugin plugin = get_plugin('admin-extra', env) plugin.add_dash_button('/devnull', 'Rnd0m', 'F') rv = webclient.get('/') print(rv.data) assert b'Rnd0m' not in rv.data rv = webclient.get('/admin/edit') print(rv.data) assert b'Rnd0m' in rv.data
def test_webclient_add_button(env, webclient): from lektor.pluginsystem import get_plugin plugin = get_plugin('admin-extra', env) plugin.add_button('/', 'zorglub', 'H') rv = webclient.get('/') assert b'zorglub' in rv.data rv = webclient.get('/blog') assert b'zorglub' in rv.data rv = webclient.get('/admin/edit') assert b'zorglub' in rv.data
def rst_to_html(text, extra_params, record): ctx = get_ctx() if ctx is None: raise RuntimeError('Context is required for markdown rendering') text = _re_dashes.sub(r"\1-\2", text) try: plugin = get_plugin('rst') config = plugin.get_config() settings = config.section_as_dict('docutils') writer_name = settings.pop('writer', 'html') extra_params.update(settings) except: writer_name = 'html' Writer = docutils.writers.get_writer_class(writer_name) pub = docutils.core.Publisher(destination_class=docutils.io.StringOutput, writer=Writer()) pub.set_components('standalone', 'restructuredtext', 'html') pub.process_programmatic_settings(None, extra_params, None) pub.set_source( source=StringIO(text), source_path=record.source_filename if record is not None else None) pub.publish() metadata = {} for docinfo in pub.document.traverse(docutils.nodes.docinfo): for element in docinfo.children: if element.tagname == 'field': name_elem, body_elem = element.children name = name_elem.astext() value = body_elem.astext() else: name = element.tagname value = element.astext() name = name.lower() if name == 'date': value = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M") metadata[name] = value parts = pub.writer.parts body = parts['html_title'] + parts['html_subtitle'] + parts['fragment'] return body, metadata
def webdav_ls_files(webdav_id, path=None, **_): """Return a list of WebDavFile named tuples for files present in a webDAV folder""" plugin = get_plugin('webdav') url = plugin.get_webdav_config(webdav_id, 'url') if path is not None: url += path username = plugin.get_webdav_config(webdav_id, 'username') pass_cmd = plugin.get_webdav_config(webdav_id, 'passcmd') password = _exec_password_command(pass_cmd) propfind_body = """<?xml version="1.0" encoding="utf-8" ?> <D:propfind xmlns:D="DAV:"> <D:prop> <D:displayname/> <D:getcontenttype/> </D:prop> </D:propfind> """ r = requests.request('PROPFIND', url, headers={ 'Depth': '1', 'Content-Type': 'application/xml' }, data=propfind_body, auth=(username, password)) xml_response = fromstring(r.content) xml_namespaces = {'D': 'DAV:'} files = [ WebdavFile(path=p.text, name=n.text, content_type=t.text) for (p, n, t) in zip( xml_response.iterfind('.//D:href', namespaces=xml_namespaces), xml_response.iterfind('.//D:displayname', namespaces=xml_namespaces), xml_response.iterfind('.//D:getcontenttype', namespaces=xml_namespaces)) if 'directory' not in t.text ] return files
def test_get_plugin_no_env_or_ctx(dummy_plugin): with pytest.raises(RuntimeError, match=r"Context is unavailable"): get_plugin("dummy-plugin")
def test_get_plugin_missing(env): with pytest.raises(LookupError, match=r"Plugin .* not found"): get_plugin("dummy-plugin", env)
def test_get_plugin_from_context(env, dummy_plugin): with Context(pad=env.new_pad()): assert get_plugin("dummy-plugin") == dummy_plugin
def test_get_plugin(env, dummy_plugin): assert get_plugin("dummy-plugin", env) == dummy_plugin