sketch-a-day/admin_scripts/generate_entries.py

134 wiersze
4.3 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
2022-04-03 00:17:27 +00:00
# [ ] use CL arguments to set tool
# [ ] use CL arguments to set Markdown comments
# [ ] use CL arguments to commit and push README.md
2022-06-25 15:05:03 +00:00
# [ ] Guard against malformed file names like sketch_2022_06_25..png
2020-07-05 22:16:26 +00:00
from os import listdir
2021-08-22 17:23:41 +00:00
from os.path import join, exists
2021-08-22 03:02:03 +00:00
from itertools import takewhile
2020-07-05 22:16:26 +00:00
from helpers import get_image_names
2020-07-05 22:16:26 +00:00
2022-06-25 15:05:03 +00:00
REPO_MAIN_URL = 'https://github.com/villares/sketch-a-day/tree/main'
2021-08-22 17:23:41 +00:00
# YEAR and base_path to sketch-a-day folder are set manually, hard-coded
2022-01-01 23:51:28 +00:00
YEAR = '2022'
2021-08-22 17:23:41 +00:00
# base_path = "/Users/villares/sketch-a-day" # 01046-10 previously
2021-08-04 03:03:30 +00:00
base_path = '/home/villares/GitHub/sketch-a-day'
2020-07-20 22:35:56 +00:00
year_path = join(base_path, YEAR)
2022-06-25 15:05:03 +00:00
2021-08-22 17:23:41 +00:00
if not exists(year_path):
sketch_folders = [] # for the benefit of next_day.py
2021-09-15 02:51:02 +00:00
print(f"{__file__}: Couldn't find the sketch-a-day year folder!")
2021-08-22 17:23:41 +00:00
else:
sketch_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
2021-08-22 03:02:03 +00:00
def most_recent_entry(readme_as_lines):
entry_images = (
2021-08-04 03:03:30 +00:00
line[line.find(YEAR) : line.find(']')]
2021-08-22 03:02:03 +00:00
for line in readme_as_lines
2021-08-04 03:03:30 +00:00
if '![' in line
)
2021-08-22 03:02:03 +00:00
try:
return next(entry_images)[:10]
except StopIteration:
return 'Something wrong. No dated images found!'
def main():
# open the readme markdown index
with open(readme_path, 'rt') as readme:
readme_as_lines = readme.readlines()
# find date of the first image seen in the readme
last_done = most_recent_entry(readme_as_lines)
2021-08-04 03:03:30 +00:00
print('Last entry: ' + last_done)
2021-08-22 03:02:03 +00:00
# find folders after the last_done one (folders start with ISO date)
rev_sorted_folders = sorted(sketch_folders, reverse=True)
not_done = lambda f: last_done not in f
new_folders = [folder for folder in takewhile(not_done, rev_sorted_folders)]
2020-07-20 23:55:48 +00:00
# find insertion point
2021-08-11 00:11:45 +00:00
insert_point = 3 # in case lines is empty
2021-08-22 03:02:03 +00:00
for insert_point, line in enumerate(readme_as_lines):
2020-07-20 23:55:48 +00:00
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:
2021-08-15 21:22:31 +00:00
if img.split('.')[0].startswith(folder):
2022-06-25 15:05:03 +00:00
entry_text = build_entry(folder, img)
2021-08-22 03:02:03 +00:00
readme_as_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-22 03:02:03 +00:00
content = ''.join(readme_as_lines)
readme.write(content)
2022-06-25 15:05:03 +00:00
def build_entry(folder, image_filename):
"""
Return a string with markdown formated
for the sketch-a-day index page entry
of image (for a certain year).
"""
tools = {
2022-04-03 03:11:53 +00:00
'pde': '[[Processing Java](https://processing.org)]',
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)]',
2022-05-06 01:39:22 +00:00
'tk': '[tkinter]',
'freecad': '[FreeCAD](https://freecadweb.org)'
}
folder_path = join(year_path, folder)
2022-06-25 15:05:03 +00:00
tools_mentioned = (t for t in tools.keys() if t in image_filename.lower())
try:
tool = next(tools_mentioned)
except StopIteration:
tool = 'py5'
for f in listdir(folder_path):
if 'pyde' in f:
tool = 'pyde'
2022-04-03 00:17:27 +00:00
elif 'pde' in f:
tool = 'pde'
docstring = search_docstring(folder_path)
2021-12-09 01:57:27 +00:00
if docstring:
comment = '\n\n' + docstring
else:
comment = ''
2022-06-25 15:05:03 +00:00
link = f'{REPO_MAIN_URL}/{YEAR}/{folder}'
return f"""
---
2022-06-25 15:05:03 +00:00
### {folder}
2022-06-25 15:05:03 +00:00
![{folder}]({YEAR}/{folder}/{image_filename})
2022-06-25 15:05:03 +00:00
[{folder}]({link}) {tools[tool]}{comment}
"""
2021-12-09 01:57:27 +00:00
def search_docstring(folder):
print(listdir(folder))
return None
2021-08-04 03:03:30 +00:00
if __name__ == '__main__':
main()