Skip to content

Commit

Permalink
fix: environment variables new system, fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Elagoht committed Jun 11, 2023
1 parent eff3c1a commit 1840c50
Showing 1 changed file with 64 additions and 76 deletions.
140 changes: 64 additions & 76 deletions GUI/Variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from re import search
from os import system

# ! REWRITE WHOLE SYSTEM WITH 2 LISTS INSTEAD OF A DICT


class VariableWin(QWidget):
def __init__(self, parent: QWidget):
super().__init__()
self.setParent(parent)
self.variables = {}
self.keys = []
self.values = []
self.current_value = ""

# Create widgets
Expand Down Expand Up @@ -51,6 +54,7 @@ def __init__(self, parent: QWidget):
self.tblVariables.currentCellChanged.connect(self.get_current_value)
self.tblVariables.setCurrentCell(0, 0)

# Get selected cell's text
def get_current_value(self, row, col):
self.current_value = "" \
if self.tblVariables.item(row, col) is None \
Expand All @@ -64,17 +68,18 @@ def get_environment_variables(self):
# Check for format
if search("^[a-zA-Z_][a-zA-Z0-9_]*=\S", line):
data = line.strip().split("=")
self.variables[data[0]] = data[1]
self.keys.append(data[0])
self.values.append(data[1])

# Reload table with existing variables
def reload_table(self):
# Set row count
length = len(self.variables.keys())
length = len(self.keys)
self.tblVariables.setRowCount(length)
# Fil Data
for row, variable in enumerate(self.variables.keys()):
# Fill data
for row, variable in enumerate(self.keys):
self.tblVariables.setItem(row, 0, QTableWidgetItem(variable))
for row, value in enumerate(self.variables.values()):
for row, value in enumerate(self.values):
self.tblVariables.setItem(row, 1, QTableWidgetItem(value))

def add_variable(self):
Expand All @@ -87,81 +92,62 @@ def add_variable(self):
return

# Add one more row
self.keys.append("")
self.values.append("")
self.tblVariables.setRowCount(length + 1)

def change_data(self, row, col):
keys = list(self.variables.keys())
values = list(self.variables.values())
new = self.tblVariables.item(row, col).text()

# Check if new data match with regex
if new != "" and col != 1:
if not search("^[a-zA-Z_][a-zA-Z0-9_]*$", new):
self.tblVariables.setItem(
row, col, QTableWidgetItem(self.current_value))
QMessageBox.information(
self,
"Disallowed format",
"Variable names should be alphanumeric names with underscore separated words. Also cannot start with numbers."
)
return
# Get rows as lists
keys = []
values = []
for row in range(self.tblVariables.rowCount()):
keys.append(
self.tblVariables.item(row, 0).text()
if self.tblVariables.item(row, 0) is not None
else ""
)
for row in range(self.tblVariables.rowCount()):
values.append(
self.tblVariables.item(row, 1).text()
if self.tblVariables.item(row, 1) is not None
else ""
)
# Assign new values
self.keys = keys
self.values = values

# Make changes
if col:
if row < len(values):
values[row] = new
else:
values.append(new)
else:
if row < len(keys):
keys[row] = new
else:
keys.append(new)

if len(values) < len(keys):
# Placeholder value
values.append("")
if len(values) > len(keys):
# Placeholder value
keys.append("")

# Re-assign to variable
self.variables = dict((k, v) for k, v in zip(keys, values))
# Check for errors
self.check_for_unique_keys()
# Update current value
self.get_current_value(row, col)

def check_for_unique_keys(self):
# Get variable list
row_items = []
for row in range(self.tblVariables.rowCount()):
row_items.append("" if self.tblVariables.item(row, 0) is None
else self.tblVariables.item(row, 0).text())
# Get non-unique items
non_unique = [item for item in row_items if row_items.count(item) > 1]

# if non-unique values are exist, warn the user
if non_unique:
QMessageBox.warning(
self,
"Non-Unique Variable",
"Do not use same variable names. In terms of using same variables, only the last one will be used."
)

# Delete a variable
def delete_variable(self):
current = self.tblVariables.currentRow()
# Check if there is a selection
if current:
del self.variables[list(self.variables.keys())[current]]
self.reload_table()
# Prompt for deletion
if QMessageBox.warning(
self, "Confirm Deletion",
"Do you really want to delete this variable?",
QMessageBox.Yes | QMessageBox.No
) == QMessageBox.Yes:
current = self.tblVariables.currentRow()
# Check if there is a selection
if current > -1:
self.keys.pop(current)
self.values.pop(current)
self.reload_table()

def save_variables(self) -> None:
# Remove duplicates
unique = dict(
(key, value)
for key, value in zip(self.keys, self.values)
# Check if variable is valid
if search("^[a-zA-Z_][a-zA-Z0-9_]*$", key)
)
keys = unique.keys()
values = unique.values()
# Get valid variables
result = ""
warn_for_empty = ""
for key, value in self.variables.items():
for key, value in zip(keys, values):
if all([len(key), len(value)]):
result += f"{key}={value}\n"
else:
Expand All @@ -171,22 +157,24 @@ def save_variables(self) -> None:
</p>"""
result = result[:-1]
html_result = result.replace("\n", "<br />\n")

# Ask for changes
msgAgreement = QMessageBox(self)
msgAgreement.setTextFormat(Qt.RichText)
agree: bool = msgAgreement.information(
self, "Check Variables",
f"""<html>
# Set Message
message = """<html>There is no valid variable. Do you want to write an empty /etc/environment file?</html>""" \
if len(self.keys) < 1 \
else f"""<html>
<p>The following variables will be written to /etc/environments file:</p>
<p>
<font color="orange" face="monospace">
{html_result}
</font>
</p>{warn_for_empty}
<p>Do you agree this changes?</p>
</html>""", QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Ok

</html>"""
# Ask for changes
msgAgreement = QMessageBox(self)
msgAgreement.setTextFormat(Qt.RichText)
agree: bool = msgAgreement.information(
self, "Check Variables",
message, QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Ok
# Do changes if user agrees
if agree:
system(f"""echo "# This config file is edited with make-endeavouros-great application.
Expand Down

0 comments on commit 1840c50

Please sign in to comment.