MCPcopy Create free account
hub / github.com/AstroPrint/AstroBox / removeTask

Method removeTask

src/astroprint/additionaltasks.py:148–193  ·  view source on GitHub ↗
(self, tId)

Source from the content-addressed store, hash-verified

146 return False
147
148 def removeTask(self, tId):
149 task = self.getTask(tId)
150
151 if task:
152 for t in self.data:
153 if t['id'] == tId:
154 self.data.remove(t)
155 break
156
157 tasksDir = settings().getBaseFolder('tasks')
158 taskPath = os.path.join(tasksDir, "%s.yaml" % tId)
159 self.errorOnRemove = None
160
161 def handlerError(func, path, exc_info):
162 self._logger.error("Unable to remove assets from [%s], path '%s': %s" % (tId, path, exc_info[1][1]))
163 self.errorOnRemove = "Error removing assets from '%s': [%s]" % (path, exc_info[1][1])
164
165 # Check if task file exists => remove it
166 if os.path.exists(taskPath) and os.path.isfile(taskPath):
167 os.remove(taskPath)
168 self._logger.info("Task [%s] Removed" % tId)
169
170 #remove asset dir
171 taskAssetsDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static', 'img', 'variant', tId)
172 if os.path.exists(taskAssetsDir) and os.path.isdir(taskAssetsDir):
173 shutil.rmtree((taskAssetsDir), False, handlerError)
174
175 if self.errorOnRemove:
176 return {'error': self.errorOnRemove}
177 else:
178 return {'removed': tId}
179
180 # No task file found, check for assets dir anyway to clean
181 self._logger.info("File [%s.yaml] not found, remove from view." % tId)
182 #remove asset dir
183 taskAssetsDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static', 'img', 'variant', tId)
184 if os.path.exists(taskAssetsDir) and os.path.isdir(taskAssetsDir):
185 self._logger.info("Found old assets dir from [%s.yaml], removing..." % tId)
186 shutil.rmtree((taskAssetsDir), False, handlerError)
187
188 if self.errorOnRemove:
189 return {'error': self.errorOnRemove}
190 else:
191 return {'removed': tId}
192 else:
193 return {'error': 'not_found'}

Callers 1

deleteTaskFunction · 0.80

Calls 5

getTaskMethod · 0.95
settingsFunction · 0.90
removeMethod · 0.80
joinMethod · 0.80
getBaseFolderMethod · 0.45

Tested by

no test coverage detected