-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplelogger.py
More file actions
146 lines (124 loc) · 4.17 KB
/
simplelogger.py
File metadata and controls
146 lines (124 loc) · 4.17 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import argparse
import datetime
import os.path
from contextlib import closing
from hamutils.adif.adi import ADIReader, ADIWriter
DEFAULT_DATE = datetime.datetime.utcnow().date()
DEFAULT_BAND = "20m"
DEFAULT_MODE = "CW"
class LogRecord:
def __init__(self, station_callsign=None, operator=None):
self._fields = {}
if station_callsign:
self._fields["station_callsign"] = station_callsign
if operator:
self._fields["operator"] = operator
@property
def fields(self):
return self._fields
def prompt_all(self):
def retry(func):
while True:
try:
func()
except EOFError:
raise
except Exception as ex:
print("exception caught", ex)
else:
break
while True:
retry(self.prompt_band)
retry(self.prompt_mode)
retry(self.prompt_call)
retry(self.prompt_datetime)
retry(self.prompt_rst)
retry(self.prompt_exchange)
if self.prompt_confirm() == "y":
return
def prompt_band(self):
global DEFAULT_BAND
band = input(f"BAND ({DEFAULT_BAND}): ")
if band == '':
band = DEFAULT_BAND
else:
DEFAULT_BAND = band
self._fields["band"] = band
def prompt_mode(self):
global DEFAULT_MODE
mode = input(f"MODE ({DEFAULT_MODE}): ").upper()
if mode == '':
mode = DEFAULT_MODE
else:
DEFAULT_MODE = mode
self._fields["mode"] = mode
def prompt_datetime(self):
global DEFAULT_DATE
current_time = datetime.datetime.now().time()
dt = input(f"DATETIME ([{DEFAULT_DATE.strftime('%Y%m%d')} ][{current_time.strftime('%H%M')}]): ")
if len(dt) == 0:
tm = current_time
dt = datetime.datetime.combine(DEFAULT_DATE, tm)
elif len(dt) <= 4:
tm = datetime.datetime.strptime(dt, "%H%M").time()
dt = datetime.datetime.combine(DEFAULT_DATE, tm)
else:
dt = datetime.datetime.strptime(dt, "%Y%m%d %H%M")
DEFAULT_DATE = dt.date()
self._fields["datetime_on"] = dt
def prompt_call(self):
call = ''
while call == '':
call = input("CALL: ").upper()
self._fields["call"] = call
def prompt_rst(self):
if DEFAULT_MODE == "CW":
rst_rcvd = "599"
rst_sent = "599"
else:
rst_rcvd = "59"
rst_sent = "59"
rst = input(f"RST ({rst_rcvd}): ")
if rst == '':
rst = rst_rcvd
else:
int(rst) # raises ValueError if not integer
self._fields["rst_rcvd"] = rst
self._fields["rst_sent"] = rst_sent
def prompt_exchange(self):
exch = input("EXCHANGE: ")
if exch:
self._fields["srx_string"] = exch
def prompt_confirm(self):
inp = ''
while inp not in ("y", "n"):
print("***QSO***")
for k, v in self._fields.items():
print(f"{k}: {v}")
print("*********")
inp = input("Confirm ([y]|n): ").lower()
if inp == '':
inp = 'y'
return inp
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--station-callsign", required=True)
parser.add_argument("--operator")
parser.add_argument("-o", "--output", required=True)
args = parser.parse_args()
oldqsos = []
if os.path.exists(args.output):
with open(args.output, "r") as f:
adif = ADIReader(f)
oldqsos.extend((qso for qso in adif))
with open(args.output, "wb") as f:
with closing(ADIWriter(f, "simplelogger.py", 0.1)) as adif:
try:
for qso in oldqsos:
adif.add_qso(**qso)
while True:
record = LogRecord(args.station_callsign, args.operator)
record.prompt_all()
adif.add_qso(**record.fields)
except EOFError:
pass