src/eric7/Tasks/TasksFile.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
23 23
24 class TasksFile(QObject): 24 class TasksFile(QObject):
25 """ 25 """
26 Class representing the tasks JSON file. 26 Class representing the tasks JSON file.
27 """ 27 """
28
28 def __init__(self, isGlobal: bool, parent: QObject = None): 29 def __init__(self, isGlobal: bool, parent: QObject = None):
29 """ 30 """
30 Constructor 31 Constructor
31 32
32 @param isGlobal flag indicating a file for global tasks 33 @param isGlobal flag indicating a file for global tasks
33 @type bool 34 @type bool
34 @param parent reference to the parent object (defaults to None) 35 @param parent reference to the parent object (defaults to None)
35 @type QObject (optional) 36 @type QObject (optional)
36 """ 37 """
37 super().__init__(parent) 38 super().__init__(parent)
38 self.__isGlobal = isGlobal 39 self.__isGlobal = isGlobal
39 40
40 def writeFile(self, filename: str) -> bool: 41 def writeFile(self, filename: str) -> bool:
41 """ 42 """
42 Public method to write the tasks data to a tasks JSON file. 43 Public method to write the tasks data to a tasks JSON file.
43 44
44 @param filename name of the tasks file 45 @param filename name of the tasks file
45 @type str 46 @type str
46 @return flag indicating a successful write 47 @return flag indicating a successful write
47 @rtype bool 48 @rtype bool
48 """ 49 """
50 # step 0: header 51 # step 0: header
51 tasksDict = {} 52 tasksDict = {}
52 if self.__isGlobal: 53 if self.__isGlobal:
53 tasksDict["header"] = { 54 tasksDict["header"] = {
54 "comment": "eric tasks file", 55 "comment": "eric tasks file",
55 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), 56 "saved": time.strftime("%Y-%m-%d, %H:%M:%S"),
56 "warning": ( 57 "warning": ("This file was generated automatically, do not edit."),
57 "This file was generated automatically, do not edit."
58 ),
59 } 58 }
60 # step 1: project scan filter 59 # step 1: project scan filter
61 tasksDict["ProjectScanFilter"] = "" 60 tasksDict["ProjectScanFilter"] = ""
62 61
63 # step 2: tasks 62 # step 2: tasks
64 tasksDict["Tasks"] = [ 63 tasksDict["Tasks"] = [
65 task.toDict() 64 task.toDict()
66 for task in ericApp().getObject("TaskViewer").getGlobalTasks() 65 for task in ericApp().getObject("TaskViewer").getGlobalTasks()
67 ] 66 ]
68 else: 67 else:
69 tasksDict["header"] = { 68 tasksDict["header"] = {
70 "comment": "eric tasks file for project {0}".format( 69 "comment": "eric tasks file for project {0}".format(
71 ericApp().getObject("Project").getProjectName()), 70 ericApp().getObject("Project").getProjectName()
72 "warning": (
73 "This file was generated automatically, do not edit."
74 ), 71 ),
72 "warning": ("This file was generated automatically, do not edit."),
75 } 73 }
76 if Preferences.getProject("TimestampFile"): 74 if Preferences.getProject("TimestampFile"):
77 tasksDict["header"]["saved"] = ( 75 tasksDict["header"]["saved"] = time.strftime("%Y-%m-%d, %H:%M:%S")
78 time.strftime('%Y-%m-%d, %H:%M:%S')
79 )
80 # step 1: project scan filter 76 # step 1: project scan filter
81 tasksDict["ProjectScanFilter"] = ( 77 tasksDict["ProjectScanFilter"] = (
82 ericApp().getObject("TaskViewer").getTasksScanFilter() 78 ericApp().getObject("TaskViewer").getTasksScanFilter()
83 ) 79 )
84 80
85 # step 2: tasks 81 # step 2: tasks
86 tasksDict["Tasks"] = [ 82 tasksDict["Tasks"] = [
87 task.toDict() 83 task.toDict()
88 for task in ericApp().getObject("TaskViewer").getProjectTasks() 84 for task in ericApp().getObject("TaskViewer").getProjectTasks()
89 ] 85 ]
90 86
91 try: 87 try:
92 jsonString = json.dumps(tasksDict, indent=2) 88 jsonString = json.dumps(tasksDict, indent=2)
93 with open(filename, "w") as f: 89 with open(filename, "w") as f:
94 f.write(jsonString) 90 f.write(jsonString)
95 except (TypeError, OSError) as err: 91 except (TypeError, OSError) as err:
98 None, 94 None,
99 self.tr("Save Tasks"), 95 self.tr("Save Tasks"),
100 self.tr( 96 self.tr(
101 "<p>The tasks file <b>{0}</b> could not be" 97 "<p>The tasks file <b>{0}</b> could not be"
102 " written.</p><p>Reason: {1}</p>" 98 " written.</p><p>Reason: {1}</p>"
103 ).format(filename, str(err)) 99 ).format(filename, str(err)),
104 ) 100 )
105 return False 101 return False
106 102
107 return True 103 return True
108 104
109 def readFile(self, filename: str) -> bool: 105 def readFile(self, filename: str) -> bool:
110 """ 106 """
111 Public method to read the tasks data from a task JSON file. 107 Public method to read the tasks data from a task JSON file.
112 108
113 @param filename name of the project file 109 @param filename name of the project file
114 @type str 110 @type str
115 @return flag indicating a successful read 111 @return flag indicating a successful read
116 @rtype bool 112 @rtype bool
117 """ 113 """
124 None, 120 None,
125 self.tr("Read Tasks"), 121 self.tr("Read Tasks"),
126 self.tr( 122 self.tr(
127 "<p>The tasks file <b>{0}</b> could not be read.</p>" 123 "<p>The tasks file <b>{0}</b> could not be read.</p>"
128 "<p>Reason: {1}</p>" 124 "<p>Reason: {1}</p>"
129 ).format(filename, str(err)) 125 ).format(filename, str(err)),
130 ) 126 )
131 return False 127 return False
132 128
133 viewer = ericApp().getObject("TaskViewer") 129 viewer = ericApp().getObject("TaskViewer")
134 if tasksDict["ProjectScanFilter"]: 130 if tasksDict["ProjectScanFilter"]:
135 viewer.setTasksScanFilter(tasksDict["ProjectScanFilter"]) 131 viewer.setTasksScanFilter(tasksDict["ProjectScanFilter"])
136 132
137 addedTasks = [] 133 addedTasks = []
138 for task in tasksDict["Tasks"]: 134 for task in tasksDict["Tasks"]:
139 addedTask = viewer.addTask( 135 addedTask = viewer.addTask(
140 task["summary"], priority=TaskPriority(task["priority"]), 136 task["summary"],
141 filename=task["filename"], lineno=task["lineno"], 137 priority=TaskPriority(task["priority"]),
142 completed=task["completed"], _time=task["created"], 138 filename=task["filename"],
139 lineno=task["lineno"],
140 completed=task["completed"],
141 _time=task["created"],
143 isProjectTask=not self.__isGlobal, 142 isProjectTask=not self.__isGlobal,
144 taskType=TaskType(task["type"]), 143 taskType=TaskType(task["type"]),
145 description=task["description"], uid=task["uid"], 144 description=task["description"],
146 parentTask=task["parent_uid"]) 145 uid=task["uid"],
146 parentTask=task["parent_uid"],
147 )
147 if addedTask: 148 if addedTask:
148 addedTasks.append((addedTask, task["expanded"])) 149 addedTasks.append((addedTask, task["expanded"]))
149 150
150 for task, expanded in addedTasks: 151 for task, expanded in addedTasks:
151 task.setExpanded(expanded) 152 task.setExpanded(expanded)
152 153
153 return True 154 return True

eric ide

mercurial