def json_return(input_json, selector): if selector[0] == 'text': return selector[1] elif selector[0] == 'text.escape': return escape(selector[1]) elif selector[0] == 'json.lower': if len(selector) > 2: default = selector[2] else: default = "" return str(json_element(input_json, selector[1], default=default)).lower() elif selector[0] == 'json.quote_plus': if len(selector) > 2: default = selector[2] else: default = "" return urllib.parse.quote_plus( str(json_element(input_json, selector[1], default=default))) elif selector[0] == 'json.num.metric': if len(selector) > 2: default = selector[2] else: default = 0 return metric( int(json_element(input_json, selector[1], default=default))) elif selector[0] == 'json.datetime.fromtimestamp': if len(selector) > 2: default = selector[2] else: default = 0 ts = json_element(input_json, selector[1], default=default) return datetime.datetime.fromtimestamp(ts).strftime(selector[2]) elif selector[0] == 'json.dict.returntrue': keys = [] json_dict = json_element(input_json, selector[1]) for key in json_dict: if json_dict[key]: keys.append(key) return selector[2].join(keys) # before general json else: if len(selector) > 2: default = selector[2] else: default = None return escape( str(json_element(input_json, selector[1], default=default)))
def handle_reactor_raw_out(self, event): try: self.logger.debug('raw irc: {} <- {}'.format( event['server'].name, escape(event['data']))) except (UnicodeDecodeError, UnicodeEncodeError): self.logger.debug('raw irc: {} <- {}'.format( event['server'].name, 'Data could not be displayed'))
def xml_return(input_xml, selector): pq_xml = pq(input_xml) if selector[0] == 'text': return selector[1] elif selector[0] == 'text.escape': return escape(selector[1]) elif selector[0] == 'jquery': return pq_xml(selector[1]).text() elif selector[0] == 'jquery.attr': return pq_xml(selector[1]).attr(selector[2])
def test_escaping(self): self.assertEqual(formatting.escape('Strawberries are \x02cool\x0f'), 'Strawberries are $bcool$r') self.assertEqual(formatting.escape('Such \x1dcool\x1d things\x02!\x0f'), 'Such $icool$i things$b!$r') self.assertEqual(formatting.escape('Lol \x03cool \x032tests\x0f!'), 'Lol $c[]cool $c[blue]tests$r!') self.assertEqual(formatting.escape('Lol cool\x03'), 'Lol cool$c[]') self.assertEqual(formatting.escape('Lol \x034cool \x032,tests\x0f!'), 'Lol $c[red]cool $c[blue],tests$r!') self.assertEqual(formatting.escape('\x02Lol \x034,2cool \x033,8tests\x0f!'), '$bLol $c[red,blue]cool $c[green,yellow]tests$r!')
def handle_discord_message(self, event): if event['channel'].name in self.channels: assembled_message = '$c[grey]<$r$b{}$b$c[grey]>$r {}'.format( escape(event['source'].name), escape(event['message'])) self.irc.msg(self.channels[event['channel'].name], assembled_message)
def handle_reactor_raw_out(self, event): try: self.logger.debug('raw irc: {} <- {}'.format(event['server'].name, escape(event['data']))) except (UnicodeDecodeError, UnicodeEncodeError): self.logger.debug('raw irc: {} <- {}'.format(event['server'].name, 'Data could not be displayed'))
def handle_discord_message(self, event): if event['channel'].name in self.channels: assembled_message = '$c[grey]<$r$b{}$b$c[grey]>$r {}'.format(escape(event['source'].name), escape(event['message'])) self.irc.msg(self.channels[event['channel'].name], assembled_message)