#!/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)