def timelapse(self): path = os.path.join(os.getcwd(), self.plugin._data_folder, '..', '..', 'timelapse') path = os.path.abspath(path) builder = EmbedBuilder() builder.set_title('Files and Details') builder.set_description('Download with /gettimelapse {filename}') builder.set_author(name=self.plugin.get_printer_name()) baseurl = self.plugin.get_settings().get(["baseurl"]) port = self.plugin.get_port() if baseurl is None or baseurl == "": baseurl = "%s:%s" % (self.plugin.get_ip_address(), port) for root, dirs, files in os.walk(path): for name in files: try: file_path = os.path.join(root, name) title = os.path.basename(file_path) description = '' description += 'Size: %s\n' % os.path.getsize(file_path) description += 'Date of Creation: %s\n' % time.ctime( os.path.getctime(file_path)) description += 'Download Path: %s\n' %\ ("http://" + baseurl + "/downloads/timelapse/" + urllib.quote(title)) builder.add_field(title=title, text=description) except Exception as e: pass return None, builder.get_embeds()
def list_system_commands(self): api_key = self.plugin.get_settings().global_get(['api', 'key']) port = self.plugin.get_settings().global_get(['server', 'port']) header = {'X-Api-Key': api_key, 'Content-Type': 'application/json'} response = requests.get('http://127.0.0.1:%s/api/system/commands' % port, headers=header) if response.status_code != 200: return None, error_embed(author=self.plugin.get_printer_name(), title="Error code: %i" % response.status_code, description=response.content) builder = EmbedBuilder() builder.set_title('List of system commands') builder.set_author(name=self.plugin.get_printer_name()) builder.set_description( 'To execute a system command, use /systemcommand {command}. ' 'Where command is similar to "core/restart"') data = json.loads(response.content) for source in data: for comm in data[source]: if 'name' not in comm: continue comm_name = comm['name'] comm_description = "%s/%s" % (source, comm['action']) if 'command' in comm: comm_description = "%s - %s" % (comm_description, comm['command']) builder.add_field(title=comm_name, text=comm_description) return None, builder.get_embeds()
def test_dispatch(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(('10.255.255.255', 1)) except Exception as e: self.fail("Can't be tested without an internet connection") finally: s.close() # Should result in 3 messages, one embed, one embed with photo, and one photo. builder = EmbedBuilder() builder.set_title("Test title") builder.set_description("No snapshot") self.assertTrue( self.discord._dispatch_message(embed=builder.get_embeds()[0])) with open(self._get_path("test_pattern.png"), "rb") as f: builder.set_description("With snapshot") builder.set_image(("snapshot.png", f)) self.assertTrue( self.discord._dispatch_message(embed=builder.get_embeds()[0])) f.seek(0) self.assertTrue( self.discord._dispatch_message(snapshot=("snapshot.png", f)))
def test_embedbuilder(self): # Success builder = EmbedBuilder() builder.set_title("Test Title") builder.set_description("This is a description") builder.set_color(COLOR_INFO) for i in range(0, 30): builder.add_field("a" * MAX_TITLE, "b" * MAX_VALUE) messages = builder.get_embeds() self.assertEqual(8, len(messages)) embed, snapshot = messages[0] self.assertEqual("Test Title", embed.title) self.assertEqual("This is a description", embed.description) for embed, snapshot in messages: self.assertEqual(COLOR_INFO, embed.color.value) self.assertIsNotNone(embed.timestamp) self.assertLessEqual(len(embed.fields), MAX_NUM_FIELDS) for field in embed.fields: self.assertEqual("a" * MAX_TITLE, field.name) self.assertEqual("b" * MAX_VALUE, field.value) if "NET_TEST" in os.environ: self.discord.send(messages=messages)
def test_embedbuilder(self): # Success builder = EmbedBuilder() builder.set_title("Test Title") builder.set_description("This is a description") builder.set_color(COLOR_INFO) for i in range(0, 30): builder.add_field("a" * MAX_TITLE, "b" * MAX_VALUE) embeds = builder.get_embeds() self.assertEqual(8, len(embeds)) first_embed = embeds[0].get_embed() self.assertEqual("Test Title", first_embed['title']) self.assertEqual("This is a description", first_embed['description']) for embed in embeds: embed_obj = embed.get_embed() self.assertEqual(COLOR_INFO, embed_obj['color']) self.assertIsNotNone(embed_obj['timestamp']) self.assertLessEqual(len(embed_obj['fields']), MAX_NUM_FIELDS) for field in embed_obj['fields']: self.assertEqual("a" * MAX_TITLE, field['name']) self.assertEqual("b" * MAX_VALUE, field['value']) if "NET_TEST" in os.environ: self.assertTrue(self.discord.send(embeds=embeds))
def test_unicode_embed(self): teststr = "٩(-̮̮̃-̃)۶ ٩(●̮̮̃•̃)۶ ٩(͡๏̯͡๏)۶ ٩(-̮̮̃•̃)." embed_builder = EmbedBuilder() embed_builder.set_title(teststr) embed_builder.set_description(teststr) embed_builder.set_author(teststr) embed_builder.add_field(teststr, teststr) embeds = embed_builder.get_embeds() self.assertIsNotNone(embeds)