Merge pull request 'fixes on empty Listbox now is disable and now works autoconnect label with read and write, delete works now with read and write' (#12) from wire-py-reformat-14-08-2024 into main

Reviewed-on: https://git.ilunix.de/punix/Wire-Py/pulls/12
This commit is contained in:
Désiré Werner Menrath 2024-09-04 20:55:51 +02:00
commit e4774abf19
4 changed files with 54 additions and 51 deletions

View File

@ -4,7 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="add class FileHandle,&#10;add Label to show autoconnect Tunnel&#10;disable checkbox when Listbox is empty or no select Tunnel"> <list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="add delete row in wg_py.xml when Tunnel delete">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/wg_func.py" beforeDir="false" afterPath="$PROJECT_DIR$/wg_func.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/wg_func.py" beforeDir="false" afterPath="$PROJECT_DIR$/wg_func.py" afterDir="false" />
@ -268,7 +268,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1725391658456</updated> <updated>1725391658456</updated>
</task> </task>
<option name="localTasksCounter" value="23" /> <task id="LOCAL-00023" summary="add if question and add autoconnect, autoconnect_var to class Filehandle in box_set no finish!">
<option name="closed" value="true" />
<created>1725434328731</created>
<option name="number" value="00023" />
<option name="presentableId" value="LOCAL-00023" />
<option name="project" value="LOCAL" />
<updated>1725434328731</updated>
</task>
<option name="localTasksCounter" value="24" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -317,6 +325,7 @@
<MESSAGE value="columnconfigure on all widgets set" /> <MESSAGE value="columnconfigure on all widgets set" />
<MESSAGE value="little fixes a &quot; &quot; to ' '" /> <MESSAGE value="little fixes a &quot; &quot; to ' '" />
<MESSAGE value="add class FileHandle,&#10;add Label to show autoconnect Tunnel&#10;disable checkbox when Listbox is empty or no select Tunnel" /> <MESSAGE value="add class FileHandle,&#10;add Label to show autoconnect Tunnel&#10;disable checkbox when Listbox is empty or no select Tunnel" />
<option name="LAST_COMMIT_MESSAGE" value="add class FileHandle,&#10;add Label to show autoconnect Tunnel&#10;disable checkbox when Listbox is empty or no select Tunnel" /> <MESSAGE value="add if question and add autoconnect, autoconnect_var to class Filehandle in box_set no finish!" />
<option name="LAST_COMMIT_MESSAGE" value="add if question and add autoconnect, autoconnect_var to class Filehandle in box_set no finish!" />
</component> </component>
</project> </project>

51
main.py
View File

@ -4,7 +4,7 @@ import tkinter as tk
from tkinter import ttk from tkinter import ttk
from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, ConToDict, GreenLabel, StartStopBTN, ShowAddress, from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, ConToDict, GreenLabel, StartStopBTN, ShowAddress,
FileHandle, ExportTunnels) FileHandle, ExportTunnels, OnOff)
font_color = '#4011a7' font_color = '#4011a7'
replacement = '' replacement = ''
@ -20,7 +20,7 @@ class MainWindow(tk.Tk):
self.y_height = 360 self.y_height = 360
self.monitor_center_x = self.winfo_screenwidth() / 2 - (self.x_width / 2) self.monitor_center_x = self.winfo_screenwidth() / 2 - (self.x_width / 2)
self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 2) self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 2)
self.resizable(width=False, height=False) #self.resizable(width=False, height=False)
self.title('Wire-Py') self.title('Wire-Py')
self.configure() self.configure()
self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y)) self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y))
@ -136,21 +136,26 @@ class FrameWidgets(ttk.Frame):
os.system('nmcli connection delete ' + str(select_tl)) os.system('nmcli connection delete ' + str(select_tl))
self.l_box.delete(self.select_tunnel[0]) self.l_box.delete(self.select_tunnel[0])
os.remove(os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf') os.remove(os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf')
try:
with open('wg_py.xml', 'r') as fr:
lines = fr.readlines()
with open('wg_py.xml_2', 'w') as fw: del_tl_read = open('wg_py.xml', 'r')
for line in lines: if 'false ' in del_tl_read:
first_word = 'false '
else:
first_word = 'true '
# strip() is used to remove '\n' word_replace = ''
# present at the end of each line # using the for loop
if line.strip('\n') != select_tl: for line in del_tl_read:
fw.write(line) line = line.strip()
print("Deleted") changes = line.replace(first_word + select_tl, '')
except: word_replace = word_replace + changes + "\n"
print("Oops! someting error")
del_tl_read.close()
del_tl = open('wg_py.xml', 'w')
del_tl.write(word_replace)
del_tl.close()
if len(self.tl) == 0:
self.wg_autostart.configure(state='disabled')
if self.a != '' and self.a == select_tl: if self.a != '' and self.a == select_tl:
self.StrVar.set(value='') self.StrVar.set(value='')
StartStopBTN.button_start(self) StartStopBTN.button_start(self)
@ -182,23 +187,7 @@ class FrameWidgets(ttk.Frame):
command=lambda: FileHandle.box_set(self)) command=lambda: FileHandle.box_set(self))
self.wg_autostart.grid(column=1, row=4, sticky='nw') self.wg_autostart.grid(column=1, row=4, sticky='nw')
on_or_off = open('wg_py.xml', 'r') OnOff.on_off(self)
res = on_or_off.read()
if 'true' in res:
self.selected_option.set(1)
self.auto_con = res
self.auto_con = self.auto_con[5:]
print(self.auto_con)
else:
self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect'
on_or_off.close()
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
self.autoconnect = tk.Label(self, textvariable=self.autoconnect_var, bd=2, fg='blue', padx=5)
self.autoconnect.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=1, row=4, sticky='we')
#self.wg_update = tk.Checkbutton(self, tex='Search automatically for\nWire-Py updates') #self.wg_update = tk.Checkbutton(self, tex='Search automatically for\nWire-Py updates')
#self.wg_update.grid(column=1, rowspan=3, row=5) #self.wg_update.grid(column=1, rowspan=3, row=5)

