Best Python code snippet using playwright-python
channel_box.py
Source:channel_box.py
...53 if DEBUG_CHANNEL_BOX:54 sprint(f"ChannelBox :: _channel_add channel_name={channel_name} channel={channel}", c="green", s=1)55 self._CHANNELS.setdefault(channel_name, {})56 self._CHANNELS[channel_name][channel] = True57 async def _remove_channel(self, channel_name, channel):58 if DEBUG_CHANNEL_BOX:59 sprint(f"ChannelBox :: _remove_channel channel_name={channel_name} channel={channel}", c="green", s=1)60 if channel in self._CHANNELS.get(channel_name, {}):61 del self._CHANNELS[channel_name][channel]62 if not any(self._CHANNELS.get(channel_name, {})):63 try:64 del self._CHANNELS[channel_name]65 except:66 pass67 await self._clean_expired()68 async def _clean_expired(self):69 if DEBUG_CHANNEL_BOX:70 sprint(f"ChannelBox :: _clean_expired", c="green", s=1)71 for channel_name in list(self._CHANNELS):72 for channel in self._CHANNELS.get(channel_name, {}):73 is_expired = await channel.is_expired()74 if is_expired:75 del self._CHANNELS[channel_name][channel]76 if not any(self._CHANNELS.get(channel_name, {})):77 try:78 del self._CHANNELS[channel_name]79 except:80 pass81channel_box = ChannelBox()82class Channel:83 def __init__(self, websocket, expires, encoding):84 self.channel_uuid = str(uuid.uuid1())85 self.websocket = websocket86 self.expires = expires87 self.encoding = encoding88 self.created = time.time()89 async def send(self, payload):90 if DEBUG_CHANNEL_BOX:91 sprint(f"Channel :: send payload={payload} webscocket={self.websocket} expires={self.expires} encoding={self.encoding} created={self.created}", c="yellow", s=1)92 websocket = self.websocket93 if self.encoding == "json":94 try:95 await websocket.send_json(payload)96 except RuntimeError:97 pass98 elif self.encoding == "text":99 try:100 await websocket.send_text(payload)101 except RuntimeError:102 pass103 elif self.encoding == "bytes":104 try:105 await websocket.send_bytes(payload)106 except RuntimeError:107 pass108 else:109 try:110 await websocket.send(payload)111 except RuntimeError:112 pass113 self.created = time.time()114 async def is_expired(self):115 if DEBUG_CHANNEL_BOX:116 sprint(f"Channel :: is_expired", c="yellow", s=1)117 return self.expires + int(self.created) < time.time()118 def __repr__(self):119 return f"Channel uuid={self.channel_uuid} expires={self.expires} encoding={self.encoding}"120class ChannelBoxEndpoint(WebSocketEndpoint):121 def __init__(self, *args, **kwargs):122 if DEBUG_CHANNEL_BOX:123 sprint(f"ChannelBoxEndpoint :: __init__ args={args} kwargs={kwargs}", c="cyan", s=1)124 super().__init__(*args, **kwargs)125 self.expires = 60 * 60 * 24126 self.encoding = "json"127 self.channel_name = None128 self.channel = None129 self.channel_box = channel_box130 async def on_connect(self, websocket, **kwargs):131 if DEBUG_CHANNEL_BOX:132 sprint(f"ChannelBoxEndpoint :: on_connect websocket={websocket}", c="cyan", s=1)133 await super().on_connect(websocket, **kwargs)134 self.channel = Channel(websocket=websocket, expires=self.expires, encoding=self.encoding)135 async def on_disconnect(self, websocket, close_code):136 if DEBUG_CHANNEL_BOX:137 sprint(f"ChannelBoxEndpoint :: on_disconnect websocket={websocket} close_code={close_code}", c="cyan", s=1)138 await super().on_disconnect(websocket, close_code)139 await self.channel_box._remove_channel(self.channel_name, self.channel)140 async def channel_send(self, payload, show=False, history=False):141 if DEBUG_CHANNEL_BOX:142 sprint(f"ChannelBoxEndpoint :: channel_send payload={payload}", c="cyan", s=1)143 await self.channel_box.channel_send(self.channel_name, payload, show=show, history=history)144 async def channel_get_or_create(self, channel_name, websocket=None):145 if DEBUG_CHANNEL_BOX:146 sprint(f"ChannelBoxEndpoint :: channel_get_or_create channel_name={channel_name} channel={self.channel}", c="cyan", s=1)147 if websocket:148 self.channel = Channel(websocket=websocket, expires=self.expires, encoding=self.encoding)149 validated_name = await self._validate_name(channel_name)150 assert validated_name, "Channel names must be valid python identifier only alphanumerics and underscores are accepted"151 await self.channel_box._channel_add(channel_name, self.channel)152 self.channel_name = channel_name153 async def _validate_name(self, name):...
frontend.py
Source:frontend.py
...69 def _add_channel(self, chan):70 """Add a channel to the list of our channels."""71 self._channels.add(chan)72 self._save_channels()73 def _remove_channel(self, chan):74 """Remove a channel from the list of our channels."""75 self._channels.discard(chan)76 self._save_channels()77 def _process_message(self, line):78 """Process a single message from IRC."""79 if line[1] == "JOIN":80 data = Data(self.nick, line, msgtype="JOIN")81 if data.nick == self.nick:82 self._add_channel(data.chan)83 self.bot.commands.call("join", data)84 elif line[1] == "PART":85 data = Data(self.nick, line, msgtype="PART")86 if data.nick == self.nick:87 self._remove_channel(data.chan)88 self.bot.commands.call("part", data)89 elif line[1] == "PRIVMSG":90 data = Data(self.nick, line, msgtype="PRIVMSG")91 if data.is_private:92 self.bot.commands.call("msg_private", data)93 else:94 self.bot.commands.call("msg_public", data)95 self.bot.commands.call("msg", data)96 elif line[1] == "NOTICE":97 data = Data(self.nick, line, msgtype="NOTICE")98 if self._auth_wait and data.nick == self.NICK_SERVICES:99 if data.msg.startswith("This nickname is registered."):100 return101 self._auth_wait = False102 sleep(2) # Wait for hostname change to propagate103 self._join_channels()104 elif line[1] == "KICK":105 if line[3] == self.nick:106 self._remove_channel(line[2])107 elif line[1] == "376": # On successful connection to the server108 # If we're supposed to auth to NickServ, do that:109 try:110 username = self.bot.config.irc["frontend"]["nickservUsername"]111 password = self.bot.config.irc["frontend"]["nickservPassword"]112 except KeyError:113 self._join_channels()114 else:115 self.logger.debug("Identifying with services")116 msg = "IDENTIFY {0} {1}".format(username, password)117 self.say(self.NICK_SERVICES, msg, hidelog=True)118 self._auth_wait = True119 elif line[1] == "401": # No such nickname120 if self._auth_wait and line[3] == self.NICK_SERVICES:...
server.py
Source:server.py
...58 packet += struct.pack('<i', len(name))59 packet += name60 self._send_packet(packet)61 print ('add channel', name, index)62 def _remove_channel(self, index):63 packet = bytes()64 packet += struct.pack('<i', CMD_REMOVE_CHANNEL)65 packet += struct.pack('<i', index)66 self._send_packet(packet)67 def socket_thread(self):68 try:69 global client_socket70 if not client_socket:71 client_socket = main_socket.accept()[0]72 self._stop()73 #for i in range(32):74 # self._remove_channel(i)75 for idx, ch in enumerate(self.channels):76 self._add_channel(ch, idx)77 self._start()78 sent_timestamp = self.channels[0].timestamp79 while True:80 ts = self.channels[0].timestamp81 if sent_timestamp < ts:82 l = ts - sent_timestamp83 sent_timestamp += l84 #l = min(l, 2)85 ## Assume same clocking on all signals86 for idx, ch in enumerate(self.channels):87 newdata = ch.buffer[-l:]88 self._send_data(idx, newdata)...
debug.py
Source:debug.py
...37 return await ctx.send(f"Channel <#{channel.id}> is already in the list")38 self.bot.config["channels"].append(channel.id)39 await ctx.send(f"Successfully added <#{channel.id}> to the channel list")40 41 async def _remove_channel(self, ctx, channel: TextChannel):42 if channel.id not in self.bot.config["channels"]:43 return await ctx.send(f"Channel <#{channel.id}> is not in the list")44 self.bot.config["channels"].remove(channel.id)45 await ctx.send(f"Successfully added <#{channel.id}> to the channel list")46 47 @commands.command(aliases=["whitelist", "channel"])48 @commands.check_any(commands.is_owner(), commands.has_permissions(administrator=True))49 async def channels(self, ctx, action: Optional[str] = None, channel: Optional[TextChannel] = None):50 if action is None or action.lower() == "list":51 return await ctx.send(utils.format_channel_message(self.bot.config["channels"]))52 53 if channel is None:54 return await ctx.send("You must specify a channel!")55 56 if action.lower() in ("add", "append"):57 return await self._add_channel(ctx, channel)58 if action.lower() in ("remove", "delete"):59 return await self._remove_channel(ctx, channel)60 await ctx.send(f"Invalid action `{action}`")61def setup(bot):...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!