From 4ba295bdf8c5e554f8b62e3f5099417dfbe9dc46 Mon Sep 17 00:00:00 2001 From: KylieGong Date: Thu, 17 Aug 2023 03:39:49 -0400 Subject: [PATCH] updated tests and parsing --- Makefile | 10 ++++++++ pyqt.py | 64 ++++++++++++++++++++++++++++++++++++--------------- testfile3.py | 8 +++++++ testfile4.csh | 8 +++++++ 4 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 testfile3.py create mode 100644 testfile4.csh diff --git a/Makefile b/Makefile index 0b92a21..77a938b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pyqt.py b/pyqt.py index 3dca29c..c9d94a2 100644 --- a/pyqt.py +++ b/pyqt.py @@ -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 = [] @@ -72,7 +70,19 @@ 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) @@ -80,17 +90,22 @@ 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: @@ -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)) @@ -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: @@ -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 diff --git a/testfile3.py b/testfile3.py new file mode 100644 index 0000000..6e4ca93 --- /dev/null +++ b/testfile3.py @@ -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 \ No newline at end of file diff --git a/testfile4.csh b/testfile4.csh new file mode 100644 index 0000000..9f91d67 --- /dev/null +++ b/testfile4.csh @@ -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 \ No newline at end of file