View File

@ -217,7 +217,7 @@ class ImportTunnel:
file.close() file.close()
os.system('nmcli con mod ' + str(self.a) + ' connection.autoconnect no') os.system('nmcli con mod ' + str(self.a) + ' connection.autoconnect no')
new_tl = open('wg_py.xml', 'a') new_tl = open('wg_py.xml', 'a')
new_tl.write('false ' + str(self.a)) new_tl.write('false ' + str(self.a) + '\n')
new_tl.close() new_tl.close()
if 'PrivateKey = ' not in read: if 'PrivateKey = ' not in read:
Message() Message()
@ -243,45 +243,51 @@ class FileHandle:
self.select_tunnel = None self.select_tunnel = None
def box_set(self): def box_set(self):
word_replace = ''
global replacement
try: try:
self.select_tunnel = self.l_box.curselection() self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0]) select_tl = self.l_box.get(self.select_tunnel[0])
if self.selected_option.get() == 0: if self.selected_option.get() == 0:
set_on = open('wg_py.xml', 'r') set_on = open('wg_py.xml', 'r')
replacement = ""
# using the for loop # using the for loop
for line in set_on: for line in set_on:
line = line.strip() line = line.strip()
changes = line.replace('true ' + select_tl, 'false ' + select_tl) changes = line.replace('true ' + select_tl, 'false ' + select_tl)
for tl in self.tl: os.system('nmcli con mod ' + str(self.auto_con) + ' connection.autoconnect no')
os.system('nmcli con mod ' + str(tl) + ' connection.autoconnect no') word_replace = word_replace + changes + "\n"
replacement = replacement + changes + "\n"
set_on.close() set_on.close()
if self.selected_option.get() == 1: if self.selected_option.get() >= 1:
set_on = open('wg_py.xml', 'r') set_on = open('wg_py.xml', 'r')
replacement = ""
# using the for loop # using the for loop
for line in set_on: for line in set_on:
line = line.strip() line = line.strip()
changes = line.replace('false ' + select_tl, 'true ' + select_tl) changes = line.replace('false ' + select_tl, 'true ' + select_tl)
os.system('nmcli con mod ' + str(select_tl) + ' connection.autoconnect yes') os.system('nmcli con mod ' + str(select_tl) + ' connection.autoconnect yes')
replacement = replacement + changes + "\n" word_replace = word_replace + changes + "\n"
set_on.close() set_on.close()
set_off = open('wg_py.xml', 'w') set_off = open('wg_py.xml', 'w')
set_off.write(replacement) set_off.write(word_replace)
set_off.close() set_off.close()
except IndexError: except IndexError:
self.selected_option.set(1) self.selected_option.set(1)
OnOff.on_off(self)
class OnOff(tk.Tk, FileHandle):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
def on_off(self):
a_connect = {}
on_or_off = open('wg_py.xml', 'r') on_or_off = open('wg_py.xml', 'r')
res = on_or_off.read() for line in on_or_off.readlines():
if 'true' in res: line_splitted = line.split()
a_connect[line_splitted[0]] = line_splitted[1]
if 'true' in a_connect:
self.selected_option.set(1) self.selected_option.set(1)
self.auto_con = res self.auto_con = a_connect['true']
self.auto_con = self.auto_con[5:]
print(self.auto_con)
else: else:
self.wg_autostart.configure(state='disabled') self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect' self.auto_con = 'no Autoconnect'

View File

@ -1 +0,0 @@
false iphone-wg