Skip to content

Commit 8dd72fb

Browse files
committed
cleaned up the code
1 parent 22ccca2 commit 8dd72fb

File tree

4 files changed

+65
-88
lines changed

4 files changed

+65
-88
lines changed

python_hackrf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '1.2.1'
1+
__version__ = '1.2.2'
22

33
from python_hackrf.pylibhackrf import pyhackrf # noqa F401
44
from python_hackrf.pyhackrf_tools import ( # noqa F401

python_hackrf/__main__.py

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
import argparse
2+
import sys
3+
14
from .pyhackrf_tools import (
5+
pyhackrf_info,
26
pyhackrf_operacake,
3-
pyhackrf_transfer,
47
pyhackrf_sweep,
5-
pyhackrf_info,
8+
pyhackrf_transfer,
69
)
710
from .pylibhackrf import pyhackrf
8-
import argparse
9-
import sys
1011

1112

12-
def main():
13+
def main() -> None:
1314
parser = argparse.ArgumentParser(
1415
description='python_hackrf is a Python wrapper for libhackrf and hackrf-tools.',
15-
usage='python_hackrf [-h] {info, sweep, operacake} ...'
16+
usage='python_hackrf [-h] {info, sweep, operacake} ...',
1617
)
1718
subparsers = parser.add_subparsers(dest='command', title='Available commands')
1819
subparsers.required = True
1920
pyhackrf_info_parser = subparsers.add_parser(
20-
'info', help='Read device information from HackRF such as serial number and firmware version.', usage='python_hackrf info [-h] [-f] [-s]'
21+
'info', help='Read device information from HackRF such as serial number and firmware version.', usage='python_hackrf info [-h] [-f] [-s]',
2122
)
2223
pyhackrf_info_parser.add_argument('-f', '--full', action='store_true', help='show info like in hackrf_info')
2324
pyhackrf_info_parser.add_argument('-s', '--serial_numbers', action='store_true', help='show only founded serial_numbers')
2425

2526
pyhackrf_operacake_parser = subparsers.add_parser(
26-
'operacake', help='Configure Opera Cake antenna switch connected to HackRF.', usage='python_hackrf operacake [-h] [-d] [-o] [-m] [-a] [-b] [-f] [-t] [-w] [-l] [-g]'
27+
'operacake', help='Configure Opera Cake antenna switch connected to HackRF.', usage='python_hackrf operacake [-h] [-d] [-o] [-m] [-a] [-b] [-f] [-t] [-w] [-l] [-g]',
2728
)
2829
pyhackrf_operacake_parser.add_argument('-d', action='store', help='serial_number. serial number of desired HackRF', metavar='')
2930
pyhackrf_operacake_parser.add_argument('-o', '--address', action='store', help='specify a particular Opera Cake by address. Default is 0', metavar='', default=0)
@@ -37,7 +38,7 @@ def main():
3738
pyhackrf_operacake_parser.add_argument('-g', '--gpio_test', action='store_true', help='test GPIO functionality of an Opera Cake')
3839

3940
pyhackrf_sweep_parser = subparsers.add_parser(
40-
'sweep', help='Command-line spectrum analyzer.', usage='python_hackrf sweep [-h] [-d] [-a] [-f] [-p] [-l] [-g] [-w] [-1] [-N] [-B] [-S] [-s] [-b] [-r]'
41+
'sweep', help='Command-line spectrum analyzer.', usage='python_hackrf sweep [-h] [-d] [-a] [-f] [-p] [-l] [-g] [-w] [-1] [-N] [-B] [-S] [-s] [-b] [-r]',
4142
)
4243
pyhackrf_sweep_parser.add_argument('-d', action='store', help='serial number of desired HackRF', metavar='')
4344
pyhackrf_sweep_parser.add_argument('-a', action='store_true', help='RX RF amplifier. If specified = Enable')
@@ -55,7 +56,7 @@ def main():
5556
pyhackrf_sweep_parser.add_argument('-r', action='store', help='<filename> output file', metavar='')
5657

5758
pyhackrf_transfer_parser = subparsers.add_parser(
58-
'transfer', help='Send and receive signals using HackRF. Input/output files consist of complex64 quadrature samples.', usage='python_hackrf transfer [-h] [-d] [-r] [-t] [-f] [-i] [-o] [-m] [-a] [-p] [-l] [-g] [-x] [-s] [-N] [-R] -[b] [-H]'
59+
'transfer', help='Send and receive signals using HackRF. Input/output files consist of complex64 quadrature samples.', usage='python_hackrf transfer [-h] [-d] [-r] [-t] [-f] [-i] [-o] [-m] [-a] [-p] [-l] [-g] [-x] [-s] [-N] [-R] -[b] [-H]',
5960
)
6061
pyhackrf_transfer_parser.add_argument('-d', action='store', help='serial number of desired HackRF', metavar='')
6162
pyhackrf_transfer_parser.add_argument('-r', action='store', help='<filename> receive data into file (use "-" for stdout)', metavar='')
@@ -79,13 +80,13 @@ def main():
7980
parser.print_help()
8081
sys.exit(0)
8182

82-
args, unparsed_args = parser.parse_known_args()
83+
args, _ = parser.parse_known_args()
8384

8485
if args.command == 'info':
8586
if args.serial_numbers:
8687
pyhackrf_info.pyhackrf_serial_numbers_list_info()
87-
else:
88-
pyhackrf_info.pyhackrf_info()
88+
return
89+
pyhackrf_info.pyhackrf_info()
8990

9091
elif args.command == 'operacake':
9192
if sum([args.list, args.mode is not None, args.f is not None, args.t is not None, args.gpio_test, (args.a is not None or args.b is not None)]) == 1:
@@ -96,80 +97,63 @@ def main():
9697
)
9798
return
9899

