def send_message(self, event_id, message, with_snapshot=False): # exec "before" script if any self.exec_script(event_id, "before") # Get snapshot if asked for snapshot = None if with_snapshot: snapshots = self.get_snapshot() if snapshots and len(snapshots) == 1: snapshot = snapshots[0] # Send to Discord bot (Somehow events can happen before discord bot has been created and initialised) if self.discord is None: self.discord = Discord() out = self.discord.send(embeds=info_embed(author=self.get_printer_name(), title=message, snapshot=snapshot)) if not out: self._logger.error("Failed to send message") return out # exec "after" script if any self.exec_script(event_id, "after") return out
def powerstatus(self): result = self.api_command("getPSUState") if result: message = "PSU is OFF" if json.loads(result.content)['isPSUOn']: message = "PSU is ON" return None, info_embed(author=self.plugin.get_printer_name(), title=message) return None, error_embed(author=self.plugin.get_printer_name(), title="Failed to get PSU status", description=unicode(result.content))
def test_info_embed(self): messages = info_embed(author="OctoPrint", title="title", description="description") self.assertEqual(1, len(messages)) embed, snapshot = messages[0] self.assertBasicEmbed(embed, author="OctoPrint", title="title", description="description", color=COLOR_INFO) if "NET_TEST" in os.environ: self.discord.send(messages=(embed, snapshot))
def test_info_embed(self): embeds = info_embed(author="OctoPrint", title="title", description="description") self.assertBasicEmbed(embeds, author="OctoPrint", title="title", description="description", color=COLOR_INFO) if "NET_TEST" in os.environ: self.assertTrue(self.discord.send(embeds=embeds))
def send_message(self, event_id, message, with_snapshot=False): # exec "before" script if any self.exec_script(event_id, "before") if self.discord is None: return # Get snapshot if asked for snapshot = None if with_snapshot: snapshot = self.get_snapshot() messages = info_embed(author=self.get_printer_name(), title=message, snapshot=snapshot) self.discord.send(messages) # exec "after" script if any self.exec_script(event_id, "after")
def snapshot(self): snapshots = self.plugin.get_snapshot() if snapshots and len(snapshots) == 1: return None, info_embed(author=self.plugin.get_printer_name(), snapshot=snapshots[0]) return None, None
def judge_is_unzippable(self, filename): autounzip = self.plugin.get_settings().get(['auto_unzip']) truncated = filename[:-4] # file is a single zip if filename.endswith('.zip'): #unzip if autounzip: return True, success_embed( author=self.plugin.get_printer_name(), title='File Received, starting to unzip....', description=filename) # inform the user that they can unzip the file themselves else: descr = filename + '\n' descr += 'Use %sunzip %s to extract all gcode files.' % ( self.plugin.get_settings().get(["prefix"]), filename) return False, success_embed( author=self.plugin.get_printer_name(), title='File Received', description=descr) # file is part of a multi-volume zip elif truncated.endswith('.zip'): # find all available multi-volumes belonging to the file in question filelist = self.get_flat_file_list() available_files = [] # get all available parts of the zip for f in filelist: temp_path = f.get('path') # check for file ending in .zip.XXX if truncated.upper() in temp_path.upper(): trunc_filename = filename[:-3] + temp_path[-3:] real_path = self.plugin.get_file_manager().path_on_disk( f.get('location'), trunc_filename) available_files.append(real_path) # sort the filenames nicely so missing files can be easily identified # for multiple files in a row, don't display all of them available_files.sort(key=lambda x: int(x[-3:])) blocks_availablefiles = [] current_block = [] prev_index = int(available_files[0][-3:]) - 1 for i in range(0, len(available_files)): curr_index = int(available_files[i][-3:]) if curr_index is prev_index + 1: current_block.append(available_files[i].rpartition('/')[2]) else: blocks_availablefiles.append(current_block) current_block = [available_files[i].rpartition('/')[2]] prev_index = curr_index blocks_availablefiles.append(current_block) differing_found = False last_index = -1 # search for signature in last block # it could be that the EOCD signature is split between two or more volumes if volumes are smaller than 65kb # search for volumes in reverse order until combined size exceeds MAX_ZIP_SIGNATURE_BLOCK signatureblock = blocks_availablefiles[-1] temp_filename = 'TEMP_' + signatureblock[-1] temp_filepath = self.plugin.get_file_manager().path_on_disk( 'local', temp_filename) temp_combinedsize = 0 minimized_block_paths = [] # minimize block size, take only as many volumes as needed to grow beyond MAX_ZIP_SIGNATURE_BLOCK # the rest of the block's volumes can't have the signature inside them if it exists for i in range(len(signatureblock) - 1, 0, -1): path = self.plugin.get_file_manager().path_on_disk( 'local', signatureblock[i]) temp_combinedsize += os.path.getsize(path) minimized_block_paths.append(path) if temp_combinedsize >= MAX_ZIP_SIGNATURE_BLOCK: # if the signature exists it's guaranteed to be found inside combined now break # concatenate the shortened block with open(temp_filepath, 'ab') as combined: for path in reversed(minimized_block_paths): with open(path, 'rb') as temp: combined.write(temp.read()) combined.close() # search for signature with open(temp_filepath, 'rb') as combined: if os.path.getsize(temp_filepath) > MAX_ZIP_SIGNATURE_BLOCK: combined.seek(-MAX_ZIP_SIGNATURE_BLOCK, os.SEEK_END) block = bytearray(combined.read()) if ZIP_EOCD_SIGNATURE in block: differing_found = True last_index = int(temp_filepath[-3:]) #for some reason, the file manager doesn't find these files combined.close() os.remove(temp_filepath) string_availablefiles = '' for b in blocks_availablefiles: line = b[0] if len(b) >= 2: line += ' - ' + b[-1] string_availablefiles += line + '\n' #we don't have the last file yet, can't find out how many volumes if differing_found is not True: return False, info_embed(author=self.plugin.get_printer_name(), title='%s of ??? Files Received' % (str(len(available_files))), description=string_availablefiles) #we have the last file, volumes are missing but we know how many volumes there are now elif len(available_files) is not last_index: return False, info_embed( author=self.plugin.get_printer_name(), title='%s of %s Files Received' % (str(len(available_files)), str(last_index)), description=string_availablefiles) #still inform the user how many files there are and how to unzip them elif autounzip is not True: descr = string_availablefiles descr += 'Use %sunzip %s to extract all gcode files.' % ( self.plugin.get_settings().get( ["prefix"]), available_files[0].rpartition('/')[2]) return False, success_embed( author=self.plugin.get_printer_name(), title='%s of %s Files Received' % (str(len(available_files)), str(last_index)), description=descr) else: return True, success_embed( author=self.plugin.get_printer_name(), title='All Files Received, starting to unzip....', description=string_availablefiles) return False, success_embed(author=self.plugin.get_printer_name(), title='File Received', description=filename)
def snapshot(self): snapshot = self.plugin.get_snapshot() if snapshot: return info_embed(author=self.plugin.get_printer_name(), snapshot=snapshot) return None