280 lines
9.9 KiB
Python
Executable file
280 lines
9.9 KiB
Python
Executable file
#! /usr/bin/python3
|
|
# Copyright (C) 2018-2019 Merspieler, merspieler _at_ airmail.cc
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License as
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import json
|
|
from time import sleep
|
|
|
|
try:
|
|
import tty, termios
|
|
except ImportError:
|
|
# Probably Windows.
|
|
try:
|
|
import msvcrt
|
|
except ImportError:
|
|
# Just give up here.
|
|
raise ImportError('getch not available')
|
|
else:
|
|
getch = msvcrt.getch
|
|
else:
|
|
def getch():
|
|
fd = sys.stdin.fileno()
|
|
old_settings = termios.tcgetattr(fd)
|
|
try:
|
|
tty.setraw(fd)
|
|
ch = sys.stdin.read(1)
|
|
finally:
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
|
return ch
|
|
|
|
argc = len(sys.argv)
|
|
try:
|
|
project = sys.argv[1]
|
|
except:
|
|
print("No project was given. See edit-settings -h for details")
|
|
sys.exit(1)
|
|
i = 1
|
|
while i < argc:
|
|
if sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
|
print("useage: edit-settings <project> [OPTIONS]")
|
|
print("You can edit your project settings\n")
|
|
print("OPTIONS")
|
|
print(" -h, --help Shows this help and exit")
|
|
sys.exit(0)
|
|
elif i != 1:
|
|
print("Unknown option " + sys.argv[i])
|
|
sys.exit(1)
|
|
i += 1
|
|
|
|
if project == "":
|
|
print("Option -p <project> is mandatory")
|
|
sys.exit(1)
|
|
|
|
if os.path.isfile("projects/" + project + "/params.ini") == False:
|
|
print("Project '" + project + "' not found")
|
|
sys.exit(2)
|
|
|
|
console_height, console_width = os.popen('stty size', 'r').read().split()
|
|
|
|
def fill_space ( string, aim_length ):
|
|
length = len(string)
|
|
while length < aim_length:
|
|
string += " "
|
|
length += 1
|
|
return string
|
|
|
|
#try:
|
|
with open("parameters") as json_data:
|
|
options = json.load(json_data)
|
|
#except:
|
|
# print("A problem occured while reading the file with all available parameters")
|
|
|
|
class colors:
|
|
SELECTED = '\033[30;42m'
|
|
HIGHLIGHT = '\033[30;46m'
|
|
ERR = '\033[37;41m'
|
|
HELP_HIGHLIGHT = '\033[96m'
|
|
NOT_SAVED = '\033[33m'
|
|
ENDC = '\033[0m'
|
|
|
|
try:
|
|
print("Reading ini file...")
|
|
ini_file = open("projects/" + project + "/params.ini", "r")
|
|
for line in ini_file:
|
|
for group in options:
|
|
for option in group["Options"]:
|
|
ret = re.search(option["Name"] + " = (.*)", line)
|
|
if ret:
|
|
value = ret.group(1).replace("'", "")
|
|
value = value.replace('"', "")
|
|
if option["type"] == "bool":
|
|
if value == "False":
|
|
option["value"] = False
|
|
else:
|
|
option["value"] = True
|
|
elif option["type"] == "str":
|
|
option["value"] = str(value)
|
|
elif option["type"] == "float":
|
|
option["value"] = float(value)
|
|
elif option["type"] == "int":
|
|
option["value"] = int(value)
|
|
except:
|
|
print("A problem occured while reading the params.ini file...")
|
|
sys.exit(2)
|
|
finally:
|
|
ini_file.close()
|
|
|
|
line_sel = 1
|
|
mode = "group-select"
|
|
selected_group = ""
|
|
err_msg = ""
|
|
saved = True
|
|
|
|
while True:
|
|
os.system('clear')
|
|
|
|
print(colors.ERR + err_msg + colors.ENDC)
|
|
draw_line = 1
|
|
while draw_line < int(console_height) - 2:
|
|
line = " "
|
|
|
|
try:
|
|
if mode == "group-select":
|
|
line += options[draw_line - 1]["Group_Name"]
|
|
line = fill_space(line, 24)
|
|
opt_count = len(options[draw_line - 1]["Options"])
|
|
if opt_count < 10:
|
|
line += " "
|
|
line += str(opt_count)
|
|
|
|
elif mode == "option-select":
|
|
line += options[selected_group]["Options"][draw_line - 1]["Name"]
|
|
line = fill_space(line, 46)
|
|
value = str(options[selected_group]["Options"][draw_line - 1]["value"])
|
|
if options[selected_group]["Options"][draw_line - 1]["changed"]:
|
|
value = colors.NOT_SAVED + value + colors.ENDC
|
|
elif options[selected_group]["Options"][draw_line - 1]["value"] != options[selected_group]["Options"][draw_line - 1]["default"]:
|
|
value = colors.HELP_HIGHLIGHT + value + colors.ENDC
|
|
line += value
|
|
except IndexError:
|
|
pass
|
|
|
|
if line_sel == draw_line:
|
|
line = colors.SELECTED + line + colors.ENDC
|
|
|
|
print(line)
|
|
draw_line += 1
|
|
|
|
# Status and help line
|
|
print("h" + colors.HIGHLIGHT + "Help " + colors.ENDC + "j" + colors.HIGHLIGHT + "Down " + colors.ENDC + "k" + colors.HIGHLIGHT + "Up " + colors.ENDC + "Enter" + colors.HIGHLIGHT + "Select" + colors.ENDC + "q" + colors.HIGHLIGHT + "Quit " + colors.ENDC + "s" + colors.HIGHLIGHT + "Save " + colors.ENDC)
|
|
|
|
key = ord(getch())
|
|
# print(key)
|
|
# sleep(1)
|
|
if key == 27 or key == 113: # ESC or Q
|
|
if saved:
|
|
break
|
|
else:
|
|
ret = input("You have made changes but you haven't saved. Do you really want to quit? (y/n): ")
|
|
if ret == "y":
|
|
break
|
|
elif key == 13: # Enter
|
|
if mode == "group-select":
|
|
mode = "option-select"
|
|
selected_group = line_sel - 1
|
|
line_sel = 1
|
|
elif mode == "option-select":
|
|
if options[selected_group]["Options"][line_sel - 1]["type"] == "bool":
|
|
options[selected_group]["Options"][line_sel - 1]["value"] = not options[selected_group]["Options"][line_sel - 1]["value"]
|
|
err_msg = ""
|
|
saved = False
|
|
options[selected_group]["Options"][line_sel - 1]["changed"] = True
|
|
elif options[selected_group]["Options"][line_sel - 1]["type"] == "int":
|
|
ret = input("Please enter a new value of type INT for '" + options[selected_group]["Options"][line_sel - 1]["Name"] + "': ")
|
|
search = re.search("^[0-9]*$", ret)
|
|
if search:
|
|
options[selected_group]["Options"][line_sel - 1]["value"] = int(ret)
|
|
err_msg = ""
|
|
saved = False
|
|
options[selected_group]["Options"][line_sel - 1]["changed"] = True
|
|
else:
|
|
err_msg = "'" + ret + "' is no valid value for type INT"
|
|
elif options[selected_group]["Options"][line_sel - 1]["type"] == "float":
|
|
ret = input("Please enter a new value of type FLOAT for '" + options[selected_group]["Options"][line_sel - 1]["Name"] + "': ")
|
|
search = re.search("^[0-9]*\.[0-9]*$|^[0-9]*$", ret)
|
|
if search:
|
|
options[selected_group]["Options"][line_sel - 1]["value"] = float(ret)
|
|
err_msg = ""
|
|
saved = False
|
|
options[selected_group]["Options"][line_sel - 1]["changed"] = True
|
|
else:
|
|
err_msg = "'" + ret + "' is no valid value for type FLOAT"
|
|
elif options[selected_group]["Options"][line_sel - 1]["type"] == "str":
|
|
options[selected_group]["Options"][line_sel - 1]["value"] = input("Please enter a new value of type STRING for '" + options[selected_group]["Options"][line_sel - 1]["Name"] + "': ")
|
|
err_msg = ""
|
|
saved = False
|
|
options[selected_group]["Options"][line_sel - 1]["changed"] = True
|
|
elif key == 106: # J
|
|
if mode == "group-select":
|
|
if line_sel < len(options):
|
|
line_sel += 1
|
|
elif mode == "option-select":
|
|
if line_sel < len(options[selected_group]["Options"]):
|
|
line_sel += 1
|
|
elif key == 107: # K
|
|
if line_sel > 1:
|
|
line_sel -= 1
|
|
elif key == 104: # H
|
|
os.system('clear')
|
|
print(colors.HELP_HIGHLIGHT + "This program is released as part of easy-osm2city.")
|
|
print("See the LICENSE file for more information." + colors.ENDC)
|
|
print("\nIn group select mode you can select a group of parameters.")
|
|
print("The groups are similar as the groups on the official docs site.")
|
|
print("\nIn option select mode you can edit the parameters value.")
|
|
print("\nControls:")
|
|
print(colors.HELP_HIGHLIGHT + " j: " + colors.ENDC + "Scroll down")
|
|
print(colors.HELP_HIGHLIGHT + " k: " + colors.ENDC + "Scroll up")
|
|
print(colors.HELP_HIGHLIGHT + " b: " + colors.ENDC + "Go back to group view")
|
|
print(colors.HELP_HIGHLIGHT + " Enter: " + colors.ENDC + "In group view, select group")
|
|
print(" On bool value, toggle True/False")
|
|
print(" On other values, bring up dialog to change value")
|
|
print(colors.HELP_HIGHLIGHT + " r: " + colors.ENDC + "Resets the selected parameter to the default value")
|
|
print(colors.HELP_HIGHLIGHT + " d: " + colors.ENDC + "Shows parameter description")
|
|
print(colors.HELP_HIGHLIGHT + " s: " + colors.ENDC + "Saves the changes to the params.ini file")
|
|
print(colors.HELP_HIGHLIGHT + " ESC q: " + colors.ENDC + "Exit the program. Ask if should be exited if changes were not saved")
|
|
print("Press any key to return.")
|
|
getch()
|
|
elif key == 100: # D
|
|
if mode == "option-select":
|
|
os.system('clear')
|
|
if options[selected_group]["Options"][line_sel - 1]["description"] != "":
|
|
print(options[selected_group]["Options"][line_sel - 1]["description"])
|
|
else:
|
|
print("No description available")
|
|
print(colors.HELP_HIGHLIGHT + "\n\n current value: " + colors.ENDC + str(options[selected_group]["Options"][line_sel - 1]["value"]))
|
|
print(colors.HELP_HIGHLIGHT + " default value: " + colors.ENDC + str(options[selected_group]["Options"][line_sel - 1]["default"]))
|
|
print(colors.HELP_HIGHLIGHT + "\nPress any key to return." + colors.ENDC)
|
|
getch()
|
|
elif key == 98: # B
|
|
if mode == "option-select":
|
|
mode = "group-select"
|
|
line_sel = selected_group + 1
|
|
err_msg = ""
|
|
elif key == 115: # S
|
|
ini_file = open("projects/" + project + "/params.ini", "w")
|
|
for group in options:
|
|
written = False
|
|
for option in group["Options"]:
|
|
# Write only non default values to file.
|
|
if option["default"] != option["value"]:
|
|
if option["type"] == "str":
|
|
ini_file.write(option["Name"] + ' = "' + str(option["value"]) + '"\n')
|
|
else:
|
|
ini_file.write(option["Name"] + " = " + str(option["value"]) + "\n")
|
|
written = True
|
|
if written:
|
|
ini_file.write("\n") # make the file more readable by seperating in blocks
|
|
ini_file.close()
|
|
saved = True
|
|
for i in range(0, len(options)):
|
|
for j in range(0, len(options[i]["Options"])):
|
|
options[i]["Options"][j]["changed"] = False
|
|
elif key == 114: # R
|
|
if mode == "option-select":
|
|
options[selected_group]["Options"][line_sel - 1]["value"] = options[selected_group]["Options"][line_sel - 1]["default"]
|