-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialConf.py
More file actions
63 lines (57 loc) · 2.18 KB
/
Copy pathserialConf.py
File metadata and controls
63 lines (57 loc) · 2.18 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
"""
SerialConf.py -> Python tool to identify or generate configuration files
Author: Michel Cantacuzene <michel@thesweaterguys.com>
GitHub: michmich112
Copyright: The Sweater Guys
"""
import json
from os import path
from listMenu import y_n_choice,input_handler
defaultFilePath = "serialconf.json"
# Checks for conf file with defaultFilePath or user input filePath with priority on user input
def get_conf_file(file_path = None):
file_name = file_path or defaultFilePath
if path.exists(file_name):
with open(file_name,'r') as confFile:
return json.load(confFile)
else:
print("> [ WARNING ] no file found with path " + file_name)
return None
# Loads the configuration file if possible,
# Prompts the user to configure ports manually or create a file through cli
def load_config(file = None):
data = get_conf_file(file)
if data is None:
if y_n_choice("Would you like to create a new config file?"):
data = create_config()
else:
print("> Manual Configuration chosen.")
data = manual_config()
else:
print("> Configuration file found")
return data
# Creates a manual configuration from user input
def manual_config():
config = {} # holds the config data
add_new = True
while add_new:
port = input_handler("Port = ", retry=True)
br = input_handler("Baud Rate (9600) = ", default=9600, acc_type=int, retry=False)
timeout = input_handler("Timeout (1s) = ",default=1, acc_type=int, retry=False)
name = input_handler("Custom port name (Optional) = ", default=None, retry=False)
config[port] = {
"port":port,
"baudrate": br,
"timeout": timeout,
"name": name
}
add_new = y_n_choice("Would you like to add another port configuration?")
return config
# User input to create and save a new config file
def create_config():
config = manual_config()
print("> Writing config to serialconf.json")
with open(defaultFilePath,'w') as config_file:
json.dump(config, config_file)
print("> Config generated. Add this file to your git repository for future use.")
return config