2020-10-27 06:27:28 +08:00
|
|
|
import re
|
2021-12-14 14:02:19 +08:00
|
|
|
from os.path import join, exists, getmtime
|
|
|
|
from jinja2 import Environment, BaseLoader, TemplateNotFound
|
2020-10-27 06:27:28 +08:00
|
|
|
|
2021-12-14 14:02:19 +08:00
|
|
|
README_TEMPLATE = "readme_template.md"
|
|
|
|
GETTING_STARTED_TEMPLATE = "getting_started.md"
|
2020-10-27 06:27:28 +08:00
|
|
|
|
2021-12-14 14:02:19 +08:00
|
|
|
with open(join("guides", GETTING_STARTED_TEMPLATE)) as getting_started_file:
|
|
|
|
getting_started = getting_started_file.read()
|
2020-10-27 06:27:28 +08:00
|
|
|
|
2021-12-14 14:02:19 +08:00
|
|
|
code_tags = re.findall(r'\{\{ code\["([^\s]*)"\] \}\}', getting_started)
|
|
|
|
demo_tags = re.findall(r'\{\{ demos\["([^\s]*)"\] \}\}', getting_started)
|
|
|
|
code, demos = {}, {}
|
|
|
|
|
|
|
|
for code_src in code_tags:
|
|
|
|
with open(join("demo", code_src, "run.py")) as code_file:
|
2021-02-27 02:51:51 +08:00
|
|
|
python_code = code_file.read()
|
|
|
|
python_code = python_code.replace('if __name__ == "__main__":\n iface.launch()', "iface.launch()")
|
2021-12-14 14:02:19 +08:00
|
|
|
code[code_src] = "```python\n" + python_code + "\n```"
|
|
|
|
|
|
|
|
for demo_src in demo_tags:
|
|
|
|
demos[demo_src] = "![" + demo_src + " interface](demo/" + demo_src + "/screenshot.gif)"
|
|
|
|
|
|
|
|
class GuidesLoader(BaseLoader):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
2020-10-27 06:27:28 +08:00
|
|
|
|
2021-12-14 14:02:19 +08:00
|
|
|
def get_source(self, environment, template):
|
|
|
|
path = join(self.path, template)
|
|
|
|
if not exists(path):
|
|
|
|
raise TemplateNotFound(template)
|
|
|
|
mtime = getmtime(path)
|
|
|
|
with open(path) as f:
|
|
|
|
source = f.read()
|
|
|
|
return source, path, lambda: mtime == getmtime(path)
|
2020-10-27 06:27:28 +08:00
|
|
|
|
2021-12-14 14:02:19 +08:00
|
|
|
readme_template = Environment(loader=GuidesLoader("guides")).get_template(README_TEMPLATE)
|
|
|
|
output_readme = readme_template.render(code=code, demos=demos)
|
2020-10-27 06:27:28 +08:00
|
|
|
|
|
|
|
with open("README.md", "w") as readme_md:
|
|
|
|
readme_md.write(output_readme)
|