[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[www_shared] branch master updated: cleanup, convention over config
From: |
gnunet |
Subject: |
[www_shared] branch master updated: cleanup, convention over config |
Date: |
Thu, 06 May 2021 18:28:41 +0200 |
This is an automated email from the git hooks/post-receive script.
dold pushed a commit to branch master
in repository www_shared.
The following commit(s) were added to refs/heads/master by this push:
new 1cffe9e cleanup, convention over config
1cffe9e is described below
commit 1cffe9eadcf164f66e633071ea48300371156356
Author: Florian Dold <florian@dold.me>
AuthorDate: Thu May 6 18:26:20 2021 +0200
cleanup, convention over config
---
make_rss.py | 95 ----------------------
rss.xml.j2 | 48 -----------
site.py | 263 +++++++++++++++++++++++++++++++-----------------------------
sitemap.py | 39 ---------
4 files changed, 136 insertions(+), 309 deletions(-)
diff --git a/make_rss.py b/make_rss.py
deleted file mode 100644
index a9753d2..0000000
--- a/make_rss.py
+++ /dev/null
@@ -1,95 +0,0 @@
-# Copyright (C) 2019 GNUnet e.V.
-#
-# This code is derived from code contributed to GNUnet e.V.
-# by nikita <nikita@n0.is>
-#
-# Permission to use, copy, modify, and/or distribute this software for any
-# purpose with or without fee is hereby granted.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
-# LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
-# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
-# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-# THIS SOFTWARE.
-#
-# SPDX-License-Identifier: 0BSD
-#
-# process a number of .xml.j2 files with jinja2 and output
-# a .xml file which according to the template results in a
-# spec conform rss file. There could be more than one file,
-# so we do it this way.
-#
-# this generator in the current form is rather simplistic and assumes
-# too much structure in the yaml file, which should be improved
-# eventually.
-
-from pathlib import Path, PurePath
-import re
-import codecs
-from inc.time import time_rfc822, time_now, conv_date_rfc822
-
-debug=0
-
-def make_rss(directory, conf, env):
- if debug > 1:
- _ = Path(".")
- q = list(_.glob("**/*.j2"))
- print(q)
- for infile in Path(directory).glob("*.xml.j2"):
- infile = str(infile)
- if debug > 1:
- print(infile)
- name, ext = re.match(r"(.*)\.([^.]+)$", infile.rstrip(".j2")).groups()
- tmpl = env.get_template(infile)
-
- def self_localized(other_locale):
- """
- Return absolute URL for the current page in another locale.
- """
- return "https://" + conf["siteconf"]["baseurl"] + "/" +
other_locale + "/" + infile.replace(directory + '/', '').rstrip(".j2")
-
- def url_localized(filename):
- return "https://" + conf["siteconf"]["baseurl"] + "/" + locale +
"/" + filename
-
- def url_static(filename):
- return "https://" + conf["siteconf"]["baseurl"] + "/static/" +
filename
-
- def url_dist(filename):
- return "https://" + conf["siteconf"]["baseurl"] + "/dist/" +
filename
-
- def url(x):
- return "https://" + conf["siteconf"]["baseurl"] + "/" + x
-
- for l in list(x for x in Path(".").glob("locale/*/") if x.is_dir()):
- locale = str(PurePath(l).name)
- if debug > 1:
- print(locale)
- content = tmpl.render(lang=locale,
- url=url,
- now=time_rfc822(time_now()),
- conv_date_rfc822=conv_date_rfc822,
- siteconf=conf["siteconf"],
- newsposts=conf["newsposts"],
- self_localized=self_localized,
- url_localized=url_localized,
- url_static=url_static,
- url_dist=url_dist,
- filename=name + "." + ext)
- outname = "./rendered/" + locale + "/" + infile.replace(directory
+ "/", '').rstrip(".j2")
- outdir = Path("rendered")
- langdir = outdir / locale
- try:
- langdir.mkdir(parents=True, exist_ok=True)
- except e as FileNotFoundError:
- print(e)
-
- with codecs.open(outname, "w", encoding='utf-8') as f:
- try:
- if debug > 1:
- print(Path.cwd())
- f.write(content)
- except e as Error:
- print(e)
diff --git a/rss.xml.j2 b/rss.xml.j2
deleted file mode 100644
index b9a29f7..0000000
--- a/rss.xml.j2
+++ /dev/null
@@ -1,48 +0,0 @@
-{#
-# Copyright (C) 2019, 2020 GNUnet e.V.
-#
-# This code is derived from code contributed to GNUnet eV
-# by nikita <nikita@n0.is>.
-#
-# Permission to use, copy, modify, and/or distribute this software for
-# any purpose with or without fee is hereby granted.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
-# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
-# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
-# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
-# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
-# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-# PERFORMANCE OF THIS SOFTWARE.
-#
-# SPDX-License-Identifier: 0BSD
-#}
-<?xml version="1.0" encoding="UTF-8"?>
-<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
- {% for siteconfitem in siteconf %}
- <channel>
- <atom:link href="https://{{ siteconfitem['baseurl'] }}/{{ lang
}}/rss.xml" rel="self" type="application/rss+xml" />
- <title>{{ siteconfitem['rsstitle'] }}</title>
- <language>{{ lang }}</language>
- <description>{{ siteconfitem['rssdescr']|e }}</description>
- <link>https://{{ siteconfitem['baseurl'] }}/</link>
- <lastBuildDate>{{ now }}</lastBuildDate>
- {% for newspostitem in newsposts %}
- <item>
- <guid>https://{{ siteconfitem['baseurl'] }}/{{ lang }}{{
siteconfitem['newsloc'] }}{{ newspostitem['page'] }}</guid>
- <link>https://{{ siteconfitem['baseurl'] }}/{{ lang }}{{
siteconfitem['newsloc'] }}{{ newspostitem['page'] }}</link>
- <pubDate>{{ conv_date_rfc822(newspostitem["date"]) }}</pubDate>
- <title>{{ newspostitem['title']|e }}</title>
- <description>
- <![CDATA[
- <article>
- {{ newspostitem['content'] }}
- </article>
- ]]>
- </description>
- </item>
- {% endfor %}
- </channel>
- {% endfor %}
-</rss>
diff --git a/site.py b/site.py
index f52f028..1b0eacf 100644
--- a/site.py
+++ b/site.py
@@ -34,16 +34,92 @@ sys.path.insert(0, ".")
import inc.i18nfix as i18nfix
from inc.textproc import cut_news_text, cut_article
from inc.fileproc import copy_files, copy_tree
-from inc.make_rss import *
+from inc.time import time_rfc822, time_now, conv_date_rfc822
+
+
+def make_helpers(root, in_file, locale):
+ """Return a dictionary of helpers that should be available in
+ the template."""
+
+ def self_localized(other_locale, relative=False):
+ """
+ Return URL for the current page in another locale.
+ """
+ abs_file = Path(in_file).resolve()
+ baseurl = os.environ.get("BASEURL")
+ if relative or not baseurl:
+ return (
+ "../"
+ + other_locale
+ + "/"
+ + str(in_file.relative_to(root / "template")).rstrip(".j2")
+ )
+ else:
+ return (
+ baseurl
+ + other_locale
+ + "/"
+ + str(Path(abs_file).relative_to(root /
"template")).rstrip(".j2")
+ )
+
+ def url_localized(filename):
+ return "../" + locale + "/" + filename
+
+ def url_static(filename):
+ return "../" + filename
+
+ def url_dist(filename):
+ return "../dist/" + filename
+
+ def svg_localized(filename):
+ lf = filename + "." + locale + ".svg"
+ if locale == "en" or not Path(lf).is_file():
+ return "../" + filename + ".svg"
+ else:
+ return "../" + lf
+
+ def url(x):
+ # TODO: look at the app root environment variable
+ # TODO: check if file exists
+ return "../" + x
+
+ return dict(
+ self_localized=self_localized,
+ url_localized=url_localized,
+ url_static=url_static,
+ url_dist=url_dist,
+ url=url,
+ svg_localized=svg_localized,
+ now=time_rfc822(time_now()),
+ conv_date_rfc822=conv_date_rfc822,
+ )
+
class SiteGenerator:
- def __init__(self, debug=0):
+ def __init__(self, debug=0, root="."):
+ self.root = Path(root).resolve()
self.debug = debug
-
- def load_config(self, name="www.yml"):
- yaml = YAML(typ='safe')
- site_configfile = Path(name)
- return yaml.load(site_configfile)
+ loader = jinja2.ChoiceLoader(
+ [
+ jinja2.FileSystemLoader(str(self.root / "template")),
+ jinja2.PrefixLoader(
+ {"common": jinja2.FileSystemLoader(str(self.root /
"common"))}
+ ),
+ ]
+ )
+ env = jinja2.Environment(
+ loader=loader,
+ extensions=["jinja2.ext.i18n"],
+ lstrip_blocks=True,
+ trim_blocks=True,
+ undefined=jinja2.StrictUndefined,
+ autoescape=False,
+ )
+ env.newstyle_gettext = True
+ self.env = env
+ yaml = YAML(typ="safe")
+ site_configfile = self.root / "www.yml"
+ self.config = yaml.load(site_configfile)
def copy_trees(self, directory):
""" Take a directory name (string) and pass it to copy_tree() as Path
object. """
@@ -51,147 +127,80 @@ class SiteGenerator:
o = Path("rendered/" + directory)
copy_tree(i, o)
- def gen_abstract(self, conf, name, member, pages, length):
- if self.debug:
- print("generating abstracts...")
+ def gen_abstract(self, name, member, pages, length):
+ conf = self.config
for item in conf[name]:
item[member] = cut_news_text(item[pages], length)
- if self.debug:
- print("cwd: " + str(Path.cwd()))
- if self.debug > 1:
- print(conf["newsposts"])
- if self.debug:
- print("[done] generating abstracts")
-
- def gen_newspost_content(self, conf, name, member, pages, lang):
- if self.debug:
- print("generating newspost content...")
+
+ def gen_newspost_content(self, name, member, pages, lang):
+ conf = self.config
for item in conf[name]:
item[member] = cut_article(item[pages], conf, lang)
- if self.debug:
- print("cwd: " + str(Path.cwd()))
- if self.debug > 1:
- print(conf["newsposts"])
- if self.debug:
- print("[done] generating newspost content")
-
- def gen_rss(self, directory, conf, env):
- make_rss(directory, conf, env)
-
- def run_localized(self, root, conf, env, locale, tr):
- abs_cwd = Path(root).resolve()
-
- for in_file in Path(".").glob(root + "/*.j2"):
- in_file = str(in_file)
- if self.debug > 1:
- print(in_file)
- name, ext = re.match(r"(.*)\.([^.]+)$",
- in_file.rstrip(".j2")).groups()
- tmpl = env.get_template(in_file)
-
- def self_localized(other_locale, relative=False):
- """
- Return URL for the current page in another locale.
- """
- abs_file = Path(in_file).resolve()
- baseurl = os.environ.get("BASEURL")
- if relative or not baseurl:
- return "../" + other_locale + "/" + in_file.replace(
- root + '/', '').rstrip(".j2")
- else:
- return baseurl + other_locale + "/" +
str(Path(abs_file).relative_to(abs_cwd)).rstrip(".j2")
-
-
- def url_localized(filename):
- return "../" + locale + "/" + filename
-
- def url_static(filename):
- return "../static/" + filename
-
- def url_dist(filename):
- return "../dist/" + filename
-
- def svg_localized(filename):
- lf = filename + "." + locale + ".svg"
- if locale == "en" or not Path(lf).is_file():
- return "../" + filename + ".svg"
- else:
- return "../" + lf
-
- def url(x):
- # TODO: look at the app root environment variable
- # TODO: check if file exists
- return "../" + x
-
- content = tmpl.render(lang=locale,
- lang_full=conf["langs_full"][locale],
- url=url,
- conf=conf,
- siteconf=conf["siteconf"],
- meetingnotesdata=conf["meetingnotes"],
- newsdata=conf["newsposts"],
- videosdata=conf["videoslist"],
- self_localized=self_localized,
- url_localized=url_localized,
- url_static=url_static,
- url_dist=url_dist,
- svg_localized=svg_localized,
- filename=name + "." + ext)
-
- out_name = "./rendered/" + locale + "/" + in_file.replace(
- root + '/', '').rstrip(".j2")
-
- outdir = Path("rendered")
-
- langdir = outdir / locale
-
- try:
- langdir.mkdir(parents=True, exist_ok=True)
- except FileNotFoundError as e:
- print(e)
- with codecs.open(out_name, "w", encoding='utf-8') as f:
+ def run_localized(self, locale, tr):
+ conf = self.config
+ root = self.root
+ env = self.env
+ template_dir = root / "template"
+ for in_file in template_dir.glob("**/*.j2"):
+ tmpl_filename = str(in_file.resolve().relative_to(template_dir))
+ tmpl = env.get_template(tmpl_filename)
+
+ filename = tmpl_filename.rstrip(".j2")
+
+ content = tmpl.render(
+ lang=locale,
+ lang_full=conf["langs_full"][locale],
+ conf=conf,
+ siteconf=conf["siteconf"],
+ meetingnotesdata=conf["meetingnotes"],
+ newsdata=conf["newsposts"],
+ newsposts=conf["newsposts"],
+ videosdata=conf["videoslist"],
+ filename=filename,
+ **make_helpers(root, in_file, locale),
+ )
+
+ out_name = root / "rendered" / locale /
str(tmpl_filename).rstrip(".j2")
+ Path(out_name).parent.mkdir(parents=True, exist_ok=True)
+ with codecs.open(out_name, "w", encoding="utf-8") as f:
try:
- if self.debug > 1:
- print(Path.cwd())
f.write(content)
except:
print(e)
+ def run(self):
+ conf = self.config
+ root = self.root
+ for l in root.glob("locale/*/"):
- def run(self, root, conf, env):
- if self.debug > 1:
- _ = Path(".")
- q = list(_.glob("**/*.j2"))
- print(q)
+ if not l.is_dir():
+ # https://bugs.python.org/issue22276
+ continue
- # https://bugs.python.org/issue22276
- for l in list(x for x in Path(".").glob("locale/*/") if x.is_dir()):
- l = str(PurePath(l).name)
- if self.debug > 1:
- print(l)
- locale = l
+ locale = str(PurePath(l).name)
try:
- tr = gettext.translation("messages",
- localedir="locale",
- languages=[locale])
+ tr = gettext.translation(
+ "messages", localedir="locale", languages=[locale]
+ )
except FileNotFoundError as e:
- print(f"WARNING: unable to find translations for locale
'{locale}'", file=sys.stderr)
+ print(
+ f"WARNING: unable to find translations for locale
'{locale}'",
+ file=sys.stderr,
+ )
continue
tr.gettext = i18nfix.wrap_gettext(tr.gettext)
+ env = self.env
env.install_gettext_translations(tr, newstyle=True)
if locale not in conf["langs_full"]:
- print(f"WARNING: skipping '{locale}, as 'langs_full' is not
configured'", file=sys.stderr)
+ print(
+ f"WARNING: skipping '{locale}, as 'langs_full' is not
configured'",
+ file=sys.stderr,
+ )
continue
- self.run_localized(root, conf, env, locale, tr)
-
-
-
-# Deprecated alias. Should be removed once all sites have
-# been updated.
-gen_site = SiteGenerator
+ self.run_localized(locale, tr)
diff --git a/sitemap.py b/sitemap.py
deleted file mode 100644
index f5b378e..0000000
--- a/sitemap.py
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (C) 2019 GNUnet e.V.
-#
-# This code is derived from code contributed to GNUnet e.V.
-# by nikita <nikita@n0.is>.
-#
-# Permission to use, copy, modify, and/or distribute this software for
-# any purpose with or without fee is hereby granted.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
-# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
-# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
-# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
-# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
-# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-# PERFORMANCE OF THIS SOFTWARE.
-#
-# SPDX-License-Identifier: 0BSD
-import os
-from pathlib import Path, PurePosixPath
-
-
-def sitemap_tree(path):
- tree = dict(name=PurePosixPath(path).name, children=[])
- try:
- mylist = os.listdir(path)
- except OSError:
- pass
- else:
- for name in mylist:
- fn = os.path.join(path, name)
- if os.path.isdir(fn):
- tree['children'].append(sitemap_tree(fn))
- else:
- np = os.path.join(name)
- if np.startswith('/'):
- np = np[1:]
- tree['children'].append(dict(name=np))
- return tree
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [www_shared] branch master updated: cleanup, convention over config,
gnunet <=