-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
122 lines (86 loc) · 3.58 KB
/
main.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script execution of the Link Budget.
Allows user to choose whether to use script or UI app
"""
from project.process import main_process, load_from_yaml
from project.settings import DEFAULT_LINK_CONFIG
from project.app.app import run_app
import argparse
import os
from pathlib import Path, WindowsPath
def run_script(config_file, decimals=2):
'''Runs Link Budget Toolbox as a script without a User Interface
Total gain and margin are printed in console as results
Parameters
----------
config_file : str
File path to configuration YAML file
decimals : int, default=2
Decimals to round off to in printed results
Returns
-------
dict
Updated configuration file, with gain of each element and total overall
gain and margin
'''
def column_print(values, indent=''):
col_width = max(len(name) for row in values for name in row) + 2
for row in values:
print(indent + "".join(word.ljust(col_width) for word in row))
# Load config
data = load_from_yaml(config_file)
result = main_process(data)
# Print Results
header = [['Input Power:', f'{data["general_values"]["input_power"]} dBm'],
['Receiver System Threshold', f"{data['general_values']['rx_sys_threshold']} dBm"]]
# Order elements according to index (ensures they are printed in logical order)
elements_ordered = {key: val for key, val in
sorted(result['elements'].items(), key=lambda item: item[1]['idx'])}
values = []
for elem, data in elements_ordered.items():
values.append([elem, f"{data['gain_loss']:.{decimals}f} dB"])
footer = [['Total Gain:', f"{result['general_values']['total_gain']:.{decimals}f} dB"],
["Margin:", f"{result['general_values']['total_margin']:.{decimals}f} dB"]]
column_print(header)
print()
print(f'Total Gain per Element:')
column_print(values, indent='\t')
print()
column_print(footer)
return result
def main():
'''Runs Link Budget Toolbox. Defaults to GUI app, unless CLI argument '-s' is passed
usage: Link Budget Toolbox [-h] [-d | -s] [-f FILE]
optional arguments:
-h, --help show this help message and exit
-d, --debug GUI app only: Print debug statements to terminal
-s, --script Run as CLI script. Does not open GUI
-f FILE, --file FILE Link Budget configuration file (YAML)
'''
parser = argparse.ArgumentParser(prog="Link Budget Toolbox",
description="Runs by default as application with GUI")
group = parser.add_mutually_exclusive_group()
group.add_argument('-d', '--debug', help='GUI app only: Print debug statements to terminal',
action="store_true")
group.add_argument('-s', '--script', help="Run as CLI script. Does not open GUI",
action="store_true")
parser.add_argument('-f', '--file', nargs=1, default=DEFAULT_LINK_CONFIG, help='Link Budget configuration file (YAML)')
args = parser.parse_args()
# ----------- Command Line Script ---------
if args.script:
file = args.file
if not isinstance(file, WindowsPath):
file = Path(os.getcwd(), file[0].strip("'"))
cfg_file = Path(os.getcwd(), file)
print(cfg_file)
run_script(str(cfg_file))
# --------- GUI Application ------------
else:
if args.debug:
run_app(log_lvl='DEBUG')
else:
run_app()
if __name__ == '__main__':
main()