diff --git a/circuit_tree.py b/circuit_tree.py new file mode 100644 index 0000000000000000000000000000000000000000..96b7928e6629f7715fcb94d37979c684e399bbfb --- /dev/null +++ b/circuit_tree.py @@ -0,0 +1,63 @@ +import os +from operator import itemgetter + +import click +from tree_format import format_tree + +from inventory_provider.db.ims import IMS, CIRCUIT_PROPERTIES + +username = 'TEST05' +password = '' +bt = os.getenv('IMS_BT') + +ds = IMS('http://83.97.94.128:2080/api', username, password, bt) + +NAV_PROPS = [ + CIRCUIT_PROPERTIES['Speed'], + CIRCUIT_PROPERTIES['Product'] +] + + +@click.command() +@click.option('-c', 'carriers', is_flag=True) +@click.argument('root_circuit_id', type=click.INT, required=True) +def cli(carriers, root_circuit_id): + if carriers: + children_prop = 'carriercircuits' + childid = 'carriercircuitid' + NAV_PROPS.append(CIRCUIT_PROPERTIES['CarrierCircuits']) + else: + children_prop = 'subcircuits' + childid = 'subcircuitid' + NAV_PROPS.append(CIRCUIT_PROPERTIES['SubCircuits']) + + def _get_childcircuit_tree(circuit_id): + + circuit = ds.get_entity_by_id( + 'circuit', circuit_id, navigation_properties=NAV_PROPS) + _tree = [ + f'{circuit["id"]} -- {circuit["name"]} -- ' + f'prod. {circuit["product"]["name"]} -- ' + f'spd. {circuit["speed"]["name"]}' + ] + + if circuit[children_prop]: + + children = [] + for child_circuit in circuit[children_prop]: + children.append(_get_childcircuit_tree( + child_circuit[childid] + )) + _tree.append(children) + else: + _tree.append([]) + return _tree + tree = _get_childcircuit_tree(root_circuit_id) + print(format_tree( + tree, format_node=itemgetter(0), get_children=itemgetter(1)) + ) + + +if __name__ == '__main__': + # 659386 + cli()