|
1 | 1 | import argparse |
2 | 2 | import json |
3 | | -import os |
4 | | -import textwrap |
5 | 3 | from argparse import ArgumentParser |
6 | | -from typing import List |
7 | 4 |
|
8 | | -from pydantic import BaseModel |
| 5 | +from pydantic import BaseModel, Extra |
9 | 6 |
|
10 | | -from coles_vs_woolies.main import display, send |
| 7 | +from coles_vs_woolies.main import send |
11 | 8 |
|
12 | 9 |
|
13 | | -def cli(): |
14 | | - example_usage = '''example: |
15 | | - python coles_vs_woolies display |
16 | | - "Cadbury Dairy Milk Chocolate Block 180g" |
17 | | - "Connoisseur Ice Cream Vanilla Caramel Brownie 4 Pack" |
18 | | - |
19 | | - python coles_vs_woolies send |
20 | | - "Cadbury Dairy Milk Chocolate Block 180g" |
21 | | - "Connoisseur Ice Cream Vanilla Caramel Brownie 4 Pack" |
22 | | - --to_addrs <me@gmail.com> <you@gmail.com> |
23 | | - ''' |
| 10 | +class ShoppingList(BaseModel, extra=Extra.allow): |
| 11 | + """ Model for the `shopping-list` json config file. """ |
| 12 | + to_addrs: list[str] |
| 13 | + products: list[str] |
| 14 | + |
24 | 15 |
|
| 16 | +def cli(): |
25 | 17 | parser = ArgumentParser( |
26 | 18 | prog='coles_vs_woolies', |
27 | 19 | description='Compare prices between Aussie grocers', |
28 | 20 | formatter_class=argparse.RawDescriptionHelpFormatter, |
29 | | - epilog=textwrap.dedent(example_usage) |
30 | 21 | ) |
31 | | - |
32 | | - subparsers = parser.add_subparsers(dest='action') |
33 | | - |
34 | | - help_product = 'List of descriptive product search terms. Brand, package weight or size should be included. ' \ |
35 | | - 'Can be file path. E.g. "Cadbury Dairy Milk Chocolate Block 180g"' \ |
36 | | - '"Connoisseur Ice Cream Vanilla Caramel Brownie 4 Pack"' |
37 | | - |
38 | | - # Display parser |
39 | | - display_parser = subparsers.add_parser('display', help='Display product price comparisons') |
40 | | - display_parser.add_argument('products', nargs='+', help=help_product) |
41 | | - |
42 | | - # Send parser |
43 | | - send_parser = subparsers.add_parser('send', help='Email product price comparisons') |
44 | | - send_parser.add_argument('products', nargs='+', help=help_product) |
45 | | - send_parser.add_argument('-t', '--to_addrs', nargs='+', help="Recipients' email address.", required=False) |
46 | | - send_parser.add_argument('-o', '--out_dir', type=str, help='Directory for saving copy of the email HTML template.', |
47 | | - required=False) |
48 | | - send_parser.add_argument('-d', '--dry_run', action='store_true', help='Disable email delivery', |
49 | | - default=False, required=False) |
| 22 | + parser.add_argument('file_path', type=str, |
| 23 | + help='File path to a JSON config shopping list; see `shopping-list.example.json`') |
| 24 | + parser.add_argument('-o', '--out_dir', type=str, required=False, |
| 25 | + help='Directory for saving copy of the email HTML template.') |
| 26 | + parser.add_argument('-d', '--dry_run', action='store_true', default=False, required=False, |
| 27 | + help='Disable email delivery') |
50 | 28 |
|
51 | 29 | # Parse inputs |
52 | 30 | kwargs = vars(parser.parse_args()) |
53 | | - action = kwargs.pop('action') |
54 | | - |
55 | | - _product_inputs = kwargs.pop('products') |
56 | | - if os.path.isfile(fp := _product_inputs[0]) and fp.endswith('.json'): |
57 | | - with open(fp, 'r') as f: |
58 | | - jobs = [_JsonInput.parse_obj(x) for x in json.load(f)] |
59 | | - _ = kwargs.pop('to_addrs', None) |
60 | | - for job in jobs: |
61 | | - _run(action, job.products, to_addrs=job.to_addrs, **kwargs) |
62 | | - else: |
63 | | - if action == 'send' and kwargs.get('to_addrs', None) is None: |
64 | | - parser.error('the following arguments are required: -t/--to_addrs') |
65 | | - products = _parse_product_inputs(_product_inputs) |
66 | | - _run(action, products, **kwargs) |
67 | | - |
68 | | - |
69 | | -class _JsonInput(BaseModel): |
70 | | - to_addrs: List[str] |
71 | | - products: List[str] |
72 | | - |
73 | | - |
74 | | -def _run(action: str, products: List[str], **kwargs): |
75 | | - if action == 'send': |
76 | | - send(products=products, **kwargs) |
77 | | - else: |
78 | | - display(products=products) |
79 | | - |
80 | | - |
81 | | -def _parse_product_inputs(args: List[str]) -> List[str]: |
82 | | - """ Return product list from input list of products/file-paths """ |
83 | | - products = [] |
84 | | - for input_ in args: |
85 | | - if os.path.isfile(input_): |
86 | | - with open(input_, 'r') as f: |
87 | | - products.extend(f.read().splitlines()) |
88 | | - else: |
89 | | - products.append(input_) |
90 | | - return sorted(list(set(products))) |
| 31 | + with open(kwargs.pop('file_path'), 'r') as fp: |
| 32 | + shopping_lists: list[ShoppingList] = [ShoppingList.parse_obj(list_) for list_ in json.load(fp)] |
| 33 | + |
| 34 | + # Run for each shopping list |
| 35 | + for shopping_list in shopping_lists: |
| 36 | + send(products=shopping_list.products, |
| 37 | + to_addrs=shopping_list.to_addrs, |
| 38 | + **kwargs) |
91 | 39 |
|
92 | 40 |
|
93 | 41 | if __name__ == '__main__': |
|
0 commit comments