Skip to content

Commit

Permalink
updated tests and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
KylieGong committed Aug 17, 2023
1 parent b5dd30c commit 4ba295b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,22 @@ test3:
test4:
python pysimplegui.py

#bash format
test5:
python pyqt.py testfile.sh

#bash format with more variables
test6:
python pyqt.py testfile2.sh

#python format
test7:
python pyqt.py testfile3.py

#csh format
test8:
python pyqt.py testfile4.csh

# collaborations
agui_t:
git clone $(URL5a) agui_t
Expand Down
64 changes: 45 additions & 19 deletions pyqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

class MainWindow(QtWidgets.QMainWindow):

def __init__(self, parameters, param_file):
def __init__(self, parameters, param_file, filetype):
super(MainWindow, self).__init__()

self.groups = parameters
self.radio_groups = []
self.loadFile = None
self.ofile = None
self.saveFile = None
self.param_file = param_file
self.param_file_type = filetype
self.sliderMultiplier = []
self.sliders = []

Expand Down Expand Up @@ -72,25 +70,42 @@ def save(self):
with open(file_path, "w") as file:
for line in contents:
for key, value in line.items():
file.write(f"{key}={','.join(value)}")
if self.param_file_type == 'csh':
file.write("set ")

file.write(f"{key}=")

if self.param_file_type == 'py':
file.write('"')

file.write(f"{','.join(value)}")

if self.param_file_type == 'py':
file.write('"')

file.write("\n")
print("saved to " + file_path)

def load(self):
options = QtWidgets.QFileDialog.Options()
load_file = self.param_file +".key" if os.path.exists(self.param_file +".key") else ""
file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a File", load_file, "All Files (*)", options=options)
#alter the values of each option

#parse the loaded file
if file:
#turn the parameters in the file into a dictionary
default_values = {}
with open(file, "r") as f:
for line in f:
if self.param_file_type == 'csh':
line = re.sub("set","",line,count=1)
label, value = line.strip().split("=")
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
value = value.split(",") if "," in value else [value]
default_values[label] = value

#go through the elements in the widget
#go through the elements in the widget and alter it to the values specified
for elements in range(self.pagelayout.count()):
element = self.pagelayout.itemAt(elements).layout()
if element is not None:
Expand Down Expand Up @@ -151,7 +166,6 @@ def createWidgetsFromGroups(self):
txt.setText(default_option)
txt.setObjectName(group_name)
if group_type == "OFILE" or group_type == "IFILE":
self.ofile = group_name
btn.clicked.connect(lambda edit=txt: self.browse("FILE", edit))
else:
btn.clicked.connect(lambda edit=txt: self.browse("DIR", edit))
Expand Down Expand Up @@ -232,7 +246,6 @@ def browse(self, gtype, txt):

if gtype == 'FILE':
file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a File", "", "All Files (*)", options=options)
self.saveFile = file
if gtype == 'DIR':
dir = QtWidgets.QFileDialog.getExistingDirectory(self, "Select Directory")
if file:
Expand Down Expand Up @@ -274,34 +287,47 @@ def gatherData(self):
return layout_data

def parsefile(file):
filetype = "sh"
with open(file, "r") as f:
# content = file.read()
lines = f.readlines()

groups = []
pattern = r"\s*(\w+)\s*=\s*([^\s#]+)\s*#\s*([^\#]+)\s*#\s*>\s*(\w+)(?:\s+(\S+))?"
# group 1 = set or None
# group 2 = name of widget
# group 3 = default values, may have "" around
# group 4 = # help or None
# group 5 = widget type
# group 6 (unused) = name=value if old format, otherwise None
# group 7 = widget parameters or None
pattern = '^\s*(set\s+)?([^#]+)\s*=([^#]+)(#.*)?#>\s+([^\s]+)(.*=[^\s]*)?(.+)?$'
for line in lines:
match = re.match(pattern, line)
if match:
group_type = match.group(4)
group_name = match.group(1)
default_option = match.group(2)
options = match.group(5).split(',') if match.group(5) else ""
help = match.group(3)

if match.group(1):
filetype = "csh"
group_type = match.group(5)
group_name = match.group(2)
default_option = match.group(3).strip()
#check for quotations
if default_option[0] == '"' and default_option[-1] == '"':
default_option = default_option[1:-1]
filetype = "py"
options = match.group(7).split(',') if match.group(7) else ""
help = match.group(4).split('#')[1].strip() if match.group(4) else ""
groups.append((group_type, group_name, options, default_option, help))

return groups
return groups, filetype

if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Dynamic GUI Builder")
parser.add_argument("param_file", help="Path to the text file containing parameters")
args = parser.parse_args()

groups = parsefile(args.param_file)
groups, filetype = parsefile(args.param_file)

app = QtWidgets.QApplication(sys.argv)
w = MainWindow(groups, args.param_file)
w = MainWindow(groups, args.param_file, filetype)
w.inputFile = args.param_file
w.adjustSize() #adjust to fit elements accordingly

Expand Down
8 changes: 8 additions & 0 deletions testfile3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
file1="foo" #> IFILE
file2="bar" # help for output file2 #> OFILE
dir3="fum" # help for input dir3 #> IDIR
dir4="baz" # help for output dir4 #> ODIR
hello="world" # help for text entry hello #> ENTRY
a=1 # help for a, between 0 and 2 #> SCALE 0:100:50
b=2 # help for b, pick 1, 2 or 3 #> RADIO 0,1,2
c="3,c" # help for c, check any of 6 #> CHECK 0,1,2,a,b,c
8 changes: 8 additions & 0 deletions testfile4.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set file1=foo # help for input file1 #> IFILE
set file2=bar # help for output file2 #> OFILE
set dir3=fum # help for input dir3 #> IDIR
set dir4=baz # help for output dir4 #> ODIR
set hello=world # help for text entry hello #> ENTRY
set a=1 # help for a, between 0 and 2 #> SCALE 0:100:50
set b=2 # help for b, pick 1, 2 or 3 #> RADIO 0,1,2
set c=3,c # help for c, check any of 6 #> CHECK 0,1,2,a,b,c

0 comments on commit 4ba295b

Please sign in to comment.