test passing!
This commit is contained in:
99
generate.py
99
generate.py
@@ -1,5 +1,13 @@
|
||||
from common import format_timestamp
|
||||
import json
|
||||
import textwrap
|
||||
|
||||
def wrap_text(text):
|
||||
return textwrap.fill(text, 80,
|
||||
replace_whitespace=False,
|
||||
break_on_hyphens=False,
|
||||
break_long_words=False)
|
||||
|
||||
|
||||
def generate_godword(value):
|
||||
return f"{' '.join(value[:10])}\n{' '.join(value[10:])}"
|
||||
@@ -26,21 +34,39 @@ def generate_diet(block):
|
||||
|
||||
def generate_exercise(block):
|
||||
if block['kind'] == 'walk':
|
||||
return f'@exercise {block["minutes"]}min {block["distance"]}km {block["steps"]}steps'
|
||||
return f'@exercise walk {block["minutes"]}min {block["distance"]}km {block["steps"]}steps'
|
||||
elif block['kind'] == 'calisthenics':
|
||||
return f'@exercise calisthenics {block["sets"]}x{block["reps"]} {block["exercise"]}'
|
||||
|
||||
assert False
|
||||
|
||||
def generate_default(block):
|
||||
return f'@{block["type"]} {block["value"]}'
|
||||
|
||||
def generate_post(block):
|
||||
result = '@post'
|
||||
|
||||
if ts := block.get('timestamp'):
|
||||
result += f' {format_timestamp(ts)}'
|
||||
|
||||
if content := block.get('content'):
|
||||
result += f' {content}'
|
||||
|
||||
return wrap_text(result)
|
||||
|
||||
def generate_timer(block):
|
||||
parts = [f'@{block["type"]}']
|
||||
|
||||
if name := block.get('name'):
|
||||
parts.append(name)
|
||||
|
||||
if ts := block.get('timestamp'):
|
||||
parts.append(format_timestamp(ts))
|
||||
|
||||
return ' '.join(parts)
|
||||
|
||||
def generate_info(block):
|
||||
return f'@info {block["value"]}'
|
||||
return wrap_text(f'@info {block["value"]}')
|
||||
|
||||
def generate_notes(block):
|
||||
parts = ['@notes']
|
||||
@@ -59,8 +85,8 @@ entry_modules = {
|
||||
'diet': generate_diet,
|
||||
'exercise': generate_exercise,
|
||||
'hide': lambda _: '@hide',
|
||||
'post': generate_timer,
|
||||
'info': generate_default,
|
||||
'post': generate_post,
|
||||
'info': generate_info,
|
||||
'notes': generate_notes,
|
||||
'behavior': generate_default,
|
||||
'task': generate_default,
|
||||
@@ -70,33 +96,56 @@ entry_modules = {
|
||||
'notify': generate_notify,
|
||||
}
|
||||
|
||||
journal = json.load(open('journal.json'))
|
||||
|
||||
for curr_day in journal:
|
||||
header, entries = journal[curr_day].values()
|
||||
|
||||
result = f'# {curr_day}\n'
|
||||
def generate_page(day, header, entries):
|
||||
result = f'# {day}'
|
||||
|
||||
for name, value in header.items():
|
||||
result += f'\n{name.title()}:\n'
|
||||
result += f'\n\n{name.title()}:\n'
|
||||
result += header_modules[name](value)
|
||||
result += '\n'
|
||||
|
||||
def format_block(block):
|
||||
if isinstance(block, str):
|
||||
return block
|
||||
else:
|
||||
return entry_modules[block['type']](block)
|
||||
def format_block(block, is_first):
|
||||
def format_text(text):
|
||||
if all(c == '\n' for c in block):
|
||||
return text
|
||||
|
||||
DUMMY_TS = '2020-02-02 02:02:02 '
|
||||
|
||||
if is_first:
|
||||
text = DUMMY_TS + text
|
||||
|
||||
length = len(text)
|
||||
|
||||
if length > 80:
|
||||
text = wrap_text(text)
|
||||
|
||||
if is_first:
|
||||
text = text.removeprefix(DUMMY_TS)
|
||||
|
||||
return text
|
||||
|
||||
def format_module(module):
|
||||
return entry_modules[module['type']](module)
|
||||
|
||||
formatted = format_text(block) if isinstance(block, str) else format_module(block)
|
||||
|
||||
if result[-1] != '\n' and not all(c == '\n' for c in formatted):
|
||||
formatted = ' ' + formatted
|
||||
|
||||
return formatted
|
||||
|
||||
for entry in entries:
|
||||
result += f'\n{format_timestamp(entry["timestamp"])} '
|
||||
if len(entry['blocks']) == 1:
|
||||
result += f'{format_block(entry["blocks"][0])}\n'
|
||||
else:
|
||||
result += '\n'
|
||||
for block in entry['blocks']:
|
||||
result += f'\n{format_block(block)}\n'
|
||||
result += f'\n\n{format_timestamp(entry["timestamp"])}'
|
||||
for i, block in enumerate(entry['blocks']):
|
||||
result += format_block(block, i == 0)
|
||||
|
||||
print(result)
|
||||
result += '\n'
|
||||
|
||||
return result
|
||||
|
||||
if __name__ == '__main__':
|
||||
journal = json.load(open('journal.json'))
|
||||
|
||||
for curr_day in journal:
|
||||
header, entries = journal[curr_day].values()
|
||||
page = generate_page(curr_day, header, entries)
|
||||
print(page)
|
||||
18
parse.py
18
parse.py
@@ -1,4 +1,3 @@
|
||||
import enum
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import re
|
||||
@@ -64,7 +63,8 @@ def parse_timestamp(timestamp):
|
||||
return datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
|
||||
|
||||
def parse_post(block):
|
||||
block = block.removeprefix('@post ')
|
||||
block = block.removeprefix('@post').strip()
|
||||
|
||||
try:
|
||||
timestamp = int(parse_timestamp(block[:19]).timestamp())
|
||||
block = block[19:]
|
||||
@@ -118,7 +118,17 @@ def parse_exercise(block):
|
||||
'steps': int(steps.removesuffix('steps')),
|
||||
}
|
||||
|
||||
return {'kind': 'INVALID'}
|
||||
elif parts[0] == 'calisthenics':
|
||||
kind, split, exercise = parts
|
||||
sets, reps = split.split('x')
|
||||
return {
|
||||
'kind': kind,
|
||||
'reps': reps,
|
||||
'sets': sets,
|
||||
'exercise': exercise,
|
||||
}
|
||||
|
||||
assert False
|
||||
|
||||
def parse_notify(block):
|
||||
tag, day, *rest = block.split()
|
||||
@@ -261,6 +271,8 @@ def parse_entry(entry):
|
||||
partial(merge_chars, '\n'),
|
||||
# attach escaped tag symbols
|
||||
partial(attach_to_prev_if, lambda p, c: c == '@' and p[-1] == '\\'),
|
||||
# ?????
|
||||
partial(attach_to_prev_if, lambda p, c: p.endswith('\\@')),
|
||||
# attach tag symbols
|
||||
partial(attach_to_next, '@'),
|
||||
# ???
|
||||
|
||||
4
test.py
4
test.py
@@ -7,7 +7,7 @@ for fpath in list(sorted((Path.home() / 'workspace' / 'journal').glob('*.md'))):
|
||||
text = fpath.read_text()
|
||||
parsed = parse_page(text)
|
||||
generated = generate_page(fpath.stem, parsed['header'], parsed['entries'])
|
||||
for l1, l2 in zip(text.split('\n'), generated.split('\n')):
|
||||
for i, (l1, l2) in enumerate(zip(text.split('\n'), generated.split('\n'))):
|
||||
if l1 != l2:
|
||||
print('\n'.join([fpath.stem, l1, l2]))
|
||||
print('\n'.join([f'LINE NR: {i}\t\tFILE: {fpath.stem}', repr(l1), repr(l2)]))
|
||||
breakpoint()
|
||||
Reference in New Issue
Block a user