/
config_checker.py
executable file
·192 lines (164 loc) · 6.27 KB
/
config_checker.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
"""
Utility for
- updating rig-remote config file to the last supported version.
- checking the sanity of the bookmarks file
- checking the sanity of the config file
Please refer to:
https://github.com/Marzona/rig-remote/blob/master/rig_remote
http://gqrx.dk/
http://gqrx.dk/doc/remote-control
http://sourceforge.net/apps/mediawiki/hamlib/index.php?title=Documentation
Author: Simone Marzona <rafael@defying.me>
License: MIT License
Copyright (c) 2015 Simone Marzona
"""
# import modules
import os
import csv
import shutil
import ConfigParser
import platform
import argparse
import logging
import pprint
from os.path import expanduser
from rig_remote.constants import (
DEFAULT_CONFIG,
RIG_URI_CONFIG,
MONITOR_CONFIG,
SCANNING_CONFIG,
MAIN_CONFIG,
CONFIG_SECTIONS,
)
# helper functions
# logging configuration
logger = logging.getLogger(__name__)
def input_arguments():
"""Argument parser.
"""
parser = argparse.ArgumentParser(
description=(
"Utility to check the configuration files and to update"\
" them to the last version."),
epilog="""Please refer to:
https://github.com/Marzona/rig-remote/wiki
http://gqrx.dk/,
http://gqrx.dk/doc/remote-control,
http://sourceforge.net/apps/mediawiki/hamlib/index.php?title=Documentation
Author: Simone Marzona <marzona@knoway.info>
License: MIT License
Copyright (c) 2017 Simone Marzona""")
parser.add_argument("--check_config",
"-cc",
type=str,
required=False,
dest="check_config",
help="Path of the config folder we want to "\
"check.",
)
parser.add_argument("--dump",
"-d",
required=False,
dest="dump",
action="store_true",
help="Dump some useful info for debugging.",
)
parser.add_argument("--update_config",
"-uc",
required=False,
type=str,
dest="update_config",
help="Path of the folder we want to update."\
"The config file will be overwritten "\
"and a .back file will be created too."
)
return parser.parse_args()
def dump_info():
"""Dumps some info regarding the environment we are running in.
The intended use is to ask the user to paste this info for
troubleshooting steps.
"""
print("Python version: {}".format(platform.python_version()))
print("\n ")
print("System's release version: {}".format(platform.version()))
print("\n ")
print("Platform: {}".format(platform.platform()))
print("\n ")
print("OS environment: {}".format(platform.os.environ))
print("\n ")
print("Platform architecture: {}".format(platform.architecture()))
print("\n ")
print("Linux distrubition name: {}".format(platform.dist()))
print("\n ")
print("System/OS name: {}".format(platform.system()))
def update_config(config):
config_file = os.path.join(config, "rig-remote.conf")
config = ConfigParser.RawConfigParser()
for section in CONFIG_SECTIONS:
config.add_section(section)
with open(config_file, "r") as cf:
print("Using config file:{}".format(config_file))
for line in cf:
line = line.rstrip("\n")
if line.split("=")[0].strip() in RIG_URI_CONFIG:
config.set("Rig URI", line.split("=")[0], line.split("=")[1])
if line.split("=")[0].strip() in MONITOR_CONFIG:
config.set("Monitor", line.split("=")[0], line.split("=")[1])
if line.split("=")[0].strip() in MAIN_CONFIG:
config.set("Main", line.split("=")[0], line.split("=")[1])
if line.split("=")[0].strip() in SCANNING_CONFIG:
config.set("Scanning", line.split("=")[0], line.split("=")[1])
shutil.copyfile(config_file, "{}.back".format(config_file))
with open(config_file, "wb") as cf:
config.write(cf)
def check_config(config):
config_file = os.path.join(config, "rig-remote.conf")
with open(config_file, "r") as cf:
print("Using config file:{}".format(config_file))
count = 0
config_data = []
for row in cf:
row = row.rstrip("\n")
if "bookmark_filename" in row:
bookmark_file = row.split("=")[1].rstrip("'").strip()
if any (["[" in row,
"#" in row,
"\n" == row,
"" == row]):
continue
count += 1
if len(row.split("=")) == 2:
config_data.append(row)
else:
print("Error in config file, line: {}".format(row))
if count < 19:
print("The configuration file seems to be "\
"missing some keyword")
print("\n ")
print("Config dump:")
pprint.pprint(config_data)
print("\n ")
print ("Bookmarks info:")
if bookmark_file:
with open(bookmark_file, "r") as bf:
row_list = []
reader = csv.reader(bf, delimiter=",")
for line in reader:
if len(line)!=4:
print("Bookmark line malformed: {}".format(line))
row_list.append(line)
print("\n ")
pprint.pprint(row_list)
# entry point
if __name__ == "__main__":
args = input_arguments()
if not any([args.check_config, args.dump, args.update_config]):
print("At least one option is required, try "\
"config_checker --help")
if args.dump:
dump_info()
elif args.update_config:
update_config(args.update_config)
elif args.check_config:
check_config(args.check_config)