From 673be27b46f74538baa0928b054ab6e05407c85d Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Wed, 2 Jan 2019 10:40:18 -0700 Subject: [PATCH] Support unit parameters --- src/synthale/cli.py | 44 ++++++++++++++++++++++++++++++++++++++--- src/synthale/recipes.py | 20 +++++++++++++------ tests/test_cli.py | 30 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/synthale/cli.py b/src/synthale/cli.py index ba8b600..df5dcf1 100644 --- a/src/synthale/cli.py +++ b/src/synthale/cli.py @@ -21,19 +21,57 @@ from synthale.recipes import load_file, load_all_files, write_recipes @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('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. 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. """ + 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)) if input_path.endswith('.xml'): - recipes = load_file(input_path) + recipes = load_file(input_path, units) else: - recipes = load_all_files(input_path) + recipes = load_all_files(input_path, units) write_recipes(recipes, output_path) diff --git a/src/synthale/recipes.py b/src/synthale/recipes.py index 67a8377..66bdaee 100644 --- a/src/synthale/recipes.py +++ b/src/synthale/recipes.py @@ -286,8 +286,12 @@ class MarkdownRecipe: ) -def load_file(path): - """Parse BeerXML file located at `path`. +def load_file(path, units={}): + """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 parsing, the message is printed to stderr and an empty list is returned. @@ -300,19 +304,23 @@ def load_file(path): recipes = [] for recipe in result: - recipes.append(MarkdownRecipe(recipe)) + recipes.append(MarkdownRecipe(recipe, **units)) return recipes -def load_all_files(path): - """Parse all files in `path` that end in `.xml`. +def load_all_files(path, units={}): + """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. """ recipes = [] for name in os.listdir(path): 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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 1bf9920..49fac46 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -16,6 +16,7 @@ # along with this program. If not, see . from click.testing import CliRunner +import pytest from synthale.cli import main @@ -55,3 +56,32 @@ def test_main_file(tmpdir): path = tmpdir.join('weizen.md') 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