@@ -96,79 +96,21 @@ def write_to_todolist_file(self, **kwargs):
96
96
- create_task
97
97
- create_section
98
98
- rename_task
99
- - delete_element
100
99
- clear_checked
101
100
- clear_ all_checked
102
101
- clear_all
103
102
'''
104
- if 'state' not in kwargs :
105
- kwargs ['state' ] = False
106
103
107
- with open (join (self .script_directory , 'data' , f"{ kwargs ['list_name' ]} .json" ), 'r+' ) as json_file :
108
- json_data = json .load (json_file )
109
- json_file .seek (0 )
110
-
111
- # print(kwargs['action'])
112
-
113
- if 'create' in kwargs ['action' ]: # value: the name of the element
114
- element = [kwargs ['value' ], kwargs ['state' ]] if 'task' in kwargs ['action' ] else [[kwargs ['value' ], kwargs ['state' ]], []]
115
- current_element = json_data ['data' ] # the root list
116
-
117
- for i in range (len (kwargs ['indices' ]) - 1 , - 1 , - 1 ):
118
- if i == 0 :
119
- break
120
- else :
121
- if isinstance (current_element [kwargs ['indices' ][i ]][0 ], str ):
122
- continue
123
- else :
124
- current_element = current_element [kwargs ['indices' ][i ]][1 ]
125
-
126
- current_element .insert (kwargs ['indices' ][i ], element )
127
- else :
128
- # print(kwargs['action'], kwargs['indices'])
129
- if kwargs ['action' ] == 'delete_element' and len (kwargs ['indices' ]) == 1 :
130
- json_data ['data' ].pop (kwargs ['indices' ][0 ])
131
- else :
132
- # current_element = json_data['data'][kwargs['indices'][-1]]
133
- current_element = json_data ['data' ] if len (kwargs ['indices' ]) == 0 else json_data ['data' ][kwargs ['indices' ][- 1 ]]
134
-
135
- for i in range (len (kwargs ['indices' ])- 1 ):
136
- if isinstance (current_element [0 ], list ):
137
- current_element = current_element [1 ]
138
-
139
- # print(kwargs['action'], current_element, kwargs['indices'], list(reversed(kwargs['indices'][:-1])), i)
140
- if kwargs ['action' ] == 'delete_element' and i == len (kwargs ['indices' ]) - 2 :
141
- current_element .pop (list (reversed (kwargs ['indices' ][:- 1 ]))[i ])
142
- break
143
-
144
- current_element = current_element [list (reversed (kwargs ['indices' ][:- 1 ]))[i ]]
145
-
146
- if kwargs ['action' ] == 'toggle_task' :
147
- current_element [1 ] = kwargs ['value' ]
148
- elif kwargs ['action' ] == 'toggle_section' :
149
- current_element [0 ][1 ] = kwargs ['value' ]
150
- elif kwargs ['action' ] == 'rename_task' :
151
- current_element [0 ] = kwargs ['value' ]
152
- elif kwargs ['action' ] == 'rename_section' :
153
- current_element [0 ][0 ] = kwargs ['value' ]
154
- elif kwargs ['action' ] == 'clear_all' :
155
- print ('CLEARRRR' )
156
- current_element [1 ] = []
157
- elif kwargs ['action' ] in ('clear_checked' , 'clear_all_checked' ):
158
- print ('clear brotha' , current_element , kwargs ['indices' ])
159
- new_element = self .clear_checked (current_element , True if kwargs ['action' ] == 'clear_all_checked' else False )
160
- if len (kwargs ['indices' ]) == 0 :
161
- print ('clear the entire list' )
162
- current_element .clear ()
163
- current_element .extend (new_element )
164
- print (current_element )
165
- else :
166
- current_element [1 ].clear ()
167
- current_element [1 ].extend (new_element )
168
-
169
-
170
- json .dump (json_data , json_file , indent = 4 )
171
- json_file .truncate ()
104
+ if 'create' in kwargs ['action' ]:
105
+ self .create_element (** kwargs )
106
+ elif 'toggle' in kwargs ['action' ]:
107
+ self .toggle_element (** kwargs )
108
+ elif 'rename' in kwargs ['action' ]:
109
+ self .rename_element (** kwargs )
110
+ elif 'clear' in kwargs ['action' ]:
111
+ self .clear (** kwargs )
112
+ elif kwargs ['action' ] == 'delete_element' :
113
+ self .delete_element (** kwargs )
172
114
173
115
def change_focus (self , list_name ):
174
116
self .app_data ['focused' ] = list_name
@@ -195,31 +137,45 @@ def move_list(self, the_list, direction):
195
137
data .insert (index_of_list + direction , data .pop (index_of_list ))
196
138
break
197
139
198
- def clear (self , focused_list , indices = [], clear_type = 'All ' ):
140
+ def clear (self , list_name , indices = [], action = 'clear_all ' ):
199
141
'''
200
- Deletes tasks based on the clear_type :
201
- - Checked : delete tasks in the same area
202
- - All Checked : delete all checked tasks in same section and sub sections
203
- - All : delete every element within it
142
+ Deletes tasks in the list or a section based on the action :
143
+ - clear_checked : delete tasks in the same area
144
+ - clear_all_checked : delete all checked tasks in same section and sub sections
145
+ - clear_all : delete every element within it
204
146
'''
205
147
206
- if clear_type == 'Checked' :
207
- self .write_to_todolist_file (list_name = focused_list , indices = indices , action = 'clear_checked' )
208
- elif clear_type == 'All Checked' :
209
- self .write_to_todolist_file (list_name = focused_list , indices = indices , action = 'clear_all_checked' )
210
- elif clear_type == 'All' :
211
- if not indices :
212
- with open (join (self .script_directory , 'data' , f'{ focused_list } .json' ), 'w' ) as json_file :
213
- json .dump ({"data" : []}, json_file , indent = 4 )
214
- else :
215
- self .write_to_todolist_file (list_name = focused_list , indices = indices , action = 'clear_all' )
148
+ with open (join (self .script_directory , 'data' , f"{ list_name } .json" ), 'r+' ) as json_file :
149
+ json_data = json .load (json_file )
150
+ json_file .seek (0 )
151
+
152
+ current_element = json_data ['data' ] if len (indices ) == 0 else json_data ['data' ][indices [0 ]]
153
+
154
+
155
+ for i in indices [1 :]:
156
+ if isinstance (current_element [0 ], list ):
157
+ current_element = current_element [1 ]
158
+ current_element = current_element [i ]
159
+
160
+ if action == 'clear_all' :
161
+ current_element [1 ] = []
162
+ elif action in ('clear_checked' , 'clear_all_checked' ):
163
+ new_element = self .clear_checked (current_element [1 ], True if action == 'clear_all_checked' else False )
164
+ if len (indices ) == 0 :
165
+ current_element .clear ()
166
+ current_element .extend (new_element )
167
+ else :
168
+ current_element [1 ].clear ()
169
+ current_element [1 ].extend (new_element )
170
+
171
+ json .dump (json_data , json_file , indent = 4 )
172
+ json_file .truncate ()
216
173
217
- def clear_checked (self , the_parent , clear_all_checked ):
174
+ def clear_checked (self , the_parent : list , clear_all_checked : bool ):
218
175
'''
219
176
loops through parent and deletes 'tasks' that are checked
220
177
'''
221
178
new_parent = []
222
- print (the_parent , "WOWWWW" )
223
179
for element in the_parent :
224
180
print (element )
225
181
if isinstance (element [1 ], list ):
@@ -228,10 +184,8 @@ def clear_checked(self, the_parent, clear_all_checked):
228
184
new_parent .append (element )
229
185
elif element [1 ] == False :
230
186
new_parent .append (element )
231
- print (new_parent , "NEWWW" )
232
187
return new_parent
233
188
234
-
235
189
def rename_list (self , old_name , new_name ):
236
190
'''
237
191
Assumes that the new file does not exist, creates the new file and places the old data into it, deleting the old json file.
@@ -256,6 +210,95 @@ def delete_list(self, list_name):
256
210
self .data .remove (todolist )
257
211
remove (join (self .script_directory , 'data' , f'{ list_name } .json' ))
258
212
213
+ def create_element (self , action , list_name , indices , value , state = None ):
214
+ with open (join (self .script_directory , 'data' , f"{ list_name } .json" ), 'r+' ) as json_file :
215
+ json_data = json .load (json_file )
216
+ json_file .seek (0 )
217
+
218
+ if state is None :
219
+ state = False
220
+
221
+ element = [value , state ] if 'task' in action else [[value , state ], []]
222
+ current_element = json_data ['data' ] # the root list
223
+
224
+ for i in indices [:- 1 ]:
225
+ if i == 0 :
226
+ break
227
+ else :
228
+ if isinstance (current_element [i ][0 ], str ):
229
+ continue
230
+ else :
231
+ current_element = current_element [i ][1 ]
232
+
233
+ current_element .insert (indices [- 1 ], element )
234
+
235
+ json .dump (json_data , json_file , indent = 4 )
236
+ json_file .truncate ()
237
+
238
+ def toggle_element (self , action , list_name , indices , value ):
239
+ with open (join (self .script_directory , 'data' , f"{ list_name } .json" ), 'r+' ) as json_file :
240
+ json_data = json .load (json_file )
241
+ json_file .seek (0 )
242
+
243
+ current_element = json_data ['data' ] if len (indices ) == 0 else json_data ['data' ][indices [0 ]]
244
+
245
+ for i in indices [1 :]:
246
+ if isinstance (current_element [0 ], list ):
247
+ current_element = current_element [1 ]
248
+ current_element = current_element [i ]
249
+
250
+ if 'task' in action :
251
+ current_element [1 ] = value
252
+ elif 'section' in action :
253
+ current_element [0 ][1 ] = value
254
+
255
+ json .dump (json_data , json_file , indent = 4 )
256
+ json_file .truncate ()
257
+
258
+ def rename_element (self , action , list_name , indices , value ):
259
+ with open (join (self .script_directory , 'data' , f"{ list_name } .json" ), 'r+' ) as json_file :
260
+ json_data = json .load (json_file )
261
+ json_file .seek (0 )
262
+
263
+ current_element = json_data ['data' ] if len (indices ) == 0 else json_data ['data' ][indices [0 ]]
264
+
265
+ for i in indices [1 :]:
266
+ if isinstance (current_element [0 ], list ):
267
+ current_element = current_element [1 ]
268
+ current_element = current_element [i ]
269
+
270
+ if 'task' in action :
271
+ current_element [0 ] = value
272
+ elif 'section' in action :
273
+ current_element [0 ][0 ] = value
274
+
275
+ json .dump (json_data , json_file , indent = 4 )
276
+ json_file .truncate ()
277
+
278
+ def delete_element (self , action , list_name , indices ):
279
+ with open (join (self .script_directory , 'data' , f"{ list_name } .json" ), 'r+' ) as json_file :
280
+ json_data = json .load (json_file )
281
+ json_file .seek (0 )
282
+
283
+ if len (indices ) == 1 :
284
+ json_data ['data' ].pop (indices [0 ])
285
+ else :
286
+ current_element = json_data ['data' ] if len (indices ) == 0 else json_data ['data' ][indices [0 ]]
287
+ indices .pop (0 )
288
+
289
+ for i in range (len (indices )):
290
+ if isinstance (current_element [0 ], list ):
291
+ current_element = current_element [1 ]
292
+
293
+ if i == len (indices ) - 1 :
294
+ current_element .pop (indices [i ])
295
+ break
296
+
297
+ current_element = current_element [indices [i ]]
298
+
299
+ json .dump (json_data , json_file , indent = 4 )
300
+ json_file .truncate ()
301
+
259
302
def close_event (self , focused ):
260
303
self .app_data ['focused' ] = focused
261
304
self .app_data ['order' ] = [i ['name' ] for i in self .data ]
0 commit comments