1
0
Fork 0
mirror of https://github.com/shouptech/synthale.git synced 2026-02-03 15:39:45 +00:00

Add generation of details

This commit is contained in:
Emma 2018-12-23 19:54:34 -07:00
parent 22aad84b9b
commit 2c50a4b513
5 changed files with 104 additions and 2 deletions

21
src/synthale/convert.py Normal file
View file

@ -0,0 +1,21 @@
"""Contains functions used to convert units."""
def liters(liters, format_spec=''):
"""Return a string with the unit appended.
See the `Format Specification Mini-Language
<https://docs.python.org/3/library/string.html#format-specification-mini-language>_`
for how to write `format_spec`.
"""
return ('{:' + format_spec + '} L').format(liters)
def gallons(liters, format_spec=''):
"""Convert liters to gallons and return a string with the unit appended.
See the `Format Specification Mini-Language
<https://docs.python.org/3/library/string.html#format-specification-mini-language>_`
for how to write `format_spec`.
"""
return ('{:' + format_spec + '} gal').format(liters * 0.264178)

View file

@ -6,18 +6,22 @@ import sys
import pybeerxml import pybeerxml
from synthale import markdown from synthale import markdown, convert
class MarkdownRecipe: class MarkdownRecipe:
"""A recipe in markdown form.""" """A recipe in markdown form."""
def __init__(self, recipe): def __init__(self, recipe, vol_unit='gallons'):
"""Create a MarkdownRecipe object. """Create a MarkdownRecipe object.
`recipe` is a recipe object from the pybeerxml package. `recipe` is a recipe object from the pybeerxml package.
`vol_unit` specifies the unit for boil size and batch size. Can be one
of 'gallons', or 'liters'.
""" """
self.recipe = recipe self.recipe = recipe
self.vol_unit = vol_unit
@property @property
def filename(self): def filename(self):
@ -43,6 +47,8 @@ class MarkdownRecipe:
'', '',
self.style, self.style,
'', '',
self.details,
'',
)) ))
@property @property
@ -64,6 +70,37 @@ class MarkdownRecipe:
self.recipe.style.name) self.recipe.style.name)
)) ))
@property
def details(self):
"""Return markdown for the recipe's details."""
if self.vol_unit == 'gallons':
boil_size = convert.gallons(self.recipe.boil_size, '.1f')
batch_size = convert.gallons(self.recipe.batch_size, '.1f')
else:
boil_size = convert.liters(self.recipe.boil_size, '.1f')
batch_size = convert.liters(self.recipe.batch_size, '.1f')
return '\n'.join((
markdown.setext_heading('Details', 2),
'{}: {}'.format(markdown.strong('Type'), self.recipe.type),
'{}: {:.1f} %'.format(markdown.strong('Batch efficiency'),
self.recipe.efficiency),
'{}: {}'.format(markdown.strong('Boil size'), boil_size),
'{}: {} min'.format(markdown.strong('Boil length'),
int(self.recipe.boil_time)),
'{}: {}'.format(markdown.strong('Batch size'), batch_size),
'{}: {:.3f}'.format(markdown.strong('Estimated OG'),
self.recipe.og),
'{}: {:.3f}'.format(markdown.strong('Estimated FG'),
self.recipe.fg),
'{}: {}'.format(markdown.strong('Estimated IBU'),
int(self.recipe.ibu)),
'{}: {}'.format(markdown.strong('Estimated SRM'),
'not implemented'),
'{}: {:.1f}'.format(markdown.strong('Estimated ABV'),
self.recipe.abv)
))
def load_file(path): def load_file(path):
"""Parse BeerXML file located at `path`. """Parse BeerXML file located at `path`.

View file

@ -20,3 +20,11 @@ def md_recipes():
coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0] coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0]
weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0] weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0]
return [MarkdownRecipe(coffeestout), MarkdownRecipe(weizen)] return [MarkdownRecipe(coffeestout), MarkdownRecipe(weizen)]
@pytest.fixture
def md_weizen():
"""Return the sample weizen recipe as a MarkdownRecipe object."""
return MarkdownRecipe(
pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0]
)

13
tests/test_convert.py Normal file
View file

@ -0,0 +1,13 @@
"""Contains tests for the synthale.convert module."""
from synthale.convert import liters, gallons
def test_liters():
"""Test liters function."""
assert liters(1, '.1f') == '1.0 L'
def test_gallons():
"""Test gallons function."""
assert gallons(1, '.1f') == '0.3 gal'

View file

@ -62,6 +62,29 @@ def test_markdown_recipe_style():
) )
def test_recipe_details(md_weizen):
"""Test valid details are generated."""
md_weizen.vol_unit = 'gallons'
assert md_weizen.details == (
'Details\n'
'-------\n'
'**Type**: All Grain\n'
'**Batch efficiency**: 72.0 %\n'
'**Boil size**: 6.3 gal\n'
'**Boil length**: 60 min\n'
'**Batch size**: 5.5 gal\n'
'**Estimated OG**: 1.051\n'
'**Estimated FG**: 1.015\n'
'**Estimated IBU**: 15\n'
'**Estimated SRM**: not implemented\n'
'**Estimated ABV**: 4.7'
)
md_weizen.vol_unit = 'liters'
assert '**Boil size**: 23.7 L' in md_weizen.details
assert '**Batch size**: 20.8 L' in md_weizen.details
def test_write_recipes(md_recipes, tmpdir): def test_write_recipes(md_recipes, tmpdir):
"""Test write_recipes function.""" """Test write_recipes function."""
write_recipes(md_recipes, str(tmpdir)) write_recipes(md_recipes, str(tmpdir))