Наконец я сделал это:
import re
import collections
import yaml
def t2k(file):
with open(file)as f:
txt=f.read()
regex=r"(?sm)\* (.*?) :(\s+-.*?)(?=^\*|\Z)"
regactions=r' - ([^\#]*)(?P<status>#.) (\[.*])'
# list of topics with action and status
topics=re.finditer(regex,txt,re.MULTILINE)
# Initiate kanban liste
list_kanban=[]
for topic in topics:
#parse action in topics
actions=re.finditer(regactions,topic[2])
for action in actions:
# create a kanban for each action
kanban={'topic':topic[1], 'actions':action[1]}
list_kanban.append({action['status'][-1:]:kanban})
# group list kanban by status
dict_kanban = collections.defaultdict(list)
for d in list_kanban:
for k, v in d.items():
dict_kanban[k].append(v)
result=[]
for statut in dict_kanban.keys():
single_topic=[]
topi_dict={}
for d in dict_kanban[statut]:
if d['topic'] not in single_topic:
single_topic.append(d['topic'])
topi_dict[d['topic']]=[d['actions']]
else:
topi_dict[d['topic']].append(d['actions'])
result.append({statut:topi_dict})
#%%
ff = open('PDCA.yaml', 'w+')
yaml.dump(result, ff, allow_unicode=True, default_flow_style=False,sort_keys=False)
print('-')
print(result)
return result
И вы используете это так:
import PDCA
PDCA.t2k('txt.md')
with open('PDCA.yaml') as f:
print(f.read())
Уверен, это не самый питон c способ, но он делает свою работу , Следующим шагом будет создание живого веб-редактора, который преобразует PDCA kanban, когда вы делаете заметки .... Интересно, буду ли я использовать flexx или remi, но это другая история