99-
elif args.mode in ('manual', 'frequency', 'time'):
100-
address = 0
101-
if args.address is not None:
102-
try:
103-
address = int(args.address)
104-
except Exception:
105-
pass
100+
if args.mode in {'manual', 'frequency', 'time'}:
101+
try:
102+
address = int(args.address)
103+
except Exception:
104+
address = 0
105+
106106
pyhackrf_operacake.pyhackrf_set_operacake_mode(
107107
address,
108108
args.mode,
109-
serial_number=args.d
109+
serial_number=args.d,
110110
)
111111
return
112112

113-
elif args.f is not None:
113+
if args.f is not None:
114114
str_freq_ranges = args.f.split(',')
115115
freq_ranges = []
116116
for freq_range in str_freq_ranges:
117-
freq_range = freq_range.split(':')
118-
port, freq_min, freq_max = None, None, None
119117
try:
120-
port = freq_range[0]
118+
port, freq_min, freq_max = freq_range.split(':')
119+
freq_min, freq_max = int(freq_min), int(freq_max)
120+
if port in pyhackrf.operacake_ports.keys():
121+
freq_ranges.extend([port, freq_min, freq_max])
121122
except Exception:
122123
pass
123-
try:
124-
freq_min = int(freq_range[1])
125-
except Exception:
126-
pass
127-
try:
128-
freq_max = int(freq_range[2])
129-
except Exception:
130-
pass
131-
132-
if freq_min is not None and freq_max is not None and port in ('A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4'):
133-
freq_ranges.extend([port, freq_min, freq_max])
134124

135125
pyhackrf_operacake.pyhackrf_set_operacake_freq_ranges(
136126
freq_ranges,
137-
serial_number=args.d
127+
serial_number=args.d,
138128
)
139129
return
140130

141-
elif args.t is not None:
131+
if args.t is not None:
142132
str_dwell_times = args.f.split(',')
143133
dwell_times = []
144134
for dwell_time in str_dwell_times:
145-
dwell_time = dwell_time.split(':')
146-
dwell, port = None
147-
if len(dwell_time) == 2:
148-
try:
149-
port = dwell_time[0]
150-
except Exception:
151-
pass
152-
try:
153-
dwell = int(dwell_time[1])
154-
except Exception:
155-
pass
156-
else:
157-
port = dwell_time[0]
158-
try:
135+
try:
136+
dwell_split = dwell_time.split(':')
137+
138+
if len(dwell_split) == 2:
139+
port, dwell = dwell_split
140+
dwell = int(dwell)
141+
else:
142+
port = dwell_split[0]
159143
dwell = int(args.w)
160-
except Exception:
161-
pass
162144

163-
if dwell is not None and port in ('A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4'):
164-
dwell_times.extend([dwell, port])
145+
if port in pyhackrf.operacake_ports.keys():
146+
dwell_times.extend([dwell, port])
147+
except Exception:
148+
pass
165149

166150
pyhackrf_operacake.pyhackrf_set_operacake_dwell_times(
167151
dwell_times,
168-
serial_number=args.d
152+
serial_number=args.d,
169153
)
170154
return
171155

172-
elif (args.a is not None or args.b is not None):
156+
if (args.a is not None or args.b is not None):
173157
address, port_a, port_b = 0, 'A1', 'B1'
174158
if args.a is not None:
175159
port_a = args.a
@@ -193,7 +177,7 @@ def main():
193177
else:
194178
port_b = 'B1'
195179

196-
if port_a in ('A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4') and port_b in ('A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4'):
180+
if port_a in pyhackrf.operacake_ports.keys() and port_b in pyhackrf.operacake_ports.keys():
197181
if port_a[0] != port_b[0]:
198182
pyhackrf_operacake.pyhackrf_set_operacake_ports(
199183
address,
@@ -219,25 +203,15 @@ def main():
219203
)
220204
return
221205

