1
0
Fork 0
mirror of https://github.com/shouptech/synthale.git synced 2026-02-03 07:29:42 +00:00

Support unit parameters

This commit is contained in:
Emma 2019-01-02 10:40:18 -07:00
parent 9c26e81bd1
commit 673be27b46
3 changed files with 85 additions and 9 deletions

View file

@ -21,19 +21,57 @@ from synthale.recipes import load_file, load_all_files, write_recipes
@click.command() @click.command()
@click.option(
'--vol-unit', '-v',
type=click.Choice(('gallons', 'liters')),
default='gallons',
help='Unit to display volumes in. Default is gallons.'
)
@click.option(
'--hop-unit', '-H',
type=click.Choice(('ounces', 'pounds', 'grams', 'kilograms')),
default='ounces',
help='Unit to display hop masses in. Default is ounces.'
)
@click.option(
'--fermentable-unit', '-f',
type=click.Choice(('ounces', 'pounds', 'grams', 'kilograms')),
default='pounds',
help='Unit to display fermentable masses in. Default is pounds.'
)
@click.option(
'--temp-unit', '-f',
type=click.Choice(('celsius', 'fahrenheit')),
default='fahrenheit',
help='Unit to display temperatures in. Default is fahrenheit.'
)
@click.argument('input_path') @click.argument('input_path')
@click.argument('output_path') @click.argument('output_path')
def main(input_path, output_path): def main(
input_path,
output_path,
vol_unit,
hop_unit,
fermentable_unit,
temp_unit
):
"""Generate markdown files from BeerXML files. """Generate markdown files from BeerXML files.
INPUT_PATH is either a directory containing XML files, or an individual XML INPUT_PATH is either a directory containing XML files, or an individual XML
file. OUTPUT_PATH is the directory to write the markdown files to. file. OUTPUT_PATH is the directory to write the markdown files to.
""" """
units = {
'vol_unit': vol_unit,
'hop_unit': hop_unit,
'fermentable_unit': fermentable_unit,
'temp_unit': temp_unit
}
click.echo("Generating markdown from '{}'...".format(input_path)) click.echo("Generating markdown from '{}'...".format(input_path))
if input_path.endswith('.xml'): if input_path.endswith('.xml'):
recipes = load_file(input_path) recipes = load_file(input_path, units)
else: else:
recipes = load_all_files(input_path) recipes = load_all_files(input_path, units)
write_recipes(recipes, output_path) write_recipes(recipes, output_path)

View file

@ -286,8 +286,12 @@ class MarkdownRecipe:
) )
def load_file(path): def load_file(path, units={}):
"""Parse BeerXML file located at `path`. """Parse BeerXML file.
`path` is the path to a BeerXML file. `units` is a dictionary defining
the units used. `units` will be unpacked and used during the creation of a
`MarkdownRecipe` object.
Return a list of MarkdownRecipe objects. If an exception is raised during Return a list of MarkdownRecipe objects. If an exception is raised during
parsing, the message is printed to stderr and an empty list is returned. parsing, the message is printed to stderr and an empty list is returned.
@ -300,19 +304,23 @@ def load_file(path):
recipes = [] recipes = []
for recipe in result: for recipe in result:
recipes.append(MarkdownRecipe(recipe)) recipes.append(MarkdownRecipe(recipe, **units))
return recipes return recipes
def load_all_files(path): def load_all_files(path, units={}):
"""Parse all files in `path` that end in `.xml`. """Parse all XML files in a directory.
`path` is a path to a directory with .xml files. `units` is a dictionary
defining the units used. `units` will be unpacked and used during the
creation of a `MarkdownRecipe` object.
Returns a list of MarkdownRecipe objects. Returns a list of MarkdownRecipe objects.
""" """
recipes = [] recipes = []
for name in os.listdir(path): for name in os.listdir(path):
if name.endswith('.xml'): if name.endswith('.xml'):
recipes.extend(load_file(os.path.join(path, name))) recipes.extend(load_file(os.path.join(path, name), units))
return recipes return recipes

View file

@ -16,6 +16,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from click.testing import CliRunner from click.testing import CliRunner
import pytest
from synthale.cli import main from synthale.cli import main
@ -55,3 +56,32 @@ def test_main_file(tmpdir):
path = tmpdir.join('weizen.md') path = tmpdir.join('weizen.md')
assert path.read().startswith('Weizen') assert path.read().startswith('Weizen')
@pytest.mark.parametrize('option,flag,expected', (
('--vol-unit', 'gallons', '6.0 gal\\'),
('--vol-unit', 'liters', '22.7 L\\'),
('--hop-unit', 'ounces', '| 1.0 oz |'),
('--hop-unit', 'pounds', '| 0.06 lb |'),
('--hop-unit', 'grams', '| 28.3 g |'),
('--hop-unit', 'kilograms', '| 0.03 kg |'),
('--fermentable-unit', 'ounces', '| 144.0 oz |'),
('--fermentable-unit', 'pounds', '| 9.00 lb |'),
('--fermentable-unit', 'grams', '| 4082.3 g |'),
('--fermentable-unit', 'kilograms', '| 4.08 kg |'),
('--temp-unit', 'fahrenheit', '| 152.0 °F |'),
('--temp-unit', 'celsius', '| 66.7 °C |')
))
def test_main_units(tmpdir, option, flag, expected):
"""Test units options."""
runner = CliRunner()
result = runner.invoke(
main, [option, flag, 'tests/recipes/coffee-stout.xml', str(tmpdir)]
)
assert result.exit_code == 0
path = tmpdir.join('coffee_stout.md')
result = path.read()
print(result)
assert expected in result