sketch-a-day/admin_scripts/generate_entries.py

97 wiersze
2.8 KiB
Python
Czysty Zwykły widok Historia

2020-07-20 23:55:48 +00:00
#! /usr/bin/python3
2020-07-05 22:41:14 +00:00
# reads folder
# finds missing folders from last_done
# gets images, checks image types
# build markdown for entries
## TO DO:
2020-07-06 02:37:57 +00:00
# [X] find last entry for me
2020-07-09 01:18:31 +00:00
# [X] insert new entries in propper place
2020-07-06 02:37:57 +00:00
# [ ] insert docstrings as text on .md file
2020-07-05 22:16:26 +00:00
from os import listdir
from os.path import isfile, join
from helpers import get_image_names
2020-07-05 22:16:26 +00:00
2021-08-04 03:03:30 +00:00
YEAR = '2021'
base_path = '/home/villares/GitHub/sketch-a-day'
# base_path = "/Users/villares/sketch-a-day" # 01046-10
2020-07-20 22:35:56 +00:00
year_path = join(base_path, YEAR)
2020-07-05 22:16:26 +00:00
folders = listdir(year_path)
2020-07-08 20:47:50 +00:00
readme_path = join(base_path, 'README.md')
2020-07-05 22:16:26 +00:00
def main():
2020-07-20 23:55:48 +00:00
# open the readme markdown index
with open(readme_path, 'rt') as readme:
lines = readme.readlines()
# find date of the first image
2021-08-04 03:03:30 +00:00
imagens = (
line[line.find(YEAR) : line.find(']')]
for line in lines
if '![' in line
)
2020-07-20 23:55:48 +00:00
last_done = next(imagens)[:10]
2021-08-04 03:03:30 +00:00
print('Last entry: ' + last_done)
2020-07-20 23:55:48 +00:00
# find folders after the last_done
new_folders = []
2021-01-23 02:54:23 +00:00
for f in reversed(sorted(folders)):
2020-07-20 23:55:48 +00:00
if last_done not in f:
new_folders.append(f)
2020-07-20 23:55:48 +00:00
else:
break
# find insertion point
for insert_point, line in enumerate(lines):
if last_done in line:
break
# iterate on new folders
for folder in reversed(new_folders):
imgs = get_image_names(year_path, folder)
2021-08-10 23:55:19 +00:00
# insert entry if matching image found
for img in imgs:
if img.split('.')[0] == folder:
entry_text = build_entry(img, YEAR)
lines.insert(insert_point - 3, entry_text)
print('Adding: ' + folder)
break
2020-07-20 23:55:48 +00:00
# overwrite the readme markdown index
with open(readme_path, 'wt') as readme:
2021-08-04 03:03:30 +00:00
content = ''.join(lines)
readme.write(content)
def build_entry(image, year, kind='pyde'):
"""
Return a string with markdown formated
for the sketch-a-day index page entry
of image (for a certain year).
"""
name, ext = image.split('.')
tools = {
2021-08-04 03:03:30 +00:00
'pyde': '[[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]',
'flat': '[[Python + flat](https://xxyxyz.org/flat)]',
'pyp5js': '[[pyp5js](https://berinhard.github.io/pyp5js/)]',
'py5': '[[py5](https://py5.ixora.io/)]',
'shoebot': '[[shoebot](http://shoebot.net/)]',
'pyxel': '[[pyxel](https://github.com/kitao/pyxel/blob/master/README.md)]',
}
tools_mentioned = matches = (t for t in tools.keys() if t in name)
try:
tool = next(tools_mentioned)
except StopIteration:
tool = 'pyde'
return """
---
![{0}]({2}/{0}/{0}.{1})
[{0}](https://github.com/villares/sketch-a-day/tree/master/{2}/{0}) {3}
2021-08-04 03:03:30 +00:00
""".format(
name, ext, year, tools[tool]
)
2021-08-04 03:03:30 +00:00
if __name__ == '__main__':
main()