Skip to content

Commit

Permalink
3.8 task system with reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
XDzzzzzZyq committed Mar 8, 2024
1 parent f54ca16 commit 83469a4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# excel temp
~$*
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from UI.UI import Panel


def main():
panel = Panel()
panel.run()


if __name__ == "__main__":
main()

7 changes: 7 additions & 0 deletions utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

import utils.fileIO as IO
import utils.process as PS
import utils.project as PJ


class JSONGenerator:
def __init__(self):
self.task = PJ.Task()

self.name = ""
self.template: dict = None
self.dataset: list[dict] = None
Expand All @@ -17,6 +21,7 @@ def __init__(self):

def import_template(self, template_dir: str):
self.template = IO.read_json(template_dir)
self.task.templ_path = template_dir

def import_dataset(self, excel_dir: str):
self.dataset, self.data_columns, self.data_size = IO.read_excel(excel_dir)
Expand All @@ -28,6 +33,7 @@ def import_dataset(self, excel_dir: str):
self.option_list[column] = PS.Options()

self.previews = [dict() for _ in range(self.data_size)]
self.task.excel_path = excel_dir

def generate_json(self, g_range: tuple[int, int] = None):
if g_range is None:
Expand Down Expand Up @@ -63,6 +69,7 @@ def export_json(self, target_dir: str, g_range: tuple[int, int] = None):

print(f"total count: {g_range[1] - g_range[0]}")
print(f"to folder: {target_dir}")
self.task.export_path = target_dir

def pick_preview(self, index: int, indent: int = 4) -> str:
return str(json.dumps(self.previews[index], indent=indent))
Expand Down
1 change: 1 addition & 0 deletions utils/fileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import json


def read_json(json_file_path: str):
# Reading JSON file content into a string
with open(json_file_path, 'r') as json_file:
Expand Down
70 changes: 70 additions & 0 deletions utils/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
from utils import fileIO as IO

_registered_properties = []


def register_property(prop_name):
_registered_properties.append(prop_name)

def getter(self):
return getattr(self, '_Task__' + prop_name)

def setter(self, value):
setattr(self, '_Task__is_saved', False)
setattr(self, '_Task__' + prop_name, value)

return property(getter, setter)


def export_properties(task):
result = dict()
for prop_name in _registered_properties:
result[prop_name] = getattr(task, prop_name)

return result


def import_properties(task, jsonobj):
for prop_name in _registered_properties:
setattr(task, prop_name, jsonobj[prop_name])


class Task():
def __init__(self, excel_path="", templ_path="", export_path=""):
# Configurations
self.__excel_path = excel_path
self.__templ_path = templ_path
self.__export_path = export_path
self.__export_with_folder = True

# "Private" Members
self.__config_path = os.getcwd() + "\\config.json"
self.__is_saved = False

# Define all serializable members
excel_path = register_property("excel_path")
templ_path = register_property("templ_path")
export_path = register_property("export_path")
export_with_folder = register_property("export_with_folder")

def read_config(self, path):
json_obj = IO.read_json(path)
import_properties(self, json_obj)

self.__config_path = path
self.__is_saved = False

def write_config(self):
json_obj = export_properties(self)

path = os.path.realpath(self.__config_path)
self.__is_saved = True
IO.write_json(json_obj, path)


if __name__ == "__main__":
task = Task()
task.read_config("../example/config.json")
task.excel_path = "123123"
task.write_config()

0 comments on commit 83469a4

Please sign in to comment.