summaryrefslogtreecommitdiff
path: root/src/pio_ini.py
blob: 2d9c7c6df2d965bd40c6867c8b5707b8d576da4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import os
import yaml

from argparse import ArgumentParser, ArgumentError
from home.pio import get_products, platformio_ini
from home.pio.exceptions import ProductConfigNotFoundError


def get_config(product: str) -> dict:
    config_path = os.path.join(
        os.getenv('HOME'), '.config',
        'homekit_pio', f'{product}.yaml'
    )
    if not os.path.exists(config_path):
        raise ProductConfigNotFoundError(f'{config_path}: product config not found')

    with open(config_path, 'r') as f:
        return yaml.safe_load(f)


if __name__ == '__main__':
    products = get_products()
    parser = ArgumentParser()
    parser.add_argument('--product', type=str, choices=products,
                        help='PIO product name')
    parser.add_argument('--target', type=str, required=True,
                        help='PIO build target')
    parser.add_argument('--node-id', type=str)
    parser.add_argument('--platform', default='espressif8266', type=str)
    parser.add_argument('--framework', default='arduino', type=str)
    parser.add_argument('--upload-port', default='/dev/ttyUSB0', type=str)
    parser.add_argument('--monitor-speed', default=115200)
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--debug-network', action='store_true')
    arg = parser.parse_args()

    if not arg.product:
        product = os.path.basename(os.path.realpath(os.getcwd()))
        if product not in products:
            raise ArgumentError(None, 'invalid product')
    else:
        product = arg.product

    product_config = get_config(product)
    if arg.target not in product_config['targets']:
        raise ArgumentError(None, f'target {arg.target} not found for product {product}')

    ini = platformio_ini(product_config=product_config,
                         target=arg.target,
                         node_id=arg.node_id,
                         platform=arg.platform,
                         framework=arg.framework,
                         upload_port=arg.upload_port,
                         monitor_speed=arg.monitor_speed,
                         debug=arg.debug,
                         debug_network=arg.debug_network)
    print(ini)