222-
print('Argument error')
223-
print(pyhackrf_sweep_parser.usage)
224-
225206
elif args.command == 'sweep':
226207
str_frequencies = args.f.split(',')
227208
frequencies = []
228209
for frequency_range in str_frequencies:
229-
frequency_range = frequency_range.split(':')
230-
freq_min, freq_max = None, None
231210
try:
232-
freq_min = int(frequency_range[0])
233-
except Exception:
234-
pass
235-
try:
236-
freq_max = int(frequency_range[1])
211+
freq_min, freq_max = map(int, frequency_range.split(':'))
212+
frequencies.extend([freq_min, freq_max])
237213
except Exception:
238214
pass
239-
if freq_min is not None and freq_max is not None:
240-
frequencies.extend([freq_min, freq_max])
241215

242216
pyhackrf_sweep.pyhackrf_sweep(
243217
frequencies=frequencies,

python_hackrf/pyhackrf_tools/pyhackrf_info.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def pyhackrf_info(print_to_console: bool = True, initialize: bool = True) -> Non
4444
print_info += f'Board ID Number: {board_id} ({board_id_name})\n'
4545
print_info += f'Firmware Version: {device.pyhackrf_version_string_read()} ({device.pyhackrf_usb_api_version_read()})\n'
4646
print_info += f'Part ID Number: 0x{read_partid_serialno[0][0]:08x} 0x{read_partid_serialno[0][1]:08x}\n'
47-
if board_rev not in (0xFE, 0xFF):
47+
if board_rev not in {0xFE, 0xFF}:
4848
print_info += f'Hardware Revision: {board_rev_name}\n'
4949
if board_rev > 0:
5050
if (board_rev & 0x80):
@@ -67,8 +67,9 @@ def pyhackrf_info(print_to_console: bool = True, initialize: bool = True) -> Non
6767

6868
if print_to_console:
6969
print(print_info)
70-
else:
71-
return print_info
70+
return None
71+
72+
return print_info
7273

7374

7475
def pyhackrf_serial_numbers_list_info(print_to_console: bool = True, initialize: bool = True) -> None | tuple[int, list]:
@@ -82,5 +83,6 @@ def pyhackrf_serial_numbers_list_info(print_to_console: bool = True, initialize:
8283

8384
if print_to_console:
8485
print(f'Serial numbers [{device_list.device_count}]: {device_list.serial_numbers}')
85-
else:
86-
return device_list.device_count, device_list.serial_numbers
86+
return None
87+
88+
return device_list.device_count, device_list.serial_numbers

python_hackrf/pyhackrf_tools/pyhackrf_operacake.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
27-
serial_number: str = None,
27+
serial_number: str | None = None,
2828
print_to_console: bool = True,
2929
) -> None | str:
3030

@@ -49,19 +49,20 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
4949
else:
5050
operacake_info = 'Opera Cakes found: None'
5151

52-
if print_to_console:
53-
print(operacake_info)
54-
else:
55-
return operacake_info
56-
5752
if initialize:
5853
device.pyhackrf_close()
5954
pyhackrf.pyhackrf_exit()
6055

56+
if print_to_console:
57+
print(operacake_info)
58+
return None
59+
60+
return operacake_info
61+
6162

6263
def pyhackrf_set_operacake_mode(address: int,
6364
mode: str,
64-
serial_number: str = None,
65+
serial_number: str | None = None,
6566
device: pyhackrf.PyHackrfDevice = None,
6667
) -> None:
6768

@@ -90,7 +91,7 @@ def pyhackrf_set_operacake_mode(address: int,
9091

9192

9293
def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
93-
serial_number: str = None,
94+
serial_number: str | None = None,
9495
device: pyhackrf.PyHackrfDevice = None,
9596
) -> None:
9697

@@ -112,7 +113,7 @@ def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
112113

113114

114115
def pyhackrf_set_operacake_dwell_times(dwell_times: list,
115-
serial_number: str = None,
116+
serial_number: str | None = None,
116117
device: pyhackrf.PyHackrfDevice = None,
117118
) -> None:
118119

@@ -136,7 +137,7 @@ def pyhackrf_set_operacake_dwell_times(dwell_times: list,
136137
def pyhackrf_set_operacake_ports(address: int,
137138
port_a: str,
138139
port_b: str,
139-
serial_number: str = None,
140+
serial_number: str | None = None,
140141
device: pyhackrf.PyHackrfDevice = None,
141142
) -> None:
142143

@@ -158,7 +159,7 @@ def pyhackrf_set_operacake_ports(address: int,
158159

159160

160161
def pyhackrf_operacake_gpio_test(address: int,
161-
serial_number: str = None,
162+
serial_number: str | None = None,
162163
device: pyhackrf.PyHackrfDevice = None,
163164
) -> None:
164165

0 commit comments

Comments
 (0)